Subversion Repositories SmartDukaan

Rev

Rev 1104 | Rev 1314 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
82 naveen 1
/**
2
 * 
3
 */
4
package in.shop2020.util;
5
 
1153 rajveer 6
 
82 naveen 7
import in.shop2020.metamodel.definitions.EnumDefinition;
8
import in.shop2020.metamodel.definitions.EnumValue;
9
import in.shop2020.metamodel.definitions.FeatureDefinition;
10
import in.shop2020.metamodel.definitions.RuleDefinition;
11
import in.shop2020.metamodel.definitions.SlideDefinition;
1050 rajveer 12
import in.shop2020.metamodel.util.CreationUtils;
82 naveen 13
import in.shop2020.metamodel.util.ExpandedBulletDefinition;
14
import in.shop2020.metamodel.util.ExpandedFacetDefinition;
15
import in.shop2020.metamodel.util.ExpandedFeatureDefinition;
16
import in.shop2020.metamodel.util.ExpandedSlideDefinition;
17
import in.shop2020.metamodel.util.ExpandedSlideFeatureDefinition;
18
 
19
import java.util.ArrayList;
20
import java.util.List;
21
 
22
import org.python.core.PyObject;
23
import org.python.core.PyString;
24
 
25
/**
26
 * Wrapper around Jython for Meta-data related to Information Retrieval data
27
 * 
28
 * @author naveen
29
 *
30
 */
31
public class IRMetaDataJythonWrapper extends JythonWrapper {
32
 
33
	/**
34
	 * Local instance of Facet Definition - Used to pick script to execute
35
	 */
36
	private ExpandedFacetDefinition expandedFacetDefinition = null;
37
 
38
	/**
39
	 * Resets current PythonInterpreter instance
40
	 */
41
	public void reset() {
42
		super.reset();
43
		this.expandedFacetDefinition = null;
44
	}
45
 
46
	/**
47
	 * 
48
	 * @param expandedFacetDefinition
49
	 */
50
	public void setExpandedFacetDefinition(
51
			ExpandedFacetDefinition expandedFacetDefinition) {
52
		this.initialize();
53
 
54
		this.expandedFacetDefinition = expandedFacetDefinition;
55
		this.py.set("expFacetDef", expandedFacetDefinition);
56
	}
57
 
58
	/**
59
	 * Executes IR Data rule from ExpandedFacetDefinition instance
60
	 * @throws Exception 
61
	 */
62
	public void executeRule() throws Exception {
63
		if(this.py == null || this.expandedFacetDefinition == null) {
64
			throw new IllegalStateException(
65
					"Not initialized properly");
66
		}
67
 
68
		FeatureDefinition featureDef = 
69
			this.expandedFacetDefinition.getFeatureDefinition();
70
 
71
		SlideDefinition slideDef = 
72
			this.expandedFacetDefinition.getSlideDefinition();
73
 
74
		List<String> possibleValues = new ArrayList<String>();
75
 
76
		// expFeatureDef
77
		if(featureDef != null) {
78
			ExpandedFeatureDefinition expFeatureDef = 
79
				new ExpandedFeatureDefinition(featureDef);
80
 
81
			this.py.set("expFeatureDef", expFeatureDef);
82
 
83
			ExpandedBulletDefinition expBulletDef = 
84
				expFeatureDef.getExpandedBulletDefinition();
85
 
86
			// Fixed
91 naveen 87
			if (expBulletDef.isEnumerated()) {
82 naveen 88
				EnumDefinition enumDef = 
89
					(EnumDefinition) expBulletDef.getDatatypeDefinition();
90
 
91
				List<EnumValue> enumValues = enumDef.getEnumValues();
92
 
93
				for(EnumValue enumValue : enumValues) {
94
					possibleValues.add(enumValue.getValue());
95
				}
90 naveen 96
 
1104 rajveer 97
				//Utils.info("possibleValues=" + possibleValues);
82 naveen 98
				this.py.set("possibleValues", possibleValues);
91 naveen 99
			} 
100
 
101
			// Get values collected across entities
102
			else {
103
 
104
				long facetDefinitionID = this.expandedFacetDefinition.getID();
105
 
106
 
1104 rajveer 107
				possibleValues = CreationUtils.getFacetValues(facetDefinitionID);
1072 rajveer 108
 
1104 rajveer 109
				//Utils.info("possibleValues=" + possibleValues);
91 naveen 110
				this.py.set("possibleValues", possibleValues);
82 naveen 111
			}
91 naveen 112
		}
82 naveen 113
 
114
		// expSlideDef
115
		else if(slideDef != null) {
116
			ExpandedSlideDefinition expSlideDef = 
117
				new ExpandedSlideDefinition(slideDef);
118
 
119
			this.py.set("expSlideDef", expSlideDef);
120
 
121
			List<ExpandedSlideFeatureDefinition> expSlideFeatureDefs = 
122
				expSlideDef.getExpandedSlideFeatureDefinitions();
123
 
124
			for(ExpandedSlideFeatureDefinition expSlideFeatureDef : 
125
					expSlideFeatureDefs) {
126
 
127
				ExpandedFeatureDefinition expFeatureDef = 
128
					expSlideFeatureDef.getExpandedFeatureDefinition();
129
 
130
				possibleValues.add(expFeatureDef.getLabel());
131
			}
132
 
133
			// Feature Labels
1104 rajveer 134
			//Utils.info("possibleValues=" + possibleValues);
82 naveen 135
			this.py.set("possibleValues", possibleValues);
136
		}
137
 
138
		RuleDefinition ruleDef = 
139
			this.expandedFacetDefinition.getIrMetadataRuleDefinition();
140
 
141
		String pyScript = ruleDef.getScript();
142
		String scriptFullPath = Utils.JYTHON_SRC_PATH + "irmetadatarules/" + 
143
			pyScript;
144
 
145
		Utils.info("ruleDef.getID()=" + ruleDef.getID());
146
		Utils.info("scriptFullPath=" + scriptFullPath);
147
 
148
		this.exec(scriptFullPath);
149
	}
150
 
151
	/**
152
	 * 
153
	 * @return
154
	 */
155
	public String getXMLSnippet() {
156
		if(this.py == null) {
157
			throw new IllegalStateException("Exec is not called yet");
158
		}
159
 
160
		PyObject xmlsnippet = this.py.get("xmlsnippet");
161
 
162
		return ((PyString)xmlsnippet).toString();
163
	}
164
}