Subversion Repositories SmartDukaan

Rev

Rev 65 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 65 Rev 70
Line 1... Line 1...
1
/**
1
/**
2
 * 
2
 * 
3
 */
3
 */
4
package in.shop2020.util;
4
package in.shop2020.util;
5
 
5
 
6
import in.shop2020.metamodel.definitions.FeatureDefinition;
-
 
7
import in.shop2020.metamodel.definitions.RuleDefinition;
-
 
8
import in.shop2020.metamodel.definitions.SlideDefinition;
-
 
9
import in.shop2020.metamodel.util.ExpandedEntity;
-
 
10
import in.shop2020.metamodel.util.ExpandedFacetDefinition;
-
 
11
import in.shop2020.metamodel.util.ExpandedFeature;
-
 
12
import in.shop2020.metamodel.util.ExpandedSlide;
-
 
13
 
-
 
14
import java.util.List;
-
 
15
 
-
 
16
import org.python.core.PyList;
-
 
17
import org.python.core.PyObject;
-
 
18
import org.python.util.PythonInterpreter;
6
import org.python.util.PythonInterpreter;
19
 
7
 
20
/**
8
/**
-
 
9
 * Wrapper around work done by Jython scripts
-
 
10
 * 
21
 * @author naveen
11
 * @author naveen
22
 *
12
 *
23
 */
13
 */
24
public class JythonWrapper {
14
public class JythonWrapper {
25
	
15
	
26
	/**
16
	/**
27
	 * Private Python interpreter instance
17
	 * Private Python interpreter instance
28
	 */
18
	 */
29
	private PythonInterpreter py = null;
19
	protected PythonInterpreter py = null;
30
	
20
 
31
	/**
-
 
32
	 * Local instance of Facet Definition - Used to pick script to execute
-
 
33
	 */
-
 
34
	private ExpandedFacetDefinition expandedFacetDefinition = null;
-
 
35
	
21
	
36
	/**
22
	/**
37
	 * Resets current PythonInterpreter instance
23
	 * Initialises PythonInterpreter instance 
38
	 */
24
	 */
39
	public void initialize() {
25
	public void initialize() {
40
		this.py = null;
-
 
41
		this.expandedFacetDefinition = null;
-
 
42
	}
-
 
43
	
-
 
44
	/**
-
 
45
	 * 
-
 
46
	 * @param expandedFacetDefinition
-
 
47
	 */
-
 
48
	public void setExpandedFacetDefinition(
-
 
49
			ExpandedFacetDefinition expandedFacetDefinition) {
-
 
50
		if(this.py == null) {
-
 
51
			this.py = new PythonInterpreter();
-
 
52
		}
-
 
53
		
-
 
54
		this.expandedFacetDefinition = expandedFacetDefinition;
-
 
55
		this.py.set("expFacetDef", expandedFacetDefinition);
-
 
56
	}
-
 
57
	
-
 
58
	/**
-
 
59
	 * 
-
 
60
	 * @param entity
-
 
61
	 */
-
 
62
	public void setExpandedEntity(ExpandedEntity expEntity) {
-
 
63
		if(this.py == null) {
-
 
64
			this.py = new PythonInterpreter();
-
 
65
		}
-
 
66
		
-
 
67
		this.py.set("expEntity", expEntity);
-
 
68
	}
-
 
69
	
-
 
70
	/**
-
 
71
	 * 
-
 
72
	 * @param featureDefinition
-
 
73
	 */
-
 
74
	public void setFeatureDefinition(FeatureDefinition featureDefinition) {
-
 
75
		if(this.py == null) {
-
 
76
			this.py = new PythonInterpreter();
-
 
77
		}
-
 
78
		
-
 
79
		this.py.set("featureDef", featureDefinition);
-
 
80
	}
-
 
81
	
-
 
82
	/**
-
 
83
	 * 
-
 
84
	 * @param feature
-
 
85
	 */
-
 
86
	public void setExpandedFeature(ExpandedFeature expandedFeature) {
-
 
87
		if(this.py == null) {
-
 
88
			this.py = new PythonInterpreter();
-
 
89
		}
-
 
90
		
-
 
91
		this.py.set("expFeature", expandedFeature);
-
 
92
	}
-
 
93
	
-
 
94
	/**
-
 
95
	 * 
-
 
96
	 * @param slideDefinition
-
 
97
	 */
-
 
98
	public void setSlideDefinition(SlideDefinition slideDefinition) {
-
 
99
		if(this.py == null) {
26
		if(this.py == null) {
100
			this.py = new PythonInterpreter();
27
			this.py = new PythonInterpreter();
101
		}
28
		}
102
		
-
 
103
		this.py.set("slideDef", slideDefinition);
-
 
104
	}
29
	}
105
	
30
	
106
	/**
31
	/**
107
	 * 
-
 
108
	 * @param slide
-
 
109
	 */
-
 
110
	public void setExpandedSlide(ExpandedSlide expSlide) {
-
 
111
		if(this.py == null) {
-
 
112
			this.py = new PythonInterpreter();
32
	 * Resets current PythonInterpreter instance
113
		}
-
 
114
		
-
 
115
		this.py.set("expSlide", expSlide);
-
 
116
	}
-
 
117
	
-
 
118
	/**
-
 
119
	 * Executes IR Data rule from ExpandedFacetDefinition instance
-
 
120
	 */
33
	 */
121
	public void execIRDataRule() {
34
	public void reset() {
122
		if(this.py == null || this.expandedFacetDefinition == null) {
-
 
123
			throw new IllegalStateException(
-
 
124
					"Not initialized properly");
-
 
125
		}
-
 
126
		
-
 
127
		RuleDefinition irdataRuleDef = 
-
 
128
			this.expandedFacetDefinition.getIrdataRuleDefinition();
-
 
129
		
-
 
130
		String pyScript = irdataRuleDef.getScript();
-
 
131
		String scriptFullPath = IR.JYTHON_SRC_PATH + "irdatarules/" + pyScript;
-
 
132
		Utils.info("irdataRuleDef.getID()=" + irdataRuleDef.getID());
-
 
133
		Utils.info("scriptFullPath=" + scriptFullPath);
-
 
134
		
-
 
135
		this.exec(scriptFullPath);
35
		this.py = null;
136
	}
36
	}
137
	
37
	
138
	/**
38
	/**
139
	 * Executes Jython script given absolute path
39
	 * Executes Jython script given absolute path
140
	 * 
40
	 * 
141
	 * @param filename
41
	 * @param filename
142
	 */
42
	 */
143
	public void exec(String filename) {
43
	public void exec(String filename) {
144
		if(this.py == null) {
44
		this.initialize();
145
			this.py = new PythonInterpreter();
-
 
146
		}
-
 
147
		
45
		
148
		this.py.execfile(filename);
46
		this.py.execfile(filename);
149
	}
47
	}
150
	
-
 
151
	/**
-
 
152
	 * 
-
 
153
	 * @return
-
 
154
	 */
-
 
155
	@SuppressWarnings("unchecked")
-
 
156
	public List getValues() {
-
 
157
		if(this.py == null) {
-
 
158
			throw new IllegalStateException("Exec is not called yet");
-
 
159
		}
-
 
160
		
-
 
161
		PyObject values = this.py.get("values");
-
 
162
		
-
 
163
		return (PyList)values;
-
 
164
	}
-
 
165
}
48
}