Subversion Repositories SmartDukaan

Rev

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

Rev 1443 Rev 1918
Line 2... Line 2...
2
 * 
2
 * 
3
 */
3
 */
4
package in.shop2020.util;
4
package in.shop2020.util;
5
 
5
 
6
import in.shop2020.metamodel.core.Entity;
6
import in.shop2020.metamodel.core.Entity;
-
 
7
import in.shop2020.metamodel.core.EntityStatus;
7
import in.shop2020.metamodel.core.Slide;
8
import in.shop2020.metamodel.core.Slide;
8
import in.shop2020.metamodel.definitions.CMPBucketDefinition;
9
import in.shop2020.metamodel.definitions.CMPBucketDefinition;
9
import in.shop2020.metamodel.definitions.Catalog;
10
import in.shop2020.metamodel.definitions.Catalog;
10
import in.shop2020.metamodel.definitions.DefinitionsContainer;
11
import in.shop2020.metamodel.definitions.DefinitionsContainer;
11
import in.shop2020.metamodel.util.CreationUtils;
12
import in.shop2020.metamodel.util.CreationUtils;
12
import in.shop2020.metamodel.util.ExpandedCMPSlideRuleDefinition;
13
import in.shop2020.metamodel.util.ExpandedCMPSlideRuleDefinition;
13
import in.shop2020.metamodel.util.ExpandedSlide;
14
import in.shop2020.metamodel.util.ExpandedSlide;
14
 
15
 
15
import java.io.File;
-
 
16
import java.util.HashMap;
16
import java.util.HashMap;
17
import java.util.List;
17
import java.util.List;
18
import java.util.Map;
18
import java.util.Map;
19
import java.util.TreeMap;
19
import java.util.TreeMap;
20
 
20
 
Line 51... Line 51...
51
				System.exit(-1);
51
				System.exit(-1);
52
			}
52
			}
53
		}
53
		}
54
		
54
		
55
		CMP cmp = new CMP(categoryID);
55
		CMP cmp = new CMP(categoryID);
-
 
56
//		
-
 
57
//		Entity entity = CreationUtils.getEntity(1000165);
-
 
58
//		 Map<Long, Double> slideScores = cmp.getSlideScores(entity);
-
 
59
//		 Utils.info(slideScores);
56
		
60
		 
-
 
61
		  
57
		// Entity ID > Map(Slide ID > Score)
62
		// Entity ID > Map(Slide ID > Score)
58
		Map<Long, Map<Long, Integer>> slideScoresByEntity = 
63
		Map<Long, Map<Long, Double>> slideScoresByEntity = cmp.getSlideScores();
59
			cmp.getSlideScores();
-
 
60
		Utils.info("slideScoresByEntity=" + slideScoresByEntity);
64
		Utils.info("slideScoresByEntity=" + slideScoresByEntity);
61
		
65
		
62
		CreationUtils.storeSlideScores(slideScoresByEntity);
66
		CreationUtils.storeSlideScores(slideScoresByEntity);
63
		
67
		
64
		// Entity ID > Final Score 
68
		// Entity ID > Final Score 
65
		Map<Long, Integer> finalScoreByEntityID = cmp.getFinalScores(
69
		Map<Long, Double> finalScoreByEntityID = cmp.getFinalScores(
66
				slideScoresByEntity);
70
				slideScoresByEntity);
67
		Utils.info("finalScoreByEntityID=" + finalScoreByEntityID);
71
		Utils.info("finalScoreByEntityID=" + finalScoreByEntityID);
68
		
72
		
69
		CreationUtils.storeDefaultEntityScores(finalScoreByEntityID);
73
		CreationUtils.storeDefaultEntityScores(finalScoreByEntityID);
70
	
74
	
Line 78... Line 82...
78
		this.categoryID = categoryID;
82
		this.categoryID = categoryID;
79
	}
83
	}
80
	
84
	
81
	/**
85
	/**
82
	 * 
86
	 * 
83
	 * @return Map<Long, Map<Long, Integer>>
87
	 * @return Map<Long, Map<Long, Double>>
84
	 * @throws Exception 
88
	 * @throws Exception 
85
	 */
