001: package org.drools.eclipse.debug;
002:
003: import java.util.ArrayList;
004: import java.util.List;
005:
006: import org.drools.eclipse.DroolsEclipsePlugin;
007: import org.eclipse.debug.core.DebugException;
008: import org.eclipse.debug.core.model.IValue;
009: import org.eclipse.debug.core.model.IVariable;
010: import org.eclipse.jdt.debug.core.IJavaArray;
011: import org.eclipse.jdt.debug.core.IJavaObject;
012: import org.eclipse.jdt.debug.core.IJavaValue;
013: import org.eclipse.jdt.debug.core.IJavaVariable;
014:
015: /**
016: * The Agenda View content provider.
017: *
018: * @author <a href="mailto:kris_verlaenen@hotmail.com">kris verlaenen </a>
019: */
020: public class AgendaViewContentProvider extends
021: DroolsDebugViewContentProvider {
022:
023: private DroolsDebugEventHandlerView view;
024:
025: public AgendaViewContentProvider(DroolsDebugEventHandlerView view) {
026: this .view = view;
027: }
028:
029: protected String getEmptyString() {
030: return "The selected working memory has an empty agenda.";
031: }
032:
033: public Object[] getChildren(Object obj) {
034: try {
035: Object[] variables = null;
036: if (obj != null
037: && obj instanceof IJavaObject
038: && "org.drools.reteoo.ReteooStatefulSession"
039: .equals(((IJavaObject) obj)
040: .getReferenceTypeName())) {
041: variables = getAgendaElements((IJavaObject) obj);
042: } else if (obj instanceof IVariable) {
043: if (view.isShowLogicalStructure()) {
044: IValue value = getLogicalValue(((IVariable) obj)
045: .getValue(), new ArrayList());
046: variables = value.getVariables();
047: }
048: if (variables == null) {
049: variables = ((IVariable) obj).getValue()
050: .getVariables();
051: }
052: }
053: if (variables == null) {
054: return new Object[0];
055: } else {
056: cache(obj, variables);
057: return variables;
058: }
059: } catch (DebugException e) {
060: DroolsEclipsePlugin.log(e);
061: return new Object[0];
062: }
063: }
064:
065: private Object[] getAgendaElements(IJavaObject workingMemoryImpl)
066: throws DebugException {
067: List result = new ArrayList();
068: IValue agendaGroupObjects = DebugUtil.getValueByExpression(
069: "return getAgenda().getAgendaGroups();",
070: workingMemoryImpl);
071: IValue focus = DebugUtil.getValueByExpression(
072: "return getAgenda().getFocus();", workingMemoryImpl);
073: if (agendaGroupObjects instanceof IJavaArray) {
074: IJavaArray agendaGroupArray = (IJavaArray) agendaGroupObjects;
075: IJavaValue[] agendaGroupValueArray = agendaGroupArray
076: .getValues();
077: for (int i = 0; i < agendaGroupValueArray.length; i++) {
078: IJavaValue agendaGroup = agendaGroupValueArray[i];
079: String name = "";
080: List activationsResult = new ArrayList();
081: IVariable[] agendaGroupVarArray = agendaGroup
082: .getVariables();
083: for (int j = 0; j < agendaGroupVarArray.length; j++) {
084: IVariable agendaGroupVar = agendaGroupVarArray[j];
085: if ("name".equals(agendaGroupVar.getName())) {
086: name = agendaGroupVar.getValue()
087: .getValueString();
088: break;
089: }
090: }
091: IJavaArray activations = (IJavaArray) DebugUtil
092: .getValueByExpression(
093: "return getActivations();", agendaGroup);
094: IJavaValue[] activationArray = activations.getValues();
095: for (int l = 0; l < activationArray.length; l++) {
096: IJavaValue activation = activationArray[l];
097: if (activation.getJavaType() != null) {
098: activationsResult.add(new VariableWrapper("["
099: + l + "]", new LazyActivationWrapper(
100: activations, activation,
101: workingMemoryImpl)));
102: }
103: }
104: boolean active = false;
105: if (agendaGroup.equals(focus)) {
106: active = true;
107: }
108: // because the debug view does not handle spaces well, all spaces
109: // in the agenda group name are replaced with '_'s.
110: name = replaceSpaces(name);
111: result
112: .add(new MyVariableWrapper(
113: name
114: + "["
115: + (active ? "focus" : "nofocus")
116: + "]",
117: new ObjectWrapper(
118: (IJavaObject) agendaGroup,
119: (IJavaVariable[]) activationsResult
120: .toArray(new IJavaVariable[activationsResult
121: .size()]))));
122: }
123: }
124: return result.toArray(new IVariable[0]);
125: }
126:
127: private String replaceSpaces(String name) {
128: return name.replace(' ', '_');
129: }
130:
131: private class LazyActivationWrapper extends ObjectWrapper {
132:
133: private IJavaValue activation;
134: private IJavaValue workingMemoryImpl;
135:
136: public LazyActivationWrapper(IJavaObject object,
137: IJavaValue activation, IJavaObject workingMemoryImpl) {
138: super (object, null);
139: this .activation = activation;
140: this .workingMemoryImpl = workingMemoryImpl;
141: }
142:
143: public IVariable[] getVariables() {
144: IVariable[] result = super .getVariables();
145: if (result == null) {
146: try {
147: List variables = new ArrayList();
148: variables
149: .add(new VariableWrapper(
150: "ruleName",
151: (IJavaValue) DebugUtil
152: .getValueByExpression(
153: "return getRule().getName();",
154: activation)));
155: String activationId = null;
156: IVariable[] activationVarArray = activation
157: .getVariables();
158: for (int j = 0; j < activationVarArray.length; j++) {
159: IVariable activationVar = activationVarArray[j];
160: if ("activationNumber".equals(activationVar
161: .getName())) {
162: activationId = activationVar.getValue()
163: .getValueString();
164: break;
165: }
166: }
167: if (activationId != null) {
168: IValue objects = DebugUtil
169: .getValueByExpression(
170: "return getActivationParameters("
171: + activationId + ");",
172: workingMemoryImpl);
173: if (objects instanceof IJavaArray) {
174: IJavaArray array = (IJavaArray) objects;
175: IJavaValue[] javaVals = array.getValues();
176: for (int k = 0; k < javaVals.length; k++) {
177: IJavaValue mapEntry = javaVals[k];
178: String key = null;
179: IJavaValue value = null;
180:
181: IVariable[] vars = mapEntry
182: .getVariables();
183: for (int j = 0; j < vars.length; j++) {
184: IVariable var = vars[j];
185: if ("key".equals(var.getName())) {
186: key = var.getValue()
187: .getValueString();
188: } else if ("value".equals(var
189: .getName())) {
190: value = (IJavaValue) var
191: .getValue();
192: }
193: }
194: variables.add(new VariableWrapper(key,
195: value));
196: }
197: result = (IJavaVariable[]) variables
198: .toArray(new IJavaVariable[variables
199: .size()]);
200: }
201: }
202: } catch (Throwable t) {
203: DroolsEclipsePlugin.log(t);
204: }
205: if (result == null) {
206: result = new IJavaVariable[0];
207: }
208: setVariables((IJavaVariable[]) result);
209: }
210: return result;
211: }
212:
213: public boolean hasVariables() {
214: return true;
215: }
216:
217: public String getValueString() throws DebugException {
218: return "Activation";
219: }
220:
221: public String getReferenceTypeName() throws DebugException {
222: return "";
223: }
224: }
225:
226: /**
227: * Special VariableWrapper that considers variables with the same name
228: * as equal.
229: */
230: private class MyVariableWrapper extends VariableWrapper {
231:
232: public MyVariableWrapper(String name, IJavaValue value) {
233: super (name, value);
234: }
235:
236: public boolean equals(Object obj) {
237: if (obj instanceof VariableWrapper) {
238: VariableWrapper var = (VariableWrapper) obj;
239: return var.getName().equals(getName());
240: }
241: return false;
242: }
243:
244: public int hashCode() {
245: return getName().hashCode();
246: }
247:
248: }
249:
250: }
|