1 package Analysis.TaskStateAnalysis;
5 import IR.ClassDescriptor;
6 import IR.TaskDescriptor;
8 import java.io.FileWriter;
9 import java.io.FileOutputStream;
12 public class ExecutionGraph {
13 private TaskAnalysis taskanalysis;
15 private Hashtable executiongraph;
16 private HashSet marked;
17 private HashSet processed;
19 public ExecutionGraph(State state, TaskAnalysis ta){
22 this.executiongraph = new Hashtable();
23 this.marked=new HashSet();
24 this.processed=new HashSet();
27 public Hashtable getExecutionGraph(){
28 return executiongraph;
31 public void createExecutionGraph() throws java.io.IOException {
32 //Cycle through classes
33 Enumeration e=taskanalysis.flagstates.keys();
35 while (e.hasMoreElements()) {
36 ClassDescriptor cdtemp=(ClassDescriptor)e.nextElement();
37 HashSet<EGTaskNode> graph=exploreGraph(cdtemp);
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>();
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);
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);
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);
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);
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);
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());
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());
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];");
127 output.println("}\n");
130 private void traverse(java.io.PrintWriter output, Vector v) {
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("];");
140 for(Iterator it2 = tn.edges();it2.hasNext();){
141 output.println("\t"+tn.getLabel()+" -> "+((EGTaskNode)((EGEdge)it2.next()).getTarget()).getLabel()+";");