KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: DeterministicNode.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
21 import java.util.ArrayList JavaDoc;
22 import java.util.LinkedList JavaDoc;
23 import java.util.TreeMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Set JavaDoc;
26 import java.util.TreeSet JavaDoc;
27
28 import SOFA.SOFAnode.Util.DFSRChecker.DFSR.CheckingException;
29 import SOFA.SOFAnode.Util.DFSRChecker.state.CompositeState;
30 import SOFA.SOFAnode.Util.DFSRChecker.state.State;
31 import SOFA.SOFAnode.Util.DFSRChecker.state.TransitionPair;
32 import SOFA.SOFAnode.Util.DFSRChecker.state.TransitionPairs;
33 import SOFA.SOFAnode.Util.DFSRChecker.utils.AnotatedProtocol;
34
35 /**
36  * Represented operation of determinization.
37  */

38 public class DeterministicNode extends TreeNode {
39
40     /**
41      * Creates a new instance of DeterministicNode
42      */

43     public DeterministicNode(TreeNode node) {
44         super("Det(" + node.protocol + ")");
45         this.nodes = new TreeNode[1];
46         this.nodes[0] = node;
47     }
48
49     /**
50      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.node.TreeNode#getInitial()
51      */

52     public State getInitial() {
53         State[] state = new State[1];
54         state[0] = nodes[0].getInitial();
55
56         return new CompositeState(state);
57     }
58
59     /**
60      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.node.TreeNode#isAccepting(SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.state.State)
61      */

62     public boolean isAccepting(State state) {
63         CompositeState cstate = (CompositeState) state;
64
65         for (int i = 0; i < cstate.states.length; ++i)
66             if (nodes[0].isAccepting(cstate.states[i]))
67                 return true;
68
69         return false;
70
71     }
72
73     /**
74      * This method creates the deterministic transition set form a
75      * nedeterministic one. It creates, of course, for each of the possible
76      * transitions via the same event a transition to a newly created
77      * CompositeState.
78      *
79      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.node.TreeNode#getTransitions(SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.state.State)
80      */

81     public TransitionPairs getTransitions(State state) throws InvalidParameterException, CheckingException {
82         CompositeState cstate = (CompositeState) state;
83         TreeMap JavaDoc transmap = new TreeMap JavaDoc();
84         LinkedList JavaDoc deps = new LinkedList JavaDoc();
85
86         int nodecnt = cstate.states.length;
87
88         for (int i = 0; i < nodecnt; ++i) {
89             TransitionPairs transp = nodes[0].getTransitions(cstate.states[i]);
90             TransitionPair[] trans = transp.transitions;
91             
92             if (transp.deps != null)
93                 deps.addAll(transp.deps);
94             
95             for (int j = 0; j < trans.length; ++j) {
96                 TreeSet JavaDoc states = (TreeSet JavaDoc) transmap.get(new Integer JavaDoc(trans[j].eventIndex));
97                 if (states != null) {
98                     states.add(trans[j].state);
99                 } else {
100                     states = new TreeSet JavaDoc();
101                     states.add(trans[j].state);
102                     transmap.put(new Integer JavaDoc(trans[j].eventIndex), states);
103                 }
104             }
105         }
106
107         TransitionPair[] result = new TransitionPair[transmap.size()];
108         Set JavaDoc keys = transmap.keySet();
109         Iterator JavaDoc it = keys.iterator();
110         int i = 0;
111
112         while (it.hasNext()) {
113             Integer JavaDoc eventindex = (Integer JavaDoc) it.next();
114             TreeSet JavaDoc states = (TreeSet JavaDoc) transmap.get(eventindex);
115             State[] resultstates = new State[states.size()];
116
117             Iterator JavaDoc it2 = states.iterator();
118             int l = 0;
119
120             while (it2.hasNext())
121                 resultstates[l++] = (State) it2.next();
122
123             result[i++] = new TransitionPair(eventindex.intValue(), new CompositeState(resultstates));
124         }
125
126         //TODO: debug only!
127
/*
128         for (i = 0; i < result.length; ++i)
129             result[i].state.getSignature();
130         */

131         
132         TransitionPairs restrans = new TransitionPairs(result);
133         restrans.deps = deps;
134         return restrans;
135     }
136
137     /**
138      *
139      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.node.TreeNode#getWeight()
140      */

141     public long getWeight() {
142         if (weight != -1)
143             return weight;
144         else {
145             return weight = nodes[0].getWeight();
146         }
147     }
148     
149     /**
150      * @return the symbolic name of the treenode denoting its type
151      */

152     public String JavaDoc[] getTypeName() {
153         String JavaDoc[] result = { "Deterministic", "DET" };
154         return result;
155     }
156     
157     
158
159     /**
160      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.node.TreeNode#getAnotatedProtocol(SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.state.State)
161      */

162     public AnotatedProtocol getAnotatedProtocol(State state) {
163         CompositeState cstate = (CompositeState) state;
164         String JavaDoc result = new String JavaDoc();
165         int cnt = cstate != null ? cstate.states.length : 0;
166         ArrayList JavaDoc indicesresult = new ArrayList JavaDoc();
167
168         for (int i = 0; i < cnt; ++i) {
169             AnotatedProtocol subresult = nodes[0].getAnotatedProtocol(cstate.states[i]);
170             
171             for (int j = 0; j < subresult.indices.size(); ++j)
172                 indicesresult.add(new Integer JavaDoc(((Integer JavaDoc)subresult.indices.get(j)).intValue() + result.length()));
173             
174         }
175         
176         result = nodes[0].getAnotatedProtocol(null).protocol;
177         
178         return new AnotatedProtocol(result, indicesresult);
179     }
180     
181
182     //---------------------------------------------------------------
183
}
Popular Tags