This commit was manufactured by cvs2svn to create tag 'buildscript'.
[IRC.git] /
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
11 public class TaskGraph {
12     TaskAnalysis taskanalysis;
13     State state;
14     Hashtable cdtonodes;
15     Hashtable nodes;
16     Hashtable<TaskNode,TaskNode> alltasknodes;
17     
18     //Colors
19     String colors[]={"red","blue","green","brown","orange","pink","black","grey","olivedrab","yellow"};
20
21     public TaskGraph(State state, TaskAnalysis taskanalysis) {
22         this.state=state;
23         this.taskanalysis=taskanalysis;
24         this.cdtonodes=new Hashtable();
25         this.alltasknodes=new Hashtable<TaskNode,TaskNode>();
26
27         for(Iterator classit=state.getClassSymbolTable().getDescriptorsIterator();classit.hasNext();) {
28             ClassDescriptor cd=(ClassDescriptor) classit.next();
29             if (cd.hasFlags())
30                 produceTaskNodes(cd);
31         }
32         produceAllTaskNodes();
33     }
34     
35     
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) {
41                 try {
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) {
46                     e.printStackTrace();
47                     throw new Error();
48                 }
49             }
50         }
51     }
52
53     /** Returns the set of TaskNodes for the class descriptor cd */
54
55     public Set getTaskNodes(ClassDescriptor cd) {
56         if (cdtonodes.containsKey(cd))
57             return ((Hashtable)cdtonodes.get(cd)).keySet();
58         else
59             return null;
60     }
61
62     private TaskNode canonicalizeTaskNode(Hashtable nodes, TaskNode node){
63         if (nodes.containsKey(node))
64             return (TaskNode)nodes.get(node);
65         else{
66             nodes.put(node,node);
67             return (TaskNode)node;
68         }
69     }
70     
71     private void produceTaskNodes(ClassDescriptor cd) {
72         Set fsnodes=taskanalysis.getFlagStates(cd);
73         if (fsnodes==null)
74             return;
75             
76         Hashtable<TaskNode,TaskNode> tasknodes=new Hashtable<TaskNode,TaskNode>();
77         cdtonodes.put(cd, tasknodes);
78
79         for(Iterator it=fsnodes.iterator();it.hasNext();) {
80             FlagState fs=(FlagState)it.next();
81             Iterator it_inedges=fs.inedges();   
82             TaskNode tn,sn;
83            
84                 if (fs.isSourceNode()) {
85                         sn=new TaskNode("Start Node");
86                         if(fs.edges().hasNext()){
87                                  addEdges(fs,sn,tasknodes);
88                         }       
89                 }
90                                                 
91                 while(it_inedges.hasNext()){   
92                         
93                     FEdge inedge=(FEdge)it_inedges.next();
94                     tn=new TaskNode(inedge.getLabel());
95                     if(fs.edges().hasNext()){
96                         addEdges(fs,tn,tasknodes);
97                         }
98             }  
99         }
100     }
101     
102     private void produceAllTaskNodes(){
103             alltasknodes=new Hashtable<TaskNode,TaskNode>();
104             
105             for(Iterator it_tasks=state.getTaskSymbolTable().getDescriptorsIterator();it_tasks.hasNext();){
106                     TaskDescriptor td=(TaskDescriptor)it_tasks.next();
107                     TaskNode tn=new TaskNode(td.getSymbol());
108                     alltasknodes.put(tn,tn);
109             }
110             TaskNode tn_runtime=new TaskNode("Runtime");
111             alltasknodes.put(tn_runtime,tn_runtime);
112             
113             int ColorID=0;
114             for(Iterator classit=state.getClassSymbolTable().getDescriptorsIterator();classit.hasNext()&&ColorID<10;) {
115             ClassDescriptor cd=(ClassDescriptor) classit.next();
116                 Set fsnodes;
117                 
118                 if (cd.hasFlags()&&((fsnodes=taskanalysis.getFlagStates(cd))!=null)){
119                         //
120                         System.out.println("\nWorking on fses of Class: "+cd.getSymbol());
121                         //
122                         for(Iterator it=fsnodes.iterator();it.hasNext();) {
123                                 FlagState fs=(FlagState)it.next();
124                                 //
125                                 System.out.println("Evaluating fs: "+fs.getTextLabel());
126                                 //
127                                         Iterator it_inedges=fs.inedges();       
128                                     TaskNode tn,sn;
129                                     
130                                     
131                                     if (fs.isSourceNode()){
132                                             //
133                                             System.out.println("A sourcenode");
134                                             //
135                                             if(fs.edges().hasNext()){
136                                                 Vector allocatingtasks=fs.getAllocatingTasks();
137                                                 //
138                                                 if (allocatingtasks.iterator().hasNext())
139                                                         System.out.println("has been allocated by "+allocatingtasks.size()+" tasks");
140                                                 //
141                                                     for(Iterator it_at=allocatingtasks.iterator();it_at.hasNext();){
142                                                             TaskDescriptor allocatingtd=(TaskDescriptor)it_at.next();
143                                                             //
144                                                             System.out.println(allocatingtd.getSymbol());
145                                                             //
146                                                             tn=new TaskNode(allocatingtd.getSymbol());
147                                                             
148                                                             addEdges(fs,tn,alltasknodes,ColorID);
149                                                     }
150                                             }
151                                     }
152                                     
153                                     while(it_inedges.hasNext()){   
154                                             FEdge inedge=(FEdge)it_inedges.next();
155                                         tn=new TaskNode(inedge.getLabel());
156                                                 if(fs.edges().hasNext()){
157                                                 addEdges(fs,tn,alltasknodes,ColorID);
158                                                 }
159                                 }
160                         }
161                         ColorID++;
162                         }
163                         
164                 }
165         }  
166         
167         public Set getAllTaskNodes(){
168                 return alltasknodes.keySet();
169         }
170                                     
171                                                     
172                     
173                     
174                 
175         
176             
177     
178   /*    private void mergeAllNodes(){
179                 Hashtable alltasks=new Hashtable();
180                 for(Iterator classit=state.getClassSymbolTable().getDescriptorsIterator();classit.hasNext();) {
181                 ClassDescriptor cd=(ClassDescriptor) classit.next();
182                         Set tnodes=((Hashtable)cdtonodes.get(cd)).keyset();
183                         while (it_tnodes=tnodes.iterator();it_nodes.hasNext()){
184                                 TaskNode tn=it_nodes.next();
185                                 if (alltasks.containsKey(tn)){
186                                         while(tn.
187                                 }
188                         }
189                         
190             
191             
192             
193     }
194     
195          */   
196     
197     private void addEdges(FlagState fs, TaskNode tn,Hashtable<TaskNode,TaskNode> tasknodes){
198             
199           //  Hashtable<TaskNode,TaskNode> tasknodes=(Hashtable<TaskNode,TaskNode>)cdtonodes.get(fs.getClassDescriptor());
200             tn=(TaskNode)canonicalizeTaskNode(tasknodes, tn);
201                 for (Iterator it_edges=fs.edges();it_edges.hasNext();){
202                         TaskNode target=new TaskNode(((FEdge)it_edges.next()).getLabel());
203                         target=(TaskNode)canonicalizeTaskNode(tasknodes,target);
204
205                         TEdge newedge=new TEdge(target);
206                         if (! tn.edgeExists(newedge))
207                                 tn.addEdge(newedge);
208             }
209
210         }
211         
212         private void addEdges(FlagState fs, TaskNode tn,Hashtable<TaskNode,TaskNode> tasknodes,int ColorID){
213                 
214                 tn=(TaskNode)canonicalizeTaskNode(tasknodes, tn);
215                 for (Iterator it_edges=fs.edges();it_edges.hasNext();){
216                         TaskNode target=new TaskNode(((FEdge)it_edges.next()).getLabel());
217                         target=(TaskNode)canonicalizeTaskNode(tasknodes,target);
218
219                         TEdge newedge=new TEdge(target);
220                         newedge.setDotNodeParameters("style=bold, color = "+colors[ColorID]);
221                         if (! tn.edgeExists(newedge))
222                                 tn.addEdge(newedge);
223             }
224
225         }
226         
227 }