simplify ExecutionGraph code
[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);
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(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(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());
85         if (fedge.getTarget()==fedge.getSource())
86             egnode.doSelfLoopMarking();
87         map.put(fedge, egnode);
88         nodes.add(egnode);
89         return egnode;
90     }
91
92     //put the graph into executiongraph
93     private void adapt(ClassDescriptor cd, HashSet<EGTaskNode> nodes) {
94         Vector tasknodes = new Vector();
95         tasknodes.addAll(nodes);
96         executiongraph.put(cd,tasknodes);
97     }
98
99     //print the contain of graph
100     private void test(Hashtable graph) {
101         System.out.println("\nGraph contains :"); 
102         Collection c = graph.values();
103         for ( Iterator it = c.iterator(); it.hasNext();){
104             EGTaskNode tn = (EGTaskNode)it.next();
105             System.out.println(tn.getTextLabel()+" ID "+tn.getLabel()+" FS "+tn.getFSName());
106         }
107     }
108     
109     //create dot files execution_classname_.dot
110     private void printDOTFile()throws java.io.IOException {
111         Enumeration e = executiongraph.keys();
112         while (e.hasMoreElements()){
113             createDOTFile((ClassDescriptor)e.nextElement());
114         }
115     }   
116     
117     private void createDOTFile(ClassDescriptor cd) throws java.io.IOException {
118         Vector v = (Vector)executiongraph.get(cd);
119         java.io.PrintWriter output;
120         File dotfile_flagstates= new File("execution"+cd.getSymbol()+".dot");
121         FileOutputStream dotstream=new FileOutputStream(dotfile_flagstates,true);
122         output = new java.io.PrintWriter(dotstream, true);
123         output.println("digraph dotvisitor {");
124         output.println("\tnode [fontsize=10,height=\"0.1\", width=\"0.1\"];");
125         output.println("\tedge [fontsize=6];");
126         traverse(output, v);
127         output.println("}\n");
128     }
129     
130     private void traverse(java.io.PrintWriter output, Vector v) {
131         EGTaskNode tn;
132         
133         for(Iterator it1 = v.iterator(); it1.hasNext();){
134             tn = (EGTaskNode)it1.next();
135             output.println("\t"+tn.getLabel()+" [label=\""+tn.getTextLabel()+"\"");
136             if (tn.isSelfLoop()) output.println(", shape=box");
137             if (tn.isMultipleParams()) output.println(", color=blue");
138             output.println("];");
139             
140             for(Iterator it2 = tn.edges();it2.hasNext();){
141                 output.println("\t"+tn.getLabel()+" -> "+((EGTaskNode)((EGEdge)it2.next()).getTarget()).getLabel()+";");
142             }
143         }
144     }
145 }