KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > Util > DFSRChecker > StandaloneMain


1 /*
2  * $Id: StandaloneMain.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;
19
20 import java.io.IOException JavaDoc;
21 import java.util.ArrayList JavaDoc;
22
23
24 import SOFA.SOFAnode.Util.DFSRChecker.DFSR.Options;
25 import SOFA.SOFAnode.Util.DFSRChecker.backend.ConsentBackend;
26 import SOFA.SOFAnode.Util.DFSRChecker.backend.DotVisBackend;
27 import SOFA.SOFAnode.Util.DFSRChecker.node.ActionRepository;
28 import SOFA.SOFAnode.Util.DFSRChecker.node.TreeNode;
29 import SOFA.SOFAnode.Util.DFSRChecker.parser.Builder;
30 import SOFA.SOFAnode.Util.DFSRChecker.parser.Debug;
31 import SOFA.SOFAnode.Util.DFSRChecker.parser.FileTokenizer;
32 import SOFA.SOFAnode.Util.DFSRChecker.parser.ProtocolReader;
33 import SOFA.SOFAnode.Util.DFSRChecker.parser.StringTokenizer;
34 import SOFA.SOFAnode.Util.SyntaxErrorException;
35
36
37 /**
38  * This class is serves for standalone use of the checker. It accepts the
39  * protocols, provisions and other information from command line....
40  */

