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();
514 rajveer 105
			if(slides != null){
106
				for(Slide slide : slides) {
107
					this.learn(slide, learnedBullets);
108
				}
79 naveen 109
			}
110
		}
111
 
112
		Utils.info("learnedBullets=" + learnedBullets);
113
 
457 rajveer 114
		String learnedBulletsDBFile = Utils.CONTENT_DB_PATH + "entities" + File.separator +
80 naveen 115
			"learnedbullets.ser";
116
 
79 naveen 117
		DBUtils.store(learnedBullets, learnedBulletsDBFile);
118
	}
119
 
120
	/**
17 naveen 121
	 * 
79 naveen 122
	 * @param slide
123
	 * @param learnedBullets
124
	 * @throws Exception 
125
	 */
82 naveen 126
	private void learn(Slide slide, Map<Long, 
127
			List<ExpandedBullet>> learnedBullets) throws Exception {
79 naveen 128
 
129
		DefinitionsContainer defs = 
130
			Catalog.getInstance().getDefinitionsContainer();
131
 
132
		// Features
133
		List<Feature> features = slide.getFeatures();
134
 
135
		if(features != null) {
136
			for(Feature feature : features) {
137
				Long featureDefID = feature.getFeatureDefinitionID();
138
 
139
				FeatureDefinition featureDef = 
140
					defs.getFeatureDefinition(featureDefID);
141
 
142
				BulletDefinition bulletDef = featureDef.getBulletDefinition();
143
 
144
				if (!bulletDef.isLearned()) {
145
					continue;
146
				}
82 naveen 147
 
148
				// Store expanded bullets to facilitate easy 
149
				// processing later
150
				ExpandedFeature expandedFeature = new ExpandedFeature(feature);
79 naveen 151
 
82 naveen 152
				List<ExpandedBullet> expBullets = 
153
					expandedFeature.getExpandedBullets();
154
 
155
				if(expBullets != null) {
79 naveen 156
 
82 naveen 157
					List<ExpandedBullet> collectedBullets = 
79 naveen 158
						learnedBullets.get(new Long(featureDefID));
159
 
160
					if(collectedBullets == null) {
82 naveen 161
						collectedBullets = new ArrayList<ExpandedBullet>();
79 naveen 162
					}
163
 
82 naveen 164
					for(ExpandedBullet expBullet : expBullets) {
165
						if(!collectedBullets.contains(expBullet)) {
166
 
167
							collectedBullets.add(expBullet);
79 naveen 168
						}
169
					}
170
 
171
					learnedBullets.put(new Long(featureDefID), 
172
							collectedBullets);
173
				}
174
 
175
			}
176
		}
177
 
178
		// Children slides
179
		List<Slide> childrenSlides = slide.getChildrenSlides();
180
 
181
		if(childrenSlides != null) {
182
			for(Slide childSlide : childrenSlides) {
183
				this.learn(childSlide, learnedBullets);
184
			}
185
		}
186
	}
17 naveen 187
}