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 {
69
		Map<Long, List<Bullet>> learnedBullets = 
70
			new HashMap<Long, List<Bullet>>();
71
 
72
		EntityContainer entContainer = 
73
			Catalog.getInstance().getEntityContainer();
74
 
75
		Map<Long, Entity> entities = entContainer.getEntities();
81 naveen 76
 
77
		// Insert Brand
78
		List<Bullet> brandBullets = new ArrayList<Bullet>();
79
		learnedBullets.put(new Long(Utils.BRAND_FEATURE_DEFINITION_ID), 
80
				brandBullets);
79 naveen 81
 
82
		for(Entity entity : entities.values()) {
81 naveen 83
 
84
			// Brand bullet
85
			Bullet brandBullet = new Bullet(
86
					new PrimitiveDataObject(entity.getBrand()));
87
 
88
			if(!brandBullets.contains(brandBullet)) {
89
				brandBullets.add(brandBullet);
90
			}
91
 
79 naveen 92
			List<Slide> slides = entity.getSlides();
93
 
94
			for(Slide slide : slides) {
95
				this.learn(slide, learnedBullets);
96
			}
97
		}
98
 
99
		Utils.info("learnedBullets=" + learnedBullets);
100
 
80 naveen 101
		String learnedBulletsDBFile = Utils.ENTITIES_DB_PATH + 
102
			"learnedbullets.ser";
103
 
79 naveen 104
		DBUtils.store(learnedBullets, learnedBulletsDBFile);
105
	}
106
 
107
	/**
17 naveen 108
	 * 
79 naveen 109
	 * @param slide
110
	 * @param learnedBullets
111
	 * @throws Exception 
112
	 */
113
	private void learn(Slide slide, Map<Long, List<Bullet>> learnedBullets) 
114
		throws Exception {
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
				}
134
 
135
				List<Bullet> bullets = feature.getBullets();
136
				if(bullets != null) {
137
 
138
					List<Bullet> collectedBullets = 
139
						learnedBullets.get(new Long(featureDefID));
140
 
141
					if(collectedBullets == null) {
142
						collectedBullets = new ArrayList<Bullet>();
143
					}
144
 
145
					for(Bullet bullet : bullets) {
146
						if(!collectedBullets.contains(bullet)) {
147
							collectedBullets.add(bullet);
148
						}
149
					}
150
 
151
					learnedBullets.put(new Long(featureDefID), 
152
							collectedBullets);
153
				}
154
 
155
			}
156
		}
157
 
158
		// Children slides
159
		List<Slide> childrenSlides = slide.getChildrenSlides();
160
 
161
		if(childrenSlides != null) {
162
			for(Slide childSlide : childrenSlides) {
163
				this.learn(childSlide, learnedBullets);
164
			}
165
		}
166
	}
17 naveen 167
}