89
	 */
86
	public Map<Long, Map<Long, Integer>> getSlideScores() throws Exception {
90
	public Map<Long, Map<Long, Double>> getSlideScores() throws Exception {
87
		Map<Long, Map<Long, Integer>> slideScoresByEntityID = 
91
		Map<Long, Map<Long, Double>> slideScoresByEntityID = 
88
			new TreeMap<Long, Map<Long, Integer>>();
92
			new TreeMap<Long, Map<Long, Double>>();
89
		
93
		
90
		// Calculate scores based on rule
94
		// Calculate scores based on rule
91
		Map<Long, Entity> entitiesMap = CreationUtils.getEntities();
95
		Map<Long, Entity> entitiesMap = CreationUtils.getEntities();
92
	
96
	
93
		for(Long entityID : entitiesMap.keySet()) {
97
		for(Long entityID : entitiesMap.keySet()) {
94
			Entity entity = entitiesMap.get(entityID);
98
			Entity entity = entitiesMap.get(entityID);
95
			
99
			
-
 
100
			if(CreationUtils.getEntityState(entityID).getStatus() != EntityStatus.READY){
-
 
101
			    continue;
-
 
102
			}
-
 
103
			//Skip categories with value as -1 or categories except mobile phones
-
 
104
			if(entity.getCategoryID() == -1 || Catalog.getInstance().getDefinitionsContainer().getCategory(entity.getCategoryID()).getParentCategory().getID() != 10001){
-
 
105
			    continue;
-
 
106
			}
96
			// If provided, filter by category
107
			// If provided, filter by category
97
			if(this.categoryID != 0L && 
108
			if(this.categoryID != 0L && 
98
					(this.categoryID != entity.getCategoryID())) {
109
					(this.categoryID != entity.getCategoryID())) {
99
				continue;
110
				continue;
100
			}
111
			}
101
			
112
			
102
			Map<Long, Integer> slideScores = this.getSlideScores(entity);
113
			Map<Long, Double> slideScores = this.getSlideScores(entity);
103
			
114
			
104
			slideScoresByEntityID.put(entityID, slideScores);
115
			slideScoresByEntityID.put(entityID, slideScores);
105
		}
116
		}
106
		
117
		
107
		return slideScoresByEntityID;
118
		return slideScoresByEntityID;
108
	}
119
	}
109
	
120
	
110
	/**
121
	/**
111
	 * 
122
	 * 
112
	 * @param entity
123
	 * @param entity
113
	 * @return Map<Long, Integer>
124
	 * @return Map<Long, Double>
114
	 * @throws Exception 
125
	 * @throws Exception 
115
	 */
126
	 */
116
	private Map<Long, Integer> getSlideScores(Entity entity) throws Exception {
127
	private Map<Long, Double> getSlideScores(Entity entity) throws Exception {
117
		Map<Long, Integer> slideScores = new HashMap<Long, Integer>();
128
		Map<Long, Double> slideScores = new HashMap<Long, Double>();
118
		
129
		
119
		DefinitionsContainer defs = 
130
		DefinitionsContainer defs = 
120
			Catalog.getInstance().getDefinitionsContainer();
131
			Catalog.getInstance().getDefinitionsContainer();
121
		
132
		
122
		// Python wrapper for executing rule in python script
133
		// Python wrapper for executing rule in python script
Line 147... Line 158...
147
			
158
			
148
			cmpJW.setExpandedCMPSlideRuleDefinition(expCMPSlideRuleDef);
159
			cmpJW.setExpandedCMPSlideRuleDefinition(expCMPSlideRuleDef);
149
			
160
			
150
			cmpJW.executeRule();
161
			cmpJW.executeRule();
151
			
162
			
152
			int score = cmpJW.getScore();
163
			double score = cmpJW.getScore();
153
			Utils.info("score=" + score);
164
			Utils.info("score=" + score);
154
			
165
			
155
			slideScores.put(new Long(slideDefinitionID), new Integer(score));
166
			slideScores.put(new Long(slideDefinitionID), new Double(score));
156
		}
167
		}
