Subversion Repositories SmartDukaan

Rev

Rev 1061 | Rev 2657 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
70 naveen 1
/**
2
 * 
3
 */
4
package in.shop2020.util;
5
 
6
import in.shop2020.metamodel.definitions.CMPRuleDefinition;
7
import in.shop2020.metamodel.util.ExpandedCMPSlideRuleDefinition;
8
import in.shop2020.metamodel.util.ExpandedSlide;
9
 
1918 rajveer 10
import org.python.core.PyFloat;
70 naveen 11
import org.python.core.PyInteger;
12
 
13
/**
14
 * Wrapper around Jython for Comparison Python scripts
15
 * 
16
 * @author naveen
17
 *
18
 */
19
public class CMPJythonWrapper extends JythonWrapper {
20
 
21
	/**
22
	 * Local instance of CMPSlideRuleDefinition - Used to pick script to execute
23
	 */
24
	private ExpandedCMPSlideRuleDefinition 
25
		expandedCMPSlideRuleDefinition = null;
26
 
27
	/**
28
	 * Resets current PythonInterpreter instance
29
	 */
30
	public void reset() {
31
		super.reset();
32
		this.expandedCMPSlideRuleDefinition = null;
33
	}
34
 
35
	/**
36
	 * Executes Comparison rule from ExpandedCMPSlideRuleDefinition instance
37
	 */
82 naveen 38
	public void executeRule() {
70 naveen 39
		if(this.py == null || this.expandedCMPSlideRuleDefinition == null) {
40
			throw new IllegalStateException(
41
					"Not initialized properly");
42
		}
43
 
44
		CMPRuleDefinition cmpRuleDef = 
45
			this.expandedCMPSlideRuleDefinition.getCMPRuleDefinition();
46
 
47
		String pyScript = cmpRuleDef.getScript();
48
		String scriptFullPath = Utils.JYTHON_SRC_PATH + "comparisonrules/" + 
49
			pyScript;
50
 
51
		Utils.info("cmpRuleDef.getID()=" + cmpRuleDef.getID());
52
		Utils.info("scriptFullPath=" + scriptFullPath);
53
 
54
		this.exec(scriptFullPath);
55
	}
56
 
57
	/**
58
	 * @param expandedCMPSlideRuleDefinition the expandedCMPSlideRuleDefinition 
59
	 * to set
60
	 */
61
	public void setExpandedCMPSlideRuleDefinition(
62
			ExpandedCMPSlideRuleDefinition expandedCMPSlideRuleDefinition) {
63
		this.initialize();
64
 
65
		this.expandedCMPSlideRuleDefinition = expandedCMPSlideRuleDefinition;
66
		this.py.set("expCMPSlideRuleDef", expandedCMPSlideRuleDefinition);
67
	}
68
 
69
	/**
70
	 * 
71
	 * @param feature
72
	 */
73
	public void setExpandedSlide(ExpandedSlide expandedSlide) {
74
		this.initialize();
75
 
76
		this.py.set("expSlide", expandedSlide);
77
	}
78
 
79
	/**
80
	 * Return integer score value
81
	 * 
82
	 * @return int score
83
	 */
1918 rajveer 84
	public double getScore() {
70 naveen 85
		if(this.py == null) {
86
			throw new IllegalStateException("Exec is not called yet");
87
		}
88
 
89
 
1918 rajveer 90
		PyFloat score = (PyFloat)this.py.get("score");
91
 
71 naveen 92
		if(score == null) {
93
			CMPRuleDefinition cmpRuleDef = 
94
				this.expandedCMPSlideRuleDefinition.getCMPRuleDefinition();
95
 
96
			String pyScript = cmpRuleDef.getScript();
97
			Utils.severe("No score returned from script " + pyScript);
98
			return -1;
99
		}
100
 
1918 rajveer 101
		return score.asDouble();
70 naveen 102
	}
103
}