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.ExpandedBullet;
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
			// Learned 
88
			if(expBulletDef.isLearned()) {
89
				EntityContainer ents = 
90
					Catalog.getInstance().getEntityContainer();
91
 
92
				List<ExpandedBullet> learnedBullets = 
93
					ents.getLearnedBullets(expFeatureDef.getID());
94
				for(ExpandedBullet expBullet : learnedBullets) {
95
 
96
					// REVISIT - for now
97
					possibleValues.add(expBullet.getValue());
98
				}
99
 
100
				this.py.set("possibleValues", possibleValues);
101
			}
102
 
103
			// Fixed
104
			else if (expBulletDef.isEnumerated()) {
105
				EnumDefinition enumDef = 
106
					(EnumDefinition) expBulletDef.getDatatypeDefinition();
107
 
108
				List<EnumValue> enumValues = enumDef.getEnumValues();
109
 
110
				for(EnumValue enumValue : enumValues) {
111
					possibleValues.add(enumValue.getValue());
112
				}
113
 
114
				this.py.set("possibleValues", possibleValues);
115
			}
116
		}	
117
 
118
		// expSlideDef
119
		else if(slideDef != null) {
120
			ExpandedSlideDefinition expSlideDef = 
121
				new ExpandedSlideDefinition(slideDef);
122
 
123
			this.py.set("expSlideDef", expSlideDef);
124
 
125
			List<ExpandedSlideFeatureDefinition> expSlideFeatureDefs = 
126
				expSlideDef.getExpandedSlideFeatureDefinitions();
127
 
128
			for(ExpandedSlideFeatureDefinition expSlideFeatureDef : 
129
					expSlideFeatureDefs) {
130
 
131
				ExpandedFeatureDefinition expFeatureDef = 
132
					expSlideFeatureDef.getExpandedFeatureDefinition();
133
 
134
				possibleValues.add(expFeatureDef.getLabel());
135
			}
136
 
137
			// Feature Labels
138
			this.py.set("possibleValues", possibleValues);
139
		}
140
 
141
		RuleDefinition ruleDef = 
142
			this.expandedFacetDefinition.getIrMetadataRuleDefinition();
143
 
144
		String pyScript = ruleDef.getScript();
145
		String scriptFullPath = Utils.JYTHON_SRC_PATH + "irmetadatarules/" + 
146
			pyScript;
147
 
148
		Utils.info("ruleDef.getID()=" + ruleDef.getID());
149
		Utils.info("scriptFullPath=" + scriptFullPath);
150
 
151
		this.exec(scriptFullPath);
152
	}
153
 
154
	/**
155
	 * 
156
	 * @return
157
	 */
158
	public String getXMLSnippet() {
159
		if(this.py == null) {
160
			throw new IllegalStateException("Exec is not called yet");
161
		}
162
 
163
		PyObject xmlsnippet = this.py.get("xmlsnippet");
164
 
165
		return ((PyString)xmlsnippet).toString();
166
	}
167
}