Fix tabbing.... Please fix your editors so they do tabbing correctly!!! (Spaces...
[IRC.git] / Robust / src / Analysis / TaskStateAnalysis / TaskGraph.java
1 package Analysis.TaskStateAnalysis;
2 import java.util.*;
3 import IR.State;
4 import IR.SymbolTable;
5 import IR.ClassDescriptor;
6 import IR.TaskDescriptor;
7 import java.io.File;
8 import java.io.FileWriter;
9 import java.io.FileOutputStream;
10
11 public class TaskGraph {
12   TaskAnalysis taskanalysis;
13   State state;
14   Hashtable cdtonodes;
15   Hashtable nodes;
16   Hashtable<TaskNode,TaskNode> alltasknodes;
17
18   //Colors
19   String colors[]={"red","blue","green","brown","orange","pink","black","grey","olivedrab","yellow"};
20
21   public TaskGraph(State state, TaskAnalysis taskanalysis) {
22     this.state=state;
23     this.taskanalysis=taskanalysis;
24     this.cdtonodes=new Hashtable();
25     this.alltasknodes=new Hashtable<TaskNode,TaskNode>();
26
27     for(Iterator classit=state.getClassSymbolTable().getDescriptorsIterator(); classit.hasNext(); ) {
28       ClassDescriptor cd=(ClassDescriptor) classit.next();
29       if (cd.hasFlags())
30         produceTaskNodes(cd);
31     }
32     produceAllTaskNodes();
33   }
34
35
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) {
41         try {
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) {
46           e.printStackTrace();
47           throw new Error();
48         }
49       }
50     }
51   }
52
53   /** Returns the set of TaskNodes for the class descriptor cd */
54
55   public Set getTaskNodes(ClassDescriptor cd) {
56     if (cdtonodes.containsKey(cd))
57       return ((Hashtable)cdtonodes.get(cd)).keySet();
58     else
59       return null;
60   }
61
62   private TaskNode canonicalizeTaskNode(Hashtable nodes, TaskNode node) {
63     if (nodes.containsKey(node))
64       return (TaskNode)nodes.get(node);
65     else {
66       nodes.put(node,node);
67       return (TaskNode)node;
68     }
69   }
70
71   private void produceTaskNodes(ClassDescriptor cd) {
72     Set fsnodes=taskanalysis.getFlagStates(cd);
73     if (fsnodes==null)
74       return;
75
76     Hashtable<TaskNode,TaskNode> tasknodes=new Hashtable<TaskNode,TaskNode>();
77     cdtonodes.put(cd, tasknodes);
78
79     for(Iterator it=fsnodes.iterator(); it.hasNext(); ) {
80       FlagState fs=(FlagState)it.next();
81       Iterator it_inedges=fs.inedges();
82       TaskNode tn,sn;
83
84       if (fs.isSourceNode()) {
85         Vector src=fs.getAllocatingTasks();
86         for(Iterator it2=src.iterator(); it2.hasNext(); ) {
87           TaskDescriptor td=(TaskDescriptor)it2.next();
88           sn=new TaskNode(td.getSymbol());
89           if(fs.edges().hasNext()) {
90             addEdges(fs,sn,tasknodes);
91           }
92         }
93       }
94
95       while(it_inedges.hasNext()) {
96
97         FEdge inedge=(FEdge)it_inedges.next();
98         tn=new TaskNode(inedge.getLabel());
99         if(fs.edges().hasNext()) {
100           addEdges(fs,tn,tasknodes);
101         }
102       }
103     }
104   }
105
106   private void produceAllTaskNodes() {
107     alltasknodes=new Hashtable<TaskNode,TaskNode>();
108
109     for(Iterator it_tasks=state.getTaskSymbolTable().getDescriptorsIterator(); it_tasks.hasNext(); ) {
110       TaskDescriptor td=(TaskDescriptor)it_tasks.next();
111       TaskNode tn=new TaskNode(td.getSymbol());
112       alltasknodes.put(tn,tn);
113     }
114     TaskNode tn_runtime=new TaskNode("Runtime");
115     alltasknodes.put(tn_runtime,tn_runtime);
116
117     int ColorID=0;
118     for(Iterator classit=state.getClassSymbolTable().getDescriptorsIterator(); classit.hasNext()&&ColorID<10; ) {
119       ClassDescriptor cd=(ClassDescriptor) classit.next();
120       Set fsnodes;
121
122       if (cd.hasFlags()&&((fsnodes=taskanalysis.getFlagStates(cd))!=null)) {
123         //
124         System.out.println("\nWorking on fses of Class: "+cd.getSymbol());
125         //
126         for(Iterator it=fsnodes.iterator(); it.hasNext(); ) {
127           FlagState fs=(FlagState)it.next();
128           //
129           System.out.println("Evaluating fs: "+fs.getTextLabel());
130           //
131           Iterator it_inedges=fs.inedges();
132           TaskNode tn,sn;
133
134
135           if (fs.isSourceNode()) {
136             //
137             System.out.println("A sourcenode");
138             //
139             if(fs.edges().hasNext()) {
140               Vector allocatingtasks=fs.getAllocatingTasks();
141               //
142               if (allocatingtasks.iterator().hasNext())
143                 System.out.println("has been allocated by "+allocatingtasks.size()+" tasks");
144               //
145               for(Iterator it_at=allocatingtasks.iterator(); it_at.hasNext(); ) {
146                 TaskDescriptor allocatingtd=(TaskDescriptor)it_at.next();
147                 //
148                 System.out.println(allocatingtd.getSymbol());
149                 //
150                 tn=new TaskNode(allocatingtd.getSymbol());
151
152                 addEdges(fs,tn,alltasknodes,ColorID);
153               }
154             }
155           }
156
157           while(it_inedges.hasNext()) {
158             FEdge inedge=(FEdge)it_inedges.next();
159             tn=new TaskNode(inedge.getLabel());
160             if(fs.edges().hasNext()) {
161               addEdges(fs,tn,alltasknodes,ColorID);
162             }
163           }
164         }
165         ColorID++;
166       }
167
168     }
169   }
170
171   public Set getAllTaskNodes() {
172     return alltasknodes.keySet();
173   }
174
175
176
177
178
179
180
181
182   /*    private void mergeAllNodes(){
183                 Hashtable alltasks=new Hashtable();
184                 for(Iterator classit=state.getClassSymbolTable().getDescriptorsIterator();classit.hasNext();) {
185                 ClassDescriptor cd=(ClassDescriptor) classit.next();
186                         Set tnodes=((Hashtable)cdtonodes.get(cd)).keyset();
187                         while (it_tnodes=tnodes.iterator();it_nodes.hasNext()){
188                                 TaskNode tn=it_nodes.next();
189                                 if (alltasks.containsKey(tn)){
190                                         while(tn.
191                                 }
192                         }
193
194
195
196
197      }
198
199    */
200
201   private void addEdges(FlagState fs, TaskNode tn,Hashtable<TaskNode,TaskNode> tasknodes) {
202
203     //  Hashtable<TaskNode,TaskNode> tasknodes=(Hashtable<TaskNode,TaskNode>)cdtonodes.get(fs.getClassDescriptor());
204     tn=(TaskNode)canonicalizeTaskNode(tasknodes, tn);
205     for (Iterator it_edges=fs.edges(); it_edges.hasNext(); ) {
206       TaskNode target=new TaskNode(((FEdge)it_edges.next()).getLabel());
207       target=(TaskNode)canonicalizeTaskNode(tasknodes,target);
208
209       TEdge newedge=new TEdge(target);
210       if (!tn.edgeExists(newedge))
211         tn.addEdge(newedge);
212     }
213
214   }
215
216   private void addEdges(FlagState fs, TaskNode tn,Hashtable<TaskNode,TaskNode> tasknodes,int ColorID) {
217
218     tn=(TaskNode)canonicalizeTaskNode(tasknodes, tn);
219     for (Iterator it_edges=fs.edges(); it_edges.hasNext(); ) {
220       TaskNode target=new TaskNode(((FEdge)it_edges.next()).getLabel());
221       target=(TaskNode)canonicalizeTaskNode(tasknodes,target);
222
223       TEdge newedge=new TEdge(target);
224       newedge.setDotNodeParameters("style=bold, color = "+colors[ColorID]);
225       if (!tn.edgeExists(newedge))
226         tn.addEdge(newedge);
227     }
228
229   }
230
231 }