Subversion Repositories SmartDukaan

Rev

Rev 64 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 64 Rev 79
Line 100... Line 100...
100
		
100
		
101
	/**
101
	/**
102
	 * @param args
102
	 * @param args
103
	 */
103
	 */
104
	public static void main(String[] args) throws Exception {
104
	public static void main(String[] args) throws Exception {
105
		String[] commands = new String[] {"show", "import"};
105
		String[] commands = new String[] {"show", "import", "learn"};
106
		
106
		
107
		String usage = "Usage: CN ["+ StringUtils.join(commands, "|") +
107
		String usage = "Usage: CN ["+ StringUtils.join(commands, "|") +
108
			"] [{Entity ID}|{Category ID} {Content file name}]\n";
108
			"] [{Entity ID}|{Category ID} {Content file name}]\n";
109
		
109
		
110
		if(args.length < 2) {
-
 
111
			System.out.println(usage);
-
 
112
			System.exit(-1);
-
 
113
		}
-
 
114
		
110
		
115
		String inputCommand = args[0];
111
		String inputCommand = args[0];
116
		
112
		
117
		if(!ArrayUtils.contains(commands, inputCommand)) {
113
		if(!ArrayUtils.contains(commands, inputCommand)) {
118
			System.out.println(usage);
114
			System.out.println(usage);
119
			System.exit(-1);
115
			System.exit(-1);
120
		}
116
		}
121
 
117
 
122
		if (inputCommand.equals("show")) {
118
		if (inputCommand.equals("show")) {
-
 
119
			if(args.length < 2) {
-
 
120
				System.out.println(usage);
-
 
121
				System.exit(-1);
-
 
122
			}
-
 
123
			
123
			String entityID = args[1];
124
			String entityID = args[1];
124
			CN cn = new CN();
125
			CN cn = new CN();
125
			cn.showEntity(new Long(entityID).longValue());
126
			cn.showEntity(new Long(entityID).longValue());
126
			System.exit(0);
127
			System.exit(0);
127
		}
128
		}
128
		
129
		
129
		if (inputCommand.equals("import")) {
130
		if (inputCommand.equals("import")) {
-
 
131
			if(args.length < 2) {
-
 
132
				System.out.println(usage);
-
 
133
				System.exit(-1);
-
 
134
			}
-
 
135
			
130
			String inputCategoryID = args[1];
136
			String inputCategoryID = args[1];
131
			if(args.length < 3) {
137
			if(args.length < 3) {
132
				System.out.println(usage);
138
				System.out.println(usage);
133
				System.exit(-1);
139
				System.exit(-1);
134
			}
140
			}
Line 137... Line 143...
137
			long categoryID = new Long(inputCategoryID).longValue();
143
			long categoryID = new Long(inputCategoryID).longValue();
138
			CN cn = new CN(categoryID, inputFilename);
144
			CN cn = new CN(categoryID, inputFilename);
139
			cn.importEntity();
145
			cn.importEntity();
140
			System.exit(0);
146
			System.exit(0);
141
		}
147
		}
-
 
148
		
-
 
149
		if (inputCommand.equals("learn")) {
-
 
150
			CN cn = new CN();
-
 
151
			cn.learn();
-
 
152
		}
142
	}
153
	}
143
	
154
	
144
	/**
155
	/**
145
	 * 
156
	 * 
146
	 */
157
	 */
Line 158... Line 169...
158
		this.srcFile = CONTENT_SRC_TXT_PATH + fileName + ".txt";
169
		this.srcFile = CONTENT_SRC_TXT_PATH + fileName + ".txt";
159
		this.dbFile = CONTENT_DB_PATH + "entities" + ".ser";
170
		this.dbFile = CONTENT_DB_PATH + "entities" + ".ser";
160
	}
171
	}
161
	
172
	
162
	/**
173
	/**
-
 
174
	 * Learns all features where definition is marked "learned=true"
-
 
175
	 * @throws Exception 
-
 
176
	 */
