1 17 18 package SOFA.SOFAnode.Util.DFSRChecker.node; 19 20 21 import java.util.ArrayList ; 22 import java.util.LinkedList ; 23 import java.util.TreeMap ; 24 import java.util.Iterator ; 25 import java.util.Set ; 26 import java.util.TreeSet ; 27 28 import SOFA.SOFAnode.Util.DFSRChecker.DFSR.CheckingException; 29 import SOFA.SOFAnode.Util.DFSRChecker.state.CompositeState; 30 import SOFA.SOFAnode.Util.DFSRChecker.state.State; 31 import SOFA.SOFAnode.Util.DFSRChecker.state.TransitionPair; 32 import SOFA.SOFAnode.Util.DFSRChecker.state.TransitionPairs; 33 import SOFA.SOFAnode.Util.DFSRChecker.utils.AnotatedProtocol; 34 35 38 public class DeterministicNode extends TreeNode { 39 40 43 public DeterministicNode(TreeNode node) { 44 super("Det(" + node.protocol + ")"); 45 this.nodes = new TreeNode[1]; 46 this.nodes[0] = node; 47 } 48 49 52 public State getInitial() { 53 State[] state = new State[1]; 54 state[0] = nodes[0].getInitial(); 55 56 return new CompositeState(state); 57 } 58 59 62 public boolean isAccepting(State state) { 63 CompositeState cstate = (CompositeState) state; 64 65 for (int i = 0; i < cstate.states.length; ++i) 66 if (nodes[0].isAccepting(cstate.states[i])) 67 return true; 68 69 return false; 70 71 } 72 73 81 public TransitionPairs getTransitions(State state) throws InvalidParameterException, CheckingException { 82 CompositeState cstate = (CompositeState) state; 83 TreeMap transmap = new TreeMap (); 84 LinkedList deps = new LinkedList (); 85 86 int nodecnt = cstate.states.length; 87 88 for (int i = 0; i < nodecnt; ++i) { 89 TransitionPairs transp = nodes[0].getTransitions(cstate.states[i]); 90 TransitionPair[] trans = transp.transitions; 91 92 if (transp.deps != null) 93 deps.addAll(transp.deps); 94 95 for (int j = 0; j < trans.length; ++j) { 96 TreeSet states = (TreeSet ) transmap.get(new Integer (trans[j].eventIndex)); 97 if (states != null) { 98 states.add(trans[j].state); 99 } else { 100 states = new TreeSet (); 101 states.add(trans[j].state); 102 transmap.put(new Integer (trans[j].eventIndex), states); 103 } 104 } 105 } 106 107 TransitionPair[] result = new TransitionPair[transmap.size()]; 108 Set keys = transmap.keySet(); 109 Iterator it = keys.iterator(); 110 int i = 0; 111 112 while (it.hasNext()) { 113 Integer eventindex = (Integer ) it.next(); 114 TreeSet states = (TreeSet ) transmap.get(eventindex); 115 State[] resultstates = new State[states.size()]; 116 117 Iterator it2 = states.iterator(); 118 int l = 0; 119 120 while (it2.hasNext()) 121 resultstates[l++] = (State) it2.next(); 122 123 result[i++] = new TransitionPair(eventindex.intValue(), new CompositeState(resultstates)); 124 } 125 126 131 132 TransitionPairs restrans = new TransitionPairs(result); 133 restrans.deps = deps; 134 return restrans; 135 } 136 137 141 public long getWeight() { 142 if (weight != -1) 143 return weight; 144 else { 145 return weight = nodes[0].getWeight(); 146 } 147 } 148 149 152 public String [] getTypeName() { 153 String [] result = { "Deterministic", "DET" }; 154 return result; 155 } 156 157 158 159 162 public AnotatedProtocol getAnotatedProtocol(State state) { 163 CompositeState cstate = (CompositeState) state; 164 String result = new String (); 165 int cnt = cstate != null ? cstate.states.length : 0; 166 ArrayList indicesresult = new ArrayList (); 167 168 for (int i = 0; i < cnt; ++i) { 169 AnotatedProtocol subresult = nodes[0].getAnotatedProtocol(cstate.states[i]); 170 171 for (int j = 0; j < subresult.indices.size(); ++j) 172 indicesresult.add(new Integer (((Integer )subresult.indices.get(j)).intValue() + result.length())); 173 174 } 175 176 result = nodes[0].getAnotatedProtocol(null).protocol; 177 178 return new AnotatedProtocol(result, indicesresult); 179 } 180 181 182 } | Popular Tags |