Rev 8093 | Blame | Compare with Previous | Last modification | View Log | RSS feed
/****/package in.shop2020.util;import in.shop2020.metamodel.core.Entity;import in.shop2020.metamodel.definitions.CMPBucketDefinition;import in.shop2020.metamodel.definitions.Catalog;import in.shop2020.metamodel.definitions.Category;import in.shop2020.metamodel.definitions.DefinitionsContainer;import in.shop2020.metamodel.util.CreationUtils;import in.shop2020.metamodel.util.ExpandedCMPSlideRuleDefinition;import in.shop2020.metamodel.util.ExpandedEntity;import in.shop2020.metamodel.util.ExpandedSlide;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.TreeMap;/*** @author naveen**/public class NewCMP {/****///private long categoryID;List<Entity> entities;/*** @param args* @throws Exception*/public static void main(String[] args) throws Exception {String usage = "Usage: CMP {Category ID}";if(args.length > 1) {System.out.println(usage);System.exit(-1);}}/**** @param entityIdItemMap*/public NewCMP(List<Entity> entities) {this.entities = entities;}/**** @return Map<Long, Map<Long, Double>>* @throws Exception*/public Map<Long, Map<Long, Double>> getSlideScores() throws Exception {Map<Long, Map<Long, Double>> slideScoresByEntityID =new TreeMap<Long, Map<Long, Double>>();for(Entity entity : entities) {// Skip categories with value as -1 or categories except mobile phones and tabletslong categoryId = entity.getCategoryID();if(categoryId == -1){continue;}Category category = Catalog.getInstance().getDefinitionsContainer().getCategory(categoryId);if(category.isComparable()) {ExpandedEntity expEntity = new ExpandedEntity(entity);Map<Long, Double> slideScores = this.getSlideScores(expEntity, true);slideScoresByEntityID.put(entity.getID(), slideScores);}}return slideScoresByEntityID;}public Map<Long, Map<Long, Double>> getSlideScores(int dummy) throws Exception {Map<Long, Map<Long, Double>> slideScoresByEntityID =new TreeMap<Long, Map<Long, Double>>();for(Entity entity : entities) {// Skip categories with value as -1 or categories except mobile phones and tabletslong categoryId = entity.getCategoryID();if(categoryId == -1){continue;}ExpandedEntity expEntity = new ExpandedEntity(entity);Map<Long, Double> slideScores = this.getSlideScores(expEntity, false);slideScoresByEntityID.put(entity.getID(), slideScores);}return slideScoresByEntityID;}/**** @param entity* @return Map<Long, Double>* @throws Exception*/private Map<Long, Double> getSlideScores(ExpandedEntity expEntity, boolean isCustomized) throws Exception {Map<Long, Double> slideScores = new HashMap<Long, Double>();DefinitionsContainer defs =Catalog.getInstance().getDefinitionsContainer();// Python wrapper for executing rule in python scriptCMPJythonWrapper cmpJW = new CMPJythonWrapper();Map<Long, Double> customScores = CreationUtils.getCustomSlideComparisonScores(expEntity.getID());// Scores are required for parent slides onlyList<ExpandedSlide> expSlides = expEntity.getExpandedSlides();if(expSlides==null){return slideScores;}if (customScores==null) {isCustomized = false;}for(ExpandedSlide expSlide : expSlides) {long slideDefinitionID = expSlide.getSlideDefinitionID();if(isCustomized && customScores.containsKey(slideDefinitionID)){slideScores.put(slideDefinitionID, customScores.get(slideDefinitionID));continue;}cmpJW.reset();cmpJW.initialize();cmpJW.setExpandedSlide(expSlide);cmpJW.setCategory(expEntity.getCategory());ExpandedCMPSlideRuleDefinition expCMPSlideRuleDef =defs.getExpandedComparisonSlideRuleDefinition(slideDefinitionID);if(expCMPSlideRuleDef == null) {Utils.warning("Could not find comparison definition for " +"slide definition ID " + slideDefinitionID);continue;}cmpJW.setExpandedCMPSlideRuleDefinition(expCMPSlideRuleDef);cmpJW.executeRule();double score = cmpJW.getScore();Utils.info("score=" + score);slideScores.put(new Long(slideDefinitionID), new Double(score));}return slideScores;}/**** @param slideScoresByEntity* @return Map<Long, Double>* @throws Exception*/public Map<Long, Double> getFinalScores(Map<Long, Map<Long, Double>> slideScoresByEntity) throws Exception {Map<Long, Double> finalScoreByEntityID = new TreeMap<Long, Double>();// Considering default Weights per Slide bucket// Slide buckets > List(Very Important, Regular, Not-so-Important)// Default Weights > List(70%, 20%, 10%)DefinitionsContainer defs =Catalog.getInstance().getDefinitionsContainer();for(Long entityID : slideScoresByEntity.keySet()) {Map<Long, Double> slideScores = slideScoresByEntity.get(entityID);Entity entity = CreationUtils.getEntity(entityID);long categoryID = entity.getCategoryID();if(categoryID == -1){continue;}if(categoryID>10010){continue;}List<CMPBucketDefinition> bucketDefs =defs.getComparisonBucketDefinitions(categoryID);Map<String, Double> bucketScores = new HashMap<String, Double>();Map<String, Double> bucketSlideCounts =new HashMap<String, Double>();for(Long slideDefinitionID : slideScores.keySet()) {Double slideScore = slideScores.get(slideDefinitionID);String bucketName = null;bucketName = defs.getComparisonBucketName(categoryID, slideDefinitionID.longValue());if(bucketName == null) {Utils.warning("Comparison bucket not defined for " +"Category ID=" + categoryID +" Slide Definition ID=" + slideDefinitionID);continue;}if(!bucketScores.containsKey(bucketName)) {bucketScores.put(bucketName, new Double(0));}if(!bucketSlideCounts.containsKey(bucketName)) {bucketSlideCounts.put(bucketName, new Double(0));}Double bucketScore = bucketScores.get(bucketName);Double bucketSlideCount = bucketSlideCounts.get(bucketName);Double newBucketScore = new Double(bucketScore.intValue() + slideScore.intValue());Double newBucketSlideCount = new Double(bucketSlideCount.intValue() + 1);bucketScores.put(bucketName, newBucketScore);bucketSlideCounts.put(bucketName, newBucketSlideCount);}int finalEntityScore = 0;// Apply weights and sum upfor(CMPBucketDefinition bucketDef : bucketDefs) {String bucketName = bucketDef.getName();Utils.info("bucketName=" + bucketName);if(bucketScores.get(bucketName)==null){continue;}double aggregateBucketScore = bucketScores.get(bucketName);Utils.info("aggregateBucketScore=" + aggregateBucketScore);double bucketSlideCount = bucketSlideCounts.get(bucketName);Utils.info("bucketSlideCount=" + bucketSlideCount);double defaultWeight = bucketDef.getDefaultWeight();Utils.info("defaultWeight=" + defaultWeight);float percentageBucketScore =(((float)aggregateBucketScore/((float)bucketSlideCount * 10))) * 100;Utils.info("percentageBucketScore=" + percentageBucketScore);float weightedBucketScore =(float)percentageBucketScore * ((float)defaultWeight / 100);Utils.info("weightedBucketScore=" + weightedBucketScore);finalEntityScore += Math.round(weightedBucketScore);}Utils.info("finalEntityScore=" + finalEntityScore);finalScoreByEntityID.put(entityID, new Double(finalEntityScore));}return finalScoreByEntityID;}}