Java API By Example, From Geeks To Geeks.

# Java > Open Source Codes > SOFA > SOFAnode > Util > DFSRChecker > node > SequenceNode

 `1 /* 2  * \$Id: SequenceNode.java,v 1.4 2005/07/08 12:04:12 kofron Exp \$ 3  *4  * Copyright 20045  * Distributed Systems Research Group6  * Department of Software Engineering7  * Faculty of Mathematics and Physics8  * Charles University, Prague9  *10  * Copyright 200511  * Formal Methods In Software Engineering Group12  * Institute of Computer Science13  * Academy of Sciences of the Czech Republic14  * 15  * This code was developed by Jan Kofron 16  */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 /**31  * This class Represents the sequence operator. It supports any finite number of32  * automata to be sequenced.33  */34 public class SequenceNode extends TreeNode {35 36     /** Creates a new instance of SequenceNode */37     public SequenceNode(TreeNode[] nodes) {38         super(getProtocol(nodes));39         this.nodes = nodes;40 41         last = nodes.length - 1;42     }43 44     /**45      * 46      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.node.TreeNode#getInitial()47      */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     /**61      * 62      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.node.TreeNode#isAccepting(SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.state.State)63      */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         else80             return false;81     }82 83     /**84      * 85      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.node.TreeNode#getTransitions(SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.state.State)86      */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[]) tmptrans136                             .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     /**168      * 169      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.node.TreeNode#getWeight()170      */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     /**184      * 185      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.node.TreeNode#forwardCut(java.util.TreeSet)186      */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         // no child node's survived200 if (cnt == 0)201             return null;202 203         // only one child node's survived and it hasn't been the only child204 // else we have to return the original node205 else if ((cnt == 1) && (nodes.length > 1))206             return active;207 208         // some of the child nodes have died209 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     /**227      * @return the symbolic name of the treenode denoting its type228      */229     public String [] getTypeName() {230         String [] result = { "Sequence", ";" };231         return result;232     }233 234     /**235      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.node.TreeNode#getAnotatedProtocol(SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.state.State)236      */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.indices250                         .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     /**263      * @return true if the last subtree state is accepting264      */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     /**275      * This method is obsolete and used only for debuging, the protocol is276      * stored during the parse process277      * 278      * @return the protocol of this tree.279      */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     //--------------------------------------------------------------------------293 294     /**295      * Size of the node array296      */297     private int last;298 299     /**300      * Specifies whether the initial state of the last automaton is accepting301      */302     private boolean lastAccepting;303 304     private boolean isAcceptingSet = false;305 306     /**307      * cached initial state308      */309     private State initial = null;310 311 }` Popular Tags