Subversion Repositories SmartDukaan

Rev

Rev 3448 | Blame | Compare with Previous | Last modification | View Log | RSS feed

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

import in.shop2020.metamodel.definitions.CMPRuleDefinition;
import in.shop2020.metamodel.definitions.Category;
import in.shop2020.metamodel.util.ExpandedCMPSlideRuleDefinition;
import in.shop2020.metamodel.util.ExpandedSlide;

import org.python.core.PyFloat;

/**
 * Wrapper around Jython for Comparison Python scripts
 * 
 * @author naveen
 *
 */
public class CMPJythonWrapper extends JythonWrapper {
        
        /**
         * Local instance of CMPSlideRuleDefinition - Used to pick script to execute
         */
        private ExpandedCMPSlideRuleDefinition 
                expandedCMPSlideRuleDefinition = null;
        
        /**
         * Resets current PythonInterpreter instance
         */
        public void reset() {
                super.reset();
                this.expandedCMPSlideRuleDefinition = null;
        }
        
        /**
         * Executes Comparison rule from ExpandedCMPSlideRuleDefinition instance
         */
        public void executeRule() {
                if(this.py == null || this.expandedCMPSlideRuleDefinition == null) {
                        throw new IllegalStateException(
                                        "Not initialized properly");
                }
                
                CMPRuleDefinition cmpRuleDef = 
                        this.expandedCMPSlideRuleDefinition.getCMPRuleDefinition();
                
                String pyScript = cmpRuleDef.getScript();
                String scriptFullPath = Utils.JYTHON_SRC_PATH + "comparisonrules/" + 
                        pyScript;
                
                Utils.info("cmpRuleDef.getID()=" + cmpRuleDef.getID());
                Utils.info("scriptFullPath=" + scriptFullPath);
                
                this.exec(scriptFullPath);
        }
        
        /**
         * @param expandedCMPSlideRuleDefinition the expandedCMPSlideRuleDefinition 
         * to set
         */
        public void setExpandedCMPSlideRuleDefinition(
                        ExpandedCMPSlideRuleDefinition expandedCMPSlideRuleDefinition) {
                this.initialize();
                
                this.expandedCMPSlideRuleDefinition = expandedCMPSlideRuleDefinition;
                this.py.set("expCMPSlideRuleDef", expandedCMPSlideRuleDefinition);
        }
        
        /**
         * 
         * @param feature
         */
        public void setExpandedSlide(ExpandedSlide expandedSlide) {
                this.initialize();
                
                this.py.set("expSlide", expandedSlide);
        }
        

        /**
         * Set the category
         * @param category
         */
        public void setCategory(Category category) {
                this.initialize();
                
                this.py.set("categoryObj", category);
        }
        
        /**
         * Return integer score value
         * 
         * @return int score
         */
        public double getScore() {
                if(this.py == null) {
                        throw new IllegalStateException("Exec is not called yet");
                }
                
                
                PyFloat score = (PyFloat)this.py.get("score");
                
                if(score == null) {
                        CMPRuleDefinition cmpRuleDef = 
                                this.expandedCMPSlideRuleDefinition.getCMPRuleDefinition();
                        
                        String pyScript = cmpRuleDef.getScript();
                        Utils.severe("No score returned from script " + pyScript);
                        return -1;
                }
                
                return score.asDouble();
        }
}