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
 
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
	}
64
 
65
	/**
79 naveen 66
	 * Learns all features where definition is marked "learned=true"
67
	 * @throws Exception 
68
	 */
69
	public void learn() throws Exception {
82 naveen 70
		Map<Long, List<ExpandedBullet>> learnedBullets = 
71
			new HashMap<Long, List<ExpandedBullet>>();
79 naveen 72
 
82 naveen 73
		DefinitionsContainer defs = 
74
			Catalog.getInstance().getDefinitionsContainer();
75
 
79 naveen 76
		EntityContainer entContainer = 
77
			Catalog.getInstance().getEntityContainer();
78
 
79
		Map<Long, Entity> entities = entContainer.getEntities();
81 naveen 80
 
81
		// Insert Brand
82 naveen 82
		List<ExpandedBullet> brandBullets = new ArrayList<ExpandedBullet>();
81 naveen 83
		learnedBullets.put(new Long(Utils.BRAND_FEATURE_DEFINITION_ID), 
84
				brandBullets);
79 naveen 85
 
82 naveen 86
		FeatureDefinition brandFeatureDef = 
87
			defs.getFeatureDefinition(Utils.BRAND_FEATURE_DEFINITION_ID);
88
 
89
		BulletDefinition brandBulletDef = brandFeatureDef.getBulletDefinition();
90
 
79 naveen 91
		for(Entity entity : entities.values()) {
81 naveen 92
 
93
			// Brand bullet
94
			Bullet brandBullet = new Bullet(
95
					new PrimitiveDataObject(entity.getBrand()));
96
 
82 naveen 97
			ExpandedBullet expBrandBullet = new ExpandedBullet(brandBullet, 
98
					brandBulletDef);
99
 
100
			if(!brandBullets.contains(expBrandBullet)) {
101
				brandBullets.add(expBrandBullet);
81 naveen 102
			}
103
 
79 naveen 104
			List<Slide> slides = entity.getSlides();
105
 
106
			for(Slide slide : slides) {
107
				this.learn(slide, learnedBullets);
108
			}
109
		}
110
 
111
		Utils.info("learnedBullets=" + learnedBullets);
112
 
457 rajveer 113
		String learnedBulletsDBFile = Utils.CONTENT_DB_PATH + "entities" + File.separator +
80 naveen 114
			"learnedbullets.ser";
115
 
79 naveen 116
		DBUtils.store(learnedBullets, learnedBulletsDBFile);
117
	}
118
 
119
	/**
17 naveen 120
	 * 
79 naveen 121
	 * @param slide
122
	 * @param learnedBullets
123
	 * @throws Exception 
124
	 */
82 naveen 125
	private void learn(Slide slide, Map<Long, 
126
			List<ExpandedBullet>> learnedBullets) throws Exception {
79 naveen 127
 
128
		DefinitionsContainer defs = 
129
			Catalog.getInstance().getDefinitionsContainer();
130
 
131
		// Features
132
		List<Feature> features = slide.getFeatures();
133
 
134
		if(features != null) {
135
			for(Feature feature : features) {
136
				Long featureDefID = feature.getFeatureDefinitionID();
137
 
138
				FeatureDefinition featureDef = 
139
					defs.getFeatureDefinition(featureDefID);
140
 
141
				BulletDefinition bulletDef = featureDef.getBulletDefinition();
142
 
143
				if (!bulletDef.isLearned()) {
144
					continue;
145
				}
82 naveen 146
 
147
				// Store expanded bullets to facilitate easy 
148
				// processing later
149
				ExpandedFeature expandedFeature = new ExpandedFeature(feature);
79 naveen 150
 
82 naveen 151
				List<ExpandedBullet> expBullets = 
152
					expandedFeature.getExpandedBullets();
153
 
154
				if(expBullets != null) {
79 naveen 155
 
82 naveen 156
					List<ExpandedBullet> collectedBullets = 
79 naveen 157
						learnedBullets.get(new Long(featureDefID));
158
 
159
					if(collectedBullets == null) {
82 naveen 160
						collectedBullets = new ArrayList<ExpandedBullet>();
79 naveen 161
					}
162
 
82 naveen 163
					for(ExpandedBullet expBullet : expBullets) {
164
						if(!collectedBullets.contains(expBullet)) {
165
 
166
							collectedBullets.add(expBullet);
79 naveen 167
						}
168
					}
169
 
170
					learnedBullets.put(new Long(featureDefID), 
171
							collectedBullets);
172
				}
173
 
174
			}
175
		}
176
 
177
		// Children slides
178
		List<Slide> childrenSlides = slide.getChildrenSlides();
179
 
180
		if(childrenSlides != null) {
181
			for(Slide childSlide : childrenSlides) {
182
				this.learn(childSlide, learnedBullets);
183
			}
184
		}
185
	}
17 naveen 186
}