KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > Util > DFSRChecker > backend > ConsentBackend


1 /*
2  * $Id: ConsentBackend.java,v 1.2 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.backend;
19
20 import java.util.TreeSet JavaDoc;
21
22 import SOFA.SOFAnode.Util.DFSRChecker.CheckingResult;
23 import SOFA.SOFAnode.Util.DFSRChecker.DFSR.AcceptingStateException;
24 import SOFA.SOFAnode.Util.DFSRChecker.DFSR.BadActivityException;
25 import SOFA.SOFAnode.Util.DFSRChecker.DFSR.CheckingException;
26 import SOFA.SOFAnode.Util.DFSRChecker.DFSR.DFSRConsent;
27 import SOFA.SOFAnode.Util.DFSRChecker.DFSR.DFSRTraverser;
28 import SOFA.SOFAnode.Util.DFSRChecker.DFSR.InfiniteActivityException;
29 import SOFA.SOFAnode.Util.DFSRChecker.DFSR.ItemAlreadyPresentException;
30 import SOFA.SOFAnode.Util.DFSRChecker.DFSR.ItemNotPresentException;
31 import SOFA.SOFAnode.Util.DFSRChecker.DFSR.NoActivityException;
32 import SOFA.SOFAnode.Util.DFSRChecker.DFSR.Options;
33 import SOFA.SOFAnode.Util.DFSRChecker.node.ActionRepository;
34 import SOFA.SOFAnode.Util.DFSRChecker.node.AlternativeNode;
35 import SOFA.SOFAnode.Util.DFSRChecker.node.ConsentNode;
36 import SOFA.SOFAnode.Util.DFSRChecker.node.DeterministicNode;
37 import SOFA.SOFAnode.Util.DFSRChecker.node.EventNode;
38 import SOFA.SOFAnode.Util.DFSRChecker.node.InvalidParameterException;
39 import SOFA.SOFAnode.Util.DFSRChecker.node.TreeNode;
40 import SOFA.SOFAnode.Util.DFSRChecker.optimizer.OptExplicit;
41 import SOFA.SOFAnode.Util.DFSRChecker.parser.Debug;
42 import SOFA.SOFAnode.Util.DFSRChecker.state.Signature;
43 import SOFA.SOFAnode.Util.DFSRChecker.state.TransitionPair;
44
45
46 /**
47  * This class joins two preotocols via a consent operator and perform the
48  * consensual compliance test.
49  */

