Subversion Repositories SmartDukaan

Rev

Rev 1050 | Rev 1153 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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;
479 rajveer 5
import in.shop2020.metamodel.util.CN;
1050 rajveer 6
import in.shop2020.storage.bdb.StorageManager;
479 rajveer 7
 
8
import java.io.PrintWriter;
9
import java.io.StringWriter;
10
import java.io.Writer;
1050 rajveer 11
import java.util.Collection;
479 rajveer 12
import java.util.List;
13
import java.util.Map;
14
 
15
 
16
/**
1050 rajveer 17
 * @author rajveer
18
 * @description 
479 rajveer 19
 *
20
 */
21
public class CreationUtils {
1050 rajveer 22
 
23
	private static final String LEARNED_BULLETS = "LEARNED_BULLETS";
24
	private static final String DEFAULT_ENTITY_SCORES = "DEFAULT_ENTITY_SCORES";
25
	private static final String FACET_VALUES = "FACET_VALUES";
26
	private static final String SLIDE_SCORES = "SLIDE_SCORES";
27
 
479 rajveer 28
 
29
	/**
30
	 * 
1050 rajveer 31
	 * @param aThrowable
479 rajveer 32
	 * @return
33
	 */
1050 rajveer 34
	public static String getStackTrace(Throwable aThrowable) {
35
	    final Writer result = new StringWriter();
36
	    final PrintWriter printWriter = new PrintWriter(result);
37
	    aThrowable.printStackTrace(printWriter);
38
	    return result.toString();
479 rajveer 39
	}
40
 
41
	/**
42
	 * 
1050 rajveer 43
	 * @param entity
479 rajveer 44
	 * @throws Exception
45
	 */
1050 rajveer 46
	public static void storeLearnedBullets(Map<Long, List<ExpandedBullet>> learnedBullets) throws Exception {
47
		StorageManager.getStorageManager().storeDataObject(CreationUtils.LEARNED_BULLETS, learnedBullets);		
479 rajveer 48
	}
49
 
50
	@SuppressWarnings("unchecked")
1050 rajveer 51
	public static Map<Long, List<ExpandedBullet>> getLearnedBullets() throws Exception {
52
		return (Map<Long, List<ExpandedBullet>>) StorageManager.getStorageManager().getDataObject(CreationUtils.LEARNED_BULLETS);		
479 rajveer 53
	}
54
 
1050 rajveer 55
	@SuppressWarnings("unchecked")
56
	public static List<ExpandedBullet> getLearnedBullets(long featureDefinitionID) throws Exception {
57
		Map<Long, List<ExpandedBullet>> learnedBullets = (Map<Long, List<ExpandedBullet>>) StorageManager.getStorageManager().getDataObject(CreationUtils.LEARNED_BULLETS);
58
		if(learnedBullets==null){
59
			return null;
479 rajveer 60
		}
1050 rajveer 61
		return learnedBullets.get(featureDefinitionID);
479 rajveer 62
	}
1050 rajveer 63
 
64
	public static void storeFacetValues(Map<Long, List<String>> facetIDFacetValues) throws Exception {
65
		StorageManager.getStorageManager().storeDataObject(CreationUtils.FACET_VALUES, facetIDFacetValues);
479 rajveer 66
	}
1050 rajveer 67
 
68
	@SuppressWarnings("unchecked")
69
	public static Map<Long, List<String>>  getFacetValues() throws Exception {
70
		return (Map<Long, List<String>>) StorageManager.getStorageManager().getDataObject(CreationUtils.FACET_VALUES);
479 rajveer 71
	}
72
 
1050 rajveer 73
	@SuppressWarnings("unchecked")
74
	public static List<String>  getFacetValues(long facetDefinitionID) throws Exception {
75
		Map<Long, List<String>> facetValues = (Map<Long, List<String>>) StorageManager.getStorageManager().getDataObject(CreationUtils.FACET_VALUES);
76
		return facetValues.get(facetDefinitionID);
479 rajveer 77
	}
1050 rajveer 78
 
479 rajveer 79
 
1050 rajveer 80
	public static void storeSlideScores( Map<Long, Map<Long, Integer>> slideScoresByEntity) throws Exception {
81
		StorageManager.getStorageManager().storeDataObject(CreationUtils.SLIDE_SCORES, slideScoresByEntity);
82
	}
83
 
479 rajveer 84
	@SuppressWarnings("unchecked")
1050 rajveer 85
	public static Map<Long, Map<Long, Integer>> getSlideScores() throws Exception {
86
		return (Map<Long, Map<Long, Integer>>) StorageManager.getStorageManager().getDataObject(CreationUtils.SLIDE_SCORES);
479 rajveer 87
	}
88
 
1050 rajveer 89
	@SuppressWarnings("unchecked")
90
	public static Map<Long, Integer> getSlideComparisonScores(long entityID) throws Exception{
91
		Map<Long, Map<Long, Integer>> slideScores = (Map<Long, Map<Long, Integer>>) StorageManager.getStorageManager().getDataObject(CreationUtils.SLIDE_SCORES);
92
		return slideScores.get(entityID);
479 rajveer 93
	}
94
 
1050 rajveer 95
 
96
	public static void storeDefaultEntityScores(Map<Long, Integer> finalScoreByEntityID) throws Exception {
97
		StorageManager.getStorageManager().storeDataObject(CreationUtils.DEFAULT_ENTITY_SCORES, finalScoreByEntityID);
479 rajveer 98
	}
99
 
1050 rajveer 100
	@SuppressWarnings("unchecked")
101
	public static Map<Long, Integer> getDefaultEntityScores() throws Exception {
102
		return (Map<Long, Integer>) StorageManager.getStorageManager().getDataObject(CreationUtils.DEFAULT_ENTITY_SCORES);
479 rajveer 103
	}
104
 
1050 rajveer 105
	@SuppressWarnings("unchecked")
106
	public static Integer getEntityComparisonScores(long entityID) throws Exception {
107
		Map<Long, Integer> scoresMap =  (Map<Long, Integer>) StorageManager.getStorageManager().getDataObject(CreationUtils.DEFAULT_ENTITY_SCORES);
108
		Integer objScore = scoresMap.get(entityID);
109
		if(objScore==null){
110
			return -1;
479 rajveer 111
		}
1050 rajveer 112
		return objScore;
479 rajveer 113
	}
114
 
115
 
116
 
117
	/**
1050 rajveer 118
	 * Resolves Entity ID into Entity object
479 rajveer 119
	 * 
120
	 * @param entityID
1050 rajveer 121
	 * @return Entity
122
	 * @throws Exception 
479 rajveer 123
	 */
1050 rajveer 124
	public static Entity getEntity(long entityID) throws Exception{
125
		return StorageManager.getStorageManager().getEntity(entityID);
479 rajveer 126
	}
127
 
1050 rajveer 128
	public static void updateEntity(Entity entity) throws Exception {
129
		entity.reorderSlides(entity.getSlideSequence());
130
		StorageManager.getStorageManager().updateEntity(entity);
131
		CreationUtils.learn(entity);
479 rajveer 132
	}
133
 
1050 rajveer 134
	public static void createEntity(Entity entity, EntityState entityMetadata) throws Exception {
135
		StorageManager.getStorageManager().createEntity(entity, entityMetadata);
479 rajveer 136
	}
1050 rajveer 137
 
138
	public static void deleteEntity(long entityId) throws Exception  {
139
		StorageManager.getStorageManager().deleteEntity(entityId);
479 rajveer 140
	}
141
 
1050 rajveer 142
 
143
	public static Map<Long, EntityState> getEntitiesState() throws Exception {
144
		return StorageManager.getStorageManager().getEntitiesMetadata();
479 rajveer 145
	}
146
 
1050 rajveer 147
	public static EntityState getEntityState(long entityId) throws Exception {
148
		return StorageManager.getStorageManager().getEntityMetadata(entityId);
149
	}
479 rajveer 150
 
1050 rajveer 151
	public static void updateEntityState(EntityState entityMetadata) throws Exception {
152
		StorageManager.getStorageManager().updateEntityMetadata(entityMetadata);
479 rajveer 153
	}
1050 rajveer 154
 
155
	private static void learn(Entity entity) throws Exception{
156
		CN cn = new CN();
157
		cn.learn(entity);	
158
	}
159
 
160
	public static Map<Long, Entity> getEntities() throws Exception {
161
		return StorageManager.getStorageManager().getEntities();
162
	}
163
 
164
	public static Collection<Entity> getEntities(long categoryId) throws Exception {
165
		return StorageManager.getStorageManager().getEntitisByCategory(categoryId);
166
	}
479 rajveer 167
 
168
}