Subversion Repositories SmartDukaan

Rev

Rev 3448 | 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;
2657 rajveer 7
import in.shop2020.metamodel.definitions.Category;
70 naveen 8
import in.shop2020.metamodel.util.ExpandedCMPSlideRuleDefinition;
9
import in.shop2020.metamodel.util.ExpandedSlide;
10
 
1918 rajveer 11
import org.python.core.PyFloat;
70 naveen 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
 
2657 rajveer 79
 
70 naveen 80
	/**
2657 rajveer 81
	 * Set the category
82
	 * @param category
83
	 */
84
	public void setCategory(Category category) {
85
		this.initialize();
86
 
87
		this.py.set("categoryObj", category);
88
	}
89
 
90
	/**
70 naveen 91
	 * Return integer score value
92
	 * 
93
	 * @return int score
94
	 */
1918 rajveer 95
	public double getScore() {
70 naveen 96
		if(this.py == null) {
97
			throw new IllegalStateException("Exec is not called yet");
98
		}
99
 
100
 
3448 rajveer 101
		PyFloat score = (PyFloat)this.py.get("score");
102
 
71 naveen 103
		if(score == null) {
104
			CMPRuleDefinition cmpRuleDef = 
105
				this.expandedCMPSlideRuleDefinition.getCMPRuleDefinition();
106
 
107
			String pyScript = cmpRuleDef.getScript();
108
			Utils.severe("No score returned from script " + pyScript);
109
			return -1;
110
		}
111
 
1918 rajveer 112
		return score.asDouble();
70 naveen 113
	}
114
}