1 11 12 20 21 27 28 package org.lsmp.djepExamples; 29 import org.lsmp.djep.djep.*; 30 import org.nfunk.jep.*; 31 import java.io.*; 32 import java.applet.*; 33 import java.awt.*; 34 import java.awt.event.ActionListener ; 35 import java.awt.event.ActionEvent ; 36 37 56 public class DJepConsole extends Applet implements ActionListener { 57 58 DJep j; 59 60 TextField inputTF; 61 62 TextField varTF; 63 64 TextField outputTF; 65 66 Button but; 67 68 69 70 private String prompt; 71 72 73 private BufferedReader br; 74 75 76 77 public DJepConsole() { 78 prompt = "DJep > "; 79 br = new BufferedReader(new InputStreamReader(System.in)); 80 81 } 82 83 84 85 public void init() 86 { 87 initialise(); 88 setLayout(new GridLayout(3,2)); 89 inputTF = new TextField("sin(x^2)",50); 90 outputTF = new TextField(50); 91 outputTF.setEditable(false); 92 varTF = new TextField("x",5); 93 but = new Button("Calculate"); 94 but.addActionListener(this); 95 inputTF.addActionListener(this); 96 97 Panel p1 = new Panel(); 98 p1.add(new Label("Differentiate:")); 99 p1.add(inputTF); 100 add(p1); 101 102 Panel p2 = new Panel(); 103 p2.add(new Label("with respect to:")); 104 p2.add(varTF); 105 p2.add(but); 106 add(p2); 107 108 Panel p3 = new Panel(); 109 p3.add(new Label("Result:")); 110 p3.add(outputTF); 111 add(p3); 112 } 113 114 118 public void actionPerformed(ActionEvent e) 119 { 120 String command = inputTF.getText(); 121 j.parseExpression(command); 122 if (j.hasError()) 123 { 124 outputTF.setText(j.getErrorInfo()); 125 } 126 else 127 { 128 try 130 { 131 Node diff = j.differentiate(j.getTopNode(),varTF.getText()); 132 Node simp = j.simplify(diff); 133 if (j.hasError()) 134 { 135 outputTF.setText(j.getErrorInfo()); 136 } 137 else 138 outputTF.setText(j.toString(simp)); 139 } 140 catch(ParseException e1) { outputTF.setText("Parse Error: "+e1.getMessage()); } 141 catch(IllegalArgumentException e2) { outputTF.setText(e2.getMessage()); } 142 catch(Exception e3) { outputTF.setText(e3.getMessage()); } 143 144 } 146 147 } 148 149 150 public static void main(String args[]) throws IOException { 151 DJepConsole c = new DJepConsole(); 152 c.run(args); 153 } 154 155 156 public void initialise() 157 { 158 j = new DJep(); 159 j.addStandardConstants(); 160 j.addStandardFunctions(); 161 j.addComplex(); 162 j.setAllowUndeclared(true); 163 j.setAllowAssignment(true); 164 j.setImplicitMul(true); 165 j.addStandardDiffRules(); 166 } 168 169 170 public void processEquation(Node node) 171 { 172 try 173 { 174 System.out.print("Parsed:\t\t"); 175 j.println(node); 176 Node processed = j.preprocess(node); 177 System.out.print("Processed:\t"); 178 j.println(processed); 179 180 Node simp = j.simplify(processed); 181 System.out.print("Simplified:\t"); 182 j.println(simp); 183 184 System.out.print("Full Brackets, no variable expansion:\t"); 185 j.getPrintVisitor().setMode(DPrintVisitor.FULL_BRACKET,true); 186 j.getPrintVisitor().setMode(DPrintVisitor.PRINT_PARTIAL_EQNS,false); 187 j.println(simp); 188 j.getPrintVisitor().setMode(DPrintVisitor.PRINT_PARTIAL_EQNS,true); 189 j.getPrintVisitor().setMode(DPrintVisitor.FULL_BRACKET,false); 190 191 try 192 { 193 Object res = j.evaluate(simp); 194 System.out.println("Value:\t"+res.toString()); 195 } 196 catch(Exception e2) 197 { 198 System.out.println("Value:\tnull "+e2.getMessage()); 199 } 200 201 208 System.out.println("Variables"); 209 ((DSymbolTable) j.getSymbolTable()).print(j.getPrintVisitor()); 210 } 211 catch(ParseException e1) { System.out.println("Parse Error: "+e1.getMessage()); } 212 catch(IllegalArgumentException e2) { System.out.println(e2.getMessage()); } 213 catch(Exception e3) { System.out.println(e3.getMessage()); } 214 } 215 216 217 218 219 public void run(String args[]) throws IOException { 220 String command=""; 221 initialise(); 222 223 if (args.length>0) { 224 String temp = args[0]; 226 for (int i=1; i<args.length; i++) temp += " " + args[i]; 227 j.parseExpression(temp); 228 if (j.hasError()) 229 System.out.println(j.getErrorInfo()); 230 else 231 { 232 processEquation(j.getTopNode()); 233 } 234 235 } else { 236 238 System.out.println("dJEPdx - Enter q to quit, rules to print the differentation rules,\ndiff(x^2,x) to differentiate,\neval(x^y,x,2,y,3) to evaluate"); 239 System.out.print(prompt); 240 241 while ((command = getCommand()) != null) { 242 j.parseExpression(command); 243 244 if (j.hasError()) { 245 System.out.println(j.getErrorInfo()); 246 } else { 247 processEquation(j.getTopNode()); 249 if (j.hasError()) { 251 System.out.println(j.getErrorInfo()); 252 } 253 } 254 System.out.print(prompt); 255 } 256 } 257 } 258 259 264 private String getCommand() throws IOException { 265 String s; 266 267 if (br == null) 268 return null; 269 270 if ( (s = br.readLine()) == null) 271 return null; 272 273 if( s.equals("rules")) 274 { 275 j.getDifferentationVisitor().printDiffRules(); 276 System.out.println("dJEPdx - Enter q to quit, rules to print the differentation rules,\ndiff(x^2,x) to differentiate,\neval(x^y,x,2,y,3) to evaluate"); 277 System.out.print(prompt); 278 return getCommand(); 279 } 280 if (s.equals("q") 281 || s.equals("quit") 282 || s.equals("exit")) 283 return null; 284 285 return s; 286 } 287 } 288 | Popular Tags |