KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: DotVisBackend.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.backend;
19
20
21 import java.util.TreeSet JavaDoc;
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.FileNotFoundException JavaDoc;
25
26
27 import SOFA.SOFAnode.Util.DFSRChecker.CheckingResult;
28 import SOFA.SOFAnode.Util.DFSRChecker.DFSR.CheckingException;
29 import SOFA.SOFAnode.Util.DFSRChecker.DFSR.DFSRDotVis;
30 import SOFA.SOFAnode.Util.DFSRChecker.DFSR.DFSRTraverser;
31 import SOFA.SOFAnode.Util.DFSRChecker.DFSR.ItemAlreadyPresentException;
32 import SOFA.SOFAnode.Util.DFSRChecker.DFSR.ItemNotPresentException;
33 import SOFA.SOFAnode.Util.DFSRChecker.DFSR.Options;
34 import SOFA.SOFAnode.Util.DFSRChecker.node.ActionRepository;
35 import SOFA.SOFAnode.Util.DFSRChecker.node.ConsentNode;
36 import SOFA.SOFAnode.Util.DFSRChecker.node.DeterministicNode;
37 import SOFA.SOFAnode.Util.DFSRChecker.node.InvalidParameterException;
38 import SOFA.SOFAnode.Util.DFSRChecker.node.TreeNode;
39 import SOFA.SOFAnode.Util.DFSRChecker.optimizer.OptExplicit;
40 import SOFA.SOFAnode.Util.DFSRChecker.parser.Debug;
41 import SOFA.SOFAnode.Util.DFSRChecker.state.Signature;
42 import SOFA.SOFAnode.Util.DFSRChecker.visualization.DotVisualizer;
43
44 /**
45  * This class is intended to visualize the parse trees and resulting automaton of protocol being checked.
46  */

