Subversion Repositories SmartDukaan

Rev

Rev 4676 | 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.EnumValue;
import in.shop2020.metamodel.definitions.FeatureDefinition;
import in.shop2020.util.Utils;

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() {
        }


        private void addHelpdoc(Map<Long, List<Long>> helpdocEntityIds, long helpdocId, long entityId){
                List<Long> entityIds = helpdocEntityIds.get(helpdocId);
                if(entityIds == null){
                        entityIds = new ArrayList<Long>();
                        helpdocEntityIds.put(helpdocId, entityIds);
                }
                if(!entityIds.contains(entityId)){
                        entityIds.add(entityId);
                }
        }
        
        public void learnHelpdocs(Entity entity) throws Exception {
                ExpandedEntity expEntity = new ExpandedEntity(entity);
                long entityId = expEntity.getID();
                Long helpdocId = null;
                Map<Long, List<Long>> helpdocEntityIds =  CreationUtils.getHelpdocEntityIds();
                if(helpdocEntityIds == null){
                        helpdocEntityIds = new HashMap<Long, List<Long>>();
                }

                List<ExpandedSlide> expSlides = expEntity.getExpandedSlides();
                if(expSlides != null){
                        for(ExpandedSlide expSlide: expSlides){
                                helpdocId = expSlide.getSlideDefinition().getHelpDocDefinitionID();
                                if(! (helpdocId == null || helpdocId == 0)){
                                        addHelpdoc(helpdocEntityIds, helpdocId, entityId);
                                }
                                
                                List<ExpandedFeature>  expFeatures = expSlide.getExpandedFeatures();
                                if(expFeatures != null){
                                        for(ExpandedFeature expFeature: expFeatures){
                                                helpdocId = expFeature.getFeatureDefinition().getHelpDocDefinitionID();
                                                if(! (helpdocId == null || helpdocId == 0)){
                                                        addHelpdoc(helpdocEntityIds, helpdocId, entityId);                              
                                                }
                                                List<ExpandedBullet> expBullets = expFeature.getExpandedBullets();
                                                if(expBullets != null){
                                                        for(ExpandedBullet expBullet: expBullets){
                                                                if(expBullet.isEnumerated()){
                                                                        EnumValue enumValue = expBullet.getExpandedEnumDataObject().getEnumValue();
                                                                        if(enumValue != null){
                                                                                helpdocId = enumValue.getHelpDocDefinitionID();
                                                                                if(! (helpdocId == null || helpdocId == 0)){
                                                                                        addHelpdoc(helpdocEntityIds, helpdocId, entityId);                              
                                                                                }
                                                                        }
                                                                }
                                                        }
                                                }
                                        }
                                }                       
                        }
                        CreationUtils.storeHelpdocEntityIds(helpdocEntityIds);
                }
                
        }
        
        /**
         * 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 Brand
                List<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);
                //learnedBullets.toString().length()
                
        }

        
        /**
         * 
         * @param slide
         * @param learnedBullets
         * @throws Exception 
         */
        private void learn(Slide slide, Map<Long, 
                        List<ExpandedBullet>> learnedBullets) throws Exception {
                
                DefinitionsContainer defs = 
                        Catalog.getInstance().getDefinitionsContainer();
                
                // Features
                List<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 later
                                ExpandedFeature 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>();
                                                learnedBullets.put(new Long(featureDefID), collectedBullets);
                                        }
                                        
                                        for(ExpandedBullet expBullet : expBullets) {
                                                if(!collectedBullets.contains(expBullet)) {             
                                                        collectedBullets.add(expBullet);
                                                }
                                        }
                                        
                                }
                                
                        }
                }
                
                // Children slides
                List<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 Brand
                List<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 bullet
                        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);
                
        }

}