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;
81 naveen 9
import in.shop2020.metamodel.core.PrimitiveDataObject;
21 naveen 10
import in.shop2020.metamodel.core.Slide;
19 naveen 11
import in.shop2020.metamodel.definitions.BulletDefinition;
17 naveen 12
import in.shop2020.metamodel.definitions.Catalog;
13
import in.shop2020.metamodel.definitions.DefinitionsContainer;
21 naveen 14
import in.shop2020.metamodel.definitions.EntityContainer;
18 naveen 15
import in.shop2020.metamodel.definitions.FeatureDefinition;
64 naveen 16
import in.shop2020.util.DBUtils;
18 naveen 17
import in.shop2020.util.Utils;
17 naveen 18
 
22 naveen 19
import java.util.ArrayList;
20
import java.util.HashMap;
21
import java.util.List;
22
import java.util.Map;
23
 
17 naveen 24
import org.apache.commons.lang.ArrayUtils;
25
 
26
/**
49 naveen 27
 * 
17 naveen 28
 * CN - Content Tool
29
 * 
80 naveen 30
 * Currently only has learning logic
31
 * 
17 naveen 32
 * @author naveen
33
 *
34
 */
35
public class CN {
34 naveen 36
 
17 naveen 37
	/**
38
	 * @param args
39
	 */
40
	public static void main(String[] args) throws Exception {
80 naveen 41
		String[] commands = new String[] {"learn"};
17 naveen 42
 
80 naveen 43
		String usage = "Usage: CN learn";
17 naveen 44
 
45
		String inputCommand = args[0];
25 naveen 46
 
17 naveen 47
		if(!ArrayUtils.contains(commands, inputCommand)) {
48
			System.out.println(usage);
49
			System.exit(-1);
50
		}
25 naveen 51
 
79 naveen 52
		if (inputCommand.equals("learn")) {
53
			CN cn = new CN();
54
			cn.learn();
55
		}
17 naveen 56
	}
57
 
58
	/**
59
	 * 
25 naveen 60
	 */
61
	public CN() {
62
	}
63
 
64
	/**
79 naveen 65
	 * Learns all features where definition is marked "learned=true"
66
	 * @throws Exception 
67
	 */
68
	public void learn() throws Exception {
82 naveen 69
		Map<Long, List<ExpandedBullet>> learnedBullets = 
70
			new HashMap<Long, List<ExpandedBullet>>();
79 naveen 71
 
82 naveen 72
		DefinitionsContainer defs = 
73
			Catalog.getInstance().getDefinitionsContainer();
74
 
79 naveen 75
		EntityContainer entContainer = 
76
			Catalog.getInstance().getEntityContainer();
77
 
78
		Map<Long, Entity> entities = entContainer.getEntities();
81 naveen 79
 
80
		// Insert Brand
82 naveen 81
		List<ExpandedBullet> brandBullets = new ArrayList<ExpandedBullet>();
81 naveen 82
		learnedBullets.put(new Long(Utils.BRAND_FEATURE_DEFINITION_ID), 
83
				brandBullets);
79 naveen 84
 
82 naveen 85
		FeatureDefinition brandFeatureDef = 
86
			defs.getFeatureDefinition(Utils.BRAND_FEATURE_DEFINITION_ID);
87
 
88
		BulletDefinition brandBulletDef = brandFeatureDef.getBulletDefinition();
89
 
79 naveen 90
		for(Entity entity : entities.values()) {
81 naveen 91
 
92
			// Brand bullet
93
			Bullet brandBullet = new Bullet(
94
					new PrimitiveDataObject(entity.getBrand()));
95
 
82 naveen 96
			ExpandedBullet expBrandBullet = new ExpandedBullet(brandBullet, 
97
					brandBulletDef);
98
 
99
			if(!brandBullets.contains(expBrandBullet)) {
100
				brandBullets.add(expBrandBullet);
81 naveen 101
			}
102
 
79 naveen 103
			List<Slide> slides = entity.getSlides();
104
 
105
			for(Slide slide : slides) {
106
				this.learn(slide, learnedBullets);
107
			}
108
		}
109
 
110
		Utils.info("learnedBullets=" + learnedBullets);
111
 
80 naveen 112
		String learnedBulletsDBFile = Utils.ENTITIES_DB_PATH + 
113
			"learnedbullets.ser";
114
 
79 naveen 115
		DBUtils.store(learnedBullets, learnedBulletsDBFile);
116
	}
117
 
118
	/**
17 naveen 119
	 * 
79 naveen 120
	 * @param slide
121
	 * @param learnedBullets
122
	 * @throws Exception 
123
	 */
82 naveen 124
	private void learn(Slide slide, Map<Long, 
125
			List<ExpandedBullet>> learnedBullets) throws Exception {
79 naveen 126
 
127
		DefinitionsContainer defs = 
128
			Catalog.getInstance().getDefinitionsContainer();
129
 
130
		// Features
131
		List<Feature> features = slide.getFeatures();
132
 
133
		if(features != null) {
134
			for(Feature feature : features) {
135
				Long featureDefID = feature.getFeatureDefinitionID();
136
 
137
				FeatureDefinition featureDef = 
138
					defs.getFeatureDefinition(featureDefID);
139
 
140
				BulletDefinition bulletDef = featureDef.getBulletDefinition();
141
 
142
				if (!bulletDef.isLearned()) {
143
					continue;
144
				}
82 naveen 145
 
146
				// Store expanded bullets to facilitate easy 
147
				// processing later
148
				ExpandedFeature expandedFeature = new ExpandedFeature(feature);
79 naveen 149
 
82 naveen 150
				List<ExpandedBullet> expBullets = 
151
					expandedFeature.getExpandedBullets();
152
 
153
				if(expBullets != null) {
79 naveen 154
 
82 naveen 155
					List<ExpandedBullet> collectedBullets = 
79 naveen 156
						learnedBullets.get(new Long(featureDefID));
157
 
158
					if(collectedBullets == null) {
82 naveen 159
						collectedBullets = new ArrayList<ExpandedBullet>();
79 naveen 160
					}
161
 
82 naveen 162
					for(ExpandedBullet expBullet : expBullets) {
163
						if(!collectedBullets.contains(expBullet)) {
164
 
165
							collectedBullets.add(expBullet);
79 naveen 166
						}
167
					}
168
 
169
					learnedBullets.put(new Long(featureDefID), 
170
							collectedBullets);
171
				}
172
 
173
			}
174
		}
175
 
176
		// Children slides
177
		List<Slide> childrenSlides = slide.getChildrenSlides();
178
 
179
		if(childrenSlides != null) {
180
			for(Slide childSlide : childrenSlides) {
181
				this.learn(childSlide, learnedBullets);
182
			}
183
		}
184
	}
17 naveen 185
}