Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
82 naveen 1
/**
2
 * 
3
 */
4
package in.shop2020.util;
5
 
6
import in.shop2020.metamodel.definitions.Catalog;
7
import in.shop2020.metamodel.definitions.EntityContainer;
8
import in.shop2020.metamodel.definitions.EnumDefinition;
9
import in.shop2020.metamodel.definitions.EnumValue;
10
import in.shop2020.metamodel.definitions.FeatureDefinition;
11
import in.shop2020.metamodel.definitions.RuleDefinition;
12
import in.shop2020.metamodel.definitions.SlideDefinition;
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
 
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
				EntityContainer ents = 
104
					Catalog.getInstance().getEntityContainer();
105
 
106
				long facetDefinitionID = this.expandedFacetDefinition.getID();
107
 
108
				possibleValues = ents.getFacetValues(facetDefinitionID);
109
 
110
				Utils.info("possibleValues=" + possibleValues);
111
				this.py.set("possibleValues", possibleValues);
82 naveen 112
			}
91 naveen 113
		}
82 naveen 114
 
115
		// expSlideDef
116
		else if(slideDef != null) {
117
			ExpandedSlideDefinition expSlideDef = 
118
				new ExpandedSlideDefinition(slideDef);
119
 
120
			this.py.set("expSlideDef", expSlideDef);
121
 
122
			List<ExpandedSlideFeatureDefinition> expSlideFeatureDefs = 
123
				expSlideDef.getExpandedSlideFeatureDefinitions();
124
 
125
			for(ExpandedSlideFeatureDefinition expSlideFeatureDef : 
126
					expSlideFeatureDefs) {
127
 
128
				ExpandedFeatureDefinition expFeatureDef = 
129
					expSlideFeatureDef.getExpandedFeatureDefinition();
130
 
131
				possibleValues.add(expFeatureDef.getLabel());
132
			}
133
 
134
			// Feature Labels
90 naveen 135
			Utils.info("possibleValues=" + possibleValues);
82 naveen 136
			this.py.set("possibleValues", possibleValues);
137
		}
138
 
139
		RuleDefinition ruleDef = 
140
			this.expandedFacetDefinition.getIrMetadataRuleDefinition();
141
 
142
		String pyScript = ruleDef.getScript();
143
		String scriptFullPath = Utils.JYTHON_SRC_PATH + "irmetadatarules/" + 
144
			pyScript;
145
 
146
		Utils.info("ruleDef.getID()=" + ruleDef.getID());
147
		Utils.info("scriptFullPath=" + scriptFullPath);
148
 
149
		this.exec(scriptFullPath);
150
	}
151
 
152
	/**
153
	 * 
154
	 * @return
155
	 */
156
	public String getXMLSnippet() {
157
		if(this.py == null) {
158
			throw new IllegalStateException("Exec is not called yet");
159
		}
160
 
161
		PyObject xmlsnippet = this.py.get("xmlsnippet");
162
 
163
		return ((PyString)xmlsnippet).toString();
164
	}
165
}