Subversion Repositories SmartDukaan

Rev

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