Subversion Repositories SmartDukaan

Rev

Rev 1050 | Rev 1153 | Go to most recent revision | Details | Compare with Previous | 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
 
457 rajveer 19
import java.io.File;
22 naveen 20
import java.util.ArrayList;
21
import java.util.HashMap;
22
import java.util.List;
23
import java.util.Map;
24
 
17 naveen 25
import org.apache.commons.lang.ArrayUtils;
26
 
27
/**
49 naveen 28
 * 
17 naveen 29
 * CN - Content Tool
30
 * 
80 naveen 31
 * Currently only has learning logic
32
 * 
17 naveen 33
 * @author naveen
34
 *
35
 */
36
public class CN {
34 naveen 37
 
17 naveen 38
	/**
39
	 * @param args
40
	 */
41
	public static void main(String[] args) throws Exception {
80 naveen 42
		String[] commands = new String[] {"learn"};
17 naveen 43
 
80 naveen 44
		String usage = "Usage: CN learn";
17 naveen 45
 
46
		String inputCommand = args[0];
25 naveen 47
 
17 naveen 48
		if(!ArrayUtils.contains(commands, inputCommand)) {
49
			System.out.println(usage);
50
			System.exit(-1);
51
		}
25 naveen 52
 
79 naveen 53
		if (inputCommand.equals("learn")) {
54
			CN cn = new CN();
55
			cn.learn();
56
		}
17 naveen 57
	}
58
 
59
	/**
60
	 * 
25 naveen 61
	 */
62
	public CN() {
63
	}
1050 rajveer 64
 
25 naveen 65
 
66
	/**
79 naveen 67
	 * Learns all features where definition is marked "learned=true"
68
	 * @throws Exception 
69
	 */
1050 rajveer 70
	public void learn(Entity entity) throws Exception {
71
		Map<Long, List<ExpandedBullet>> learnedBullets =  CreationUtils.getLearnedBullets();
72
		if(learnedBullets == null){
73
			learnedBullets = new HashMap<Long, List<ExpandedBullet>>();
74
		}
79 naveen 75
 
1050 rajveer 76
		DefinitionsContainer defs = Catalog.getInstance().getDefinitionsContainer();
82 naveen 77
 
79 naveen 78
 
81 naveen 79
		// Insert Brand
1050 rajveer 80
		List<ExpandedBullet> brandBullets = learnedBullets.get(Utils.BRAND_FEATURE_DEFINITION_ID);
81
		if(brandBullets == null){
82
			brandBullets = new ArrayList<ExpandedBullet>();
83
			learnedBullets.put(Utils.BRAND_FEATURE_DEFINITION_ID, brandBullets);
84
		}
79 naveen 85
 
1050 rajveer 86
		FeatureDefinition brandFeatureDef = defs.getFeatureDefinition(Utils.BRAND_FEATURE_DEFINITION_ID);
82 naveen 87
		BulletDefinition brandBulletDef = brandFeatureDef.getBulletDefinition();
1050 rajveer 88
		Bullet brandBullet = new Bullet(new PrimitiveDataObject(entity.getBrand()));
89
		ExpandedBullet expBrandBullet = new ExpandedBullet(brandBullet,brandBulletDef);
82 naveen 90
 
1050 rajveer 91
		if(!brandBullets.contains(expBrandBullet)) {
92
			brandBullets.add(expBrandBullet);
93
		}
94
 
95
		List<Slide> slides = entity.getSlides();
96
		if(slides != null){
97
			for(Slide slide : slides) {
98
				this.learn(slide, learnedBullets);
81 naveen 99
			}
79 naveen 100
		}
1050 rajveer 101
 
79 naveen 102
		Utils.info("learnedBullets=" + learnedBullets);
1050 rajveer 103
		CreationUtils.storeLearnedBullets(learnedBullets);
79 naveen 104
 
105
	}
1050 rajveer 106
 
79 naveen 107
 
108
	/**
17 naveen 109
	 * 
79 naveen 110
	 * @param slide
111
	 * @param learnedBullets
112
	 * @throws Exception 
113
	 */
82 naveen 114
	private void learn(Slide slide, Map<Long, 
115
			List<ExpandedBullet>> learnedBullets) throws Exception {
79 naveen 116
 
117
		DefinitionsContainer defs = 
118
			Catalog.getInstance().getDefinitionsContainer();
119
 
120
		// Features
121
		List<Feature> features = slide.getFeatures();
122
 
123
		if(features != null) {
124
			for(Feature feature : features) {
125
				Long featureDefID = feature.getFeatureDefinitionID();
126
 
127
				FeatureDefinition featureDef = 
128
					defs.getFeatureDefinition(featureDefID);
129
 
130
				BulletDefinition bulletDef = featureDef.getBulletDefinition();
131
 
132
				if (!bulletDef.isLearned()) {
133
					continue;
134
				}
82 naveen 135
 
136
				// Store expanded bullets to facilitate easy 
137
				// processing later
138
				ExpandedFeature expandedFeature = new ExpandedFeature(feature);
79 naveen 139
 
82 naveen 140
				List<ExpandedBullet> expBullets = 
141
					expandedFeature.getExpandedBullets();
142
 
143
				if(expBullets != null) {
79 naveen 144
 
82 naveen 145
					List<ExpandedBullet> collectedBullets = 
79 naveen 146
						learnedBullets.get(new Long(featureDefID));
147
 
148
					if(collectedBullets == null) {
82 naveen 149
						collectedBullets = new ArrayList<ExpandedBullet>();
79 naveen 150
					}
151
 
82 naveen 152
					for(ExpandedBullet expBullet : expBullets) {
153
						if(!collectedBullets.contains(expBullet)) {
154
 
155
							collectedBullets.add(expBullet);
79 naveen 156
						}
157
					}
158
 
159
					learnedBullets.put(new Long(featureDefID), 
160
							collectedBullets);
161
				}
162
 
163
			}
164
		}
165
 
166
		// Children slides
167
		List<Slide> childrenSlides = slide.getChildrenSlides();
168
 
169
		if(childrenSlides != null) {
170
			for(Slide childSlide : childrenSlides) {
171
				this.learn(childSlide, learnedBullets);
172
			}
173
		}
174
	}
1050 rajveer 175
 
176
 
177
 
178
	/**
179
	 * Learns all features where definition is marked "learned=true"
180
	 * @throws Exception 
181
	 */
182
	public void learn() throws Exception {
183
		Map<Long, List<ExpandedBullet>> learnedBullets = 
184
			new HashMap<Long, List<ExpandedBullet>>();
185
 
186
		DefinitionsContainer defs = 
187
			Catalog.getInstance().getDefinitionsContainer();
188
 
189
 
190
		Map<Long, Entity> entities = CreationUtils.getEntities();
191
 
192
		// Insert Brand
193
		List<ExpandedBullet> brandBullets = new ArrayList<ExpandedBullet>();
194
		learnedBullets.put(new Long(Utils.BRAND_FEATURE_DEFINITION_ID), 
195
				brandBullets);
196
 
197
		FeatureDefinition brandFeatureDef = 
198
			defs.getFeatureDefinition(Utils.BRAND_FEATURE_DEFINITION_ID);
199
 
200
		BulletDefinition brandBulletDef = brandFeatureDef.getBulletDefinition();
201
 
202
		for(Entity entity : entities.values()) {
203
 
204
			// Brand bullet
205
			Bullet brandBullet = new Bullet(
206
					new PrimitiveDataObject(entity.getBrand()));
207
 
208
			ExpandedBullet expBrandBullet = new ExpandedBullet(brandBullet, 
209
					brandBulletDef);
210
 
211
			if(!brandBullets.contains(expBrandBullet)) {
212
				brandBullets.add(expBrandBullet);
213
			}
214
 
215
			List<Slide> slides = entity.getSlides();
216
			if(slides != null){
217
				for(Slide slide : slides) {
218
					this.learn(slide, learnedBullets);
219
				}
220
			}
221
		}
222
 
223
		Utils.info("learnedBullets=" + learnedBullets);
224
		CreationUtils.storeLearnedBullets(learnedBullets);
225
 
226
	}
227
 
17 naveen 228
}