KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lsmp > djepExamples > GroupConsole


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 /**
11  * GroupConsole - JEP Example Applet
12  * Copyright (c) 2000 Richard Morris
13  *
14  * @author Richard Morris
15  */

16
17 package org.lsmp.djepExamples;
18
19 import org.lsmp.djep.groupJep.*;
20 import org.lsmp.djep.groupJep.groups.*;
21 import org.lsmp.djep.groupJep.interfaces.*;
22 import org.lsmp.djep.groupJep.values.*;
23 import org.nfunk.jep.*;
24 import java.io.*;
25 import java.math.*;
26 import java.util.*;
27
28 /**
29 * This class implements a simple command line utility for evaluating
30 * mathematical expressions.
31 *
32 * Usage: java org.nfunk.jepexamples.Console [expression]
33 *
34 * If an argument is passed, it is interpreted as an expression
35 * and evaluated. Otherwise, a prompt is printed, and the user can enter
36 * expressions to be evaluated. To exit from the command prompt a 'q' must
37 * be entered.
38 */

39 class GroupConsole {
40     
41     /** The prompt string */
42     private String JavaDoc prompt;
43     
44     /** The input reader */
45     private BufferedReader br;
46     /** The Jep instance */
47     private GroupJep j;
48     /** Name of the current group */
49     String JavaDoc groupName="Q";
50     /** where to give a dump of tree after parsing. **/
51     boolean dumpTree = false;
52     /** whether to print symbol table after each line. **/
53     boolean dumpSymbols=false;
54     
55     /** Constructor */
56     public GroupConsole() {
57         prompt = "JEP > ";
58         br = new BufferedReader(new InputStreamReader(System.in));
59
60     }
61
62     /** Creates a new Console object and calls run() */
63     public static void main(String JavaDoc args[]) throws IOException {
64         GroupConsole c = new GroupConsole();
65         c.run(args);
66     }
67     
68     /** The input loop */
69     public void run(String JavaDoc args[]) throws IOException {
70         String JavaDoc command="";
71         j = new GroupJep(new Rationals());
72         //j.setTraverse(true);
73
j.setAllowAssignment(true);
74         j.setAllowUndeclared(true);
75         String JavaDoc temp="";
76         for(int i=0;i<args.length;++i)
77         {
78             if(args[i].equals("--dumpTree"))
79                 dumpTree = true;
80             else if(args[i].equals("--dumpSymbols"))
81                 dumpSymbols = true;
82             else
83                 temp += " " + args[i];
84         }
85         if(temp.length()!=0)
86         {
87             j.parseExpression(temp);
88             if (j.hasError())
89                 System.out.println(j.getErrorInfo());
90             else
91             {
92                 Object JavaDoc val = j.getValueAsObject();
93                 if(j.hasError())
94                     System.out.println(j.getErrorInfo());
95                 else
96                     System.out.println(val);
97             }
98         }
99         else
100         {
101             System.out.println("Using rationals. To change group type 'group g' where g is one of: ");
102             System.out.println("I - integers, Q - Rationals, Rn - Reals(n dp),"+
103                     " or Zn for Integers mod n");
104             System.out.println("A n, Algebraic Intergers Z(sqrt(n))");
105             System.out.println("JEP - Enter q to quit");
106             // no arguments - interactive mode
107

108             System.out.println("Current Group: "+j.getGroup().toString());
109                             
110             while ((command = getCommand()) != null)
111             {
112                 try
113                 {
114                     Node node = j.parse(command);
115                     Object JavaDoc value = j.evaluate(node);
116                     if(value instanceof HasComplexValueI)
117                         System.out.println(value.toString()+"="
118                             +((HasComplexValueI) value).getComplexValue());
119                     else
120                         System.out.println(value.toString());
121                 }
122                 catch(Exception JavaDoc e)
123                 {
124                     System.out.println(e.getClass().getName());
125                     System.out.println(e.getMessage());
126                     e.printStackTrace();
127                 }
128             }
129         }
130         
131     }
132     // split
133
String JavaDoc[] split(String JavaDoc s)
134     {
135         StringTokenizer st = new StringTokenizer(s);
136         int tokCount = st.countTokens();
137         String JavaDoc res[] = new String JavaDoc[tokCount];
138         int pos=0;
139         while (st.hasMoreTokens()) {
140             res[pos++]=st.nextToken();
141         }
142         return res;
143     }
144     /**
145      * Get a command from the input.
146      * @return null if an error occures, or if the user enters a terminating
147      * command
148      */

149     private String JavaDoc getCommand() throws IOException {
150         String JavaDoc s;
151         
152         System.out.print(prompt);
153
154         if (br == null)
155             return null;
156
157         if ( (s = br.readLine()) == null)
158             return null;
159
160         if (s.equals("q")
161             || s.equals("quit")
162             || s.equals("exit"))
163             return null;
164         
165         if(s.equals("group"))
166         {
167             System.out.println("Current Group: "+j.getGroup().toString());
168             return getCommand();
169         }
170         else if(s.startsWith("group"))
171         {
172           try
173           {
174             groupName = s.substring(6);
175             // split into string separated by spaces
176
String JavaDoc splits[] = split(groupName);
177             System.out.println("Changing group to '"+groupName+"'");
178             if(splits[0].equals("Z"))
179             {
180                     j = new GroupJep(new Integers());
181             }
182             else if(splits[0].equals("Q"))
183             {
184                     j = new GroupJep(new Rationals());
185             }
186             else if(splits[0].equals("R"))
187             {
188                 j = new GroupJep(new BigReals(
189                             Integer.parseInt(splits[1]),
190                             BigDecimal.ROUND_HALF_EVEN ));
191             }
192             else if(splits[0].equals("P"))
193             {
194                 j = new GroupJep(new PermutationGroup(
195                             Integer.parseInt(splits[1]))
196                             );
197             }
198             else if(splits[0].equals("Zn"))
199             {
200                 j = new GroupJep(new Zn(new BigInteger(splits[1])));
201             }
202             else if(splits[0].equals("Qu"))
203             {
204                 j = new GroupJep(new Quartonians());
205                 j.addStandardConstants();
206             }
207             else if(splits[0].equals("extend") && splits.length == 2)
208             {
209                 RingI ring = (RingI) j.getGroup();
210                 j = new GroupJep(new FreeGroup(ring, splits[1]));
211                 j.addStandardConstants();
212             }
213             else if(splits[0].equals("extend"))
214             {
215                 RingI ring = (RingI) j.getGroup();
216
217                 int deg = splits.length-3;
218                 Number JavaDoc coeffs[] = new Number JavaDoc[deg+1];
219                 for(int i=0;i<=deg;++i)
220                     coeffs[i] = ring.valueOf(splits[splits.length-i-1]);
221                 Polynomial p1 = new Polynomial(ring,splits[1],coeffs);
222
223                 j = new GroupJep(new AlgebraicExtension(ring, p1));
224                 j.addStandardConstants();
225             }
226             else
227                 System.out.println("Invalid group: "+groupName);
228
229             System.out.println("Current Group: "+j.getGroup().toString());
230             return getCommand();
231           }
232           catch(Exception JavaDoc e)
233           {
234             e.printStackTrace();
235             return getCommand();
236           }
237         }
238         return s;
239     }
240 }
241
Popular Tags