41 public class StandaloneMain {
42
43     /**
44      * @param args
45      * commandline arguments
46      */

47     public static void main(String JavaDoc[] args) {
48
49         // command-line data
50
String JavaDoc[] cldata = Options.parseCommandLine(args);
51
52         if (((cldata == null) || (cldata.length < 4)) && (Options.inputfile.equals("")))
53             printUsageAndExit();
54
55
56         try {
57             if ((Options.action == Options.ACTIONTESTCOMPLIANCE) || (Options.action == Options.ACTIONTESTCONSENT)) {
58
59                 long ms = System.currentTimeMillis();
60
61                 ActionRepository repository = new ActionRepository();
62
63                 Object JavaDoc[] parsed = buildTrees(cldata, repository);
64                 TreeNode[] nodes = (TreeNode[])parsed[0];
65                 String JavaDoc[] provs = (String JavaDoc[])parsed[1];
66                 String JavaDoc unbound = (String JavaDoc)parsed[2];
67
68                 String JavaDoc result;
69
70                 result = new ConsentBackend(repository).perform(nodes, provs, unbound.split(",")).toString();
71
72                 if (result != null)
73                     System.out.println(result);
74                 else
75                     System.out.println("OK");
76
77                 Debug.println("Taken " + (System.currentTimeMillis() - ms) / 1000 + " seconds.");
78
79             } else { // visualization
80
ActionRepository repository = new ActionRepository();
81
82                 Object JavaDoc[] parsed = buildTrees(cldata, repository);
83                 TreeNode[] nodes = (TreeNode[])parsed[0];
84                 String JavaDoc[] provs = (String JavaDoc[])parsed[1];
85                 String JavaDoc unbound = (String JavaDoc)parsed[2];
86
87
88                 DotVisBackend visb = new DotVisBackend(repository);
89
90                 //in visualization, missing required bindings are not tested,
91
// passing an empty array
92
String JavaDoc result = visb.perform(nodes, provs, unbound.split(",")).toString();
93                 if (result != null)
94                     System.out.println(result);
95
96                 System.out.println("Visualization completed!");
97             }
98         } catch (SyntaxErrorException e) {
99             System.out.println("Syntax error exception: " + e.getMessage());
100         }
101     }
102
103     /**
104      * Prints usage and exits.
105      *
106      */

107     private static void printUsageAndExit() {
108         System.out.println("Syntax:\njava -jar checker.jar --action=<test|testconsent|visualizedot> [--verbose|-v=<0|1|2>] [--nomultinodes|-m] [--noexplicit|-e] [--noforwardcut|-f] [--nobadactivity|-b] [--nonoactivity|-n] [--infiniteactivity|-i=<yes|no|notrace>] [(--totalmem|-t)=<size>] [(--cachesize|-s)=<size>] <((--file|-f)=<inputfile>) | (<protocol1> <provisions_1_2> <protocol2> [<provisions_2_3> <protocol3>, ...] <unbound_operations)>");
109         System.exit(1);
110     }
111
112     /**
113      * Erases all whitespaces from the given string.
114      *
115      * @return String without whitespaces
116      */

117     private static String JavaDoc removeSpaces(String JavaDoc src) {
118         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(src.length());
119
120         for (int i = 0; i < src.length(); ++i) {
121             char c = src.charAt(i);
122             if (!Builder.isWhiteSpace(c))
123                 buffer.append(c);
124         }
125
126         return new String JavaDoc(buffer);
127     }
128
129     /**
130      * Builds the parse trees.
131      *
132      * @return Object[3] first item are TreeNodes[], second String[]
133      * representing provisions and the third String representing the
134      * unbound operations
135      * @throws SyntaxErrorException
136      */

137      static Object JavaDoc[] buildTrees(String JavaDoc[] inputdata, ActionRepository repository) throws SyntaxErrorException {
138         TreeNode[] nodes;
139         String JavaDoc[] provisions;
140         String JavaDoc unbound;
141
142         ArrayList JavaDoc prots = new ArrayList JavaDoc();
143         ArrayList JavaDoc provs = new ArrayList JavaDoc();
144         Builder parser = new Builder(repository);
145
146         ProtocolReader reader;
147
148         if (Options.inputfile.equals("")) {
149             int i;
150             int j;
151             nodes = new TreeNode[inputdata.length / 2];
152             provisions = new String JavaDoc[inputdata.length / 2 - 1];
153
154             for (i = 0, j = 0; i < inputdata.length / 2 - 1; ++i) {
155                 nodes[i] = parser.build(new StringTokenizer(inputdata[j++]));
156                 provisions[i] = removeSpaces(inputdata[j++]);
157             }
158
159             // the last protocol
160
nodes[i] = parser.build(new StringTokenizer(inputdata[j++]));
161
162             // the last argument is a list of methods of unbound interfaces,
163
// coma separated
164
unbound = removeSpaces(inputdata[j++]);
165
166             Object JavaDoc[] result = new Object JavaDoc[3];
167
168             result[0] = nodes;
169             result[1] = provisions;
170             result[2] = unbound;
171
172             return result;
173
174         } else {
175             // the input data are in a file
176
try {
177                 reader = new FileTokenizer(Options.inputfile);
178             } catch (IOException JavaDoc e) {
179                 throw new SyntaxErrorException("Cannot read the source file");
180             }
181
182             while (true) {
183                 reader.resetIndex();
184                 TreeNode node = parser.build(reader);
185                 if (node != null) {
186                     prots.add(node);
187                     provs.add(parseSynchro(reader));
188                 } else
189                     break;
190             }
191             
192             try {
193                 reader.close();
194             }
195             catch (IOException JavaDoc e) {
196                 Debug.println("Warning: unable to close input file");
197             }
198
199             nodes = new TreeNode[prots.size()];
200             provisions = new String JavaDoc[provs.size() - 1];
201             unbound = removeSpaces((String JavaDoc) provs.get(provs.size() - 1));
202
203             for (int i = 0; i < prots.size(); ++i)
204                 nodes[i] = (TreeNode) prots.get(i);
205
206             for (int i = 0; i < provs.size() - 1; ++i)
207                 provisions[i] = (String JavaDoc) provs.get(i);
208
209             Object JavaDoc[] result = new Object JavaDoc[3];
210
211             result[0] = nodes;
212             result[1] = provisions;
213             result[2] = unbound;
214
215             return result;
216         }
217
218     }
219
220      /**
221       * Parses the synchro operations
222       * @param reader the reader to be used for reading input data
223       * @return string with synchro operations
224       * @throws SyntaxErrorException in a case syntax error occurs.
225       */

226     private static String JavaDoc parseSynchro(ProtocolReader reader) throws SyntaxErrorException {
227         String JavaDoc result = new String JavaDoc();
228         char c;
229         
230         try {
231             while ((c = (char)reader.read()) != (char)-1)
232                 result += c;
233             
234             Debug.println("Synchroops: " + result);
235             return result;
236         }
237         catch (IOException JavaDoc e) {
238             throw new SyntaxErrorException("Error while reading the input file");
239         }
240     }
241
242     /** @link dependency */
243     /* # DFSRChecker lnkDFSRChecker; */
244
245     /** @link dependency */
246     /* # DotVisBackend lnkDotVisBackend; */
247 }
248
Popular Tags