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 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);
95 while(it_inedges.hasNext()){
97 FEdge inedge=(FEdge)it_inedges.next();
98 tn=new TaskNode(inedge.getLabel());
99 if(fs.edges().hasNext()){
100 addEdges(fs,tn,tasknodes);
106 private void produceAllTaskNodes(){
107 alltasknodes=new Hashtable<TaskNode,TaskNode>();
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);
114 TaskNode tn_runtime=new TaskNode("Runtime");
115 alltasknodes.put(tn_runtime,tn_runtime);
118 for(Iterator classit=state.getClassSymbolTable().getDescriptorsIterator();classit.hasNext()&&ColorID<10;) {
119 ClassDescriptor cd=(ClassDescriptor) classit.next();
122 if (cd.hasFlags()&&((fsnodes=taskanalysis.getFlagStates(cd))!=null)){
124 System.out.println("\nWorking on fses of Class: "+cd.getSymbol());
126 for(Iterator it=fsnodes.iterator();it.hasNext();) {
127 FlagState fs=(FlagState)it.next();
129 System.out.println("Evaluating fs: "+fs.getTextLabel());
131 Iterator it_inedges=fs.inedges();
135 if (fs.isSourceNode()){
137 System.out.println("A sourcenode");
139 if(fs.edges().hasNext()){
140 Vector allocatingtasks=fs.getAllocatingTasks();
142 if (allocatingtasks.iterator().hasNext())
143 System.out.println("has been allocated by "+allocatingtasks.size()+" tasks");
145 for(Iterator it_at=allocatingtasks.iterator();it_at.hasNext();){
146 TaskDescriptor allocatingtd=(TaskDescriptor)it_at.next();
148 System.out.println(allocatingtd.getSymbol());
150 tn=new TaskNode(allocatingtd.getSymbol());
152 addEdges(fs,tn,alltasknodes,ColorID);
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);
171 public Set getAllTaskNodes(){
172 return alltasknodes.keySet();
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)){
201 private void addEdges(FlagState fs, TaskNode tn,Hashtable<TaskNode,TaskNode> tasknodes){
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);
209 TEdge newedge=new TEdge(target);
210 if (! tn.edgeExists(newedge))
216 private void addEdges(FlagState fs, TaskNode tn,Hashtable<TaskNode,TaskNode> tasknodes,int ColorID){
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);
223 TEdge newedge=new TEdge(target);
224 newedge.setDotNodeParameters("style=bold, color = "+colors[ColorID]);
225 if (! tn.edgeExists(newedge))