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;
28 * Creates a new flagstate with all flags set to false.
29 * @param cd ClassDescriptor
31 public FlagState(ClassDescriptor cd) {
32 this.flagstate=new HashSet();
34 this.tags=new Hashtable<TagDescriptor,Integer>();
35 this.uid=FlagState.nodeid++;
36 this.issourcenode=false;
40 * Creates a new flagstate with flags set according to the HashSet.
41 * If the flag exists in the hashset, it's set to true else set to false.
42 * @param cd ClassDescriptor
43 * @param flagstate a <CODE>HashSet</CODE> containing FlagDescriptors
45 private FlagState(HashSet flagstate, ClassDescriptor cd,Hashtable<TagDescriptor,Integer> tags) {
46 this.flagstate=flagstate;
49 this.uid=FlagState.nodeid++;
50 this.issourcenode=false;
59 * @param fd FlagDescriptor
60 * @return true if the flagstate contains fd else false.
62 public boolean get(FlagDescriptor fd) {
63 return flagstate.contains(fd);
66 /** Checks if the flagstate is a source node.
67 * @return true if the flagstate is a sourcenode(i.e. Is the product of an allocation site).
70 public boolean isSourceNode(){
74 /** Sets the flagstate as a source node.
76 public void setAsSourceNode(){
79 this.tasks=new Vector();
83 public void addAllocatingTask(TaskDescriptor task){
87 public Vector getAllocatingTasks(){
92 public String toString() {
93 return cd.toString()+getTextLabel();
96 /** @return Iterator over the flags in the flagstate.
99 public Iterator getFlags() {
100 return flagstate.iterator();
103 public int numFlags(){
104 return flagstate.size();
107 public FlagState[] setTag(TagDescriptor tag){
108 HashSet newset1=(HashSet)flagstate.clone();
109 Hashtable<TagDescriptor,Integer> newtags1=(Hashtable<TagDescriptor,Integer>)tags.clone();
111 if (tags.containsKey(tag)){
112 //Code could try to remove flag that doesn't exist
114 switch (tags.get(tag).intValue()){
116 newtags1.put(tag,new Integer(MULTITAGS));
117 return new FlagState[] {this, new FlagState(newset1, cd, newtags1)};
119 return new FlagState[] {this};
124 newtags1.put(tag,new Integer(ONETAG));
125 return new FlagState[] {new FlagState(newset1,cd,newtags1)};
129 public int getTagCount(String tagtype){
130 for (Enumeration en=getTags();en.hasMoreElements();){
131 TagDescriptor td=(TagDescriptor)en.nextElement();
132 if (tagtype.equals(td.getSymbol()))
133 return tags.get(td).intValue(); //returns either ONETAG or MULTITAG
138 public FlagState[] clearTag(TagDescriptor tag){
139 if (tags.containsKey(tag)){
140 switch(tags.get(tag).intValue()){
142 HashSet newset=(HashSet)flagstate.clone();
143 Hashtable<TagDescriptor,Integer> newtags=(Hashtable<TagDescriptor,Integer>)tags.clone();
145 return new FlagState[]{new FlagState(newset,cd,newtags)};
148 //two possibilities - count remains 2 or becomes 1
150 HashSet newset1=(HashSet)flagstate.clone();
151 Hashtable<TagDescriptor,Integer> newtags1=(Hashtable<TagDescriptor,Integer>)tags.clone();
154 HashSet newset2=(HashSet)flagstate.clone();
155 Hashtable<TagDescriptor,Integer> newtags2=(Hashtable<TagDescriptor,Integer>)tags.clone();
156 newtags1.put(tag,new Integer(ONETAG));
157 return new FlagState[] {new FlagState(newset1, cd, newtags2),
158 new FlagState(newset2, cd, newtags2)};
163 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(){