1 17 18 package SOFA.SOFAnode.Util.DFSRChecker.node; 19 20 import java.util.ArrayList ; 21 22 23 import SOFA.SOFAnode.Util.DFSRChecker.DFSR.CheckingException; 24 import SOFA.SOFAnode.Util.DFSRChecker.state.CompositeState; 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 31 34 public class AndParallelNode extends TreeNode { 35 36 37 public AndParallelNode(TreeNode[] nodes) { 38 super(getProtocol(nodes)); 39 this.nodes = nodes; 40 } 41 42 46 public State getInitial() { 47 State[] init = new State[nodes.length]; 48 49 for (int i = 0; i < nodes.length; ++i) { 50 init[i] = nodes[i].getInitial(); 51 } 52 return new CompositeState(init); 53 } 54 55 59 public boolean isAccepting(State state) { 60 61 State[] states = ((CompositeState) state).states; 62 63 for (int i = 0; i < nodes.length; ++i) 64 if (!nodes[i].isAccepting(states[i])) 65 return false; 66 67 return true; 68 } 69 70 74 public TransitionPairs getTransitions(State state) throws InvalidParameterException, CheckingException { 75 76 CompositeState cstate = (CompositeState) state; 77 int statecnt = cstate.states.length; 78 int totalcnt = 0; 79 80 TransitionPair[][] transmatrix = new TransitionPair[statecnt][]; 81 State[] origstates = new State[statecnt]; 82 83 for (int i = 0; i < statecnt; ++i) { 85 origstates[i] = cstate.states[i]; 86 } 87 88 for (int i = 0; i < statecnt; ++i) { 90 transmatrix[i] = nodes[i].getTransitions(cstate.states[i]).transitions; 91 totalcnt += transmatrix[i].length; 92 } 93 94 TransitionPair[] result = new TransitionPair[totalcnt]; 96 97 for (int i = 0, idx = 0; i < statecnt; ++i) { 99 100 for (int j = 0; j < transmatrix[i].length; ++j, ++idx) { 102 103 107 State[] item = new State[statecnt]; 108 109 for (int k = 0; k < statecnt; ++k) 111 item[k] = origstates[k]; 112 113 item[i] = transmatrix[i][j].state; 114 115 result[idx] = new TransitionPair(transmatrix[i][j].eventIndex, new CompositeState(item)); 116 } 117 } 118 119 return new TransitionPairs(result); 120 } 121 122 126 public long getWeight() { 127 if (weight != -1) 128 return weight; 129 else { 130 weight = 1; 131 for (int i = 0; i < nodes.length; ++i) 132 weight *= nodes[i].getWeight(); 133 134 return weight; 135 } 136 } 137 138 141 public String [] getTypeName() { 142 String [] result = { "AndParallel", "|" }; 143 return result; 144 } 145 146 147 150 public AnotatedProtocol getAnotatedProtocol(State state) { 151 CompositeState cstate = (CompositeState) state; 152 int nodecnt = nodes.length; 153 String result = new String (); 154 ArrayList indicesresult = new ArrayList (); 155 156 result = "("; 157 for (int i = 0; i < nodecnt; ++i) { 158 AnotatedProtocol subresult = nodes[i].getAnotatedProtocol((cstate != null) ? cstate.states[i] : null); 159 160 for (int j = 0; j < subresult.indices.size(); ++j) 161 indicesresult.add(new Integer (((Integer )subresult.indices.get(j)).intValue() + result.length())); 162 163 result += subresult.protocol; 164 165 if (i < nodecnt - 1) 166 result += ")|("; 167 } 168 result += ")"; 169 170 return new AnotatedProtocol(result, indicesresult); 171 } 172 173 174 180 static private String getProtocol(TreeNode[] nodes) { 181 String result = new String (); 182 result = "(" + nodes[0].protocol + ")"; 183 184 for (int i = 1; i < nodes.length; ++i) { 185 result += " | (" + nodes[i].protocol + ")"; 186 } 187 188 return result; 189 190 } 191 192 } 193 194 | Popular Tags |