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;
29 * Creates a new flagstate with all flags set to false.
30 * @param cd ClassDescriptor
32 public FlagState(ClassDescriptor cd) {
33 this.flagstate=new HashSet();
35 this.tags=new Hashtable<TagDescriptor,Integer>();
36 this.uid=FlagState.nodeid++;
37 this.issourcenode=false;
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
46 private FlagState(HashSet flagstate, ClassDescriptor cd,Hashtable<TagDescriptor,Integer> tags) {
47 this.flagstate=flagstate;
50 this.uid=FlagState.nodeid++;
51 this.issourcenode=false;
56 * @param fd FlagDescriptor
57 * @return true if the flagstate contains fd else false.
59 public boolean get(FlagDescriptor fd) {
60 return flagstate.contains(fd);
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).
67 public boolean isSourceNode(){
71 /** Sets the flagstate as a source node.
73 public void setAsSourceNode(){
76 this.tasks=new Vector();
80 public void addAllocatingTask(TaskDescriptor task){
84 public Vector getAllocatingTasks(){
89 public String toString() {
90 return cd.toString()+getTextLabel();
93 /** @return Iterator over the flags in the flagstate.
96 public Iterator getFlags() {
97 return flagstate.iterator();
100 public FlagState setTag(TagDescriptor tag){
102 HashSet newset=(HashSet)flagstate.clone();
103 Hashtable<TagDescriptor,Integer> newtags=(Hashtable<TagDescriptor,Integer>)tags.clone();
105 if (newtags.containsKey(tag)){
106 switch (newtags.get(tag).intValue()){
108 newtags.put(tag,new Integer(MULTITAGS));
111 newtags.put(tag,new Integer(MULTITAGS));
116 newtags.put(tag,new Integer(ONETAG));
119 return new FlagState(newset,cd,newtags);
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
133 public FlagState[] clearTag(TagDescriptor tag){
134 FlagState[] retstates;
136 if (tags.containsKey(tag)){
137 switch(tags.get(tag).intValue()){
139 HashSet newset=(HashSet)flagstate.clone();
140 Hashtable<TagDescriptor,Integer> newtags=(Hashtable<TagDescriptor,Integer>)tags.clone();
142 retstates=new FlagState[]{new FlagState(newset,cd,newtags)};
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);
161 throw new Error("Invalid Operation: Can not clear a tag that doesn't exist.");
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.
172 public String toString(FlagDescriptor[] flags)
174 StringBuffer sb = new StringBuffer(flagstate.size());
175 for(int i=0;i < flags.length; i++)
183 return new String(sb);
187 * @return returns the classdescriptor of the flagstate.
190 public ClassDescriptor getClassDescriptor(){
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>.
200 public FlagState setFlag(FlagDescriptor fd, boolean status) {
201 HashSet newset=(HashSet) flagstate.clone();
202 Hashtable<TagDescriptor,Integer> newtags=(Hashtable<TagDescriptor,Integer>)tags.clone();
205 else if (newset.contains(fd)){
209 return new FlagState(newset, cd,newtags);
212 /** Tests for equality of two flagstate objects.
215 public boolean equals(Object o) {
216 if (o instanceof FlagState) {
217 FlagState fs=(FlagState)o;
220 return (fs.flagstate.equals(flagstate) & fs.tags.equals(tags));
225 public int hashCode() {
226 return cd.hashCode()^flagstate.hashCode()^tags.hashCode();
229 public String getLabel() {
236 public String getTextLabel() {
238 for(Iterator it=getFlags();it.hasNext();) {
239 FlagDescriptor fd=(FlagDescriptor) it.next();
243 label+=", "+fd.toString();
245 for (Enumeration en_tags=getTags();en_tags.hasMoreElements();){
246 TagDescriptor td=(TagDescriptor)en_tags.nextElement();
247 switch (tags.get(td).intValue()){
250 label=td.toString()+"(1)";
252 label+=", "+td.toString()+"(1)";
256 label=td.toString()+"(n)";
258 label+=", "+td.toString()+"(n)";
269 public Enumeration getTags(){