001 // GraphLab Project: http://graphlab.sharif.edu 002 // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology 003 // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/ 004 package graphlab.plugins.main.core.actions; 005 006 import graphlab.graph.atributeset.GraphAttrSet; 007 import graphlab.graph.graph.EdgeModel; 008 import graphlab.graph.graph.GraphModel; 009 import graphlab.graph.graph.VertexModel; 010 import graphlab.platform.Application; 011 import graphlab.platform.core.AbstractAction; 012 import graphlab.platform.core.BlackBoard; 013 import graphlab.plugins.main.core.actions.edge.AddEdge; 014 import graphlab.plugins.main.core.actions.vertex.AddVertex; 015 import graphlab.ui.UIUtils; 016 017 import java.util.HashMap; 018 019 /** 020 * @author azin azadi 021 */ 022 public class BlackBoardWatcher extends AbstractAction { 023 /** 024 * constructor 025 * 026 * @param bb the blackboard of the action 027 */ 028 public BlackBoardWatcher(BlackBoard bb) { 029 super(bb); 030 listen4Event(UIUtils.getUIEventKey("debug blackboard")); 031 } 032 033 public void performAction(String eventName, Object value) { 034 GraphModel g = blackboard.getData(GraphAttrSet.name); 035 BlackBoardDebug bbd = new BlackBoardDebug(g); 036 // new Thread(){ 037 // public void run() { 038 Application gg = blackboard.getData(Application.APPLICATION_INSTANCE); 039 gg.run(bbd); 040 // } 041 // }.start(); 042 } 043 } 044 045 class BlackBoardDebug extends BlackBoard { 046 GraphModel g; 047 048 public BlackBoardDebug(GraphModel g) { 049 this.g = g; 050 } 051 052 /** 053 * searchs in the stackTrace and returns the 054 * first nonblackboard classname.callingmethod.linenumber 055 */ 056 private StackTraceElement getCallingMethod() { 057 StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); 058 int n = 2; 059 StackTraceElement st = stackTrace[n]; 060 String c = st.getClassName(); 061 while (c.contains("blackboard") || c.contains("AbstractAction")) { 062 ++n; 063 c = stackTrace[n].getClassName(); 064 } 065 return stackTrace[n]; 066 // return "*"+clazz+"."+s.getMethodName();//+":"+s.getLineNumber(); 067 } 068 069 // HashMap<String, Vertex> knownPlaces=new HashMap<String, Vertex>(); 070 HashMap<String, VertexModel> knownLogs = new HashMap<String, VertexModel>(); 071 HashMap<StackTraceElement, VertexModel> knownTraces = new HashMap<StackTraceElement, VertexModel>(); 072 // public Log getLog(String name) { 073 // addEdge(getCallingMethod(),name); 074 // return super.getLog(name); 075 // } 076 077 public void setData(String name, Object value) { 078 addEdge(getCallingMethod(), name); 079 super.setData(name, value); 080 } 081 082 public <t> t getData(String name) { 083 addEdge(getCallingMethod(), name); 084 return (t) super.getData(name); 085 } 086 087 private VertexModel getLogVertex(String name) { 088 VertexModel _ = knownLogs.get(name); 089 if (_ == null || !g.containsVertex(_)) { 090 _ = addVertex(); 091 _.setLabel(name); 092 _.setColor(2); 093 knownLogs.put(name, _); 094 } 095 return _; 096 } 097 098 private VertexModel addVertex() { 099 return AddVertex.addVertexToRandomPosition(g); 100 } 101 102 HashMap<VertexModel, VertexModel> methodOwners = new HashMap<VertexModel, VertexModel>(); 103 HashMap<String, VertexModel> classes = new HashMap<String, VertexModel>(); 104 HashMap<String, HashMap<String, VertexModel>> methods = new HashMap<String, HashMap<String, VertexModel>>(); 105 106 private VertexModel getClassVertex(StackTraceElement ste) { 107 String clazz = ste.getClassName(); 108 String method = ste.getMethodName(); 109 VertexModel _class = classes.get(clazz); 110 if (methods.get(clazz) == null) 111 methods.put(clazz, new HashMap<String, VertexModel>()); 112 VertexModel _method = methods.get(clazz).get(method); 113 VertexModel _methodParent = methodOwners.get(_method); 114 if (_class == null || !g.containsVertex(_class)) { 115 _class = addVertex(); 116 _class.setLabel(clazz.substring(clazz.lastIndexOf(".") + 1)); 117 _class.setColor(4); 118 classes.put(clazz, _class); 119 } 120 if (_method == null || _methodParent != _class || !g.containsVertex(_method)) { 121 _method = addVertex(); 122 methodOwners.put(_method, _class); 123 methods.get(clazz).put(method, _method); 124 AddEdge.doJob(g, _class, _method).setLabel(""); 125 _method.setLabel(method + "()"); 126 _method.setColor(3); 127 128 } 129 return _method; 130 } 131 132 private EdgeModel addEdge(StackTraceElement ste, String logName) { 133 g.setShowChangesOnView(true); 134 EdgeModel e = AddEdge.doJob(g, getClassVertex(ste), getLogVertex(logName)); 135 e.setLabel(ste.getLineNumber() + ""); 136 e.setWeight(1 + e.getWeight()); 137 g.setShowChangesOnView(false); 138 return e; 139 } 140 // public Variable getVar(String name) { 141 //// addEdge(getCallingMethod(),name); 142 // return super.getVar(name); 143 // } 144 }