1 package Analysis.TaskStateAnalysis;
2 import Analysis.TaskStateAnalysis.*;
10 /** This class is used to hold the flag states that a class in the Bristlecone
11 * program can exist in, during runtime.
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;
19 private static int nodeid=0;
21 private final HashSet flagstate;
22 private final ClassDescriptor cd;
23 private final Hashtable<TagDescriptor,Integer> tags;
24 private boolean issourcenode;
27 private boolean marked=false;
31 * Creates a new flagstate with all flags set to false.
32 * @param cd ClassDescriptor
34 public FlagState(ClassDescriptor cd) {
35 this.flagstate=new HashSet();
37 this.tags=new Hashtable<TagDescriptor,Integer>();
38 this.uid=FlagState.nodeid++;
39 this.issourcenode=false;
43 * Creates a new flagstate with flags set according to the HashSet.
44 * If the flag exists in the hashset, it's set to true else set to false.
45 * @param cd ClassDescriptor
46 * @param flagstate a <CODE>HashSet</CODE> containing FlagDescriptors
48 private FlagState(HashSet flagstate, ClassDescriptor cd,Hashtable<TagDescriptor,Integer> tags) {
49 this.flagstate=flagstate;
52 this.uid=FlagState.nodeid++;
53 this.issourcenode=false;
61 public boolean isMarked() {
65 public void doUnmarking() {
69 public void doMarking() {
74 * @param fd FlagDescriptor
75 * @return true if the flagstate contains fd else false.
77 public boolean get(FlagDescriptor fd) {
78 return flagstate.contains(fd);
81 /** Checks if the flagstate is a source node.
82 * @return true if the flagstate is a sourcenode(i.e. Is the product of an allocation site).
85 public boolean isSourceNode(){
89 /** Sets the flagstate as a source node.
91 public void setAsSourceNode(){
94 this.tasks=new Vector();
98 public void addAllocatingTask(TaskDescriptor task){
102 public Vector getAllocatingTasks(){
107 public String toString() {
108 return cd.toString()+getTextLabel();
111 /** @return Iterator over the flags in the flagstate.
114 public Iterator getFlags() {
115 return flagstate.iterator();
118 public FlagState setTag(TagDescriptor tag){
120 HashSet newset=(HashSet)flagstate.clone();
121 Hashtable<TagDescriptor,Integer> newtags=(Hashtable<TagDescriptor,Integer>)tags.clone();
123 if (newtags.containsKey(tag)){
124 switch (newtags.get(tag).intValue()){
126 newtags.put(tag,new Integer(MULTITAGS));
129 newtags.put(tag,new Integer(MULTITAGS));
134 newtags.put(tag,new Integer(ONETAG));
137 return new FlagState(newset,cd,newtags);
141 public int getTagCount(String tagtype){
142 for (Enumeration en=getTags();en.hasMoreElements();){
143 TagDescriptor td=(TagDescriptor)en.nextElement();
144 if (tagtype.equals(td.getSymbol()))
145 return tags.get(td).intValue(); //returns either ONETAG or MULTITAG
151 public FlagState[] clearTag(TagDescriptor tag){
152 FlagState[] retstates;
154 if (tags.containsKey(tag)){
155 switch(tags.get(tag).intValue()){
157 HashSet newset=(HashSet)flagstate.clone();
158 Hashtable<TagDescriptor,Integer> newtags=(Hashtable<TagDescriptor,Integer>)tags.clone();
160 retstates=new FlagState[]{new FlagState(newset,cd,newtags)};
164 //when tagcount is more than 2, COUNT stays at MULTITAGS
165 retstates=new FlagState[2];
166 HashSet newset1=(HashSet)flagstate.clone();
167 Hashtable<TagDescriptor,Integer> newtags1=(Hashtable<TagDescriptor,Integer>)tags.clone();
168 retstates[1]=new FlagState(newset1,cd,newtags1);
169 //when tagcount is 2, COUNT changes to ONETAG
170 HashSet newset2=(HashSet)flagstate.clone();
171 Hashtable<TagDescriptor,Integer> newtags2=(Hashtable<TagDescriptor,Integer>)tags.clone();
172 newtags1.put(tag,new Integer(ONETAG));
173 retstates[1]=new FlagState(newset2,cd,newtags2);
179 throw new Error("Invalid Operation: Can not clear a tag that doesn't exist.");
185 /** Creates a string description of the flagstate
186 * e.g. a flagstate with five flags could look like 01001
187 * @param flags an array of flagdescriptors.
188 * @return string representation of the flagstate.
190 public String toString(FlagDescriptor[] flags)
192 StringBuffer sb = new StringBuffer(flagstate.size());
193 for(int i=0;i < flags.length; i++)
201 return new String(sb);
205 * @return returns the classdescriptor of the flagstate.
208 public ClassDescriptor getClassDescriptor(){
212 /** Sets the status of a specific flag in a flagstate after cloning it.
213 * @param fd FlagDescriptor of the flag whose status is being set.
214 * @param status boolean value
215 * @return the new flagstate with <CODE>fd</CODE> set to <CODE>status</CODE>.
218 public FlagState setFlag(FlagDescriptor fd, boolean status) {
219 HashSet newset=(HashSet) flagstate.clone();
220 Hashtable<TagDescriptor,Integer> newtags=(Hashtable<TagDescriptor,Integer>)tags.clone();
223 else if (newset.contains(fd)){
227 return new FlagState(newset, cd,newtags);
230 /** Tests for equality of two flagstate objects.
233 public boolean equals(Object o) {
234 if (o instanceof FlagState) {
235 FlagState fs=(FlagState)o;
238 return (fs.flagstate.equals(flagstate) & fs.tags.equals(tags));
243 public int hashCode() {
244 return cd.hashCode()^flagstate.hashCode()^tags.hashCode();
247 public String getLabel() {
254 public String getTextLabel() {
256 for(Iterator it=getFlags();it.hasNext();) {
257 FlagDescriptor fd=(FlagDescriptor) it.next();
261 label+=", "+fd.toString();
263 for (Enumeration en_tags=getTags();en_tags.hasMoreElements();){
264 TagDescriptor td=(TagDescriptor)en_tags.nextElement();
265 switch (tags.get(td).intValue()){
268 label=td.toString()+"(1)";
270 label+=", "+td.toString()+"(1)";
274 label=td.toString()+"(n)";
276 label+=", "+td.toString()+"(n)";
287 public Enumeration getTags(){