more code for state machines in dfj traversers
[IRC.git] / Robust / src / Analysis / Disjoint / SMFEState.java
1 package Analysis.Disjoint;
2
3 import java.util.*;
4 import java.io.*;
5
6 import IR.*;
7 import IR.Flat.*;
8
9 //////////////////////////////////////////////
10 //
11 //  SMFEState is part of a 
12 //  (S)tate (M)achine (F)or (E)ffects.
13 //
14 //  StateMachineForEffects describes an intial
15 //  state and the effect transtions a DFJ
16 //  traverser should make from the current state
17 //  when searching for possible runtime conflicts.
18 //
19 //////////////////////////////////////////////
20
21 public class SMFEState {
22
23   // uniquely identifies this state
24   protected FlatNode id;
25
26   // all possible effects in this state
27   protected Set<Effect> effects;
28
29   // the given effect allows a transition to a
30   // set of new states
31   protected Hashtable< Effect, Set<SMFEState> > e2states;
32
33   
34   public SMFEState( FlatNode id ) {
35     this.id = id;
36     effects  = new HashSet<Effect>();
37     e2states = new Hashtable< Effect, Set<SMFEState> >();
38   }
39
40   public void addEffect( Effect e ) {
41     effects.add( e );
42   }
43
44   // the given effect allows the transition to the new state
45   public void addTransition( Effect    effect,
46                              SMFEState stateTo
47                              ) {
48
49     Set<SMFEState> states = e2states.get( effect );
50     if( states == null ) {
51       states = new HashSet<SMFEState>();
52     }
53     states.add( stateTo );
54   }
55
56   public FlatNode getID() {
57     return id;
58   }
59
60
61   public boolean equals( Object o ) {
62     if( o == null ) {
63       return false;
64     }
65
66     if( !(o instanceof SMFEState) ) {
67       return false;
68     }
69
70     SMFEState state = (SMFEState) o;
71
72     return id.equals( state.id );
73   }
74
75   public int hashCode() {
76     return id.hashCode();
77   }
78
79
80   public String toStringDOT() {
81     
82     // first create the state as a node in DOT graph
83     String s = "  "+id.nodeid+
84       "[shape=box,label=\"";
85
86     if( effects.size() == 1 ) {
87       s += effects.iterator().next().toString();
88
89     } else if( effects.size() > 1 ) {
90
91       Iterator<Effect> eItr = effects.iterator();
92       while( eItr.hasNext() ) {
93         Effect e = eItr.next();
94         s += e.toString();
95
96         if( eItr.hasNext() ) {
97           s += "\\n";
98         }
99       }
100     }
101
102     s += "\"];";
103
104     // then each transition is an edge
105     Iterator<Effect> eItr = e2states.keySet().iterator();
106     while( eItr.hasNext() ) {
107       Effect         e      = eItr.next();
108       Set<SMFEState> states = e2states.get( e );
109
110       Iterator<SMFEState> sItr = states.iterator();
111       while( sItr.hasNext() ) {
112         SMFEState state = sItr.next();
113
114         s += "\n  "+
115           id.nodeid+" -> "+state.id.nodeid+
116           "[label=\""+e+"\"];";
117       }
118     }
119
120     return s;
121   }
122 }