| 70 |
naveen |
1 |
/**
|
|
|
2 |
*
|
|
|
3 |
*/
|
|
|
4 |
package in.shop2020.util;
|
|
|
5 |
|
|
|
6 |
import in.shop2020.metamodel.core.Entity;
|
|
|
7 |
import in.shop2020.metamodel.core.Slide;
|
|
|
8 |
import in.shop2020.metamodel.definitions.Catalog;
|
|
|
9 |
import in.shop2020.metamodel.definitions.DefinitionsContainer;
|
|
|
10 |
import in.shop2020.metamodel.definitions.EntityContainer;
|
|
|
11 |
import in.shop2020.metamodel.util.ExpandedCMPSlideRuleDefinition;
|
|
|
12 |
import in.shop2020.metamodel.util.ExpandedSlide;
|
|
|
13 |
|
|
|
14 |
import java.util.HashMap;
|
|
|
15 |
import java.util.List;
|
|
|
16 |
import java.util.Map;
|
|
|
17 |
import java.util.TreeMap;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* @author naveen
|
|
|
21 |
*
|
|
|
22 |
*/
|
|
|
23 |
public class CMP {
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
*
|
|
|
27 |
*/
|
|
|
28 |
private long categoryID;
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* @param args
|
|
|
32 |
* @throws Exception
|
|
|
33 |
*/
|
|
|
34 |
public static void main(String[] args) throws Exception {
|
|
|
35 |
String usage = "Usage: CMP {Category ID}";
|
|
|
36 |
|
|
|
37 |
if(args.length > 1) {
|
|
|
38 |
System.out.println(usage);
|
|
|
39 |
System.exit(-1);
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
long categoryID = 0L;
|
|
|
43 |
if(args.length > 0) {
|
|
|
44 |
try {
|
|
|
45 |
categoryID = Long.parseLong(args[0]);
|
|
|
46 |
}
|
|
|
47 |
catch (NumberFormatException nfe) {
|
|
|
48 |
System.out.println(usage);
|
|
|
49 |
System.exit(-1);
|
|
|
50 |
}
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
CMP cmp = new CMP(categoryID);
|
|
|
54 |
|
|
|
55 |
// Entity ID > List(Slide ID > Score)
|
|
|
56 |
Map<Long, Map<Long, Integer>> slideScoresByEntity =
|
|
|
57 |
cmp.getSlideScores();
|
|
|
58 |
|
|
|
59 |
String dbFile = Utils.COMPARISONS_DB_PATH + "slidescores.ser";
|
|
|
60 |
DBUtils.store(slideScoresByEntity, dbFile);
|
|
|
61 |
|
|
|
62 |
// Entity ID > Final Score
|
|
|
63 |
Map<Long, Integer> finalScoreByEntityID = cmp.getFinalScores(
|
|
|
64 |
slideScoresByEntity);
|
|
|
65 |
|
|
|
66 |
dbFile = Utils.COMPARISONS_DB_PATH + "defaultentityscores.ser";
|
|
|
67 |
DBUtils.store(finalScoreByEntityID, dbFile);
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
/**
|
|
|
71 |
*
|
|
|
72 |
* @param categoryID
|
|
|
73 |
*/
|
|
|
74 |
public CMP(long categoryID) {
|
|
|
75 |
this.categoryID = categoryID;
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
*
|
|
|
80 |
* @return Map<Long, Map<Long, Integer>>
|
|
|
81 |
* @throws Exception
|
|
|
82 |
*/
|
|
|
83 |
private Map<Long, Map<Long, Integer>> getSlideScores() throws Exception {
|
|
|
84 |
Map<Long, Map<Long, Integer>> slideScoresByEntityID =
|
|
|
85 |
new TreeMap<Long, Map<Long, Integer>>();
|
|
|
86 |
|
|
|
87 |
// Calculate scores based on rule
|
|
|
88 |
EntityContainer ents =
|
|
|
89 |
Catalog.getInstance().getEntityContainer();
|
|
|
90 |
|
|
|
91 |
Map<Long, Entity> entitiesMap = ents.getEntities();
|
|
|
92 |
|
|
|
93 |
for(Long entityID : entitiesMap.keySet()) {
|
|
|
94 |
Entity entity = entitiesMap.get(entityID);
|
|
|
95 |
|
|
|
96 |
// If provided, filter by category
|
|
|
97 |
if(this.categoryID != 0L &&
|
|
|
98 |
(this.categoryID != entity.getCategoryID())) {
|
|
|
99 |
continue;
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
Map<Long, Integer> slideScores = this.getSlideScores(entity);
|
|
|
103 |
|
|
|
104 |
slideScoresByEntityID.put(entityID, slideScores);
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
return slideScoresByEntityID;
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
/**
|
|
|
111 |
*
|
|
|
112 |
* @param entity
|
|
|
113 |
* @return Map<Long, Integer>
|
|
|
114 |
* @throws Exception
|
|
|
115 |
*/
|
|
|
116 |
private Map<Long, Integer> getSlideScores(Entity entity) throws Exception {
|
|
|
117 |
Map<Long, Integer> slideScores = new HashMap<Long, Integer>();
|
|
|
118 |
|
|
|
119 |
DefinitionsContainer defs =
|
|
|
120 |
Catalog.getInstance().getDefinitionsContainer();
|
|
|
121 |
|
|
|
122 |
// Python wrapper for executing rule in python script
|
|
|
123 |
CMPJythonWrapper cmpJW = new CMPJythonWrapper();
|
|
|
124 |
|
|
|
125 |
// Scores are required for parent slides only
|
|
|
126 |
List<Slide> slides = entity.getSlides();
|
|
|
127 |
for(Slide slide : slides) {
|
|
|
128 |
ExpandedSlide expSlide = new ExpandedSlide(slide);
|
| 71 |
naveen |
129 |
cmpJW.reset();
|
|
|
130 |
cmpJW.initialize();
|
|
|
131 |
|
| 70 |
naveen |
132 |
cmpJW.setExpandedSlide(expSlide);
|
|
|
133 |
|
|
|
134 |
long slideDefinitionID = slide.getSlideDefinitionID();
|
|
|
135 |
ExpandedCMPSlideRuleDefinition expCMPSlideRuleDef =
|
|
|
136 |
defs.getExpandedComparisonSlideRuleDefinition(
|
|
|
137 |
slideDefinitionID);
|
|
|
138 |
|
| 71 |
naveen |
139 |
if(expCMPSlideRuleDef == null) {
|
|
|
140 |
Utils.warning("Could not find comparison definition for " +
|
|
|
141 |
"slide definition ID " + slideDefinitionID);
|
|
|
142 |
continue;
|
|
|
143 |
}
|
|
|
144 |
|
| 70 |
naveen |
145 |
cmpJW.setExpandedCMPSlideRuleDefinition(expCMPSlideRuleDef);
|
|
|
146 |
|
|
|
147 |
cmpJW.execCMPRule();
|
|
|
148 |
|
|
|
149 |
int score = cmpJW.getScore();
|
|
|
150 |
Utils.info("score=" + score);
|
|
|
151 |
|
|
|
152 |
slideScores.put(new Long(slideDefinitionID), new Integer(score));
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
return slideScores;
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
/**
|
|
|
159 |
*
|
|
|
160 |
* @param slideScoresByEntity
|
|
|
161 |
* @return Map<Long, Integer>
|
|
|
162 |
*/
|
|
|
163 |
private Map<Long, Integer> getFinalScores(
|
|
|
164 |
Map<Long, Map<Long, Integer>> slideScoresByEntity) {
|
|
|
165 |
Map<Long, Integer> finalScoreByEntityID = new TreeMap<Long, Integer>();
|
|
|
166 |
|
|
|
167 |
// Considering default Weights per Slide bucket
|
|
|
168 |
// Slide buckets > List(Very Important, Regular, Not-so-Important)
|
|
|
169 |
// Default Weights > List(70%, 20%, 10%)
|
|
|
170 |
|
|
|
171 |
// TODO
|
|
|
172 |
|
|
|
173 |
return finalScoreByEntityID;
|
|
|
174 |
}
|
|
|
175 |
}
|