157
		
168
		
158
		return slideScores;
169
		return slideScores;
159
	}
170
	}
160
	
171
	
161
	/**
172
	/**
162
	 * 
173
	 * 
163
	 * @param slideScoresByEntity
174
	 * @param slideScoresByEntity
164
	 * @return Map<Long, Integer>
175
	 * @return Map<Long, Double>
165
	 * @throws Exception 
176
	 * @throws Exception 
166
	 */
177
	 */
167
	public Map<Long, Integer> getFinalScores(
178
	public Map<Long, Double> getFinalScores(
168
			Map<Long, Map<Long, Integer>> slideScoresByEntity) throws Exception {
179
			Map<Long, Map<Long, Double>> slideScoresByEntity) throws Exception {
169
		Map<Long, Integer> finalScoreByEntityID = new TreeMap<Long, Integer>();
180
		Map<Long, Double> finalScoreByEntityID = new TreeMap<Long, Double>();
170
		
181
		
171
		// Considering default Weights per Slide bucket
182
		// Considering default Weights per Slide bucket
172
		// Slide buckets > List(Very Important, Regular, Not-so-Important)
183
		// Slide buckets > List(Very Important, Regular, Not-so-Important)
173
		// Default Weights > List(70%, 20%, 10%)
184
		// Default Weights > List(70%, 20%, 10%)
174
		DefinitionsContainer defs = 
185
		DefinitionsContainer defs = 
175
			Catalog.getInstance().getDefinitionsContainer();
186
			Catalog.getInstance().getDefinitionsContainer();
176
		
187
		
177
		for(Long entityID : slideScoresByEntity.keySet()) {
188
		for(Long entityID : slideScoresByEntity.keySet()) {
178
			Map<Long, Integer> slideScores = slideScoresByEntity.get(entityID);
189
			Map<Long, Double> slideScores = slideScoresByEntity.get(entityID);
179
			
190
			
180
			Entity entity = CreationUtils.getEntity(entityID);
191
			Entity entity = CreationUtils.getEntity(entityID);
181
			long categoryID = entity.getCategoryID();
192
			long categoryID = entity.getCategoryID();
182
			if(categoryID == -1){
193
			if(categoryID == -1){
183
				continue;
194
				continue;
Line 187... Line 198...
187
			}
198
			}
188
			
199
			
189
			List<CMPBucketDefinition> bucketDefs = 
200
			List<CMPBucketDefinition> bucketDefs = 
190
				defs.getComparisonBucketDefinitions(categoryID);
201
				defs.getComparisonBucketDefinitions(categoryID);
191
			
202
			
192
			Map<String, Integer> bucketScores = new HashMap<String, Integer>();
203
			Map<String, Double> bucketScores = new HashMap<String, Double>();
193
			Map<String, Integer> bucketSlideCounts = 
204
			Map<String, Double> bucketSlideCounts = 
194
				new HashMap<String, Integer>();
205
				new HashMap<String, Double>();
195
			
206
			
196
			for(Long slideDefinitionID : slideScores.keySet()) {
207
			for(Long slideDefinitionID : slideScores.keySet()) {
197
				Integer slideScore = slideScores.get(slideDefinitionID);
208
				Double slideScore = slideScores.get(slideDefinitionID);
198
				
209
				
199
				Slide slide = CreationUtils.getSlide(entityID, slideDefinitionID);
210
				Slide slide = CreationUtils.getSlide(entityID, slideDefinitionID);
200
				
211
				
201
				String bucketName = null;
212
				String bucketName = null;
202
				if(CreationUtils.isBorrowedSlide(slide.getSlideDefinitionID(), entity.getCategoryID())) {
213
				if(CreationUtils.isBorrowedSlide(slide.getSlideDefinitionID(), entity.getCategoryID())) {
Line 216... Line 227...
216
					
227
					
217
					continue;
228
					continue;
218
				}
229
				}
219
 
230
 
220
				if(!bucketScores.containsKey(bucketName)) {
231
				if(!bucketScores.containsKey(bucketName)) {
221
					bucketScores.put(bucketName, new Integer(0));
232
					bucketScores.put(bucketName, new Double(0));
222
				}
233
				}
223
				
234
				
224
				if(!bucketSlideCounts.containsKey(bucketName)) {
235
				if(!bucketSlideCounts.containsKey(bucketName)) {
225
					bucketSlideCounts.put(bucketName, new Integer(0));
236
					bucketSlideCounts.put(bucketName, new Double(0));
226
				}
237
				}
227
				
238
				
228
				Integer bucketScore = bucketScores.get(bucketName);
239
				Double bucketScore = bucketScores.get(bucketName);
229
				Integer bucketSlideCount = bucketSlideCounts.get(bucketName);
240
				Double bucketSlideCount = bucketSlideCounts.get(bucketName);
230
				
241
				
231
				
242
				
232
				Integer newBucketScore =  new Integer(
243
				Double newBucketScore =  new Double(
233
						bucketScore.intValue() + slideScore.intValue());
244
						bucketScore.intValue() + slideScore.intValue());
234
				
245
				
235
				Integer newBucketSlideCount =  new Integer(
246
				Double newBucketSlideCount =  new Double(
236
						bucketSlideCount.intValue() + 1);
247
						bucketSlideCount.intValue() + 1);
237
				
248
				
238
				bucketScores.put(bucketName, newBucketScore);
249
				bucketScores.put(bucketName, newBucketScore);
239
				bucketSlideCounts.put(bucketName, newBucketSlideCount);
250
				bucketSlideCounts.put(bucketName, newBucketSlideCount);
240
			}
251
			}
Line 247... Line 258...
247
				Utils.info("bucketName=" + bucketName);
258
				Utils.info("bucketName=" + bucketName);
248
				
259
				
249
				if(bucketScores.get(bucketName)==null){
260
				if(bucketScores.get(bucketName)==null){
250
					continue;
261
					continue;
251
				}
262
				}
252
				int aggregateBucketScore = bucketScores.get(bucketName);
263
				double aggregateBucketScore = bucketScores.get(bucketName);
253
				Utils.info("aggregateBucketScore=" + aggregateBucketScore);
264
				Utils.info("aggregateBucketScore=" + aggregateBucketScore);
254
				
265
				
255
				int bucketSlideCount = bucketSlideCounts.get(bucketName);
266
				double bucketSlideCount = bucketSlideCounts.get(bucketName);
256
				Utils.info("bucketSlideCount=" + bucketSlideCount);
267
				Utils.info("bucketSlideCount=" + bucketSlideCount);
257
				
268
				
258
				int defaultWeight = bucketDef.getDefaultWeight();
269
				double defaultWeight = bucketDef.getDefaultWeight();
259
				Utils.info("defaultWeight=" + defaultWeight);
270
				Utils.info("defaultWeight=" + defaultWeight);
260
				
271
				
261
				float percentageBucketScore = 
272
				float percentageBucketScore = 
262
					(((float)aggregateBucketScore/
273
					(((float)aggregateBucketScore/
263
							((float)bucketSlideCount * 10))) * 100;
274
							((float)bucketSlideCount * 10))) * 100;
Line 270... Line 281...
270
				
281
				
271
				finalEntityScore += Math.round(weightedBucketScore);
282
				finalEntityScore += Math.round(weightedBucketScore);
272
			}
283
			}
273
			Utils.info("finalEntityScore=" + finalEntityScore);
284
			Utils.info("finalEntityScore=" + finalEntityScore);
274
			
285
			
275
			finalScoreByEntityID.put(entityID, new Integer(finalEntityScore));
286
			finalScoreByEntityID.put(entityID, new Double(finalEntityScore));
276
		}
287
		}
277
		
288
		
278
		return finalScoreByEntityID;
289
		return finalScoreByEntityID;
279
	}
290
	}
280
}
291
}