1 package Analysis.TaskStateAnalysis;
5 import IR.ClassDescriptor;
6 import IR.TaskDescriptor;
8 import java.io.FileWriter;
9 import java.io.FileOutputStream;
11 public class TaskGraph {
12 TaskAnalysis taskanalysis;
16 Hashtable<TaskNode,TaskNode> alltasknodes;
19 String colors[]={"red","blue","green","brown","orange","pink","black","grey","olivedrab","yellow"};
21 public TaskGraph(State state, TaskAnalysis taskanalysis) {
23 this.taskanalysis=taskanalysis;
24 this.cdtonodes=new Hashtable();
25 this.alltasknodes=new Hashtable<TaskNode,TaskNode>();
27 for(Iterator classit=state.getClassSymbolTable().getDescriptorsIterator();classit.hasNext();) {
28 ClassDescriptor cd=(ClassDescriptor) classit.next();
32 produceAllTaskNodes();
36 public void createDOTfiles() {
37 for(Iterator it_classes=(Iterator)cdtonodes.keys();it_classes.hasNext();) {
38 ClassDescriptor cd=(ClassDescriptor) it_classes.next();
39 Set tasknodes=getTaskNodes(cd);
40 if (tasknodes!=null) {
42 File dotfile_tasknodes=new File("graph"+cd.getSymbol()+"_task.dot");
43 FileOutputStream dotstream=new FileOutputStream(dotfile_tasknodes,true);
44 TaskNode.DOTVisitor.visit(dotstream,tasknodes);
45 } catch(Exception e) {
53 /** Returns the set of TaskNodes for the class descriptor cd */
55 public Set getTaskNodes(ClassDescriptor cd) {
56 if (cdtonodes.containsKey(cd))
57 return ((Hashtable)cdtonodes.get(cd)).keySet();
62 private TaskNode canonicalizeTaskNode(Hashtable nodes, TaskNode node){
63 if (nodes.containsKey(node))
64 return (TaskNode)nodes.get(node);
67 return (TaskNode)node;
71 private void produceTaskNodes(ClassDescriptor cd) {
72 Set fsnodes=taskanalysis.getFlagStates(cd);
76 Hashtable<TaskNode,TaskNode> tasknodes=new Hashtable<TaskNode,TaskNode>();
77 cdtonodes.put(cd, tasknodes);
79 for(Iterator it=fsnodes.iterator();it.hasNext();) {
80 FlagState fs=(FlagState)it.next();
81 Iterator it_inedges=fs.inedges();
84 if (fs.isSourceNode()) {
85 sn=new TaskNode("Start Node");
86 if(fs.edges().hasNext()){
87 addEdges(fs,sn,tasknodes);
91 while(it_inedges.hasNext()){
93 FEdge inedge=(FEdge)it_inedges.next();
94 tn=new TaskNode(inedge.getLabel());
95 if(fs.edges().hasNext()){
96 addEdges(fs,tn,tasknodes);
102 private void produceAllTaskNodes(){
103 alltasknodes=new Hashtable<TaskNode,TaskNode>();
105 for(Iterator it_tasks=state.getTaskSymbolTable().getDescriptorsIterator();it_tasks.hasNext();){
106 TaskDescriptor td=(TaskDescriptor)it_tasks.next();
107 TaskNode tn=new TaskNode(td.getSymbol());
108 alltasknodes.put(tn,tn);
110 TaskNode tn_runtime=new TaskNode("Runtime");
111 alltasknodes.put(tn_runtime,tn_runtime);
114 for(Iterator classit=state.getClassSymbolTable().getDescriptorsIterator();classit.hasNext()&&ColorID<10;) {
115 ClassDescriptor cd=(ClassDescriptor) classit.next();
118 if (cd.hasFlags()&&((fsnodes=taskanalysis.getFlagStates(cd))!=null)){
120 System.out.println("\nWorking on fses of Class: "+cd.getSymbol());
122 for(Iterator it=fsnodes.iterator();it.hasNext();) {
123 FlagState fs=(FlagState)it.next();
125 System.out.println("Evaluating fs: "+fs.getTextLabel());
127 Iterator it_inedges=fs.inedges();
131 if (fs.isSourceNode()){
133 System.out.println("A sourcenode");
135 if(fs.edges().hasNext()){
136 Vector allocatingtasks=fs.getAllocatingTasks();
138 if (allocatingtasks.iterator().hasNext())
139 System.out.println("has been allocated by "+allocatingtasks.size()+" tasks");
141 for(Iterator it_at=allocatingtasks.iterator();it_at.hasNext();){
142 TaskDescriptor allocatingtd=(TaskDescriptor)it_at.next();
144 System.out.println(allocatingtd.getSymbol());
146 tn=new TaskNode(allocatingtd.getSymbol());
148 addEdges(fs,tn,alltasknodes,ColorID);
153 while(it_inedges.hasNext()){
154 FEdge inedge=(FEdge)it_inedges.next();
155 tn=new TaskNode(inedge.getLabel());
156 if(fs.edges().hasNext()){
157 addEdges(fs,tn,alltasknodes,ColorID);
167 public Set getAllTaskNodes(){
168 return alltasknodes.keySet();
178 /* private void mergeAllNodes(){
179 Hashtable alltasks=new Hashtable();
180 for(Iterator classit=state.getClassSymbolTable().getDescriptorsIterator();classit.hasNext();) {
181 ClassDescriptor cd=(ClassDescriptor) classit.next();
182 Set tnodes=((Hashtable)cdtonodes.get(cd)).keyset();
183 while (it_tnodes=tnodes.iterator();it_nodes.hasNext()){
184 TaskNode tn=it_nodes.next();
185 if (alltasks.containsKey(tn)){
197 private void addEdges(FlagState fs, TaskNode tn,Hashtable<TaskNode,TaskNode> tasknodes){
199 // Hashtable<TaskNode,TaskNode> tasknodes=(Hashtable<TaskNode,TaskNode>)cdtonodes.get(fs.getClassDescriptor());
200 tn=(TaskNode)canonicalizeTaskNode(tasknodes, tn);
201 for (Iterator it_edges=fs.edges();it_edges.hasNext();){
202 TaskNode target=new TaskNode(((FEdge)it_edges.next()).getLabel());
203 target=(TaskNode)canonicalizeTaskNode(tasknodes,target);
205 TEdge newedge=new TEdge(target);
206 if (! tn.edgeExists(newedge))
212 private void addEdges(FlagState fs, TaskNode tn,Hashtable<TaskNode,TaskNode> tasknodes,int ColorID){
214 tn=(TaskNode)canonicalizeTaskNode(tasknodes, tn);
215 for (Iterator it_edges=fs.edges();it_edges.hasNext();){
216 TaskNode target=new TaskNode(((FEdge)it_edges.next()).getLabel());
217 target=(TaskNode)canonicalizeTaskNode(tasknodes,target);
219 TEdge newedge=new TEdge(target);
220 newedge.setDotNodeParameters("style=bold, color = "+colors[ColorID]);
221 if (! tn.edgeExists(newedge))