001: package org.drools.eclipse.editors.rete.model;
002:
003: import java.util.ArrayList;
004: import java.util.HashMap;
005: import java.util.List;
006: import java.util.Map;
007:
008: import org.drools.reteoo.AccumulateNodeVertex;
009: import org.drools.reteoo.AlphaNodeVertex;
010: import org.drools.reteoo.BaseVertex;
011: import org.drools.reteoo.CollectNodeVertex;
012: import org.drools.reteoo.EvalConditionNodeVertex;
013: import org.drools.reteoo.ExistsNodeVertex;
014: import org.drools.reteoo.FromNodeVertex;
015: import org.drools.reteoo.JoinNodeVertex;
016: import org.drools.reteoo.LeftInputAdapterNodeVertex;
017: import org.drools.reteoo.NotNodeVertex;
018: import org.drools.reteoo.ObjectTypeNodeVertex;
019: import org.drools.reteoo.QueryTerminalNodeVertex;
020: import org.drools.reteoo.ReteVertex;
021: import org.drools.reteoo.RightInputAdapterNodeVertex;
022: import org.drools.reteoo.RuleTerminalNodeVertex;
023: import org.drools.spi.Constraint;
024: import org.eclipse.ui.views.properties.IPropertyDescriptor;
025: import org.eclipse.ui.views.properties.IPropertySource;
026: import org.eclipse.ui.views.properties.PropertyDescriptor;
027:
028: /**
029: * Providing IPropertySource for property sheets that show
030: * rete graph vertex properties.
031: *
032: */
033: public class VertexPropertySource implements IPropertySource {
034:
035: private static final String VERTEX_FROM = "From BaseVertex";
036:
037: private static final String VERTEX_ACCUMULATE = "Accumulate BaseVertex";
038:
039: private static final String VERTEX_COLLECT = "Collect BaseVertex";
040:
041: private static final String VERTEX_EXISTS = "Exists BaseVertex";
042:
043: private static final String VERTEX_TERMINAL = "Terminal BaseVertex";
044:
045: private static final String VERTEX_QUERY_TERMINAL = "Query Terminal BaseVertex";
046:
047: private static final String VERTEX_EVAL_CONDITION = "Eval Condition BaseVertex";
048:
049: private static final String VERTEX_NOT = "Not BaseVertex";
050:
051: private static final String VERTEX_JOIN = "Join BaseVertex";
052:
053: private static final String VERTEX_RIGHT_INPUT_ADAPTER = "Right Input Adapter BaseVertex";
054:
055: private static final String VERTEX_LEFT_INPUT_ADAPTER = "Left Input Adapter BaseVertex";
056:
057: private static final String VERTEX_ALPHA = "Alpha BaseVertex";
058:
059: private static final String VERTEX_OBJECT_TYPE = "Object Type BaseVertex";
060:
061: private static final String VERTEX_RETE = "Rete BaseVertex";
062:
063: private static final String CONSTRAINT_CAP = "Constraint";
064:
065: private static final String CONSTRAINT = "constraint";
066:
067: public String ID_ROOT = "vertex"; //$NON-NLS-1$
068:
069: public String ID_ID = "id"; //$NON-NLS-1$
070: public String ID_HTML = "html"; //$NON-NLS-1$
071:
072: private IPropertyDescriptor[] descriptors;
073:
074: private final IPropertyDescriptor PROP_NAME = new PropertyDescriptor(
075: "name", "Name");
076: private final IPropertyDescriptor PROP_ID = new PropertyDescriptor(
077: "id", "ID");
078: private final IPropertyDescriptor PROP_RULE = new PropertyDescriptor(
079: "rule", "Rule");
080:
081: private final IPropertyDescriptor PROP_QUERY = new PropertyDescriptor(
082: "query", "Query");
083: // Alpha-specific
084: private final IPropertyDescriptor PROP_ALPHA_FIELD_NAME = new PropertyDescriptor(
085: "fieldName", "Field Name");
086: private final IPropertyDescriptor PROP_ALPHA_EVALUATOR = new PropertyDescriptor(
087: "evaluator", "Evaluator");
088: private final IPropertyDescriptor PROP_ALPHA_VALUE = new PropertyDescriptor(
089: "value", "Value");
090:
091: // ObjectType specific
092: private final IPropertyDescriptor PROP_OBJ_TYPE = new PropertyDescriptor(
093: "objectType", "Object Type");
094:
095: private final static String CAT_GENERAL = "General";
096: private final static String CAT_OTHER = "Other";
097:
098: protected BaseVertex vertex = null;
099:
100: // Map<String,NodeValue>
101: private Map values = new HashMap();
102:
103: /**
104: * Constructor initializing properties from <code>vertex</code>
105: *
106: * @param vertex source vertex for this property source
107: */
108: public VertexPropertySource(BaseVertex vertex) {
109: this .vertex = vertex;
110:
111: initProperties(vertex);
112: }
113:
114: final private void initProperties(BaseVertex vertex) {
115:
116: List descriptorList = new ArrayList();
117:
118: if (vertex instanceof ExistsNodeVertex) {
119: initExistsNodeProperties((ExistsNodeVertex) vertex,
120: descriptorList, values);
121: } else if (vertex instanceof FromNodeVertex) {
122: initFromNodeProperties((FromNodeVertex) vertex,
123: descriptorList, values);
124: } else if (vertex instanceof AccumulateNodeVertex) {
125: initAccumulateNodeProperties((AccumulateNodeVertex) vertex,
126: descriptorList, values);
127: } else if (vertex instanceof CollectNodeVertex) {
128: initCollectNodeProperties((CollectNodeVertex) vertex,
129: descriptorList, values);
130: } else if (vertex instanceof RuleTerminalNodeVertex) {
131: initTerminalNodeProperties((RuleTerminalNodeVertex) vertex,
132: descriptorList, values);
133: } else if (vertex instanceof QueryTerminalNodeVertex) {
134: initQueryTerminalNodeProperties(
135: (QueryTerminalNodeVertex) vertex, descriptorList,
136: values);
137: } else if (vertex instanceof EvalConditionNodeVertex) {
138: initEvalConditionNodeProperties(
139: (EvalConditionNodeVertex) vertex, descriptorList,
140: values);
141: } else if (vertex instanceof NotNodeVertex) {
142: initNotNodeProperties((NotNodeVertex) vertex,
143: descriptorList, values);
144: } else if (vertex instanceof JoinNodeVertex) {
145: initJoinNodeProperties((JoinNodeVertex) vertex,
146: descriptorList, values);
147: } else if (vertex instanceof RightInputAdapterNodeVertex) {
148: initRightInputAdapterNodeProperties(descriptorList, values);
149: } else if (vertex instanceof LeftInputAdapterNodeVertex) {
150: initLeftInputAdapterNodeProperties(
151: (LeftInputAdapterNodeVertex) vertex,
152: descriptorList, values);
153: } else if (vertex instanceof AlphaNodeVertex) {
154: initAlphaNodeProperties((AlphaNodeVertex) vertex,
155: descriptorList, values);
156: } else if (vertex instanceof ObjectTypeNodeVertex) {
157: initObjectTypeNodeProperties((ObjectTypeNodeVertex) vertex,
158: descriptorList, values);
159: } else if (vertex instanceof ReteVertex) {
160: initReteNodeProperties((ReteVertex) vertex, descriptorList,
161: values);
162: }
163:
164: descriptors = (IPropertyDescriptor[]) descriptorList
165: .toArray(new IPropertyDescriptor[0]);
166: }
167:
168: private void initExistsNodeProperties(ExistsNodeVertex vertex,
169: List descriptorList, Map valueMap) {
170: addProperty(PROP_NAME, VERTEX_EXISTS, descriptorList, valueMap);
171: addProperty(PROP_ID, Integer.toString(vertex.getId()),
172: descriptorList, valueMap);
173:
174: }
175:
176: private void initCollectNodeProperties(CollectNodeVertex vertex,
177: List descriptorList, Map valueMap) {
178: addProperty(PROP_NAME, VERTEX_COLLECT, descriptorList, valueMap);
179: addProperty(PROP_ID, Integer.toString(vertex.getId()),
180: descriptorList, valueMap);
181:
182: }
183:
184: private void initAccumulateNodeProperties(
185: AccumulateNodeVertex vertex, List descriptorList,
186: Map valueMap) {
187: addProperty(PROP_NAME, VERTEX_ACCUMULATE, descriptorList,
188: valueMap);
189: addProperty(PROP_ID, Integer.toString(vertex.getId()),
190: descriptorList, valueMap);
191: }
192:
193: private void initFromNodeProperties(FromNodeVertex vertex,
194: List descriptorList, Map valueMap) {
195: addProperty(PROP_NAME, VERTEX_FROM, descriptorList, valueMap);
196: addProperty(PROP_ID, Integer.toString(vertex.getId()),
197: descriptorList, valueMap);
198: }
199:
200: private void initReteNodeProperties(ReteVertex vertex,
201: List descriptorList, Map valueMap) {
202: addProperty(PROP_NAME, VERTEX_RETE, descriptorList, valueMap);
203: addProperty(PROP_ID, Integer.toString(vertex.getId()),
204: descriptorList, valueMap);
205: }
206:
207: private void initObjectTypeNodeProperties(
208: ObjectTypeNodeVertex vertex, List descriptorList,
209: Map valueMap) {
210: addProperty(PROP_NAME, VERTEX_OBJECT_TYPE, descriptorList,
211: valueMap);
212: addProperty(PROP_OBJ_TYPE, vertex.getObjectType(),
213: descriptorList, valueMap);
214:
215: }
216:
217: private void initAlphaNodeProperties(AlphaNodeVertex vertex,
218: List descriptorList, Map valueMap) {
219: addProperty(PROP_NAME, VERTEX_ALPHA, descriptorList, valueMap);
220: addProperty(PROP_ALPHA_FIELD_NAME, vertex.getFieldName(),
221: descriptorList, valueMap);
222: addProperty(PROP_ALPHA_EVALUATOR, vertex.getEvaluator(),
223: descriptorList, valueMap);
224: addProperty(PROP_ALPHA_VALUE, vertex.getValue(),
225: descriptorList, valueMap);
226:
227: Constraint constraint = vertex.getConstraint();
228: if (constraint == null) {
229: return;
230: }
231: IPropertyDescriptor prop = new PropertyDescriptor(CONSTRAINT,
232: CONSTRAINT_CAP);
233: addProperty(prop, constraint.toString(), descriptorList,
234: valueMap);
235:
236: }
237:
238: private void initLeftInputAdapterNodeProperties(
239: LeftInputAdapterNodeVertex vertex, List descriptorList,
240: Map valueMap) {
241: addProperty(PROP_NAME, VERTEX_LEFT_INPUT_ADAPTER,
242: descriptorList, valueMap);
243:
244: }
245:
246: private void initRightInputAdapterNodeProperties(
247: List descriptorList, Map valueMap) {
248: addProperty(PROP_NAME, VERTEX_RIGHT_INPUT_ADAPTER,
249: descriptorList, valueMap);
250: }
251:
252: private void initJoinNodeProperties(JoinNodeVertex vertex,
253: List descriptorList, Map valueMap) {
254:
255: addProperty(PROP_NAME, VERTEX_JOIN, descriptorList, valueMap);
256: addProperty(PROP_ID, Integer.toString(vertex.getId()),
257: descriptorList, valueMap);
258:
259: Constraint[] constraints = vertex.getConstraints();
260:
261: if (constraints == null) {
262: return;
263: }
264:
265: for (int i = 0, length = constraints.length; i < length; i++) {
266: PropertyDescriptor prop = new PropertyDescriptor(CONSTRAINT
267: + (i + 1), CONSTRAINT_CAP + " " + (i + 1));
268: addOther(prop, constraints[i].toString(), descriptorList,
269: valueMap);
270: }
271:
272: }
273:
274: private void initNotNodeProperties(NotNodeVertex vertex,
275: List descriptorList, Map valueMap) {
276: addProperty(PROP_NAME, VERTEX_NOT, descriptorList, valueMap);
277: addProperty(PROP_ID, Integer.toString(vertex.getId()),
278: descriptorList, valueMap);
279: }
280:
281: private void initEvalConditionNodeProperties(
282: EvalConditionNodeVertex vertex, List descriptorList,
283: Map valueMap) {
284: addProperty(PROP_NAME, VERTEX_EVAL_CONDITION, descriptorList,
285: valueMap);
286: addProperty(PROP_ID, Integer.toString(vertex.getId()),
287: descriptorList, valueMap);
288: }
289:
290: private void initTerminalNodeProperties(
291: RuleTerminalNodeVertex node, List descriptorList,
292: Map valueMap) {
293:
294: addProperty(PROP_NAME, VERTEX_TERMINAL, descriptorList,
295: valueMap);
296: addProperty(PROP_ID, Integer.toString(node.getId()),
297: descriptorList, valueMap);
298: addProperty(PROP_RULE, node.getRuleName(), descriptorList,
299: valueMap);
300:
301: }
302:
303: private void initQueryTerminalNodeProperties(
304: QueryTerminalNodeVertex node, List descriptorList,
305: Map valueMap) {
306:
307: addProperty(PROP_NAME, VERTEX_QUERY_TERMINAL, descriptorList,
308: valueMap);
309: addProperty(PROP_ID, Integer.toString(node.getId()),
310: descriptorList, valueMap);
311: addProperty(PROP_QUERY, node.getQueryName(), descriptorList,
312: valueMap);
313:
314: }
315:
316: private void addProperty(IPropertyDescriptor field, String value,
317: List descriptorList, Map valueMap) {
318: descriptorList.add(field);
319: valueMap.put(field.getId().toString(), new NodeValue(
320: CAT_GENERAL, value));
321: if (field instanceof PropertyDescriptor) {
322: ((PropertyDescriptor) field).setAlwaysIncompatible(true);
323: ((PropertyDescriptor) field).setCategory(CAT_GENERAL);
324: }
325:
326: }
327:
328: private void addOther(IPropertyDescriptor field, String value,
329: List descriptorList, Map valueMap) {
330: descriptorList.add(field);
331: valueMap.put(field.getId().toString(), new NodeValue(CAT_OTHER,
332: value));
333:
334: if (field instanceof PropertyDescriptor) {
335: ((PropertyDescriptor) field).setAlwaysIncompatible(true);
336: ((PropertyDescriptor) field).setCategory(CAT_OTHER);
337: }
338:
339: }
340:
341: /* (non-Javadoc)
342: * @see org.eclipse.ui.views.properties.IPropertySource#getEditableValue()
343: */
344: public Object getEditableValue() {
345: return null;
346: }
347:
348: /* (non-Javadoc)
349: * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyValue(java.lang.Object)
350: */
351: public Object getPropertyValue(Object propName) {
352: return getPropertyValue((String) propName);
353: }
354:
355: /**
356: * Property value.
357: *
358: * @param propName
359: * @return
360: */
361: public Object getPropertyValue(String propName) {
362: return ((NodeValue) (values.get(propName))).value;
363: }
364:
365: /* (non-Javadoc)
366: * @see org.eclipse.ui.views.properties.IPropertySource#setPropertyValue(java.lang.Object, java.lang.Object)
367: */
368: public void setPropertyValue(Object propName, Object value) {
369: setPropertyValue(propName, value);
370: }
371:
372: /* (non-Javadoc)
373: * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyDescriptors()
374: */
375: public IPropertyDescriptor[] getPropertyDescriptors() {
376: return descriptors;
377: }
378:
379: /**
380: * Doing nothing as resetting properties from property sheet is not possible.
381: */
382: public void resetPropertyValue(Object propName) {
383: }
384:
385: /* (non-Javadoc)
386: * @see org.eclipse.ui.views.properties.IPropertySource#isPropertySet(java.lang.Object)
387: */
388: public boolean isPropertySet(Object propName) {
389: return values.containsKey(propName);
390: }
391:
392: private class NodeValue {
393: final String category;
394: final String value;
395:
396: NodeValue(String category, String value) {
397: this.category = category;
398: this.value = value;
399: }
400: }
401:
402: }
|