Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
58 naveen 1
/**
2
 * 
3
 */
4
package in.shop2020.util;
5
 
63 naveen 6
import in.shop2020.metamodel.core.Feature;
7
import in.shop2020.metamodel.core.Slide;
8
import in.shop2020.metamodel.definitions.FeatureDefinition;
9
import in.shop2020.metamodel.definitions.RuleDefinition;
10
import in.shop2020.metamodel.definitions.SlideDefinition;
64 naveen 11
import in.shop2020.metamodel.util.ExpandedEntity;
63 naveen 12
import in.shop2020.metamodel.util.ExpandedFacetDefinition;
13
 
58 naveen 14
import java.util.List;
15
 
16
import org.python.core.PyList;
17
import org.python.core.PyObject;
18
import org.python.util.PythonInterpreter;
19
 
20
/**
21
 * @author naveen
22
 *
23
 */
24
public class JythonWrapper {
25
 
26
	/**
63 naveen 27
	 * Private Python interpreter instance
58 naveen 28
	 */
63 naveen 29
	private PythonInterpreter py = null;
58 naveen 30
 
31
	/**
63 naveen 32
	 * Local instance of Facet Definition - Used to pick script to execute
58 naveen 33
	 */
63 naveen 34
	private ExpandedFacetDefinition expandedFacetDefinition = null;
58 naveen 35
 
36
	/**
63 naveen 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
	 */
64 naveen 62
	public void setExpandedEntity(ExpandedEntity expEntity) {
63 naveen 63
		if(this.py == null) {
64
			this.py = new PythonInterpreter();
65
		}
66
 
64 naveen 67
		this.py.set("expEntity", expEntity);
63 naveen 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
	}
137
 
138
	/**
58 naveen 139
	 * Executes Jython script given absolute path
140
	 * 
141
	 * @param filename
142
	 */
143
	public void exec(String filename) {
144
		if(this.py == null) {
145
			this.py = new PythonInterpreter();
146
		}
147
 
148
		this.py.execfile(filename);
149
	}
150
 
151
	/**
152
	 * 
153
	 * @return
154
	 */
155
	@SuppressWarnings("unchecked")
156
	public List getValues() {
63 naveen 157
		if(this.py == null) {
158
			throw new IllegalStateException("Exec is not called yet");
159
		}
160
 
58 naveen 161
		PyObject values = this.py.get("values");
162
 
163
		return (PyList)values;
164
	}
165
}