-
 
177
	public void learn() throws Exception {
-
 
178
		Map<Long, List<Bullet>> learnedBullets = 
-
 
179
			new HashMap<Long, List<Bullet>>();
-
 
180
		
-
 
181
		EntityContainer entContainer = 
-
 
182
			Catalog.getInstance().getEntityContainer();
-
 
183
		
-
 
184
		Map<Long, Entity> entities = entContainer.getEntities();
-
 
185
		
-
 
186
		for(Entity entity : entities.values()) {
-
 
187
			List<Slide> slides = entity.getSlides();
-
 
188
			
-
 
189
			for(Slide slide : slides) {
-
 
190
				this.learn(slide, learnedBullets);
-
 
191
			}
-
 
192
		}
-
 
193
		
-
 
194
		Utils.info("learnedBullets=" + learnedBullets);
-
 
195
		
-
 
196
		String learnedBulletsDBFile = CN.CONTENT_DB_PATH + "learnedbullets.ser";
-
 
197
		DBUtils.store(learnedBullets, learnedBulletsDBFile);
-
 
198
	}
-
 
199
	
-
 
200
	/**
-
 
201
	 * 
-
 
202
	 * @param slide
-
 
203
	 * @param learnedBullets
-
 
204
	 * @throws Exception 
-
 
205
	 */
-
 
206
	private void learn(Slide slide, Map<Long, List<Bullet>> learnedBullets) 
-
 
207
		throws Exception {
-
 
208
		
-
 
209
		DefinitionsContainer defs = 
-
 
210
			Catalog.getInstance().getDefinitionsContainer();
-
 
211
		
-
 
212
		// Features
-
 
213
		List<Feature> features = slide.getFeatures();
-
 
214
		
-
 
215
		if(features != null) {
-
 
216
			for(Feature feature : features) {
-
 
217
				Long featureDefID = feature.getFeatureDefinitionID();
-
 
218
				
-
 
219
				FeatureDefinition featureDef = 
-
 
220
					defs.getFeatureDefinition(featureDefID);
-
 
221
				
-
 
222
				BulletDefinition bulletDef = featureDef.getBulletDefinition();
-
 
223
				
-
 
224
				if (!bulletDef.isLearned()) {
-
 
225
					continue;
-
 
226
				}
-
 
227
				
-
 
228
				List<Bullet> bullets = feature.getBullets();
-
 
229
				if(bullets != null) {
-
 
230
					
-
 
231
					List<Bullet> collectedBullets = 
-
 
232
						learnedBullets.get(new Long(featureDefID));
-
 
233
					
-
 
234
					if(collectedBullets == null) {
-
 
235
						collectedBullets = new ArrayList<Bullet>();
-
 
236
					}
-
 
237
					
-
 
238
					for(Bullet bullet : bullets) {
-
 
239
						if(!collectedBullets.contains(bullet)) {
-
 
240
							collectedBullets.add(bullet);
-
 
241
						}
-
 
242
					}
-
 
243
					
-
 
244
					learnedBullets.put(new Long(featureDefID), 
-
 
245
							collectedBullets);
-
 
246
				}
-
 
247
				
-
 
248
			}
-
 
249
		}
-
 
250
		
-
 
251
		// Children slides
-
 
252
		List<Slide> childrenSlides = slide.getChildrenSlides();
-
 
253
		
-
 
254
		if(childrenSlides != null) {
-
 
255
			for(Slide childSlide : childrenSlides) {
-
 
256
				this.learn(childSlide, learnedBullets);
-
 
257
			}
-
 
258
		}
-
 
259
	}
-
 
260
	
-
 
261
	/**
163
	 * 
262
	 * 
164
	 * @throws Exception
263
	 * @throws Exception
165
	 */
264
	 */
166
	public void showEntity(long entityID) throws Exception {
265
	public void showEntity(long entityID) throws Exception {
167
		EntityContainer entContainer = 
266
		EntityContainer entContainer =