50 public class ConsentBackend implements IBackend {
51
52     /**
53      * Creates a new instance of ConsentBackend.
54      * @param repository action repository
55      */

56     public ConsentBackend(ActionRepository repository) {
57         this.repository = repository;
58     }
59
60     /**
61      * Checks the consensual compliance and composition of protocols in the way that
62      * it creates an "inverse" protocol of the frame protocol and then tries to
63      * combine the protocols using consent operator.
64      *
65      * @param nodes two or more parse tree nodes for two or more protocols are expected (architecture-frame and subcomponents-frame)
66      * @param synchroops synchro operations for the consent operator for the sequence of protocols.
67      * @param unboundops methods of unbound interfaces.
68      *
69      */

70     public CheckingResult perform(TreeNode[] nodes, String JavaDoc[] synchroops, String JavaDoc[] unboundops) {
71
72         //basic checking of parameters
73
if (nodes.length < 2) {
74             return new CheckingResult(CheckingResult.ERR_OTHERERROR, "Consent backend: wrong number of items within the first array parameter (at least two required).\n");
75         }
76
77         if (synchroops.length != nodes.length - 1) {
78             return new CheckingResult(CheckingResult.ERR_OTHERERROR, "Consent backend: wrong number of items within the second array parameter (1 lower than the number of components required).\n");
79         }
80
81         // invert the frame protocol, if it is a frame protocol, i.e. we are
82
// testing compliance
83
if (Options.action == Options.ACTIONTESTCOMPLIANCE)
84             nodes[0] = invertProtocol(nodes[0], repository);
85
86         TreeNode result = new DeterministicNode(nodes[nodes.length - 1]);
87
88         TreeSet JavaDoc unbound = new TreeSet JavaDoc();
89         for (int y = 0; y < unboundops.length; ++y) {
90             unbound.add(new Integer JavaDoc(repository.getItemIndex(unboundops[y])));
91         }
92
93         for (int j = nodes.length - 2; j > 0; --j) {
94             String JavaDoc[] sprov;
95             TreeSet JavaDoc prov = new TreeSet JavaDoc();
96             sprov = synchroops[j].split(",");
97
98             for (int i = 0; i < sprov.length; ++i) {
99                 int index = repository.getItemIndex(sprov[i]);
100                 if (index == -1) {
101                     return new CheckingResult(CheckingResult.ERR_OTHERERROR, "Invalid provisions - not occuring within the protocols.");
102                 } else {
103                     prov.add(new Integer JavaDoc(index));
104                 }
105             }
106
107             // create the consent node joining the two protocols
108
result = new ConsentNode(new DeterministicNode(nodes[j]), result, prov, new TreeSet JavaDoc(), repository, true);
109         }
110
111         //-------------- consensual compliance test
112
String JavaDoc[] sprov;
113         TreeSet JavaDoc prov = new TreeSet JavaDoc();
114         sprov = synchroops[0].split(",");
115
116         for (int i = 0; i < sprov.length; ++i) {
117             int index = repository.getItemIndex(sprov[i]);
118             if (index == -1) {
119                 return new CheckingResult(CheckingResult.ERR_OTHERERROR, "Invalid provisions - not occuring within the protocols.");
120             } else {
121                 prov.add(new Integer JavaDoc(index));
122             }
123         }
124
125         ConsentNode top = new ConsentNode(new DeterministicNode(nodes[0]), result, prov, unbound, repository, true);
126         
127         Debug.println("State space estimate: " + top.getWeight());
128
129         // perform the forward cutting and explicit optimizations
130
Debug.print("Optimizing the parse tree for the composition test......");
131
132         try {
133             if (Options.forwardcutting)
134                 top = (ConsentNode) top.forwardCut(repository.getAllUsedEventIndices());
135             
136             if (top != null)
137                 Signature.setSize(top.getLeafCount());
138
139             if ((Options.explicit) && (top != null))
140                 top = (ConsentNode) new OptExplicit(top, Options.explicitsize).perform();
141         } catch (InvalidParameterException e) {
142             return new CheckingResult(CheckingResult.ERR_OTHERERROR, "Error while performing optimization. Bailing out....");
143         }
144
145         Debug.println("done.");
146         /**/
147
148         // check the compliance
149
if (top != null) {
150             
151             TransitionPair.repository = repository;
152             
153             DFSRTraverser dfsr1 = new DFSRTraverser(top, repository);
154
155             try {
156                 dfsr1.run(new DFSRConsent(top));
157             } catch (AcceptingStateException e) {
158                 return new CheckingResult(CheckingResult.ERR_OTHERERROR, "Internal Checker error " + e.getMessage());
159             } catch (BadActivityException e) {
160                 return new CheckingResult(CheckingResult.ERR_BADACTIVITY, "Composition error detected - " + e.getMessage());
161             } catch (NoActivityException e) {
162                 return new CheckingResult(CheckingResult.ERR_NOACTIVITY, "Composition error detected - " + e.getMessage());
163             } catch (InfiniteActivityException e) {
164                 return new CheckingResult(CheckingResult.ERR_INFINITEACTIVITY, "Composition error detected - " + e.getMessage());
165             } catch (CheckingException e) {
166                 return new CheckingResult(CheckingResult.ERR_OTHERERROR, "Unknown error while checking - " + e.getMessage());
167             } catch (InvalidParameterException e) {
168                 return new CheckingResult(CheckingResult.ERR_OTHERERROR, "Error while checking (Invalid parameter exception): " + e.getMessage());
169             } catch (ItemAlreadyPresentException e) {
170                 return new CheckingResult(CheckingResult.ERR_OTHERERROR, "Error while checking (ItemAlreadyPresentException): " + e.getMessage());
171             } catch (ItemNotPresentException e) {
172                 return new CheckingResult(CheckingResult.ERR_OTHERERROR, "Error while checking (ItemNotPresentException): " + e.getMessage());
173             }
174         } else
175             Debug.println("Warning: Automaton for consent is empty!");
176
177         Debug.println("Protocols are composition error free.");
178
179         return new CheckingResult(CheckingResult.ERR_OK, "OK");
180     }
181
182     public static TreeNode invertProtocol(TreeNode protocol, ActionRepository repository) {
183
184         if (protocol instanceof EventNode) {
185             return new EventNode(repository.getInverseAction(((EventNode) protocol).getEventIndex()), repository);
186         }
187         else if ((protocol instanceof AlternativeNode) && (((AlternativeNode)protocol).getorparallel())) {
188             protocol.changeChild(2, invertProtocol(protocol.getChildren()[2], repository));
189             
190             if (protocol.getChildren()[0] instanceof EventNode)
191                 protocol.changeChild(0, invertProtocol(protocol.getChildren()[0], repository));
192                 
193             if (protocol.getChildren()[1] instanceof EventNode)
194                 protocol.changeChild(1, invertProtocol(protocol.getChildren()[1], repository));
195             
196             return protocol;
197         
198         } else {
199             TreeNode[] children = protocol.getChildren();
200             for (int i = 0; i < children.length; ++i) {
201                 protocol.changeChild(i, invertProtocol(children[i], repository));
202             }
203
204             return protocol;
205         }
206     }
207     
208     
209     //-----------------------------------------------------------------
210

211     /**
212      * Action repository for protocol inversion
213      */

214     private ActionRepository repository;
215
216 }
Popular Tags