*Fixed the big graph
[IRC.git] / Robust / src / Analysis / TaskStateAnalysis / FlagState.java
1 package Analysis.TaskStateAnalysis;
2 import Analysis.TaskStateAnalysis.*;
3 import IR.*;
4 import IR.Tree.*;
5 import IR.Flat.*;
6 import java.util.*;
7 import java.io.*;
8 import Util.GraphNode;
9
10 /** This class is used to hold the flag states that a class in the Bristlecone 
11  *  program can exist in, during runtime.
12  */
13 public class FlagState extends GraphNode {
14     public static final int ONETAG=1;
15     public static final int NOTAGS=0;
16     public static final int MULTITAGS=-1;
17     
18     private int uid;
19     private static int nodeid=0;
20
21     private final HashSet flagstate;
22     private final ClassDescriptor cd;
23     private final Hashtable<TagDescriptor,Integer> tags;
24     private boolean issourcenode;
25     private Vector tasks;
26     
27
28     /** Class constructor
29      *  Creates a new flagstate with all flags set to false.
30      *  @param cd ClassDescriptor
31      */
32     public FlagState(ClassDescriptor cd) {
33         this.flagstate=new HashSet();
34         this.cd=cd;
35         this.tags=new Hashtable<TagDescriptor,Integer>();
36         this.uid=FlagState.nodeid++;
37         this.issourcenode=false;
38     }
39
40     /** Class constructor
41      *  Creates a new flagstate with flags set according to the HashSet.
42      *  If the flag exists in the hashset, it's set to true else set to false.
43      *  @param cd ClassDescriptor
44      *  @param flagstate a <CODE>HashSet</CODE> containing FlagDescriptors
45      */
46     private FlagState(HashSet flagstate, ClassDescriptor cd,Hashtable<TagDescriptor,Integer> tags) {
47         this.flagstate=flagstate;
48         this.cd=cd;
49         this.tags=tags;
50         this.uid=FlagState.nodeid++;
51         this.issourcenode=false;
52         
53     }
54     
55     /** Accessor method
56       *  @param fd FlagDescriptor
57       *  @return true if the flagstate contains fd else false.
58       */
59     public boolean get(FlagDescriptor fd) {
60         return flagstate.contains(fd);
61     }
62     
63     /** Checks if the flagstate is a source node. 
64      *  @return true if the flagstate is a sourcenode(i.e. Is the product of an allocation site).
65      */
66       
67     public boolean isSourceNode(){
68             return issourcenode;
69         }
70         
71         /**  Sets the flagstate as a source node. 
72      */
73         public void setAsSourceNode(){
74                 if(!issourcenode){
75                         issourcenode=true;
76                         this.tasks=new Vector();
77                 }
78         }
79         
80         public void addAllocatingTask(TaskDescriptor task){
81                 tasks.add(task);
82         }
83
84         public Vector getAllocatingTasks(){
85                 return tasks;
86         }
87                 
88     
89     public String toString() {
90         return cd.toString()+getTextLabel();
91     }
92
93     /** @return Iterator over the flags in the flagstate.
94      */
95      
96     public Iterator getFlags() {
97         return flagstate.iterator();
98     }
99     
100     public FlagState setTag(TagDescriptor tag){
101            
102             HashSet newset=(HashSet)flagstate.clone();
103             Hashtable<TagDescriptor,Integer> newtags=(Hashtable<TagDescriptor,Integer>)tags.clone();
104             
105             if (newtags.containsKey(tag)){
106                    switch (newtags.get(tag).intValue()){
107                            case ONETAG:
108                                         newtags.put(tag,new Integer(MULTITAGS));
109                                         break;
110                            case MULTITAGS:
111                                         newtags.put(tag,new Integer(MULTITAGS));
112                                         break;
113                         }
114                 }
115                 else{
116                         newtags.put(tag,new Integer(ONETAG));
117                 }
118                 
119                 return new FlagState(newset,cd,newtags);
120                                 
121     }
122
123     public int getTagCount(String tagtype){
124                 for (Enumeration en=getTags();en.hasMoreElements();){
125                         TagDescriptor td=(TagDescriptor)en.nextElement();
126                         if (tagtype.equals(td.getSymbol()))
127                                 return tags.get(td).intValue();   //returns either ONETAG or MULTITAG
128                 }
129                 return NOTAGS;
130                 
131     }
132     
133     public FlagState[] clearTag(TagDescriptor tag){
134             FlagState[] retstates;
135             
136             if (tags.containsKey(tag)){
137             switch(tags.get(tag).intValue()){
138                     case ONETAG:
139                         HashSet newset=(HashSet)flagstate.clone();
140                         Hashtable<TagDescriptor,Integer> newtags=(Hashtable<TagDescriptor,Integer>)tags.clone();
141                         newtags.remove(tag);
142                         retstates=new FlagState[]{new FlagState(newset,cd,newtags)};
143                         return retstates;
144                         
145                     case MULTITAGS:
146                         //when tagcount is more than 2, COUNT stays at MULTITAGS
147                         retstates=new FlagState[2];
148                         HashSet newset1=(HashSet)flagstate.clone();
149                         Hashtable<TagDescriptor,Integer> newtags1=(Hashtable<TagDescriptor,Integer>)tags.clone();
150                         retstates[1]=new FlagState(newset1,cd,newtags1);
151                         //when tagcount is 2, COUNT changes to ONETAG
152                         HashSet newset2=(HashSet)flagstate.clone();
153                         Hashtable<TagDescriptor,Integer> newtags2=(Hashtable<TagDescriptor,Integer>)tags.clone();
154                         newtags1.put(tag,new Integer(ONETAG));
155                         retstates[1]=new FlagState(newset2,cd,newtags2);
156                         return retstates;
157                 default:
158                         return null;                    
159         }
160                 }else{
161                         throw new Error("Invalid Operation: Can not clear a tag that doesn't exist.");
162                         
163                 }
164                 
165     }
166     
167     /** Creates a string description of the flagstate
168      *  e.g.  a flagstate with five flags could look like 01001
169      *  @param flags an array of flagdescriptors.
170      *  @return string representation of the flagstate.
171      */
172         public String toString(FlagDescriptor[] flags)
173         {
174                 StringBuffer sb = new StringBuffer(flagstate.size());
175                 for(int i=0;i < flags.length; i++)
176                 {
177                         if (get(flags[i]))
178                                 sb.append(1);
179                         else
180                                 sb.append(0);
181                 }
182                         
183                 return new String(sb);
184         }
185
186         /** Accessor method
187          *  @return returns the classdescriptor of the flagstate.
188          */
189          
190     public ClassDescriptor getClassDescriptor(){
191         return cd;
192     }
193
194         /** Sets the status of a specific flag in a flagstate after cloning it.
195          *  @param      fd FlagDescriptor of the flag whose status is being set.
196          *  @param  status boolean value
197          *  @return the new flagstate with <CODE>fd</CODE> set to <CODE>status</CODE>.
198          */
199          
200     public FlagState setFlag(FlagDescriptor fd, boolean status) {
201         HashSet newset=(HashSet) flagstate.clone();
202         Hashtable<TagDescriptor,Integer> newtags=(Hashtable<TagDescriptor,Integer>)tags.clone();
203         if (status)
204             newset.add(fd);
205         else if (newset.contains(fd)){
206             newset.remove(fd);
207         }
208         
209         return new FlagState(newset, cd,newtags);
210     }
211     
212     /** Tests for equality of two flagstate objects.
213     */
214     
215     public boolean equals(Object o) {
216         if (o instanceof FlagState) {
217             FlagState fs=(FlagState)o;
218             if (fs.cd!=cd)
219                 return false;
220             return (fs.flagstate.equals(flagstate) & fs.tags.equals(tags));
221         }
222         return false;
223     }
224
225     public int hashCode() {
226         return cd.hashCode()^flagstate.hashCode()^tags.hashCode();
227     }
228
229     public String getLabel() {
230         return "N"+uid;
231     }
232     
233     
234         
235
236     public String getTextLabel() {
237         String label=null;
238         for(Iterator it=getFlags();it.hasNext();) {
239             FlagDescriptor fd=(FlagDescriptor) it.next();
240             if (label==null)
241                 label=fd.toString();
242             else
243                 label+=", "+fd.toString();
244         }
245         for (Enumeration en_tags=getTags();en_tags.hasMoreElements();){
246                 TagDescriptor td=(TagDescriptor)en_tags.nextElement();
247                 switch (tags.get(td).intValue()){
248                 case ONETAG:
249                     if (label==null)
250                         label=td.toString()+"(1)";
251                     else
252                         label+=", "+td.toString()+"(1)";
253                     break;
254                 case MULTITAGS:
255                     if (label==null)
256                         label=td.toString()+"(n)";
257                     else
258                         label+=", "+td.toString()+"(n)";
259                     break;
260                 default:
261                     break;
262                 }
263         }
264         if (label==null)
265             return " ";
266         return label;
267     }
268     
269     public Enumeration getTags(){
270             return tags.keys();
271     }
272 }