Rev 1061 | Rev 1229 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
/****/package in.shop2020.metamodel.util;import in.shop2020.metamodel.core.Bullet;import in.shop2020.metamodel.core.Entity;import in.shop2020.metamodel.core.Feature;import in.shop2020.metamodel.core.PrimitiveDataObject;import in.shop2020.metamodel.core.Slide;import in.shop2020.metamodel.definitions.BulletDefinition;import in.shop2020.metamodel.definitions.Catalog;import in.shop2020.metamodel.definitions.DefinitionsContainer;import in.shop2020.metamodel.definitions.FeatureDefinition;import in.shop2020.util.DBUtils;import in.shop2020.util.Utils;import java.io.File;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import org.apache.commons.lang.ArrayUtils;/**** CN - Content Tool** Currently only has learning logic** @author naveen**/public class CN {/*** @param args*/public static void main(String[] args) throws Exception {String[] commands = new String[] {"learn"};String usage = "Usage: CN learn";String inputCommand = args[0];if(!ArrayUtils.contains(commands, inputCommand)) {System.out.println(usage);System.exit(-1);}if (inputCommand.equals("learn")) {CN cn = new CN();cn.learn();}}/****/public CN() {}/*** Learns all features where definition is marked "learned=true"* @throws Exception*/public void learn(Entity entity) throws Exception {Map<Long, List<ExpandedBullet>> learnedBullets = CreationUtils.getLearnedBullets();if(learnedBullets == null){learnedBullets = new HashMap<Long, List<ExpandedBullet>>();}DefinitionsContainer defs = Catalog.getInstance().getDefinitionsContainer();// Insert BrandList<ExpandedBullet> brandBullets = learnedBullets.get(Utils.BRAND_FEATURE_DEFINITION_ID);if(brandBullets == null){brandBullets = new ArrayList<ExpandedBullet>();learnedBullets.put(Utils.BRAND_FEATURE_DEFINITION_ID, brandBullets);}FeatureDefinition brandFeatureDef = defs.getFeatureDefinition(Utils.BRAND_FEATURE_DEFINITION_ID);BulletDefinition brandBulletDef = brandFeatureDef.getBulletDefinition();Bullet brandBullet = new Bullet(new PrimitiveDataObject(entity.getBrand()));ExpandedBullet expBrandBullet = new ExpandedBullet(brandBullet,brandBulletDef);if(!brandBullets.contains(expBrandBullet)) {brandBullets.add(expBrandBullet);}List<Slide> slides = entity.getSlides();if(slides != null){for(Slide slide : slides) {this.learn(slide, learnedBullets);}}Utils.info("learnedBullets=" + learnedBullets);CreationUtils.storeLearnedBullets(learnedBullets);}/**** @param slide* @param learnedBullets* @throws Exception*/private void learn(Slide slide, Map<Long,List<ExpandedBullet>> learnedBullets) throws Exception {DefinitionsContainer defs =Catalog.getInstance().getDefinitionsContainer();// FeaturesList<Feature> features = slide.getFeatures();if(features != null) {for(Feature feature : features) {Long featureDefID = feature.getFeatureDefinitionID();FeatureDefinition featureDef =defs.getFeatureDefinition(featureDefID);BulletDefinition bulletDef = featureDef.getBulletDefinition();if (!bulletDef.isLearned()) {continue;}// Store expanded bullets to facilitate easy// processing laterExpandedFeature expandedFeature = new ExpandedFeature(feature);List<ExpandedBullet> expBullets =expandedFeature.getExpandedBullets();if(expBullets != null) {List<ExpandedBullet> collectedBullets =learnedBullets.get(new Long(featureDefID));if(collectedBullets == null) {collectedBullets = new ArrayList<ExpandedBullet>();}for(ExpandedBullet expBullet : expBullets) {if(!collectedBullets.contains(expBullet)) {collectedBullets.add(expBullet);}}learnedBullets.put(new Long(featureDefID),collectedBullets);}}}// Children slidesList<Slide> childrenSlides = slide.getChildrenSlides();if(childrenSlides != null) {for(Slide childSlide : childrenSlides) {this.learn(childSlide, learnedBullets);}}}/*** Learns all features where definition is marked "learned=true"* @throws Exception*/public void learn() throws Exception {Map<Long, List<ExpandedBullet>> learnedBullets =new HashMap<Long, List<ExpandedBullet>>();DefinitionsContainer defs =Catalog.getInstance().getDefinitionsContainer();Map<Long, Entity> entities = CreationUtils.getEntities();// Insert BrandList<ExpandedBullet> brandBullets = new ArrayList<ExpandedBullet>();learnedBullets.put(new Long(Utils.BRAND_FEATURE_DEFINITION_ID),brandBullets);FeatureDefinition brandFeatureDef =defs.getFeatureDefinition(Utils.BRAND_FEATURE_DEFINITION_ID);BulletDefinition brandBulletDef = brandFeatureDef.getBulletDefinition();for(Entity entity : entities.values()) {// Brand bulletBullet brandBullet = new Bullet(new PrimitiveDataObject(entity.getBrand()));ExpandedBullet expBrandBullet = new ExpandedBullet(brandBullet,brandBulletDef);if(!brandBullets.contains(expBrandBullet)) {brandBullets.add(expBrandBullet);}List<Slide> slides = entity.getSlides();if(slides != null){for(Slide slide : slides) {this.learn(slide, learnedBullets);}}}Utils.info("learnedBullets=" + learnedBullets);CreationUtils.storeLearnedBullets(learnedBullets);}}