01: package org.drools.eclipse.debug;
02:
03: import java.util.ArrayList;
04: import java.util.List;
05:
06: import org.drools.eclipse.DroolsEclipsePlugin;
07: import org.eclipse.core.runtime.CoreException;
08: import org.eclipse.debug.core.DebugException;
09: import org.eclipse.debug.core.model.ILogicalStructureTypeDelegate;
10: import org.eclipse.debug.core.model.IValue;
11: import org.eclipse.debug.core.model.IVariable;
12: import org.eclipse.jdt.debug.core.IJavaArray;
13: import org.eclipse.jdt.debug.core.IJavaClassType;
14: import org.eclipse.jdt.debug.core.IJavaInterfaceType;
15: import org.eclipse.jdt.debug.core.IJavaObject;
16: import org.eclipse.jdt.debug.core.IJavaType;
17: import org.eclipse.jdt.debug.core.IJavaValue;
18: import org.eclipse.jdt.debug.core.IJavaVariable;
19:
20: /**
21: * The logical structures of Agenda.
22: *
23: * @author <a href="mailto:kris_verlaenen@hotmail.com">kris verlaenen </a>
24: */
25: public class AgendaLogicalStructureDelegate implements
26: ILogicalStructureTypeDelegate {
27:
28: public boolean providesLogicalStructure(IValue value) {
29: if (!(value instanceof IJavaObject)) {
30: return false;
31: }
32: IJavaObject javaValue = (IJavaObject) value;
33: try {
34: IJavaType type = javaValue.getJavaType();
35: if (!(type instanceof IJavaClassType)) {
36: return false;
37: }
38: IJavaClassType classType = (IJavaClassType) type;
39: IJavaInterfaceType[] interfaceTypes = classType
40: .getAllInterfaces();
41: for (int i = 0; i < interfaceTypes.length; i++) {
42: if ("org.drools.spi.Activation"
43: .equals(interfaceTypes[i].getName())) {
44: return true;
45: }
46: }
47: return false;
48: } catch (DebugException e) {
49: DroolsEclipsePlugin.log(e);
50: return false;
51: }
52: }
53:
54: public IValue getLogicalStructure(IValue value)
55: throws CoreException {
56: if (!(value instanceof IJavaObject)) {
57: return null;
58: }
59: IJavaObject javaValue = (IJavaObject) value;
60: if (!providesLogicalStructure(value)) {
61: return null;
62: }
63: IJavaArray ruleParameters = (IJavaArray) DebugUtil
64: .getValueByExpression(
65: "return getRule().getDeclarations();", value);
66: List variables = new ArrayList();
67: variables.add(new VariableWrapper("ruleName",
68: (IJavaValue) DebugUtil.getValueByExpression(
69: "return getRule().getName();", value)));
70:
71: IJavaValue[] javaValues = ruleParameters.getValues();
72: for (int j = 0; j < javaValues.length; j++) {
73: IJavaValue declaration = javaValues[j];
74: IVariable[] vars = declaration.getVariables();
75: for (int k = 0; k < vars.length; k++) {
76: IVariable declarationVar = vars[k];
77: if ("identifier".equals(declarationVar.getName())) {
78: String paramName = declarationVar.getValue()
79: .getValueString();
80: IJavaValue varValue = (IJavaValue) DebugUtil
81: .getValueByExpression(
82: "return getRule().getDeclaration(\""
83: + paramName
84: + "\").getValue(((org.drools.common.InternalFactHandle) getTuple().get(getRule().getDeclaration(\""
85: + paramName
86: + "\"))).getObject());",
87: value);
88: if (varValue != null) {
89: variables.add(new VariableWrapper(paramName,
90: varValue));
91: }
92: break;
93: }
94: }
95: }
96: return new ObjectWrapper(javaValue, (IJavaVariable[]) variables
97: .toArray(new IJavaVariable[variables.size()]));
98: }
99: }
|