KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: AdjustmentNode.java,v 1.4 2005/07/08 12:04:11 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.TreeSet JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.TreeMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 import SOFA.SOFAnode.Util.DFSRChecker.DFSR.CheckingException;
26 import SOFA.SOFAnode.Util.DFSRChecker.state.CompositeState;
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  * Adjustment node represent the adjustment operator
35  */

36 public class AdjustmentNode extends TreeNode {
37
38     /** Creates a new instance of AdjustmentNode */
39     public AdjustmentNode(TreeNode node1, TreeNode node2, TreeSet JavaDoc events) {
40         //super(getUnion(node1, node2));
41
super("(" + node1.protocol + ") " + "|S|" + " (" + node2.protocol + ")");
42         this.events = events;
43         this.nodes = new TreeNode[2];
44         this.nodes[0] = node1;
45         this.nodes[1] = node2;
46     }
47
48     /**
49      *
50      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.node.TreeNode#getInitial()
51      */

52     public State getInitial() {
53         State[] states = new State[2];
54         states[0] = nodes[0].getInitial();
55         states[1] = nodes[1].getInitial();
56
57         return new CompositeState(states);
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         CompositeState cstate = (CompositeState) state;
66
67         return (nodes[0].isAccepting(cstate.states[0])) && (nodes[1].isAccepting(cstate.states[1]));
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         CompositeState cstate = (CompositeState) state;
76
77         TransitionPair[] trans1 = nodes[0].getTransitions(cstate.states[0]).transitions;
78         TransitionPair[] trans2 = nodes[1].getTransitions(cstate.states[1]).transitions;
79
80         ArrayList JavaDoc result = new ArrayList JavaDoc();
81         TreeMap JavaDoc sync = new TreeMap JavaDoc();
82
83         //we divide the transitions of node1 (states[0]) into two groups
84
// - that of not present within the events set and the others
85
for (int i = 0; i < trans1.length; ++i) {
86             Integer JavaDoc key = new Integer JavaDoc(trans1[i].eventIndex);
87             Integer JavaDoc basekey = new Integer JavaDoc(ActionRepository.getPure(key.intValue()));
88
89             if (events.contains(basekey))
90                 if (sync.containsKey(key))
91                     ((ArrayList JavaDoc) sync.get(key)).add(trans1[i]);
92                 else {
93                     ArrayList JavaDoc value = new ArrayList JavaDoc();
94                     value.add(trans1[i]);
95                     sync.put(new Integer JavaDoc(trans1[i].eventIndex), value);
96                 }
97             else {
98                 State[] states = new State[2];
99                 states[0] = trans1[i].state;
100                 states[1] = cstate.states[1];
101
102                 result.add(new TransitionPair(trans1[i].eventIndex, new CompositeState(states)));
103             }
104         }
105
106         // now complete the result array
107
for (int i = 0; i < trans2.length; ++i) {
108             Integer JavaDoc key = new Integer JavaDoc(trans2[i].eventIndex);
109             Integer JavaDoc basekey = new Integer JavaDoc(ActionRepository.getPure(key.intValue()));
110
111             if (events.contains(basekey)) {
112                 if (sync.containsKey(key)) {
113                     Iterator JavaDoc it = ((ArrayList JavaDoc) sync.get(key)).iterator();
114
115                     while (it.hasNext()) {
116                         State[] states = new State[2];
117                         states[0] = ((TransitionPair) it.next()).state;
118                         states[1] = trans2[i].state;
119
120                         result.add(new TransitionPair(trans2[i].eventIndex, new CompositeState(states)));
121                     }
122                 }
123             }
124
125             else {
126                 State[] states = new State[2];
127                 states[0] = cstate.states[0];
128                 states[1] = trans2[i].state;
129
130                 result.add(new TransitionPair(trans2[i].eventIndex, new CompositeState(states)));
131             }
132         }
133
134         TransitionPair[] result1 = new TransitionPair[result.size()];
135         Iterator JavaDoc it = result.iterator();
136         int i = 0;
137
138         while (it.hasNext())
139             result1[i++] = (TransitionPair) it.next();
140
141         return new TransitionPairs(result1);
142     }
143
144     /**
145      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.node.TreeNode#getWeight()
146      */

147     public long getWeight() {
148         if (weight != -1)
149             return weight;
150         else {
151             return weight = nodes[0].getWeight() * nodes[1].getWeight();
152         }
153     }
154
155     /**
156      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.node.TreeNode#forwardCut(java.util.TreeSet)
157      */

158     public TreeNode forwardCut(TreeSet JavaDoc livingevents) {
159         TreeSet JavaDoc exlivev = new TreeSet JavaDoc(livingevents);
160
161         exlivev.addAll(events);
162         TreeSet JavaDoc exlivev2 = new TreeSet JavaDoc(exlivev);
163
164         nodes[0] = nodes[0].forwardCut(exlivev);
165         nodes[1] = nodes[1].forwardCut(exlivev2);
166
167         if ((nodes[0] == null) || (nodes[1] == null))
168             return null;
169
170         else
171             return this;
172     }
173
174     /**
175      * @return the symbolic name of the treenode denoting its type
176      */

177     public String JavaDoc[] getTypeName() {
178         String JavaDoc[] result = { "Adjustment", "/" };
179         return result;
180     }
181
182     /**
183      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.node.TreeNode#getAnotatedProtocol(SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.state.State)
184      */

185     public AnotatedProtocol getAnotatedProtocol(State state) {
186         throw new RuntimeException JavaDoc(this.getClass().getName() + ".getAnotatedProtocol is not implemented");
187     }
188
189     //--------------------------------------------------------------------------
190

191     /**
192      * The set of event for composition operator
193      */

194     final private TreeSet JavaDoc events;
195
196
197 }
Popular Tags