Subversion Repositories SmartDukaan

Rev

Rev 63 | Blame | Last modification | View Log | RSS feed

/**
 * 
 */
package in.shop2020.util;

import in.shop2020.metamodel.core.Feature;
import in.shop2020.metamodel.core.Slide;
import in.shop2020.metamodel.definitions.FeatureDefinition;
import in.shop2020.metamodel.definitions.RuleDefinition;
import in.shop2020.metamodel.definitions.SlideDefinition;
import in.shop2020.metamodel.util.ExpandedEntity;
import in.shop2020.metamodel.util.ExpandedFacetDefinition;

import java.util.List;

import org.python.core.PyList;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;

/**
 * @author naveen
 *
 */
public class JythonWrapper {
        
        /**
         * Private Python interpreter instance
         */
        private PythonInterpreter py = null;
        
        /**
         * Local instance of Facet Definition - Used to pick script to execute
         */
        private ExpandedFacetDefinition expandedFacetDefinition = null;
        
        /**
         * Resets current PythonInterpreter instance
         */
        public void initialize() {
                this.py = null;
                this.expandedFacetDefinition = null;
        }
        
        /**
         * 
         * @param expandedFacetDefinition
         */
        public void setExpandedFacetDefinition(
                        ExpandedFacetDefinition expandedFacetDefinition) {
                if(this.py == null) {
                        this.py = new PythonInterpreter();
                }
                
                this.expandedFacetDefinition = expandedFacetDefinition;
                this.py.set("expFacetDef", expandedFacetDefinition);
        }
        
        /**
         * 
         * @param entity
         */
        public void setExpandedEntity(ExpandedEntity expEntity) {
                if(this.py == null) {
                        this.py = new PythonInterpreter();
                }
                
                this.py.set("expEntity", expEntity);
        }
        
        /**
         * 
         * @param featureDefinition
         */
        public void setFeatureDefinition(FeatureDefinition featureDefinition) {
                if(this.py == null) {
                        this.py = new PythonInterpreter();
                }
                
                this.py.set("featureDef", featureDefinition);
        }
        
        /**
         * 
         * @param feature
         */
        public void setFeature(Feature feature) {
                if(this.py == null) {
                        this.py = new PythonInterpreter();
                }
                
                this.py.set("feature", feature);
        }
        
        /**
         * 
         * @param slideDefinition
         */
        public void setSlideDefinition(SlideDefinition slideDefinition) {
                if(this.py == null) {
                        this.py = new PythonInterpreter();
                }
                
                this.py.set("slideDef", slideDefinition);
        }
        
        /**
         * 
         * @param slide
         */
        public void setSlide(Slide slide) {
                if(this.py == null) {
                        this.py = new PythonInterpreter();
                }
                
                this.py.set("slide", slide);
        }
        
        /**
         * Executes IR Data rule from ExpandedFacetDefinition instance
         */
        public void execIRDataRule() {
                if(this.py == null || this.expandedFacetDefinition == null) {
                        throw new IllegalStateException(
                                        "Not initialized properly");
                }
                
                RuleDefinition irdataRuleDef = 
                        this.expandedFacetDefinition.getIrdataRuleDefinition();
                
                String pyScript = irdataRuleDef.getScript();
                String scriptFullPath = IR.JYTHON_SRC_PATH + "irdatarules/" + pyScript;
                Utils.info("irdataRuleDef.getID()=" + irdataRuleDef.getID());
                Utils.info("scriptFullPath=" + scriptFullPath);
                
                this.exec(scriptFullPath);
        }
        
        /**
         * Executes Jython script given absolute path
         * 
         * @param filename
         */
        public void exec(String filename) {
                if(this.py == null) {
                        this.py = new PythonInterpreter();
                }
                
                this.py.execfile(filename);
        }
        
        /**
         * 
         * @return
         */
        @SuppressWarnings("unchecked")
        public List getValues() {
                if(this.py == null) {
                        throw new IllegalStateException("Exec is not called yet");
                }
                
                PyObject values = this.py.get("values");
                
                return (PyList)values;
        }
}