Subversion Repositories SmartDukaan

Rev

Rev 1153 | Rev 2471 | 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;
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
 
457 rajveer 18
import java.io.File;
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
	}
1050 rajveer 63
 
25 naveen 64
 
65
	/**
79 naveen 66
	 * Learns all features where definition is marked "learned=true"
67
	 * @throws Exception 
68
	 */
1050 rajveer 69
	public void learn(Entity entity) throws Exception {
70
		Map<Long, List<ExpandedBullet>> learnedBullets =  CreationUtils.getLearnedBullets();
71
		if(learnedBullets == null){
72
			learnedBullets = new HashMap<Long, List<ExpandedBullet>>();
73
		}
79 naveen 74
 
1050 rajveer 75
		DefinitionsContainer defs = Catalog.getInstance().getDefinitionsContainer();
82 naveen 76
 
79 naveen 77
 
81 naveen 78
		// Insert Brand
1050 rajveer 79
		List<ExpandedBullet> brandBullets = learnedBullets.get(Utils.BRAND_FEATURE_DEFINITION_ID);
80
		if(brandBullets == null){
81
			brandBullets = new ArrayList<ExpandedBullet>();
82
			learnedBullets.put(Utils.BRAND_FEATURE_DEFINITION_ID, brandBullets);
83
		}
79 naveen 84
 
1050 rajveer 85
		FeatureDefinition brandFeatureDef = defs.getFeatureDefinition(Utils.BRAND_FEATURE_DEFINITION_ID);
82 naveen 86
		BulletDefinition brandBulletDef = brandFeatureDef.getBulletDefinition();
1050 rajveer 87
		Bullet brandBullet = new Bullet(new PrimitiveDataObject(entity.getBrand()));
88
		ExpandedBullet expBrandBullet = new ExpandedBullet(brandBullet,brandBulletDef);
82 naveen 89
 
1050 rajveer 90
		if(!brandBullets.contains(expBrandBullet)) {
91
			brandBullets.add(expBrandBullet);
92
		}
93
 
94
		List<Slide> slides = entity.getSlides();
95
		if(slides != null){
96
			for(Slide slide : slides) {
97
				this.learn(slide, learnedBullets);
81 naveen 98
			}
79 naveen 99
		}
1050 rajveer 100
 
1229 rajveer 101
		//Utils.info("learnedBullets=" + learnedBullets);
1050 rajveer 102
		CreationUtils.storeLearnedBullets(learnedBullets);
79 naveen 103
 
104
	}
1050 rajveer 105
 
79 naveen 106
 
107
	/**
17 naveen 108
	 * 
79 naveen 109
	 * @param slide
110
	 * @param learnedBullets
111
	 * @throws Exception 
112
	 */
82 naveen 113
	private void learn(Slide slide, Map<Long, 
114
			List<ExpandedBullet>> learnedBullets) throws Exception {
79 naveen 115
 
116
		DefinitionsContainer defs = 
117
			Catalog.getInstance().getDefinitionsContainer();
118
 
119
		// Features
120
		List<Feature> features = slide.getFeatures();
121
 
122
		if(features != null) {
123
			for(Feature feature : features) {
124
				Long featureDefID = feature.getFeatureDefinitionID();
125
 
126
				FeatureDefinition featureDef = 
127
					defs.getFeatureDefinition(featureDefID);
128
 
129
				BulletDefinition bulletDef = featureDef.getBulletDefinition();
130
 
131
				if (!bulletDef.isLearned()) {
132
					continue;
133
				}
82 naveen 134
 
135
				// Store expanded bullets to facilitate easy 
136
				// processing later
137
				ExpandedFeature expandedFeature = new ExpandedFeature(feature);
79 naveen 138
 
82 naveen 139
				List<ExpandedBullet> expBullets = 
140
					expandedFeature.getExpandedBullets();
141
 
142
				if(expBullets != null) {
79 naveen 143
 
82 naveen 144
					List<ExpandedBullet> collectedBullets = 
79 naveen 145
						learnedBullets.get(new Long(featureDefID));
146
 
147
					if(collectedBullets == null) {
82 naveen 148
						collectedBullets = new ArrayList<ExpandedBullet>();
79 naveen 149
					}
150
 
82 naveen 151
					for(ExpandedBullet expBullet : expBullets) {
152
						if(!collectedBullets.contains(expBullet)) {
153
 
154
							collectedBullets.add(expBullet);
79 naveen 155
						}
156
					}
157
 
158
					learnedBullets.put(new Long(featureDefID), 
159
							collectedBullets);
160
				}
161
 
162
			}
163
		}
164
 
165
		// Children slides
166
		List<Slide> childrenSlides = slide.getChildrenSlides();
167
 
168
		if(childrenSlides != null) {
169
			for(Slide childSlide : childrenSlides) {
170
				this.learn(childSlide, learnedBullets);
171
			}
172
		}
173
	}
1050 rajveer 174
 
175
 
176
 
177
	/**
178
	 * Learns all features where definition is marked "learned=true"
179
	 * @throws Exception 
180
	 */
181
	public void learn() throws Exception {
182
		Map<Long, List<ExpandedBullet>> learnedBullets = 
183
			new HashMap<Long, List<ExpandedBullet>>();
184
 
185
		DefinitionsContainer defs = 
186
			Catalog.getInstance().getDefinitionsContainer();
187
 
188
 
189
		Map<Long, Entity> entities = CreationUtils.getEntities();
190
 
191
		// Insert Brand
192
		List<ExpandedBullet> brandBullets = new ArrayList<ExpandedBullet>();
193
		learnedBullets.put(new Long(Utils.BRAND_FEATURE_DEFINITION_ID), 
194
				brandBullets);
195
 
196
		FeatureDefinition brandFeatureDef = 
197
			defs.getFeatureDefinition(Utils.BRAND_FEATURE_DEFINITION_ID);
198
 
199
		BulletDefinition brandBulletDef = brandFeatureDef.getBulletDefinition();
200
 
201
		for(Entity entity : entities.values()) {
202
 
203
			// Brand bullet
204
			Bullet brandBullet = new Bullet(
205
					new PrimitiveDataObject(entity.getBrand()));
206
 
207
			ExpandedBullet expBrandBullet = new ExpandedBullet(brandBullet, 
208
					brandBulletDef);
209
 
210
			if(!brandBullets.contains(expBrandBullet)) {
211
				brandBullets.add(expBrandBullet);
212
			}
213
 
214
			List<Slide> slides = entity.getSlides();
215
			if(slides != null){
216
				for(Slide slide : slides) {
217
					this.learn(slide, learnedBullets);
218
				}
219
			}
220
		}
221
 
1229 rajveer 222
		//Utils.info("learnedBullets=" + learnedBullets);
1050 rajveer 223
		CreationUtils.storeLearnedBullets(learnedBullets);
224
 
225
	}
226
 
17 naveen 227
}