KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nfunk > jepexamples > JEPTester


1 /*****************************************************************************
2
3 JEP - Java Math Expression Parser 2.3.0
4       October 3 2004
5       (c) Copyright 2004, Nathan Funk and Richard Morris
6       See LICENSE.txt for license information.
7
8 *****************************************************************************/

9
10 package org.nfunk.jepexamples;
11
12 import java.io.*;
13
14 import org.nfunk.jep.JEP;
15 import org.nfunk.jep.type.Complex;
16
17 /**
18 * This class is designed for testing the validity of JEP evaluations.
19 * Expressions from a text file are evaluated with JEP in pairs of two, and
20 * the result are compared. If they do not match the two expressions are
21 * printed to standard output.<p>
22 * Take for example an input text file containing the two lines
23 * <pre>1+2
24 * 3</pre>.
25 * The expressions '1+2' and '3' are evaluated with JEP and the results compared.
26 */

27 public class JEPTester {
28
29     /** The parser */
30     JEP myParser;
31     
32     /** Current line position */
33     int lineCount;
34
35     /**
36      * Constructor
37      */

38     public JEPTester() {
39         // Set up the parser
40
myParser = new JEP();
41         myParser.setImplicitMul(true);
42         myParser.addStandardFunctions();
43         myParser.addStandardConstants();
44         myParser.addComplex();
45         myParser.setTraverse(false);
46         lineCount = 0;
47     }
48     
49     /**
50      * The main method checks the arguments and creates an instance
51      * and calls it's run method.
52      */

53     public static void main(String JavaDoc args[]) {
54         String JavaDoc fileName;
55         
56         // get filename from argument, or use default
57
if (args!=null && args.length>0) {
58             fileName = args[0];
59         } else {
60             fileName = "JEPTesterExpressions.txt";
61             println("Using default input file: " + fileName);
62             println("Start with \"java org.nfunk.jepexamples."+
63             "JEPTester <filename>\" to load a different input file.");
64         }
65         
66         // Create an instance of this class and analyse the file
67
JEPTester jt = new JEPTester();
68         jt.run(fileName);
69     }
70
71     /**
72      * Loads the file specified in fileName. Evaluates the expressions listed
73      * in it and compares the expressions with the results.
74      */

75     public void run(String JavaDoc fileName) {
76         BufferedReader reader;
77         Complex c1, c2;
78         boolean hasError = false;
79
80         // Load the input file
81
try {
82             reader = new BufferedReader(new FileReader(fileName));
83         } catch (Exception JavaDoc e) {
84             println("File \""+fileName+"\" not found");
85             return;
86         }
87         
88         // reset the line count
89
lineCount = 0;
90         
91         // cycle through the expressions in pairs of two
92
println("Evaluating and comparing expressions...");
93         while (true) {
94             // get values of a pair of two lines
95
c1 = parseNextLine(reader);
96             c2 = parseNextLine(reader);
97
98             if (c1==null || c2==null) break;
99             
100             // TODO: add comparison method that handles all types (Strings...)
101
if (!c1.equals(c2,1e-15)) {
102                 hasError = true;
103                 print("Line: " + lineCount + ": ");
104                 if (c1.im() == 0)
105                     print("" + c1.re() + " != ");
106                 else
107                     print("" + c1 + " != ");
108
109                 if (c2.im() == 0)
110                     println("" + c2.re());
111                 else
112                     println("" + c2);
113             }
114         }
115         
116         if (!hasError) {
117             print("\n" + lineCount + " lines processed. No errors were found.\n\n");
118         }
119     }
120     
121     /**
122      * Parses a single line from the reader, and returns the
123      * evaluation of that line.
124      */

125     private Complex parseNextLine(BufferedReader reader) {
126         Complex value;
127         String JavaDoc line, errorStr;
128         
129         // cycle till a valid line is found
130
do {
131             try {
132                 line = reader.readLine();
133                 lineCount++;
134             } catch (Exception JavaDoc e) {
135                 return null;
136             }
137
138             if (line==null) return null;
139
140         } while (line.length()==0 || line.trim().charAt(0)=='#');
141             
142         // parse the expression
143
myParser.parseExpression(line);
144         // did an error occur while parsing?
145
errorStr = myParser.getErrorInfo();
146         if (errorStr != null) {
147             println("Error while parsing line " + lineCount + ": " + errorStr);
148             return null;
149         }
150         
151         // evaluate the expression
152
value = myParser.getComplexValue();
153         // did an error occur while evaluating?
154
errorStr = myParser.getErrorInfo();
155         if ((value == null) || (errorStr != null)) {
156             println("Error while evaluating line " + lineCount + ": " + errorStr);
157             return null;
158         }
159             
160         return value;
161     }
162     
163     /**
164      * Helper function for printing.
165      */

166     private static void print(String JavaDoc str) {
167         System.out.print(str);
168     }
169
170     /**
171      * Helper function for printing lines.
172      */

173     private static void println(String JavaDoc str) {
174         System.out.println(str);
175     }
176 }
177
Popular Tags