47 public class DotVisBackend implements IBackend {
48
49     public DotVisBackend(ActionRepository repository) {
50         this.repository = repository;
51     }
52
53     /**
54      * @see SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.backend.IBackend#perform(SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.node.TreeNode[], java.lang.String[], java.lang.String[])
55      */

56     public CheckingResult perform(TreeNode[] nodes, String JavaDoc[] synchroops, String JavaDoc[] unboundops) {
57         //basic checking of parameters
58
//basic checking of parameters
59
if (nodes.length < 2) {
60             return new CheckingResult(CheckingResult.ERR_OTHERERROR, "Visualization backend: wrong number of items within the first array parameter (at least two required.\n");
61         }
62
63         if (synchroops.length != nodes.length - 1) {
64             return new CheckingResult(CheckingResult.ERR_OTHERERROR, "Visualization backend: wrong number of items within the second array parameter (1 lower than the number of components required).\n");
65         }
66
67         DotVisualizer dotviz = new DotVisualizer(repository);
68
69         // parse tree visualization
70
for (int i = 0; i < nodes.length; ++i) {
71
72             dotviz.visualize(nodes[i], "pt_" + i + ".dt");
73
74         }
75
76         /*
77         try {
78             for (int i = 0; i < nodes.length; ++i) {
79
80                 Signature.setSize(nodes[i].getLeafCount());
81
82                 FileOutputStream outputfile = new FileOutputStream("a_" + i + ".dt");
83
84                 outputfile.write("digraph G {\n size = \"7,11\";\n".getBytes());
85                 new DFSRTraverser(nodes[i], repository).run(new DFSRDotVis(outputfile, nodes[i], repository));
86                 outputfile.write(("\n}").getBytes());
87
88                 outputfile.close();
89
90                 Debug.println("Automaton for protocol written to a_" + i + ".dt");
91             }
92         } catch (IOException e) {
93             return "Error while writing to the file a_?.dt";
94         } catch (InvalidParameterException e) {
95             return "Error while checking (Invalid parameter exception): " + e.getMessage();
96         } catch (ItemAlreadyPresentException e) {
97             return "Error while checking (ItemAlreadyPresentException): " + e.getMessage();
98         } catch (ItemNotPresentException e) {
99             return "Error while checking (ItemNotPresentException): " + e.getMessage();
100         } catch (CheckingException e) {
101             return "End of checking while vizualizating, need a fix! - " + e.getMessage();
102         }
103         */

104         nodes[0] = ConsentBackend.invertProtocol(nodes[0], repository);
105
106         TreeNode result = new DeterministicNode(nodes[nodes.length - 1]);
107
108         TreeSet JavaDoc unbound = new TreeSet JavaDoc();
109         for (int y = 0; y < unboundops.length; ++y) {
110             unbound.add(new Integer JavaDoc(repository.getItemIndex(unboundops[y])));
111         }
112
113         for (int j = nodes.length - 2; j > 0; --j) {
114             String JavaDoc[] sprov;
115             TreeSet JavaDoc prov = new TreeSet JavaDoc();
116             sprov = synchroops[j].split(",");
117
118             for (int i = 0; i < sprov.length; ++i) {
119                 int index = repository.getItemIndex(sprov[i]);
120                 if (index == -1) {
121                     return new CheckingResult(CheckingResult.ERR_OTHERERROR, "Invalid provisions - not occuring within the protocols.");
122                 } else {
123                     prov.add(new Integer JavaDoc(index));
124                 }
125             }
126
127             // create the consent node joining the two protocols
128
result = new ConsentNode(new DeterministicNode(nodes[j]), result, prov, unbound, repository, false);
129                 
130         }
131
132         String JavaDoc[] sprov;
133         TreeSet JavaDoc prov = new TreeSet JavaDoc();
134         sprov = synchroops[0].split(",");
135
136         for (int i = 0; i < sprov.length; ++i) {
137             int index = repository.getItemIndex(sprov[i]);
138             if (index == -1) {
139                 return new CheckingResult(CheckingResult.ERR_OTHERERROR, "Invalid provisions - not occuring within the protocols.");
140             } else {
141                 prov.add(new Integer JavaDoc(index));
142             }
143         }
144
145         ConsentNode top = new ConsentNode(new DeterministicNode(nodes[0]), result, prov, unbound, repository, false);
146
147         Debug.println("State space estimate: " + top.getWeight());
148
149         // perform the forward cutting and explicit optimizations
150
Debug.print("Optimizing the parse tree for the composition test......");
151         try {
152             if (Options.forwardcutting)
153                 top = (ConsentNode) top.forwardCut(repository.getAllUsedEventIndices());
154             
155             if (top != null)
156                 Signature.setSize(top.getLeafCount());
157
158             if ((Options.explicit) && (top != null))
159                 top = (ConsentNode) new OptExplicit(top, Options.explicitsize).perform();
160         } catch (InvalidParameterException e) {
161             return new CheckingResult(CheckingResult.ERR_OTHERERROR, "Error while performing optimization. Bailing out....");
162         }
163
164         Debug.println("done.");
165         /**/
166
167         if (top != null) {
168             DFSRTraverser dfsr1 = new DFSRTraverser(top, repository);
169
170             try {
171                 FileOutputStream JavaDoc outputfile = new FileOutputStream JavaDoc("a_main.dt");
172
173                 outputfile.write("digraph G {\n size = \"7,11\";\n".getBytes());
174                 dfsr1.run(new DFSRDotVis(outputfile, top, repository));
175                 outputfile.write(("\n}").getBytes());
176
177                 outputfile.close();
178             } catch (CheckingException e) {
179                 return new CheckingResult(CheckingResult.ERR_OTHERERROR, "End of checking while vizualizating, need a fix! - " + e.getMessage());
180             } catch (InvalidParameterException e) {
181                 return new CheckingResult(CheckingResult.ERR_OTHERERROR, "Error while checking (Invalid parameter exception): " + e.getMessage());
182             } catch (ItemAlreadyPresentException e) {
183                 return new CheckingResult(CheckingResult.ERR_OTHERERROR, "Error while checking (ItemAlreadyPresentException): " + e.getMessage());
184             } catch (ItemNotPresentException e) {
185                 return new CheckingResult(CheckingResult.ERR_OTHERERROR, "Error while checking (ItemNotPresentException): " + e.getMessage());
186             } catch (FileNotFoundException JavaDoc e) {
187                 return new CheckingResult(CheckingResult.ERR_OTHERERROR, "Couldn't open file automaton1.dt");
188             } catch (IOException JavaDoc e) {
189                 return new CheckingResult(CheckingResult.ERR_OTHERERROR, "IOException while writing to the file a_main.dt");
190             }
191         }
192
193         return null;
194     }
195
196     //----------------------------------------------------------------------------------
197
/**
198      * Action repository
199      */

200     private ActionRepository repository;
201
202     /** @link dependency */
203     /* # SOFA.SOFAnode.Util.objectweb.fractal.behprotocols.checker.parser.Builder lnkBuilder; */
204
205     /** @link dependency */
206     /* # DFSRTraverser lnkDFSRTraverser; */
207 }
Popular Tags