Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 naveen 1
/**
2
 * 
3
 */
4
package in.shop2020.metamodel.util;
5
 
20 naveen 6
import in.shop2020.metamodel.core.Bullet;
21 naveen 7
import in.shop2020.metamodel.core.Entity;
8
import in.shop2020.metamodel.core.Feature;
9
import in.shop2020.metamodel.core.Slide;
19 naveen 10
import in.shop2020.metamodel.definitions.BulletDefinition;
17 naveen 11
import in.shop2020.metamodel.definitions.Catalog;
12
import in.shop2020.metamodel.definitions.DefinitionsContainer;
21 naveen 13
import in.shop2020.metamodel.definitions.EntityContainer;
18 naveen 14
import in.shop2020.metamodel.definitions.FeatureDefinition;
64 naveen 15
import in.shop2020.util.DBUtils;
18 naveen 16
import in.shop2020.util.Utils;
17 naveen 17
 
22 naveen 18
import java.util.ArrayList;
19
import java.util.HashMap;
20
import java.util.List;
21
import java.util.Map;
22
 
17 naveen 23
import org.apache.commons.lang.ArrayUtils;
24
 
25
/**
49 naveen 26
 * 
17 naveen 27
 * CN - Content Tool
28
 * 
80 naveen 29
 * Currently only has learning logic
30
 * 
17 naveen 31
 * @author naveen
32
 *
33
 */
34
public class CN {
34 naveen 35
 
17 naveen 36
	/**
37
	 * @param args
38
	 */
39
	public static void main(String[] args) throws Exception {
80 naveen 40
		String[] commands = new String[] {"learn"};
17 naveen 41
 
80 naveen 42
		String usage = "Usage: CN learn";
17 naveen 43
 
44
		String inputCommand = args[0];
25 naveen 45
 
17 naveen 46
		if(!ArrayUtils.contains(commands, inputCommand)) {
47
			System.out.println(usage);
48
			System.exit(-1);
49
		}
25 naveen 50
 
79 naveen 51
		if (inputCommand.equals("learn")) {
52
			CN cn = new CN();
53
			cn.learn();
54
		}
17 naveen 55
	}
56
 
57
	/**
58
	 * 
25 naveen 59
	 */
60
	public CN() {
61
	}
62
 
63
	/**
79 naveen 64
	 * Learns all features where definition is marked "learned=true"
65
	 * @throws Exception 
66
	 */
67
	public void learn() throws Exception {
68
		Map<Long, List<Bullet>> learnedBullets = 
69
			new HashMap<Long, List<Bullet>>();
70
 
71
		EntityContainer entContainer = 
72
			Catalog.getInstance().getEntityContainer();
73
 
74
		Map<Long, Entity> entities = entContainer.getEntities();
75
 
76
		for(Entity entity : entities.values()) {
77
			List<Slide> slides = entity.getSlides();
78
 
79
			for(Slide slide : slides) {
80
				this.learn(slide, learnedBullets);
81
			}
82
		}
83
 
84
		Utils.info("learnedBullets=" + learnedBullets);
85
 
80 naveen 86
		String learnedBulletsDBFile = Utils.ENTITIES_DB_PATH + 
87
			"learnedbullets.ser";
88
 
79 naveen 89
		DBUtils.store(learnedBullets, learnedBulletsDBFile);
90
	}
91
 
92
	/**
17 naveen 93
	 * 
79 naveen 94
	 * @param slide
95
	 * @param learnedBullets
96
	 * @throws Exception 
97
	 */
98
	private void learn(Slide slide, Map<Long, List<Bullet>> learnedBullets) 
99
		throws Exception {
100
 
101
		DefinitionsContainer defs = 
102
			Catalog.getInstance().getDefinitionsContainer();
103
 
104
		// Features
105
		List<Feature> features = slide.getFeatures();
106
 
107
		if(features != null) {
108
			for(Feature feature : features) {
109
				Long featureDefID = feature.getFeatureDefinitionID();
110
 
111
				FeatureDefinition featureDef = 
112
					defs.getFeatureDefinition(featureDefID);
113
 
114
				BulletDefinition bulletDef = featureDef.getBulletDefinition();
115
 
116
				if (!bulletDef.isLearned()) {
117
					continue;
118
				}
119
 
120
				List<Bullet> bullets = feature.getBullets();
121
				if(bullets != null) {
122
 
123
					List<Bullet> collectedBullets = 
124
						learnedBullets.get(new Long(featureDefID));
125
 
126
					if(collectedBullets == null) {
127
						collectedBullets = new ArrayList<Bullet>();
128
					}
129
 
130
					for(Bullet bullet : bullets) {
131
						if(!collectedBullets.contains(bullet)) {
132
							collectedBullets.add(bullet);
133
						}
134
					}
135
 
136
					learnedBullets.put(new Long(featureDefID), 
137
							collectedBullets);
138
				}
139
 
140
			}
141
		}
142
 
143
		// Children slides
144
		List<Slide> childrenSlides = slide.getChildrenSlides();
145
 
146
		if(childrenSlides != null) {
147
			for(Slide childSlide : childrenSlides) {
148
				this.learn(childSlide, learnedBullets);
149
			}
150
		}
151
	}
17 naveen 152
}