1 17 18 package SOFA.SOFAnode.Util.DFSRChecker.node; 19 20 import java.util.ArrayList ; 21 import java.util.TreeSet ; 22 23 import SOFA.SOFAnode.Util.DFSRChecker.DFSR.CheckingException; 24 import SOFA.SOFAnode.Util.DFSRChecker.state.IndexState; 25 import SOFA.SOFAnode.Util.DFSRChecker.state.State; 26 import SOFA.SOFAnode.Util.DFSRChecker.state.TransitionPair; 27 import SOFA.SOFAnode.Util.DFSRChecker.state.TransitionPairs; 28 import SOFA.SOFAnode.Util.DFSRChecker.utils.AnotatedProtocol; 29 30 34 public class SequenceNode extends TreeNode { 35 36 37 public SequenceNode(TreeNode[] nodes) { 38 super(getProtocol(nodes)); 39 this.nodes = nodes; 40 41 last = nodes.length - 1; 42 } 43 44 48 public State getInitial() { 49 if (initial == null) { 50 State[] states = new State[nodes.length]; 51 for (int i = 0; i < nodes.length; ++i) 52 states[i] = nodes[i].getInitial(); 53 54 initial = new IndexState(0, states); 55 } 56 57 return initial; 58 } 59 60 64 public boolean isAccepting(State state) { 65 IndexState istate = (IndexState) state; 66 if ((istate.index == last) 67 && (nodes[last].isAccepting(istate.states[istate.index]))) 68 return true; 69 70 else if (nodes[istate.index].isAccepting(istate.states[istate.index])) { 71 for (int i = istate.index + 1; i <= last; ++i) { 72 if (!nodes[i].isAccepting(nodes[i].getInitial())) 73 return false; 74 } 75 76 return true; 77 } 78 79 else 80 return false; 81 } 82 83 87 public TransitionPairs getTransitions(State state) throws InvalidParameterException, CheckingException { 88 IndexState istate = (IndexState) state; 89 int stindex = istate.index; 90 91 if ((stindex != last) 92 && (nodes[stindex].isAccepting(istate.states[stindex]))) { 93 94 ArrayList tmptrans = new ArrayList (); 95 96 TransitionPair[] atrans = nodes[stindex].getTransitions(istate.states[stindex]).transitions; 97 98 int tmpindex = stindex; 99 int bsize = 0; 100 do { 101 ++tmpindex; 102 103 TransitionPair[] btrans = nodes[tmpindex].getTransitions(nodes[tmpindex].getInitial()).transitions; 104 105 tmptrans.add(btrans); 106 107 bsize += btrans.length; 108 109 } while ((nodes[tmpindex].isAccepting(nodes[tmpindex].getInitial())) 110 && (tmpindex < last)); 111 112 int alen = atrans.length; 113 int blen = bsize; 114 115 TransitionPair[] trans = new TransitionPair[alen + blen]; 116 117 for (int i = 0; i < alen; ++i) { 118 State[] tmpstates = new State[istate.states.length]; 119 for (int k = 0; k < istate.states.length; ++k) 120 tmpstates[k] = istate.states[k]; 121 122 tmpstates[stindex] = atrans[i].state; 123 124 trans[i] = new TransitionPair(atrans[i].eventIndex, 125 new IndexState(stindex, tmpstates)); 126 } 127 128 int idx = alen; 129 for (int j = 0; j < tmptrans.size(); ++j) { 130 for (int i = 0; i < ((TransitionPair[]) tmptrans.get(j)).length; ++i, ++idx) { 131 State[] tmpstates = new State[istate.states.length]; 132 for (int k = 0; k < istate.states.length; ++k) 133 tmpstates[k] = istate.states[k]; 134 135 TransitionPair current = ((TransitionPair[]) tmptrans 136 .get(j))[i]; 137 tmpstates[stindex + 1 + j] = current.state; 138 139 trans[idx] = new TransitionPair(current.eventIndex, 140 new IndexState(stindex + 1 + j, tmpstates)); 141 } 142 } 143 144 return new TransitionPairs(trans); 145 } 146 147 else { 148 TransitionPair[] atrans = nodes[stindex].getTransitions(istate.states[stindex]).transitions; 149 int alen = atrans.length; 150 151 TransitionPair[] trans = new TransitionPair[alen]; 152 for (int i = 0; i < alen; ++i) { 153 State[] tmpstates = new State[istate.states.length]; 154 for (int k = 0; k < istate.states.length; ++k) 155 tmpstates[k] = istate.states[k]; 156 157 tmpstates[stindex] = atrans[i].state; 158 159 trans[i] = new TransitionPair(atrans[i].eventIndex, 160 new IndexState(stindex, tmpstates)); 161 } 162 163 return new TransitionPairs(trans); 164 } 165 } 166 167 171 public long getWeight() { 172 if (weight != -1) 173 return weight; 174 else { 175 weight = 0; 176 for (int i = 0; i < nodes.length; ++i) 177 weight += nodes[i].getWeight() - 1; 178 179 return weight + 1; 180 } 181 } 182 183 187 public TreeNode forwardCut(TreeSet livingevents) { 188 TreeNode active = null; 189 int cnt = 0; 190 191 for (int i = 0; i < nodes.length; ++i) { 192 nodes[i] = nodes[i].forwardCut(livingevents); 193 if (nodes[i] != null) { 194 ++cnt; 195 active = nodes[i]; 196 } 197 } 198 199 if (cnt == 0) 201 return null; 202 203 else if ((cnt == 1) && (nodes.length > 1)) 206 return active; 207 208 else if (nodes.length != cnt) { 210 TreeNode[] newchildren = new TreeNode[cnt]; 211 212 for (int i = 0, j = 0; i < cnt; ++i) { 213 while (nodes[j] == null) 214 ++j; 215 216 newchildren[i] = nodes[j++]; 217 } 218 219 nodes = newchildren; 220 last = nodes.length - 1; 221 } 222 223 return this; 224 } 225 226 229 public String [] getTypeName() { 230 String [] result = { "Sequence", ";" }; 231 return result; 232 } 233 234 237 public AnotatedProtocol getAnotatedProtocol(State state) { 238 IndexState istate = (IndexState) state; 239 int nodecnt = nodes.length; 240 String result = new String (); 241 ArrayList indicesresult = new ArrayList (); 242 243 for (int i = 0; i < nodecnt; ++i) { 244 AnotatedProtocol subresult = nodes[i] 245 .getAnotatedProtocol(((istate != null) && (istate.index == i)) ? istate.states[i] 246 : null); 247 248 for (int j = 0; j < subresult.indices.size(); ++j) 249 indicesresult.add(new Integer (((Integer ) subresult.indices 250 .get(j)).intValue() 251 + result.length())); 252 253 result += subresult.protocol; 254 255 if (i < nodecnt - 1) 256 result += "; "; 257 } 258 259 return new AnotatedProtocol(result, indicesresult); 260 } 261 262 265 private boolean isLastAccepting() { 266 if (!isAcceptingSet) { 267 lastAccepting = nodes[last].isAccepting(nodes[last].getInitial()); 268 isAcceptingSet = true; 269 } 270 271 return lastAccepting; 272 } 273 274 280 static private String getProtocol(TreeNode[] nodes) { 281 String result = new String (); 282 result = "(" + nodes[0].protocol + ")"; 283 284 for (int i = 1; i < nodes.length; ++i) { 285 result += "; (" + nodes[i].protocol + ")"; 286 } 287 288 return result; 289 290 } 291 292 294 297 private int last; 298 299 302 private boolean lastAccepting; 303 304 private boolean isAcceptingSet = false; 305 306 309 private State initial = null; 310 311 } | Popular Tags |