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