001: /*
002: * Created on Nov 8, 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.Hashtable;
011: import java.util.Vector;
012:
013: /**
014: * @author muc
015: *
016: * To change the template for this generated type comment go to
017: * Window>Preferences>Java>Code Generation>Code and Comments
018: */
019: public class AdjacencyMatrix {
020: public static final String KEY_SEPERATOR = " -> ";
021: // private static Logger logger = Logger.getLogger(AdjacencyMatrix.class.getName());
022: private SearchMethod state = null;
023: private Hashtable adjacencMatrix = new Hashtable();
024: private MethodMap allMethods = new MethodMap();
025:
026: public AdjacencyMatrix(SearchMethod sm) {
027: super ();
028: state = sm;
029: state.setAdjacencyMatrix(this );
030: }
031:
032: public int size() {
033: return adjacencMatrix.size();
034: }
035:
036: //!! add Null pointer handling for parameters
037: public String toKey(Object nodeA, Object nodeB) {
038: StringBuffer sb = new StringBuffer(nodeA.toString());
039: sb.append(AdjacencyMatrix.KEY_SEPERATOR);
040: sb.append(nodeB.toString());
041: // return (nodeA.toString() + AdjacencyMatrix.KEY_SEPERATOR + nodeB.toString());
042: return sb.toString();
043: }
044:
045: //!! Should return the first node in the edge representation
046: public String extractStartingNode(Object key) {
047:
048: if (AdjacencyMatrix.isKeyHashCode(key)) {
049:
050: int closingBrace = ((String) key).indexOf(")");
051: return ((String) key).substring(1, closingBrace);
052: } else {
053: int pos = key.toString().indexOf(
054: AdjacencyMatrix.KEY_SEPERATOR);
055: if (pos > -1)
056: return key.toString().substring(
057: 0,
058: key.toString().indexOf(
059: AdjacencyMatrix.KEY_SEPERATOR));
060:
061: }
062: return "";
063: }
064:
065: public static boolean isKeyHashCode(Object key) {
066: if (key instanceof String) {
067: if (((String) key).length() > 0) {
068: if (((String) key).charAt(0) == '('
069: && ((String) key).indexOf(")") > 1) {
070: return true;
071: }
072: }
073: }
074: return false;
075: }
076:
077: public static String nodeNameWithoutHashcode(String key) {
078:
079: if (AdjacencyMatrix.isKeyHashCode(key)) {
080:
081: int closingBrace = key.indexOf(")");
082: return key.substring(closingBrace + 1, key.length());
083: } else {
084: return key;
085: }
086: }
087:
088: public boolean compareNodes(String adjFoundNodeA, String searchKey) {
089: if (AdjacencyMatrix.isKeyHashCode(searchKey)) {
090: return adjFoundNodeA.toString()
091: .equals(searchKey.toString());
092: } else {
093: int closingBrace = adjFoundNodeA.indexOf(")") + 1;
094: // System.out.println(" adjFoundNodeA.substring(1,closingBrace): " + adjFoundNodeA.substring(closingBrace,adjFoundNodeA.length() ));
095: return searchKey.toString().equals(
096: adjFoundNodeA.substring(closingBrace, adjFoundNodeA
097: .length()));
098: }
099: }
100:
101: // !! Should return the first node in the edge representation
102: public String extractSuccessorNode(Object key) {
103: int pos = key.toString().indexOf(AdjacencyMatrix.KEY_SEPERATOR);
104: if (pos > -1)
105: return key.toString().substring(
106: pos + AdjacencyMatrix.KEY_SEPERATOR.length());
107: // return "99";
108: else {
109: return "";
110: }
111: }
112:
113: //!! use Interface
114: public void putEdge(EdgeImpl e) {
115: adjacencMatrix.put(e, new Boolean(false));
116: return;
117: }
118:
119: public void put(Object nodeA, Object nodeB) {
120: EdgeImpl edg = new EdgeImpl(nodeA, nodeB);
121: // System.out.println( "Adj " + edg.toString() );
122: adjacencMatrix.put(edg, new Boolean(false));
123: // adjacencMatrix.put( this.toKey(nodeA , nodeB) , new Boolean(false));
124: return;
125: }
126:
127: //!! shouldnt be public
128: private void put(Object key) {
129: adjacencMatrix.put(key, new Boolean(false));
130: return;
131: }
132:
133: public void setVisitFlag(Object nodeA, Object nodeB) {
134: this .setVisitFlag(this .toKey(nodeA, nodeB));
135: }
136:
137: public boolean isVisited(Object nodeA, Object nodeB) {
138: return this .isVisited(this .toKey(nodeA, nodeB));
139: }
140:
141: public void setVisitFlag(Object key) {
142: adjacencMatrix.put(key, new Boolean(true));
143: return;
144: }
145:
146: /*
147: * Sideeffect: set visited Flag
148: */
149: // should be moved to Adj Matrix
150: public Vector visitAllSuccessorsOf( Object nodeA) {
151: // System.out.println("-> visitAllSuccessorsOf: " + nodeA.toString());
152:
153: Vector result = new Vector();
154:
155: Enumeration enum = this .getAllKeys();
156: while (enum.hasMoreElements()) {
157: Object key = (Object) enum.nextElement();
158: Object adjFoundNodeA = extractStartingNode(key);
159:
160: // System.out.println("check for node: " + foundNodeA.toString() + " Already Visited: " + adjacencyMatrix.isVisited(key));
161: //!! toString should not be necessary
162: // if (foundNodeA.toString().equals(nodeA.toString()) && !adjacencyMatrix.isVisited(key)) {
163: if ( this .compareNodes( adjFoundNodeA.toString(), nodeA.toString()) && !this .isVisited(key)) {
164: this .setVisitFlag(key);
165: // System.out.println("foundNodeA: " + key);
166: result.add( this .extractSuccessorNode(key) );
167: }
168: }
169: return result;
170: }
171:
172: /*
173: * not Sideeffect, no set visited Flag
174: */
175:
176: public Vector visitAllSuccessorsOfStartingPoint( Object nodeA) {
177: // System.out.println("-> visitAllSuccessorsOf: " + nodeA.toString());
178:
179: Vector result = new Vector();
180:
181: Enumeration enum = this .getAllKeys();
182: while (enum.hasMoreElements()) {
183: Object key = (Object) enum.nextElement();
184: Object adjFoundNodeA = extractStartingNode(key);
185:
186: // System.out.println("check for node: " + foundNodeA.toString() + " Already Visited: " + adjacencyMatrix.isVisited(key));
187: //!! toString should not be necessary
188: // if (foundNodeA.toString().equals(nodeA.toString()) && !adjacencyMatrix.isVisited(key)) {
189: if ( this .compareNodes( adjFoundNodeA.toString(), nodeA.toString()) && !this .isVisited(key)) {
190: // this.setVisitFlag(key);
191: // System.out.println("foundNodeA as Successor of Starting Point: " + key);
192: // String successorKey = this.extractSuccessorNode(key);
193: MethodWrapper mw = this .getMethodFor(extractStartingNode(key));
194: result.add( mw );
195: }
196: }
197: return result;
198: }
199:
200: public Vector getAllSuccessorsOf( Object nodeA){
201: Vector vc = new Vector();
202: Enumeration enum = this .adjacencMatrix.keys();
203: while (enum.hasMoreElements()){
204: Object key = (Object)enum.nextElement();
205: Object foundNodeA = extractStartingNode(key);
206: if( foundNodeA.equals( nodeA) ){
207: vc.add( this .extractSuccessorNode( key ));
208: }
209: }
210:
211: return vc;
212: }
213:
214: /*
215: * Sideeffect: set visited Flag
216: */
217: public Vector getAllNotVisitedSuccessorsOf( Object nodeA){
218: Vector vc = new Vector();
219: Enumeration enum = this .adjacencMatrix.keys();
220: while (enum.hasMoreElements()){
221: Object key = (Object)enum.nextElement();
222:
223: Object foundNodeA = extractStartingNode(key);
224: //!! implement equals
225: if( foundNodeA.equals( nodeA ) && !this .isVisited(key) ){
226: vc.add( this .extractSuccessorNode( key ));
227: this .setVisitFlag( key );
228: }
229: }
230: return vc;
231: }
232:
233: public Vector getTraceOf(Object nodeA) {
234: // System.out.println ( "-> getTraceOf: "+ nodeA.toString());
235: // return this.state.traverseFor( nodeA);
236:
237: // this.state.setResultTraceList( new TraceList(nodeA) );
238: // return this.state.extractTraceList( nodeA);
239: return this .state.extractTraceListForKey((String) nodeA);
240: // return null;
241: }
242:
243: private boolean isAvaiable(Object key) {
244:
245: //!! no toString necessary
246: try {
247: adjacencMatrix.get(key.toString());
248: return true;
249: } catch (Exception e) {
250: return false;
251: }
252:
253: }
254:
255: public boolean isVisited(Object key) {
256: Boolean ret = null;
257: //!! no toString necessary
258: // try{ ret = (Boolean)adjacencMatrix.get(key.toString());
259: try {
260: ret = (Boolean) adjacencMatrix.get(key);
261: if (ret == null) {
262: ret = new Boolean(false);
263: }
264: } catch (Exception e) {
265: ret = new Boolean(false);
266: }
267: return ret.booleanValue();
268: }
269:
270: public void clearAllVisitFlags(){
271: Enumeration enum = adjacencMatrix.keys();
272: while (enum.hasMoreElements()){
273: Object key = (Object)enum.nextElement();
274: this .put(key);
275: }
276: }
277:
278: public String toString(){
279: StringBuffer sb = new StringBuffer();
280: Enumeration enum = adjacencMatrix.keys();
281: while (enum.hasMoreElements()){
282: sb.append( ((Object)enum.nextElement()).toString() );
283: sb.append( "\n" );
284: }
285: return sb.toString();
286: }
287:
288: public Enumeration getAllKeys() {
289: return adjacencMatrix.keys();
290:
291: }
292:
293: /**
294: * @return Returns the state.
295: */
296: public SearchMethod getSearchMethod() {
297: return state;
298: }
299:
300: //!! job: nitializeMatrix(CallerTraceService callerTraceService)
301: /*
302: public void initializeMatrix(CallerTraceService callerTraceService) {
303:
304: Vector allMethodsInvoked = new Vector();
305: Enumeration enum = callerTraceService.getAllClasses().elements();
306: while (enum.hasMoreElements()) {
307: Type c = (Type) enum.nextElement();
308: callerTraceService.getAllMethodsImplemented().addAll(c.getMethodMap());
309: Enumeration enumMet = c.getMethodMap().elements();
310: while (enumMet.hasMoreElements()) {
311: MethodImplemented mi = (MethodImplemented) enumMet.nextElement();
312: // logger.debug( "\t" +mi.toString() );
313: allMethodsInvoked.addAll(mi.getInvokedMethods().values());
314: callerTraceService.getAllMethods().put (callerTraceService.getMethodKey(mi), mi);
315: }
316: // logger.debug( c.toString() );
317: }
318:
319: for (int i = 0; i< allMethodsInvoked.size(); i++){
320: MethodInvoked called = (MethodInvoked) allMethodsInvoked.elementAt(i);
321: if ( CallerTraceService.doYouPassMethodNameFilter(called.getName() )){
322: callerTraceService.getAllMethods().put (callerTraceService.getMethodKey(called), called);
323:
324: //!! strategy pattern for Key generation is needed
325: logger.debug("experimentell " + callerTraceService.getMethodKey( called ) +" -> " + callerTraceService.getMethodKey( called.getDeclarationMethod() )) ;
326: put( callerTraceService.getMethodKey( called ) , callerTraceService.getMethodKey( called.getDeclarationMethod() ) );
327: }
328: }
329:
330: // logger.info ( allMethods );
331: callerTraceService.traceCaller();
332: callerTraceService.reporting();
333: return;
334: }
335: */
336:
337: //!! schwachsinn
338: public MethodWrapper getMethodFor(Object key) {
339:
340: if (key instanceof String) {
341: if (AdjacencyMatrix.isKeyHashCode(key)) {
342: return (MethodWrapper) allMethods.get(key);
343: } else {
344: String str = "here we should search for key suffix";
345: str.equals(null);
346: return null;
347: }
348: } else {
349: return (MethodWrapper) allMethods.get(key);
350: }
351: }
352:
353: /**
354: * @return Returns the allMethods.
355: */
356: public MethodMap getAllMethods() {
357: return allMethods;
358: }
359:
360: /**
361: * @param allMethods The allMethods to set.
362: */
363: public void setAllMethods(MethodMap allMethods) {
364: this.allMethods = allMethods;
365: }
366:
367: }
|