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, fs);
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);
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);
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);
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);
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());
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());
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];");
125 output.println("}\n");
128 private void traverse(java.io.PrintWriter output, Set v) {
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("];");
137 for(Iterator it2 = tn.edges(); it2.hasNext();){
138 output.println("\t"+tn.getLabel()+" -> "+((EGTaskNode)((EGEdge)it2.next()).getTarget()).getLabel()+";");