Subversion Repositories SmartDukaan

Rev

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

Rev 58 Rev 63
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.core.Entity;
-
 
7
import in.shop2020.metamodel.core.Feature;
-
 
8
import in.shop2020.metamodel.core.Slide;
-
 
9
import in.shop2020.metamodel.definitions.FeatureDefinition;
-
 
10
import in.shop2020.metamodel.definitions.RuleDefinition;
-
 
11
import in.shop2020.metamodel.definitions.SlideDefinition;
-
 
12
import in.shop2020.metamodel.util.ExpandedFacetDefinition;
-
 
13
 
6
import java.util.List;
14
import java.util.List;
7
 
15
 
8
import org.python.core.PyList;
16
import org.python.core.PyList;
9
import org.python.core.PyObject;
17
import org.python.core.PyObject;
10
import org.python.util.PythonInterpreter;
18
import org.python.util.PythonInterpreter;
Line 14... Line 22...
14
 *
22
 *
15
 */
23
 */
16
public class JythonWrapper {
24
public class JythonWrapper {
17
	
25
	
18
	/**
26
	/**
19
	 * Jython source path
27
	 * Private Python interpreter instance
20
	 */
28
	 */
21
	public static String JYTHON_SRC_PATH = "src/jython/";
29
	private PythonInterpreter py = null;
22
	
30
	
23
	/**
31
	/**
24
	 * Private Python interpreter instance
32
	 * Local instance of Facet Definition - Used to pick script to execute
25
	 */
33
	 */
-
 
34
	private ExpandedFacetDefinition expandedFacetDefinition = null;
-
 
35
	
-
 
36
	/**
26
	private PythonInterpreter py = null;
37
	 * Resets current PythonInterpreter instance
-
 
38
	 */
-
 
39
	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 setEntity(Entity entity) {
-
 
63
		if(this.py == null) {
-
 
64
			this.py = new PythonInterpreter();
-
 
65
		}
-
 
66
		
-
 
67
		this.py.set("entity", entity);
-
 
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 setFeature(Feature feature) {
-
 
87
		if(this.py == null) {
-
 
88
			this.py = new PythonInterpreter();
-
 
89
		}
-
 
90
		
-
 
91
		this.py.set("feature", feature);
-
 
92
	}
-
 
93
	
-
 
94
	/**
-
 
95
	 * 
-
 
96
	 * @param slideDefinition
-
 
97
	 */
-
 
98
	public void setSlideDefinition(SlideDefinition slideDefinition) {
-
 
99
		if(this.py == null) {
-
 
100
			this.py = new PythonInterpreter();
-
 
101
		}
-
 
102
		
-
 
103
		this.py.set("slideDef", slideDefinition);
-
 
104
	}
-
 
105
	
-
 
106
	/**
-
 
107
	 * 
-
 
108
	 * @param slide
-
 
109
	 */
-
 
110
	public void setSlide(Slide slide) {
-
 
111
		if(this.py == null) {
-
 
112
			this.py = new PythonInterpreter();
-
 
113
		}
-
 
114
		
-
 
115
		this.py.set("slide", slide);
-
 
116
	}
-
 
117
	
-
 
118
	/**
-
 
119
	 * Executes IR Data rule from ExpandedFacetDefinition instance
-
 
120
	 */
-
 
121
	public void execIRDataRule() {
-
 
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);
-
 
136
	}
27
	
137
	
28
	/**
138
	/**
29
	 * Executes Jython script given absolute path
139
	 * Executes Jython script given absolute path
30
	 * 
140
	 * 
31
	 * @param filename
141
	 * @param filename
Line 42... Line 152...
42
	 * 
152
	 * 
43
	 * @return
153
	 * @return
44
	 */
154
	 */
45
	@SuppressWarnings("unchecked")
155
	@SuppressWarnings("unchecked")
46
	public List getValues() {
156
	public List getValues() {
-
 
157
		if(this.py == null) {
-
 
158
			throw new IllegalStateException("Exec is not called yet");
-
 
159
		}
-
 
160
		
47
		PyObject values = this.py.get("values");
161
		PyObject values = this.py.get("values");
48
		
162
		
49
		return (PyList)values;
163
		return (PyList)values;
50
	}
164
	}
51
}
165
}