| 479 |
rajveer |
1 |
package in.shop2020.metamodel.util;
|
|
|
2 |
|
|
|
3 |
import in.shop2020.metamodel.core.Entity;
|
| 1050 |
rajveer |
4 |
import in.shop2020.metamodel.core.EntityState;
|
| 1153 |
rajveer |
5 |
import in.shop2020.metamodel.core.Feature;
|
|
|
6 |
import in.shop2020.metamodel.core.Slide;
|
| 479 |
rajveer |
7 |
import in.shop2020.metamodel.util.CN;
|
| 1050 |
rajveer |
8 |
import in.shop2020.storage.bdb.StorageManager;
|
| 479 |
rajveer |
9 |
|
|
|
10 |
import java.io.PrintWriter;
|
|
|
11 |
import java.io.StringWriter;
|
|
|
12 |
import java.io.Writer;
|
| 1153 |
rajveer |
13 |
import java.util.ArrayList;
|
| 1050 |
rajveer |
14 |
import java.util.Collection;
|
| 479 |
rajveer |
15 |
import java.util.List;
|
|
|
16 |
import java.util.Map;
|
|
|
17 |
|
|
|
18 |
|
|
|
19 |
/**
|
| 1050 |
rajveer |
20 |
* @author rajveer
|
|
|
21 |
* @description
|
| 479 |
rajveer |
22 |
*
|
|
|
23 |
*/
|
|
|
24 |
public class CreationUtils {
|
| 1050 |
rajveer |
25 |
|
|
|
26 |
private static final String LEARNED_BULLETS = "LEARNED_BULLETS";
|
|
|
27 |
private static final String DEFAULT_ENTITY_SCORES = "DEFAULT_ENTITY_SCORES";
|
|
|
28 |
private static final String FACET_VALUES = "FACET_VALUES";
|
|
|
29 |
private static final String SLIDE_SCORES = "SLIDE_SCORES";
|
|
|
30 |
|
| 479 |
rajveer |
31 |
|
|
|
32 |
/**
|
|
|
33 |
*
|
| 1050 |
rajveer |
34 |
* @param aThrowable
|
| 479 |
rajveer |
35 |
* @return
|
|
|
36 |
*/
|
| 1050 |
rajveer |
37 |
public static String getStackTrace(Throwable aThrowable) {
|
|
|
38 |
final Writer result = new StringWriter();
|
|
|
39 |
final PrintWriter printWriter = new PrintWriter(result);
|
|
|
40 |
aThrowable.printStackTrace(printWriter);
|
|
|
41 |
return result.toString();
|
| 479 |
rajveer |
42 |
}
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
*
|
| 1050 |
rajveer |
46 |
* @param entity
|
| 479 |
rajveer |
47 |
* @throws Exception
|
|
|
48 |
*/
|
| 1050 |
rajveer |
49 |
public static void storeLearnedBullets(Map<Long, List<ExpandedBullet>> learnedBullets) throws Exception {
|
|
|
50 |
StorageManager.getStorageManager().storeDataObject(CreationUtils.LEARNED_BULLETS, learnedBullets);
|
| 479 |
rajveer |
51 |
}
|
|
|
52 |
|
|
|
53 |
@SuppressWarnings("unchecked")
|
| 1050 |
rajveer |
54 |
public static Map<Long, List<ExpandedBullet>> getLearnedBullets() throws Exception {
|
|
|
55 |
return (Map<Long, List<ExpandedBullet>>) StorageManager.getStorageManager().getDataObject(CreationUtils.LEARNED_BULLETS);
|
| 479 |
rajveer |
56 |
}
|
|
|
57 |
|
| 1050 |
rajveer |
58 |
@SuppressWarnings("unchecked")
|
|
|
59 |
public static List<ExpandedBullet> getLearnedBullets(long featureDefinitionID) throws Exception {
|
|
|
60 |
Map<Long, List<ExpandedBullet>> learnedBullets = (Map<Long, List<ExpandedBullet>>) StorageManager.getStorageManager().getDataObject(CreationUtils.LEARNED_BULLETS);
|
|
|
61 |
if(learnedBullets==null){
|
|
|
62 |
return null;
|
| 479 |
rajveer |
63 |
}
|
| 1050 |
rajveer |
64 |
return learnedBullets.get(featureDefinitionID);
|
| 479 |
rajveer |
65 |
}
|
| 1050 |
rajveer |
66 |
|
|
|
67 |
public static void storeFacetValues(Map<Long, List<String>> facetIDFacetValues) throws Exception {
|
|
|
68 |
StorageManager.getStorageManager().storeDataObject(CreationUtils.FACET_VALUES, facetIDFacetValues);
|
| 479 |
rajveer |
69 |
}
|
| 1050 |
rajveer |
70 |
|
|
|
71 |
@SuppressWarnings("unchecked")
|
|
|
72 |
public static Map<Long, List<String>> getFacetValues() throws Exception {
|
|
|
73 |
return (Map<Long, List<String>>) StorageManager.getStorageManager().getDataObject(CreationUtils.FACET_VALUES);
|
| 479 |
rajveer |
74 |
}
|
|
|
75 |
|
| 1050 |
rajveer |
76 |
@SuppressWarnings("unchecked")
|
|
|
77 |
public static List<String> getFacetValues(long facetDefinitionID) throws Exception {
|
|
|
78 |
Map<Long, List<String>> facetValues = (Map<Long, List<String>>) StorageManager.getStorageManager().getDataObject(CreationUtils.FACET_VALUES);
|
|
|
79 |
return facetValues.get(facetDefinitionID);
|
| 479 |
rajveer |
80 |
}
|
| 1050 |
rajveer |
81 |
|
| 479 |
rajveer |
82 |
|
| 1050 |
rajveer |
83 |
public static void storeSlideScores( Map<Long, Map<Long, Integer>> slideScoresByEntity) throws Exception {
|
|
|
84 |
StorageManager.getStorageManager().storeDataObject(CreationUtils.SLIDE_SCORES, slideScoresByEntity);
|
|
|
85 |
}
|
|
|
86 |
|
| 479 |
rajveer |
87 |
@SuppressWarnings("unchecked")
|
| 1050 |
rajveer |
88 |
public static Map<Long, Map<Long, Integer>> getSlideScores() throws Exception {
|
|
|
89 |
return (Map<Long, Map<Long, Integer>>) StorageManager.getStorageManager().getDataObject(CreationUtils.SLIDE_SCORES);
|
| 479 |
rajveer |
90 |
}
|
|
|
91 |
|
| 1050 |
rajveer |
92 |
@SuppressWarnings("unchecked")
|
|
|
93 |
public static Map<Long, Integer> getSlideComparisonScores(long entityID) throws Exception{
|
|
|
94 |
Map<Long, Map<Long, Integer>> slideScores = (Map<Long, Map<Long, Integer>>) StorageManager.getStorageManager().getDataObject(CreationUtils.SLIDE_SCORES);
|
|
|
95 |
return slideScores.get(entityID);
|
| 479 |
rajveer |
96 |
}
|
|
|
97 |
|
| 1050 |
rajveer |
98 |
|
|
|
99 |
public static void storeDefaultEntityScores(Map<Long, Integer> finalScoreByEntityID) throws Exception {
|
|
|
100 |
StorageManager.getStorageManager().storeDataObject(CreationUtils.DEFAULT_ENTITY_SCORES, finalScoreByEntityID);
|
| 479 |
rajveer |
101 |
}
|
|
|
102 |
|
| 1050 |
rajveer |
103 |
@SuppressWarnings("unchecked")
|
|
|
104 |
public static Map<Long, Integer> getDefaultEntityScores() throws Exception {
|
|
|
105 |
return (Map<Long, Integer>) StorageManager.getStorageManager().getDataObject(CreationUtils.DEFAULT_ENTITY_SCORES);
|
| 479 |
rajveer |
106 |
}
|
|
|
107 |
|
| 1050 |
rajveer |
108 |
@SuppressWarnings("unchecked")
|
|
|
109 |
public static Integer getEntityComparisonScores(long entityID) throws Exception {
|
|
|
110 |
Map<Long, Integer> scoresMap = (Map<Long, Integer>) StorageManager.getStorageManager().getDataObject(CreationUtils.DEFAULT_ENTITY_SCORES);
|
|
|
111 |
Integer objScore = scoresMap.get(entityID);
|
|
|
112 |
if(objScore==null){
|
|
|
113 |
return -1;
|
| 479 |
rajveer |
114 |
}
|
| 1050 |
rajveer |
115 |
return objScore;
|
| 479 |
rajveer |
116 |
}
|
|
|
117 |
|
|
|
118 |
|
|
|
119 |
|
|
|
120 |
/**
|
| 1050 |
rajveer |
121 |
* Resolves Entity ID into Entity object
|
| 479 |
rajveer |
122 |
*
|
|
|
123 |
* @param entityID
|
| 1050 |
rajveer |
124 |
* @return Entity
|
|
|
125 |
* @throws Exception
|
| 479 |
rajveer |
126 |
*/
|
| 1050 |
rajveer |
127 |
public static Entity getEntity(long entityID) throws Exception{
|
|
|
128 |
return StorageManager.getStorageManager().getEntity(entityID);
|
| 479 |
rajveer |
129 |
}
|
|
|
130 |
|
| 1050 |
rajveer |
131 |
public static void updateEntity(Entity entity) throws Exception {
|
|
|
132 |
entity.reorderSlides(entity.getSlideSequence());
|
|
|
133 |
StorageManager.getStorageManager().updateEntity(entity);
|
|
|
134 |
CreationUtils.learn(entity);
|
| 479 |
rajveer |
135 |
}
|
|
|
136 |
|
| 1050 |
rajveer |
137 |
public static void createEntity(Entity entity, EntityState entityMetadata) throws Exception {
|
|
|
138 |
StorageManager.getStorageManager().createEntity(entity, entityMetadata);
|
| 479 |
rajveer |
139 |
}
|
| 1050 |
rajveer |
140 |
|
|
|
141 |
public static void deleteEntity(long entityId) throws Exception {
|
|
|
142 |
StorageManager.getStorageManager().deleteEntity(entityId);
|
| 479 |
rajveer |
143 |
}
|
|
|
144 |
|
| 1050 |
rajveer |
145 |
|
|
|
146 |
public static Map<Long, EntityState> getEntitiesState() throws Exception {
|
|
|
147 |
return StorageManager.getStorageManager().getEntitiesMetadata();
|
| 479 |
rajveer |
148 |
}
|
|
|
149 |
|
| 1050 |
rajveer |
150 |
public static EntityState getEntityState(long entityId) throws Exception {
|
|
|
151 |
return StorageManager.getStorageManager().getEntityMetadata(entityId);
|
|
|
152 |
}
|
| 479 |
rajveer |
153 |
|
| 1050 |
rajveer |
154 |
public static void updateEntityState(EntityState entityMetadata) throws Exception {
|
|
|
155 |
StorageManager.getStorageManager().updateEntityMetadata(entityMetadata);
|
| 479 |
rajveer |
156 |
}
|
| 1050 |
rajveer |
157 |
|
|
|
158 |
private static void learn(Entity entity) throws Exception{
|
|
|
159 |
CN cn = new CN();
|
|
|
160 |
cn.learn(entity);
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
public static Map<Long, Entity> getEntities() throws Exception {
|
|
|
164 |
return StorageManager.getStorageManager().getEntities();
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
public static Collection<Entity> getEntities(long categoryId) throws Exception {
|
|
|
168 |
return StorageManager.getStorageManager().getEntitisByCategory(categoryId);
|
|
|
169 |
}
|
| 1153 |
rajveer |
170 |
|
|
|
171 |
/**
|
|
|
172 |
*
|
|
|
173 |
* @param slide
|
|
|
174 |
* @param featureDefinitionID
|
|
|
175 |
* @return Feature
|
|
|
176 |
*/
|
|
|
177 |
public static Feature getFeature(Slide slide, long featureDefinitionID) {
|
|
|
178 |
List<Feature> features = slide.getFeatures();
|
|
|
179 |
|
|
|
180 |
if(features != null) {
|
|
|
181 |
for(Feature feature : features) {
|
|
|
182 |
if(feature.getFeatureDefinitionID() == featureDefinitionID) {
|
|
|
183 |
return feature;
|
|
|
184 |
}
|
|
|
185 |
}
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
Feature feature = null;
|
|
|
189 |
|
|
|
190 |
List<Slide> childrenSlides = slide.getChildrenSlides();
|
|
|
191 |
if(childrenSlides != null) {
|
|
|
192 |
for(Slide childSlide : childrenSlides) {
|
|
|
193 |
|
|
|
194 |
feature = CreationUtils.getFeature(childSlide, featureDefinitionID);
|
|
|
195 |
if(feature == null) {
|
|
|
196 |
continue;
|
|
|
197 |
}
|
|
|
198 |
else {
|
|
|
199 |
break;
|
|
|
200 |
}
|
|
|
201 |
}
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
return feature;
|
|
|
205 |
}
|
| 479 |
rajveer |
206 |
|
| 1153 |
rajveer |
207 |
|
|
|
208 |
/**
|
|
|
209 |
* Utility method to find out Slide object in Entity instance
|
|
|
210 |
*
|
|
|
211 |
* @param entityID
|
|
|
212 |
* @param slideDefinitionID
|
|
|
213 |
* @return
|
|
|
214 |
* @throws Exception
|
|
|
215 |
*/
|
|
|
216 |
public static Slide getSlide(long entityID, long slideDefinitionID)
|
|
|
217 |
throws Exception {
|
|
|
218 |
Entity entity = CreationUtils.getEntity(entityID);
|
|
|
219 |
|
|
|
220 |
List<Slide> slides = entity.getSlides();
|
|
|
221 |
|
|
|
222 |
Slide resultSlide = null;
|
|
|
223 |
|
|
|
224 |
if(slides != null) {
|
|
|
225 |
for(Slide slide : slides) {
|
|
|
226 |
|
|
|
227 |
if(slide.getSlideDefinitionID() == slideDefinitionID) {
|
|
|
228 |
return slide;
|
|
|
229 |
}
|
|
|
230 |
|
|
|
231 |
resultSlide = CreationUtils.getSlide(slide, slideDefinitionID);
|
|
|
232 |
|
|
|
233 |
if(resultSlide == null) {
|
|
|
234 |
continue;
|
|
|
235 |
}
|
|
|
236 |
else {
|
|
|
237 |
break;
|
|
|
238 |
}
|
|
|
239 |
}
|
|
|
240 |
}
|
|
|
241 |
|
|
|
242 |
return resultSlide;
|
|
|
243 |
}
|
|
|
244 |
|
|
|
245 |
|
|
|
246 |
/**
|
|
|
247 |
*
|
|
|
248 |
* @param slide
|
|
|
249 |
* @param slideDefinitionID
|
|
|
250 |
* @return
|
|
|
251 |
*/
|
|
|
252 |
public static Slide getSlide(Slide slide, long slideDefinitionID) {
|
|
|
253 |
|
|
|
254 |
List<Slide> childrenSlides = slide.getChildrenSlides();
|
|
|
255 |
|
|
|
256 |
Slide resultSlide = null;
|
|
|
257 |
|
|
|
258 |
if(childrenSlides != null) {
|
|
|
259 |
for(Slide childSlide : childrenSlides) {
|
|
|
260 |
if(childSlide.getSlideDefinitionID() == slideDefinitionID) {
|
|
|
261 |
return childSlide;
|
|
|
262 |
}
|
|
|
263 |
|
|
|
264 |
resultSlide = CreationUtils.getSlide(childSlide, slideDefinitionID);
|
|
|
265 |
if(resultSlide == null) {
|
|
|
266 |
continue;
|
|
|
267 |
}
|
|
|
268 |
else {
|
|
|
269 |
break;
|
|
|
270 |
}
|
|
|
271 |
}
|
|
|
272 |
}
|
|
|
273 |
|
|
|
274 |
return resultSlide;
|
|
|
275 |
}
|
|
|
276 |
|
|
|
277 |
/**
|
|
|
278 |
* Utility method to find out Feature object in Entity instance
|
|
|
279 |
*
|
|
|
280 |
* @param entityID
|
|
|
281 |
* @param featureDefinitionID
|
|
|
282 |
* @return Feature
|
|
|
283 |
* @throws Exception
|
|
|
284 |
*/
|
|
|
285 |
public static Feature getFeature(long entityID, long featureDefinitionID)
|
|
|
286 |
throws Exception {
|
|
|
287 |
Entity entity = CreationUtils.getEntity(entityID);
|
|
|
288 |
|
|
|
289 |
Feature feature = null;
|
|
|
290 |
|
|
|
291 |
List<Slide> slides = entity.getSlides();
|
|
|
292 |
for(Slide slide : slides) {
|
|
|
293 |
feature = CreationUtils.getFeature(slide, featureDefinitionID);
|
|
|
294 |
|
|
|
295 |
// Until all slides are searched
|
|
|
296 |
if(feature == null) {
|
|
|
297 |
continue;
|
|
|
298 |
}
|
|
|
299 |
else {
|
|
|
300 |
break;
|
|
|
301 |
}
|
|
|
302 |
}
|
|
|
303 |
|
|
|
304 |
return feature;
|
|
|
305 |
}
|
|
|
306 |
|
|
|
307 |
|
|
|
308 |
/**
|
|
|
309 |
* Returns expand form of entity object. All references are resolved into
|
|
|
310 |
* corresponding detail object
|
|
|
311 |
*
|
|
|
312 |
* @param entityID
|
|
|
313 |
* @return ExpandedEntity
|
|
|
314 |
* @throws Exception
|
|
|
315 |
*/
|
|
|
316 |
public static ExpandedEntity getExpandedEntity(long entityID) throws Exception {
|
|
|
317 |
Entity entity = CreationUtils.getEntity(entityID);
|
|
|
318 |
if(entity==null){
|
|
|
319 |
System.out.println("Entity is null");
|
|
|
320 |
return null;
|
|
|
321 |
}
|
|
|
322 |
System.out.println( entity.getCategoryID());
|
|
|
323 |
ExpandedEntity expEntity = new ExpandedEntity(entity);
|
|
|
324 |
return expEntity;
|
|
|
325 |
}
|
|
|
326 |
|
|
|
327 |
|
|
|
328 |
/**
|
|
|
329 |
* Returns list of borrowed slides
|
|
|
330 |
*
|
|
|
331 |
* @param entity
|
|
|
332 |
* @return list of borrowed slides
|
|
|
333 |
*/
|
|
|
334 |
public static List<Slide> getBorrowedSlides(Entity entity) {
|
|
|
335 |
List<Slide> borrowedSlides = new ArrayList<Slide>();
|
|
|
336 |
|
|
|
337 |
List<Slide> slides = entity.getSlides();
|
|
|
338 |
|
|
|
339 |
for(Slide slide : slides) {
|
|
|
340 |
if(slide.isBorrowed()) {
|
|
|
341 |
borrowedSlides.add(slide);
|
|
|
342 |
}
|
|
|
343 |
}
|
|
|
344 |
|
|
|
345 |
return borrowedSlides;
|
|
|
346 |
}
|
|
|
347 |
|
|
|
348 |
/**
|
|
|
349 |
* Returns first parent slide with matching label
|
|
|
350 |
*
|
|
|
351 |
* @param expEntity2
|
|
|
352 |
* @param slideLabel
|
|
|
353 |
* @return Null if not found
|
|
|
354 |
*/
|
|
|
355 |
public static ExpandedSlide getParentExpandedSlide(ExpandedEntity expEntity2, String slideLabel) {
|
|
|
356 |
|
|
|
357 |
ExpandedSlide matchingSlide = null;
|
|
|
358 |
List<ExpandedSlide> expSlides = expEntity2.getExpandedSlides();
|
|
|
359 |
|
|
|
360 |
for(ExpandedSlide expSlide : expSlides) {
|
|
|
361 |
if(expSlide.getSlideDefinition().getLabel().equals(slideLabel)) {
|
|
|
362 |
matchingSlide = expSlide;
|
|
|
363 |
break;
|
|
|
364 |
}
|
|
|
365 |
}
|
|
|
366 |
|
|
|
367 |
return matchingSlide;
|
|
|
368 |
}
|
|
|
369 |
|
| 479 |
rajveer |
370 |
}
|