1 9 10 package org.nfunk.jep; 11 12 import java.io.*; 13 import java.util.*; 14 import org.nfunk.jep.function.*; 15 import org.nfunk.jep.type.*; 16 17 31 public class JEP { 32 33 34 private static final boolean debug = false; 35 36 37 private boolean traverse; 38 39 40 protected boolean allowUndeclared; 41 42 43 protected boolean allowAssignment; 44 45 46 protected boolean implicitMul; 47 48 49 protected SymbolTable symTab; 50 51 52 protected FunctionTable funTab; 53 54 55 protected Vector errorList; 56 57 58 protected Parser parser; 59 60 61 private Node topNode; 62 63 64 protected EvaluatorVisitor ev; 65 66 67 protected NumberFactory numberFactory; 68 69 70 protected OperatorSet opSet; 71 72 80 public JEP() { 81 topNode = null; 82 traverse = false; 83 allowUndeclared = false; 84 allowAssignment = false; 85 implicitMul = false; 86 numberFactory = new DoubleNumberFactory(); 87 opSet = new OperatorSet(); 88 initSymTab(); 89 initFunTab(); 90 errorList = new Vector(); 91 ev = new EvaluatorVisitor(); 92 parser = new Parser(new StringReader("")); 93 94 } 98 99 107 public JEP(boolean traverse_in, 108 boolean allowUndeclared_in, 109 boolean implicitMul_in, 110 NumberFactory numberFactory_in) { 111 topNode = null; 112 traverse = traverse_in; 113 allowUndeclared = allowUndeclared_in; 114 implicitMul = implicitMul_in; 115 if (numberFactory_in == null) { 116 numberFactory = new DoubleNumberFactory(); 117 } else { 118 numberFactory = numberFactory_in; 119 } 120 initSymTab(); 121 initFunTab(); 122 errorList = new Vector(); 123 ev = new EvaluatorVisitor(); 124 parser = new Parser(new StringReader("")); 125 126 parseExpression(""); 129 } 130 131 137 protected JEP(JEP j) 138 { 139 this.topNode = null; 140 this.traverse = j.traverse; 141 this.allowUndeclared = j.allowUndeclared; 142 this.allowAssignment = j.allowAssignment; 143 this.implicitMul = j.implicitMul; 144 this.ev = j.ev; 145 this.funTab = j.funTab; 146 this.numberFactory = j.numberFactory; 147 this.parser = j.parser; 148 this.symTab = j.symTab; 149 this.errorList = j.errorList; 150 } 151 152 155 public void initSymTab() { 156 symTab = new SymbolTable(new VariableFactory()); 158 } 159 160 163 public void initFunTab() { 164 funTab = new FunctionTable(); 166 } 167 168 177 public void addStandardFunctions() { 178 funTab.put("sin", new Sine()); 180 funTab.put("cos", new Cosine()); 181 funTab.put("tan", new Tangent()); 182 funTab.put("asin", new ArcSine()); 183 funTab.put("acos", new ArcCosine()); 184 funTab.put("atan", new ArcTangent()); 185 funTab.put("atan2", new ArcTangent2()); 186 187 funTab.put("sinh", new SineH()); 188 funTab.put("cosh", new CosineH()); 189 funTab.put("tanh", new TanH()); 190 funTab.put("asinh", new ArcSineH()); 191 funTab.put("acosh", new ArcCosineH()); 192 funTab.put("atanh", new ArcTanH()); 193 194 funTab.put("log", new Logarithm()); 195 funTab.put("ln", new NaturalLogarithm()); 196 funTab.put("exp", new Exp()); 197 198 funTab.put("sqrt",new SquareRoot()); 199 funTab.put("abs", new Abs()); 200 funTab.put("mod", new Modulus()); 201 funTab.put("sum", new Sum()); 202 203 funTab.put("rand", new org.nfunk.jep.function.Random()); 204 205 funTab.put("if", new If()); 207 funTab.put("str", new Str()); 208 } 209 210 215 public void addStandardConstants() { 216 symTab.addConstant("pi", new Double (Math.PI)); 218 symTab.addConstant("e", new Double (Math.E)); 219 } 220 221 230 public void addComplex() { 231 symTab.addConstant("i", new Complex(0,1)); 233 funTab.put("re", new Real()); 234 funTab.put("im", new Imaginary()); 235 funTab.put("arg", new Arg()); 236 funTab.put("cmod", new Abs()); 237 funTab.put("complex", new ComplexPFMC()); 238 funTab.put("polar", new Polar()); 239 } 240 241 249 public void addFunction(String functionName, 250 PostfixMathCommandI function) { 251 funTab.put(functionName, function); 252 } 253 254 263 public Double addVariable(String name, double value) { 264 Double object = new Double (value); 265 symTab.makeVarIfNeeded(name, object); 266 return object; 267 } 268 269 273 public void addConstant(String name,Object value) { 274 symTab.addConstant(name, value); 275 } 276 286 public Complex addVariable(String name, double re, double im) { 287 Complex object = new Complex(re,im); 288 symTab.makeVarIfNeeded(name, object); 289 return object; 290 } 291 292 300 public void addVariable(String name, Object object) { 301 symTab.makeVarIfNeeded(name, object); 302 } 303 304 313 public Object removeVariable(String name) { 314 return symTab.remove(name); 315 } 316 322 public Object getVarValue(String name) { 323 return symTab.getVar(name).getValue(); 324 } 325 333 public boolean setVarValue(String name,Object val) { 334 return symTab.setVarValue(name,val); 335 } 336 342 public Variable getVar(String name) { 343 return symTab.getVar(name); 344 } 345 346 353 public Object removeFunction(String name) { 354 return funTab.remove(name); 355 } 356 357 365 public void setTraverse(boolean value) { 366 traverse = value; 367 } 368 369 373 public boolean getTraverse() { return traverse; } 374 375 384 public void setImplicitMul(boolean value) { 385 implicitMul = value; 386 } 387 388 392 public boolean getImplicitMul() { return implicitMul; } 393 394 407 public void setAllowUndeclared(boolean value) { 408 allowUndeclared = value; 409 } 410 411 415 public boolean getAllowUndeclared() { return allowUndeclared; } 416 417 420 public void setAllowAssignment(boolean value) { 421 allowAssignment = value; 422 } 423 424 428 public boolean getAllowAssignment() { return allowAssignment; } 429 430 431 436 public void parseExpression(String expression_in) { 437 Reader reader = new StringReader(expression_in); 438 439 try { 440 errorList.removeAllElements(); 442 topNode = parser.parseStream(reader, this); 443 } 444 catch (Throwable e) 445 { 446 topNode = null; 448 449 if (e instanceof ParseException) { 451 errorList.addElement(((ParseException)e).getMessage()); 454 } else { 456 if (debug) { 459 System.out.println(e.getMessage()); 460 e.printStackTrace(); 461 } 462 errorList.addElement("Syntax error"); 463 } 464 } 465 466 467 if (traverse && !hasError()) { 470 ParserVisitor v = new ParserDumpVisitor(); 471 try 472 { 473 topNode.jjtAccept(v, null); 474 } 475 catch(ParseException e) 476 { 477 errorList.addElement(e.getMessage()); 478 } 479 } 480 } 481 482 493 public Node parse(String expression) throws ParseException 494 { 495 java.io.StringReader sr = new java.io.StringReader (expression); 496 Node node = parser.parseStream(sr, this); 497 return node; 498 } 499 500 510 public Object evaluate(Node node) throws Exception 511 { 512 return ev.getValue(node, new Vector(), this.symTab); 513 } 514 515 524 public double getValue() { 525 Object value = getValueAsObject(); 526 527 if(value == null) return Double.NaN; 528 529 if(value instanceof Complex) 530 { 531 Complex c = (Complex) value; 532 if( c.im() != 0.0) return Double.NaN; 533 return c.re(); 534 } 535 if (value != null && value instanceof Number ) { 536 return ((Number )value).doubleValue(); 537 } 538 539 return Double.NaN; 540 } 541 542 543 548 public Complex getComplexValue() { 549 Object value = getValueAsObject(); 550 551 if (value == null) { 552 return null; 553 } else if (value instanceof Complex) { 554 return (Complex)value; 555 } else if (value instanceof Number ) { 556 return new Complex(((Number )value).doubleValue(), 0); 557 } else { 558 return null; 559 } 560 } 561 562 563 564 572 public Object getValueAsObject() { 573 Object result; 574 575 if (topNode != null && !hasError()) { 576 try { 578 result = ev.getValue(topNode,errorList,symTab); 579 } catch (Exception e) { 580 if (debug) System.out.println(e); 581 errorList.addElement("Error during evaluation"); 582 return null; 583 } 584 585 return result; 586 } else { 587 return null; 588 } 589 } 590 591 597 public boolean hasError() { 598 return !errorList.isEmpty(); 599 } 600 601 607 public String getErrorInfo() { 608 if (hasError()) { 609 String str = ""; 610 611 for (int i=0; i<errorList.size(); i++) { 613 str += errorList.elementAt(i) + "\n"; 614 } 615 616 return str; 617 } else { 618 return null; 619 } 620 } 621 622 629 public Node getTopNode() { 630 return topNode; 631 } 632 633 638 public SymbolTable getSymbolTable() { 639 return symTab; 640 } 641 642 647 public FunctionTable getFunctionTable() { 648 return funTab; 649 } 650 651 652 656 public NumberFactory getNumberFactory() { 657 return numberFactory; 658 } 659 660 665 public OperatorSet getOperatorSet() { 666 return opSet; 667 } 668 669 674 public Parser getParser() {return parser; } 675 678 679 700 } 701 702 | Popular Tags |