switch to spaces only..
[IRC.git] / Robust / src / Analysis / TaskStateAnalysis / ExecutionGraph.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 import Util.Edge;
11
12 public class ExecutionGraph {
13   private TaskAnalysis taskanalysis;
14   private State state;
15   private Hashtable executiongraph;
16   private HashSet marked;
17   private HashSet processed;
18
19   public ExecutionGraph(State state, TaskAnalysis ta) {
20     this.taskanalysis=ta;
21     this.state=state;
22     this.executiongraph = new Hashtable();
23     this.marked=new HashSet();
24     this.processed=new HashSet();
25   }
26
27   public Hashtable getExecutionGraph() {
28     return executiongraph;
29   }
30
31   public void createExecutionGraph() throws java.io.IOException {
32     //Cycle through classes
33     Enumeration e=taskanalysis.flagstates.keys();
34
35     while (e.hasMoreElements()) {
36       ClassDescriptor cdtemp=(ClassDescriptor)e.nextElement();
37       HashSet<EGTaskNode> graph=exploreGraph(cdtemp);
38       adapt(cdtemp,graph);
39     }
40     printDOTFile();
41   }
42
43   private HashSet<EGTaskNode> exploreGraph(ClassDescriptor cd) {
44     LinkedList<FlagState> fifo = new LinkedList<FlagState>();
45     HashSet<EGTaskNode> nodes=new HashSet<EGTaskNode>();
46     Hashtable<FEdge, EGTaskNode> map=new Hashtable<FEdge, EGTaskNode>();
47
48     // Go through nodes
49     Iterator<FlagState> it = taskanalysis.getFlagStates(cd).iterator();
50     while (it.hasNext()) {
51       FlagState fs = it.next();
52       if(fs.isSourceNode()) {
53         for (Iterator allocit = ((Vector)fs.getAllocatingTasks()).iterator(); allocit.hasNext(); ) {
54           TaskDescriptor alloctask=(TaskDescriptor)allocit.next();
55           EGTaskNode srcnode=new EGTaskNode(alloctask.getSymbol(),alloctask, fs);
56           nodes.add(srcnode);
57           srcnode.setSource();
58           for (Iterator edges = fs.edges(); edges.hasNext(); ) {
59             FEdge edge = (FEdge)edges.next();
60             EGTaskNode targetnode=getNode(edge, map, nodes);
61             EGEdge newedge=new EGEdge(fs, targetnode);
62             srcnode.addEdge(newedge);
63           }
64         }
65       }
66       for(Iterator init=fs.inedges(); init.hasNext(); ) {
67         FEdge inedge=(FEdge)init.next();
68         EGTaskNode srcnode=getNode(inedge, map, nodes);
69         for(Iterator outit=fs.edges(); outit.hasNext(); ) {
70           FEdge outedge=(FEdge)outit.next();
71           EGTaskNode dstnode=getNode(outedge, map, nodes);
72           EGEdge newedge=new EGEdge(fs,dstnode);
73           srcnode.addEdge(newedge);
74         }
75       }
76
77     }
78     return nodes;
79   }
80
81   private EGTaskNode getNode(FEdge fedge, Hashtable<FEdge, EGTaskNode> map, HashSet<EGTaskNode> nodes) {
82     if (map.containsKey(fedge))
83       return map.get(fedge);
84     EGTaskNode egnode=new EGTaskNode(fedge.getLabel(), (FlagState) fedge.getSource(), fedge.getTask(), fedge.getIndex(), (FlagState) fedge.getTarget());
85     map.put(fedge, egnode);
86     nodes.add(egnode);
87     return egnode;
88   }
89
90   //put the graph into executiongraph
91   private void adapt(ClassDescriptor cd, HashSet<EGTaskNode> nodes) {
92     HashSet tasknodes = new HashSet();
93     tasknodes.addAll(nodes);
94     executiongraph.put(cd,tasknodes);
95   }
96
97   //print the contain of graph
98   private void test(Hashtable graph) {
99     System.out.println("\nGraph contains :");
100     Collection c = graph.values();
101     for ( Iterator it = c.iterator(); it.hasNext(); ) {
102       EGTaskNode tn = (EGTaskNode)it.next();
103       System.out.println(tn.getTextLabel()+" ID "+tn.getLabel()+" FS "+tn.getFSName());
104     }
105   }
106
107   //create dot files execution_classname_.dot
108   private void printDOTFile() throws java.io.IOException {
109     Enumeration e = executiongraph.keys();
110     while (e.hasMoreElements()) {
111       createDOTFile((ClassDescriptor)e.nextElement());
112     }
113   }
114
115   private void createDOTFile(ClassDescriptor cd) throws java.io.IOException {
116     Set s = (Set)executiongraph.get(cd);
117     java.io.PrintWriter output;
118     File dotfile_flagstates= new File("execution"+cd.getSymbol()+".dot");
119     FileOutputStream dotstream=new FileOutputStream(dotfile_flagstates,false);
120     output = new java.io.PrintWriter(dotstream, true);
121     output.println("digraph dotvisitor {");
122     output.println("\tnode [fontsize=10,height=\"0.1\", width=\"0.1\"];");
123     output.println("\tedge [fontsize=6];");
124     traverse(output, s);
125     output.println("}\n");
126   }
127
128   private void traverse(java.io.PrintWriter output, Set v) {
129     EGTaskNode tn;
130
131     for(Iterator it1 = v.iterator(); it1.hasNext(); ) {
132       tn = (EGTaskNode)it1.next();
133       output.println("\t"+tn.getLabel()+" [label=\""+tn.getTextLabel()+"\"");
134       if (tn.isMultipleParams()) output.println(", color=blue");
135       output.println("];");
136
137       for(Iterator it2 = tn.edges(); it2.hasNext(); ) {
138         output.println("\t"+tn.getLabel()+" -> "+((EGTaskNode)((EGEdge)it2.next()).getTarget()).getLabel()+";");
139       }
140     }
141   }
142 }