Subversion Repositories SmartDukaan

Rev

Rev 2657 | Rev 3448 | 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;
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
import org.python.core.PyInteger;
13
 
14
/**
15
 * Wrapper around Jython for Comparison Python scripts
16
 * 
17
 * @author naveen
18
 *
19
 */
20
public class CMPJythonWrapper extends JythonWrapper {
21
 
22
	/**
23
	 * Local instance of CMPSlideRuleDefinition - Used to pick script to execute
24
	 */
25
	private ExpandedCMPSlideRuleDefinition 
26
		expandedCMPSlideRuleDefinition = null;
27
 
28
	/**
29
	 * Resets current PythonInterpreter instance
30
	 */
31
	public void reset() {
32
		super.reset();
33
		this.expandedCMPSlideRuleDefinition = null;
34
	}
35
 
36
	/**
37
	 * Executes Comparison rule from ExpandedCMPSlideRuleDefinition instance
38
	 */
82 naveen 39
	public void executeRule() {
70 naveen 40
		if(this.py == null || this.expandedCMPSlideRuleDefinition == null) {
41
			throw new IllegalStateException(
42
					"Not initialized properly");
43
		}
44
 
45
		CMPRuleDefinition cmpRuleDef = 
46
			this.expandedCMPSlideRuleDefinition.getCMPRuleDefinition();
47
 
48
		String pyScript = cmpRuleDef.getScript();
49
		String scriptFullPath = Utils.JYTHON_SRC_PATH + "comparisonrules/" + 
50
			pyScript;
51
 
52
		Utils.info("cmpRuleDef.getID()=" + cmpRuleDef.getID());
53
		Utils.info("scriptFullPath=" + scriptFullPath);
54
 
55
		this.exec(scriptFullPath);
56
	}
57
 
58
	/**
59
	 * @param expandedCMPSlideRuleDefinition the expandedCMPSlideRuleDefinition 
60
	 * to set
61
	 */
62
	public void setExpandedCMPSlideRuleDefinition(
63
			ExpandedCMPSlideRuleDefinition expandedCMPSlideRuleDefinition) {
64
		this.initialize();
65
 
66
		this.expandedCMPSlideRuleDefinition = expandedCMPSlideRuleDefinition;
67
		this.py.set("expCMPSlideRuleDef", expandedCMPSlideRuleDefinition);
68
	}
69
 
70
	/**
71
	 * 
72
	 * @param feature
73
	 */
74
	public void setExpandedSlide(ExpandedSlide expandedSlide) {
75
		this.initialize();
76
 
77
		this.py.set("expSlide", expandedSlide);
78
	}
79
 
2657 rajveer 80
 
70 naveen 81
	/**
2657 rajveer 82
	 * Set the category
83
	 * @param category
84
	 */
85
	public void setCategory(Category category) {
86
		this.initialize();
87
 
88
		this.py.set("categoryObj", category);
89
	}
90
 
91
	/**
70 naveen 92
	 * Return integer score value
93
	 * 
94
	 * @return int score
95
	 */
1918 rajveer 96
	public double getScore() {
70 naveen 97
		if(this.py == null) {
98
			throw new IllegalStateException("Exec is not called yet");
99
		}
100
 
3443 rajveer 101
		PyFloat score;
102
		try{
103
			 score = (PyFloat)this.py.get("score");
104
		}catch(Exception e){
105
			Utils.severe("No score returned from script");
106
			score = null;
107
		}
70 naveen 108
 
71 naveen 109
		if(score == null) {
110
			CMPRuleDefinition cmpRuleDef = 
111
				this.expandedCMPSlideRuleDefinition.getCMPRuleDefinition();
112
 
113
			String pyScript = cmpRuleDef.getScript();
114
			Utils.severe("No score returned from script " + pyScript);
115
			return -1;
116
		}
117
 
1918 rajveer 118
		return score.asDouble();
70 naveen 119
	}
120
}