Subversion Repositories SmartDukaan

Rev

Rev 79 | Rev 81 | 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.Slide;
import in.shop2020.metamodel.definitions.BulletDefinition;
import in.shop2020.metamodel.definitions.Catalog;
import in.shop2020.metamodel.definitions.DefinitionsContainer;
import in.shop2020.metamodel.definitions.EntityContainer;
import in.shop2020.metamodel.definitions.FeatureDefinition;
import in.shop2020.util.DBUtils;
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() {
        }
        
        /**
         * Learns all features where definition is marked "learned=true"
         * @throws Exception 
         */
        public void learn() throws Exception {
                Map<Long, List<Bullet>> learnedBullets = 
                        new HashMap<Long, List<Bullet>>();
                
                EntityContainer entContainer = 
                        Catalog.getInstance().getEntityContainer();
                
                Map<Long, Entity> entities = entContainer.getEntities();
                
                for(Entity entity : entities.values()) {
                        List<Slide> slides = entity.getSlides();
                        
                        for(Slide slide : slides) {
                                this.learn(slide, learnedBullets);
                        }
                }
                
                Utils.info("learnedBullets=" + learnedBullets);
                
                String learnedBulletsDBFile = Utils.ENTITIES_DB_PATH + 
                        "learnedbullets.ser";
                
                DBUtils.store(learnedBullets, learnedBulletsDBFile);
        }
        
        /**
         * 
         * @param slide
         * @param learnedBullets
         * @throws Exception 
         */
        private void learn(Slide slide, Map<Long, List<Bullet>> 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;
                                }
                                
                                List<Bullet> bullets = feature.getBullets();
                                if(bullets != null) {
                                        
                                        List<Bullet> collectedBullets = 
                                                learnedBullets.get(new Long(featureDefID));
                                        
                                        if(collectedBullets == null) {
                                                collectedBullets = new ArrayList<Bullet>();
                                        }
                                        
                                        for(Bullet bullet : bullets) {
                                                if(!collectedBullets.contains(bullet)) {
                                                        collectedBullets.add(bullet);
                                                }
                                        }
                                        
                                        learnedBullets.put(new Long(featureDefID), 
                                                        collectedBullets);
                                }
                                
                        }
                }
                
                // Children slides
                List<Slide> childrenSlides = slide.getChildrenSlides();
                
                if(childrenSlides != null) {
                        for(Slide childSlide : childrenSlides) {
                                this.learn(childSlide, learnedBullets);
                        }
                }
        }
}