KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > Util > DFSRChecker > node > AlternativeNode


1 /*
2  * $Id: AlternativeNode.java,v 1.4 2005/07/08 12:04:12 kofron Exp $
3  *
4  * Copyright 2004
5  * Distributed Systems Research Group
6  * Department of Software Engineering
7  * Faculty of Mathematics and Physics
8  * Charles University, Prague
9  *
10  * Copyright 2005
11  * Formal Methods In Software Engineering Group
12  * Institute of Computer Science
13  * Academy of Sciences of the Czech Republic
14  *
15  * This code was developed by Jan Kofron <kofron@nenya.ms.mff.cuni.cz>
16  */

17
18
19 package SOFA.SOFAnode.Util.DFSRChecker.node;
20
21 import java.util.ArrayList JavaDoc;
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 /**
34  * Alternative node represent the alternative operator.
35  */

36 public class AlternativeNode extends TreeNode {
37
38     /** Creates a new instance of AlternativeNode */
39     public AlternativeNode(TreeNode[] nodes) {
40         super(getProtocol(nodes));
41         this.nodes = nodes;
42
43         isorparallel = false;
44         initState = null;
45     }
46
47     /**
48      *
49      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.node.TreeNode#getInitial()
50      */

51     public State getInitial() {
52         if (initState == null) {
53             /*
54              * State[] states = new State[nodes.length]; for (int i = 0; i <
55              * states.length; ++i) states[i] = nodes[i].getInitial();
56              */

57             initState = new DenotedState(-1, new SimpleState(0), logchildrencnt);
58         }
59
60         return initState;
61     }
62
63     /**
64      *
65      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.node.TreeNode#isAccepting(SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.state.State)
66      */

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     /**
83      *
84      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.node.TreeNode#getTransitions(SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.state.State)
85      */

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     /**
126      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.node.TreeNode#getWeight()
127      */

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     /**
141      * @return the symbolic name of the treenode denoting its type
142      */

143     public String JavaDoc[] getTypeName() {
144         String JavaDoc[] result = { "Alternative", "+" };
145         return result;
146     }
147
148     /**
149      * Overrides the default implementation, because of the non-determinism that
150      * must be handled here. This is one of the only two places where
151      * non-determinism can inherently appear.
152      *
153      * @return number of bits needed to keep the determinized signature of this
154      * subtree
155      */

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         // adding the space necessary for identifying a child
172
// and assume that all of the children transition may start
173
// with the same transition
174
cnt += (nodes.length * max);
175
176         return cnt;
177     }
178
179     /**
180      * Sets the orparallel operator bit needed for protocol inverting.
181      */

182     public void setorparallel() {
183         isorparallel = true;
184     }
185
186     /**
187      * @return the orparallel bit
188      */

189     public boolean getorparallel() {
190         return isorparallel;
191     }
192
193     /**
194      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.node.TreeNode#getAnotatedProtocol(SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.state.State)
195      */

196     public AnotatedProtocol getAnotatedProtocol(State state) {
197         DenotedState dstate = (DenotedState) state;
198         int nodecnt = nodes.length;
199         String JavaDoc result = new String JavaDoc();
200         ArrayList JavaDoc indicesresult = new ArrayList JavaDoc();
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 JavaDoc(((Integer JavaDoc)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 JavaDoc(0));
218         
219         return new AnotatedProtocol(result, indicesresult);
220     }
221
222
223     
224     /**
225      * This method is obsolete and used only for debuging, the protocol is
226      * stored during the parse process.
227      *
228      * @deprecated
229      * @return the protocol of this tree.
230      */

231     static private String JavaDoc getProtocol(TreeNode[] nodes) {
232         String JavaDoc result = new String JavaDoc();
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     //--------------------------------------------------------------------------
244
/**
245      * Initial state of the automaton
246      */

247     private DenotedState initState;
248
249     /**
250      * logarithm of child count
251      */

252     private int logchildrencnt;
253
254     /**
255      * true if this is an orparallel node
256      */

257     private boolean isorparallel;
258
259 }
Popular Tags