KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: RepetitionNode.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 import java.util.TreeSet JavaDoc;
22
23 import SOFA.SOFAnode.Util.DFSRChecker.DFSR.CheckingException;
24 import SOFA.SOFAnode.Util.DFSRChecker.state.State;
25 import SOFA.SOFAnode.Util.DFSRChecker.state.TransitionPair;
26 import SOFA.SOFAnode.Util.DFSRChecker.state.TransitionPairs;
27 import SOFA.SOFAnode.Util.DFSRChecker.utils.AnotatedProtocol;
28
29
30 /**
31  * Represents repetition operator
32  */

33 public class RepetitionNode extends TreeNode {
34
35     /** Creates a new instance of RepetitionNode */
36     public RepetitionNode(TreeNode node) {
37         //super(node.getActiveEvents());
38
super("(" + node.protocol + ")*");
39         this.nodes = new TreeNode[1];
40         this.nodes[0] = node;
41
42         //this.initial = node.getInitial(); // this cannot be since forward
43
// cutting may change the tree structure!!!
44
this.initial = null;
45         //this.initial.getSignature(new Signature(), 0);
46

47     }
48
49     /**
50      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.node.TreeNode#getInitial()
51      */

52     public State getInitial() {
53         if (initial == null)
54             initial = nodes[0].getInitial();
55
56         return initial;
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         return this.initial.equals(state) || nodes[0].isAccepting(state);
64     }
65
66     /**
67      * @version 2.2.0
68      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.node.TreeNode#getTransitions(SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.state.State)
69      */

70     /*
71      * public TransitionPair[] getTransitions(State state) throws
72      * InvalidParameterException, CheckingException { State initial =
73      * nodes[0].getInitial();; TransitionPair[] origtrans =
74      * nodes[0].getTransitions(state); int transcnt = origtrans.length;
75      * ArrayList addedtrans = new ArrayList(); boolean acc = false;
76      *
77      * for (int i = 0; i < transcnt; ++i) { if
78      * (nodes[0].isAccepting(origtrans[i].state)) { acc = true;
79      * addedtrans.add(new TransitionPair(origtrans[i].eventIndex, initial)); } }
80      *
81      * if (acc) { TransitionPair[] newtrans = new TransitionPair[transcnt +
82      * addedtrans.size()];
83      *
84      * int i = 0; for (; i < transcnt; ++i) newtrans[i] = origtrans[i];
85      *
86      * Iterator it = addedtrans.iterator();
87      *
88      * while (it.hasNext()) newtrans[i++] = (TransitionPair)it.next();
89      *
90      *
91      * return newtrans; } else return origtrans; }
92      */

93
94     /**
95      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.node.TreeNode#getTransitions(SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.state.State)
96      */

97     public TransitionPairs getTransitions(State state) throws InvalidParameterException, CheckingException {
98         State initial = nodes[0].getInitial();
99         
100         TransitionPair[] origtrans = nodes[0].getTransitions(state).transitions;
101         int transcnt = origtrans.length;
102
103         if (nodes[0].isAccepting(state)) {
104             initial = nodes[0].getInitial();
105             TransitionPair[] trans = nodes[0].getTransitions(initial).transitions;
106
107             TransitionPair[] result = new TransitionPair[trans.length + transcnt];
108
109             for (int i = 0; i < transcnt; ++i)
110                 result[i] = origtrans[i];
111
112             for (int i = 0; i < trans.length; ++i)
113                 result[i + transcnt] = trans[i];
114
115             return new TransitionPairs(result);
116         }
117
118         else {
119             return new TransitionPairs(origtrans);
120         }
121     }
122
123
124     /**
125      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.node.TreeNode#getWeight()
126      */

127     public long getWeight() {
128         if (weight != -1)
129             return weight;
130         else {
131             return weight = nodes[0].getWeight();
132         }
133     }
134
135     /**
136      * @return the symbolic name of the treenode denoting its type
137      */

138     public String JavaDoc[] getTypeName() {
139         String JavaDoc[] result = { "Repetition", "*" };
140         return result;
141     }
142
143     /**
144      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.node.TreeNode#forwardCut(TreeSet)
145      */

146     public TreeNode forwardCut(TreeSet JavaDoc livingevents) {
147         TreeNode active = null;
148         int cnt = 0;
149
150         nodes[0] = nodes[0].forwardCut(livingevents);
151         if (nodes[0] != null) {
152             if (nodes[0] instanceof RepetitionNode)
153                 return nodes[0];
154             else
155                 return this;
156         } else
157             return null;
158
159     }
160
161     /**
162      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.node.TreeNode#getLeafCount()
163      * @return number of children of subtree multiplicated by two for the case
164      * of nondeterminism.
165      */

166     public int getLeafCount() {
167         return (nodes[0].getLeafCount()) * 2;
168     }
169     
170     /**
171      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.node.TreeNode#getAnotatedProtocol(SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.state.State)
172      */

173     public AnotatedProtocol getAnotatedProtocol(State state) {
174         String JavaDoc result = new String JavaDoc();;
175         ArrayList JavaDoc indices = new ArrayList JavaDoc();
176         
177         result = "(";
178         
179         AnotatedProtocol subresult = nodes[0].getAnotatedProtocol(state);
180         
181         for (int i = 0 ; i < subresult.indices.size(); ++i)
182             indices.add(new Integer JavaDoc(((Integer JavaDoc)subresult.indices.get(i)).intValue() + result.length()));
183         
184         result += subresult.protocol + ")*";
185         
186         return new AnotatedProtocol(result, indices);
187     }
188
189     //----------------------------------------------------------------------------------
190
/**
191      * The initial node - we need this for comparison during compliance test,
192      * since the signatures are lazy evaluated.
193      */

194     private State initial;
195
196 }
Popular Tags