1 17 18 19 package SOFA.SOFAnode.Util.DFSRChecker.node; 20 21 import java.util.ArrayList ; 22 23 24 import SOFA.SOFAnode.Util.DFSRChecker.DFSR.CheckingException; 25 import SOFA.SOFAnode.Util.DFSRChecker.state.DenotedState; 26 import SOFA.SOFAnode.Util.DFSRChecker.state.SimpleState; 27 import SOFA.SOFAnode.Util.DFSRChecker.state.State; 28 import SOFA.SOFAnode.Util.DFSRChecker.state.TransitionPair; 29 import SOFA.SOFAnode.Util.DFSRChecker.state.TransitionPairs; 30 import SOFA.SOFAnode.Util.DFSRChecker.utils.AnotatedProtocol; 31 32 33 36 public class AlternativeNode extends TreeNode { 37 38 39 public AlternativeNode(TreeNode[] nodes) { 40 super(getProtocol(nodes)); 41 this.nodes = nodes; 42 43 isorparallel = false; 44 initState = null; 45 } 46 47 51 public State getInitial() { 52 if (initState == null) { 53 57 initState = new DenotedState(-1, new SimpleState(0), logchildrencnt); 58 } 59 60 return initState; 61 } 62 63 67 public boolean isAccepting(State state) { 68 int index = ((DenotedState) state).index; 69 70 if (index == -1) { 71 for (int i = 0; i < nodes.length; ++i) 72 if (nodes[i].isAccepting(nodes[i].getInitial())) 73 return true; 74 75 return false; 76 } else { 77 return nodes[index].isAccepting(((DenotedState) state).state); 78 } 79 80 } 81 82 86 public TransitionPairs getTransitions(State state) throws InvalidParameterException, CheckingException { 87 DenotedState dstate = (DenotedState) state; 88 int stindex = dstate.index; 89 int nodecnt = nodes.length; 90 int totallen = 0; 91 92 if (stindex == -1) { 93 TransitionPair[][] trans = new TransitionPair[nodecnt][]; 94 95 for (int i = 0; i < nodecnt; ++i) { 96 trans[i] = nodes[i].getTransitions(nodes[i].getInitial()).transitions; 97 totallen += trans[i].length; 98 } 99 100 TransitionPair[] result = new TransitionPair[totallen]; 101 102 for (int i = 0, idx = 0; idx < nodecnt; ++idx) { 103 for (int j = 0; j < trans[idx].length; ++j, ++i) { 104 105 State newstate = trans[idx][j].state; 106 result[i] = new TransitionPair(trans[idx][j].eventIndex, new DenotedState(idx, newstate, logchildrencnt)); 107 } 108 } 109 110 return new TransitionPairs(result); 111 112 } else { 113 114 TransitionPair[] nodetrans = nodes[stindex].getTransitions(dstate.state).transitions; 115 int nodelen = nodetrans.length; 116 TransitionPair[] trans = new TransitionPair[nodetrans.length]; 117 118 for (int i = 0; i < nodetrans.length; ++i) 119 trans[i] = new TransitionPair(nodetrans[i].eventIndex, new DenotedState(stindex, nodetrans[i].state, logchildrencnt)); 120 121 return new TransitionPairs(trans); 122 } 123 } 124 125 128 public long getWeight() { 129 if (weight != -1) 130 return weight; 131 else { 132 long weight = 0; 133 for (int i = 0; i < nodes.length; ++i) 134 weight += nodes[i].getWeight(); 135 136 return weight; 137 } 138 } 139 140 143 public String [] getTypeName() { 144 String [] result = { "Alternative", "+" }; 145 return result; 146 } 147 148 156 public int getLeafCount() { 157 int cnt = 0; 158 for (int i = 0; i < nodes.length; ++i) 159 cnt += nodes[i].getLeafCount(); 160 161 int max = 0; 162 for (int i = 0; i < 32; ++i) { 163 if ((nodes.length >>> i) == 0) { 164 max = i; 165 break; 166 } 167 } 168 169 this.logchildrencnt = max; 170 171 cnt += (nodes.length * max); 175 176 return cnt; 177 } 178 179 182 public void setorparallel() { 183 isorparallel = true; 184 } 185 186 189 public boolean getorparallel() { 190 return isorparallel; 191 } 192 193 196 public AnotatedProtocol getAnotatedProtocol(State state) { 197 DenotedState dstate = (DenotedState) state; 198 int nodecnt = nodes.length; 199 String result = new String (); 200 ArrayList indicesresult = new ArrayList (); 201 202 result = "("; 203 for (int i = 0; i < nodecnt; ++i) { 204 AnotatedProtocol subresult = nodes[i].getAnotatedProtocol(((dstate != null) && (dstate.index == i)) ? dstate.state : null); 205 206 for (int j = 0; j < subresult.indices.size(); ++j) 207 indicesresult.add(new Integer (((Integer )subresult.indices.get(j)).intValue() + result.length())); 208 209 result += subresult.protocol; 210 211 if (i < nodecnt - 1) 212 result += ")+("; 213 } 214 result += ")"; 215 216 if ((dstate != null) && (dstate.index == -1)) 217 indicesresult.add(new Integer (0)); 218 219 return new AnotatedProtocol(result, indicesresult); 220 } 221 222 223 224 231 static private String getProtocol(TreeNode[] nodes) { 232 String result = new String (); 233 result = "(" + nodes[0].protocol + ")"; 234 235 for (int i = 1; i < nodes.length; ++i) { 236 result += " + (" + nodes[i].protocol + ")"; 237 } 238 239 return result; 240 241 } 242 243 247 private DenotedState initState; 248 249 252 private int logchildrencnt; 253 254 257 private boolean isorparallel; 258 259 } | Popular Tags |