001: /*
002: * Created on Nov 21, 2003
003: *
004: * To change the template for this generated file go to
005: * Window>Preferences>Java>Code Generation>Code and Comments
006: */
007: package org.hammurapi.inspectors.metrics.callertrace;
008:
009: import java.util.Enumeration;
010: import java.util.Vector;
011:
012: /**
013: * @author mucbj0
014: *
015: * To change the template for this generated type comment go to
016: * Window>Preferences>Java>Code Generation>Code and Comments
017: */
018: public class BreadthSearch implements SearchMethod {
019: // private static Logger logger = Logger.getLogger(BreadthSearch.class.getName());
020: private TraceList resultTraceList = new TraceList("init");
021: private AdjacencyMatrix adjacencyMatrix = null;
022:
023: //!! obsolete
024: public Vector traverseFor(Object nodeA) {
025: return null;
026: }
027:
028: /*
029: * Extract all starting point methods which fitting to key
030: */
031: public TraceList extractTraceListForKey(String key) {
032:
033: this .setResultTraceList(new TraceList(key));
034:
035: Vector initialLayerStack = adjacencyMatrix
036: .visitAllSuccessorsOfStartingPoint(key);
037: for (int i = 0; i < initialLayerStack.size(); i++) {
038: Trace t = new Trace();
039: MethodWrapper mw = (MethodWrapper) initialLayerStack
040: .elementAt(i);
041: TracedMethod tmm = new TracedMethod(mw);
042: tmm.setIsKeyTrue();
043: t.add(tmm);
044: resultTraceList.add(t);
045: }
046: Vector tempTesultTraceList = resultTraceList;
047: /* for ( int j=0; j<tempTesultTraceList.size(); j++){
048: Trace t = (Trace)tempTesultTraceList.elementAt(j);
049: TracedMethod tmm = (TracedMethod)t.elementAt(0);
050: }
051: */
052: extractTraceList();
053:
054: return this .getResultTraceList();
055: }
056:
057: // public TraceList extractTraceList( Object nodeA) {
058: public TraceList extractTraceList( ) {
059:
060: // System.out.println("++ " + nodeA);
061: // System.out.println("++ " + resultTraceList);
062: Vector layerStack = new Vector();
063:
064: /*
065: //-- root node load
066: if (resultTraceList.size() == 0) {
067: resultTraceList = new TraceList( nodeA );
068:
069: Vector initialLayerStack = adjacencyMatrix.visitAllSuccessorsOfStartingPoint(nodeA);
070: for ( int i=0; i<initialLayerStack.size(); i++){
071: Trace t = new Trace();
072: MethodWrapper mw = (MethodWrapper)initialLayerStack.elementAt(i);
073: TracedMethod tmm = new TracedMethod(mw);
074: tmm.setIsKeyTrue();
075: t.add(tmm);
076: resultTraceList.add(t);
077: }
078: extractTraceList( nodeA);
079: }
080: */
081: for (Enumeration enum = resultTraceList.elements(); enum.hasMoreElements();) {
082: Trace t = (Trace) enum.nextElement();
083: TracedMethod tm = (TracedMethod) t.elementAt(t.size() - 1);
084: // System.out.println("tm.isEndpoint() " + tm.isEndpoint());
085:
086: // if (tm.getMethod()!= null && !tm.isEndpoint()) {
087: if ( !tm.isEndpoint()) {
088: // String key = tm.getMethod().getName();
089: Object key = tm.toKey();
090: // System.out.println("key: " + key.toString());
091:
092: layerStack = adjacencyMatrix.visitAllSuccessorsOf(key);
093: // System.out.println("has " + layerStack.size() + " Children ");
094: if (layerStack.size() == 0) {
095: tm.setEndpointTrue();
096: } else {
097: // System.out.println("key not an endpoint: " + layerStack.size());
098: if (layerStack.size() == 1) {
099: Object key1 = (Object) layerStack.firstElement();
100: t.add(traceMethodFactory(key1));
101: } else if (layerStack.size() > 1) {
102:
103: retrieveTracesForAllLayerStack(layerStack, t);
104:
105: // first element goes to trace 1
106: Object key2 = (Object) layerStack.firstElement();
107: t.add( traceMethodFactory(key2));
108: }
109: }
110: extractTraceList( );
111: // logger.debug("kk " + resultTraceList);
112: }
113: }
114: return resultTraceList;
115: }
116:
117: /**
118: * @param layerStack
119: * @param t
120: */
121: private void retrieveTracesForAllLayerStack(Vector layerStack,
122: Trace t) {
123: // ignore first element
124: for (int j = 1; j < layerStack.size(); j++) {
125: Object key2 = (Object) layerStack.elementAt(j);
126: // System.out.println("build model for children: " + key2.toString());
127: // System.out.println("clone " + t);
128:
129: // Trace tadd = (Trace) t.clone();
130: Trace tadd = resultTraceList.retrieveTraceFor(t);
131: if (tadd == null) {
132:
133: tadd = resultTraceList.cloneTraceFor(t);
134: tadd.add(traceMethodFactory(key2));
135: // //!!* t.add(traceMethodFactory(key2));
136: resultTraceList.add(tadd);
137: } else {
138: tadd.add(traceMethodFactory(key2));
139: }
140: // //!!* resultTraceList.add(t);
141: }
142: }
143:
144: private TracedMethod traceMethodFactory(Object methodName) {
145:
146: // MethodWrapper m = (MethodWrapper) adjacencyMatrix.getAllMethods().get(methodName.toString());
147: MethodWrapper m = (MethodWrapper) adjacencyMatrix
148: .getMethodFor(methodName);
149: // if (m != null ){
150: TracedMethod tm = new TracedMethod(m);
151: if (m == null) {
152: // System.out.println("Scheissen in deer Hoose " + methodName.toString() );
153: tm.setSearchKey(methodName.toString());
154: } else {
155: tm.setMethod(m);
156: }
157: // tm.setIsKeyTrue();
158:
159: return tm;
160: }
161:
162: /**
163: * @return Returns the adjacencyMatrix.
164: */
165: public AdjacencyMatrix getAdjacencyMatrix() {
166: return adjacencyMatrix;
167: }
168:
169: /**
170: * @param adjacencyMatrix The adjacencyMatrix to set.
171: */
172: public void setAdjacencyMatrix(AdjacencyMatrix adjacencyMatrix) {
173: this .adjacencyMatrix = adjacencyMatrix;
174: }
175:
176: /**
177: * @return Returns the resultTraceList.
178: */
179: public TraceList getResultTraceList() {
180: return resultTraceList;
181: }
182:
183: /**
184: * @param resultTraceList The resultTraceList to set.
185: */
186: public void setResultTraceList(TraceList resultTraceList) {
187: this.resultTraceList = resultTraceList;
188: }
189:
190: }
|