KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: AndParallelNode.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 package SOFA.SOFAnode.Util.DFSRChecker.node;
19
20 import java.util.ArrayList JavaDoc;
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 /**
32  * Represents andparalell operator
33  */

34 public class AndParallelNode extends TreeNode {
35
36     /** Creates a new instance of AndParallelNode */
37     public AndParallelNode(TreeNode[] nodes) {
38         super(getProtocol(nodes));
39         this.nodes = nodes;
40     }
41
42     /**
43      *
44      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.node.TreeNode#getInitial()
45      */

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     /**
56      *
57      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.node.TreeNode#isAccepting(SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.state.State)
58      */

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

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         // the original states with no transitions
84
for (int i = 0; i < statecnt; ++i) {
85             origstates[i] = cstate.states[i];
86         }
87
88         // prepare the matrix of transitions
89
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         // fill in the list of transitions
95
TransitionPair[] result = new TransitionPair[totalcnt];
96
97         // iteration over all states
98
for (int i = 0, idx = 0; i < statecnt; ++i) {
99
100             // iteration over the transition count of the given state
101
for (int j = 0; j < transmatrix[i].length; ++j, ++idx) {
102
103                 /**
104                  * one item of the result the result is array of transitionpairs
105                  * of composite states
106                  */

107                 State[] item = new State[statecnt];
108
109                 // last iteration again over all states
110
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     /**
123      *
124      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.node.TreeNode#getWeight()
125      */

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

141     public String JavaDoc[] getTypeName() {
142         String JavaDoc[] result = { "AndParallel", "|" };
143         return result;
144     }
145
146     
147     /**
148      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.node.TreeNode#getAnotatedProtocol(SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.state.State)
149      */

150     public AnotatedProtocol getAnotatedProtocol(State state) {
151         CompositeState cstate = (CompositeState) state;
152         int nodecnt = nodes.length;
153         String JavaDoc result = new String JavaDoc();
154         ArrayList JavaDoc indicesresult = new ArrayList JavaDoc();
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 JavaDoc(((Integer JavaDoc)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     /**
175      * This method is obsolete and used only for debuging, the protocol is
176      * stored during the parse process.
177      *
178      * @return the protocol of this tree.
179      */

180     static private String JavaDoc getProtocol(TreeNode[] nodes) {
181         String JavaDoc result = new String JavaDoc();
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