|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectweka.core.MathematicalExpression
public class MathematicalExpression
Class to build (and evaluate) the tree of the grammar:
Exp -> Term '+' Exp
| Term '-' Exp
| Term
Term -> Atom '*' Term
| Atom '/' Term
| Atom
Atom -> <number>
| '('Exp')'
| function '(' Params ')'
| '-' Atom | VARIABLE
| Disjunction
Params -> E
| E,Params
Disjunction -> Conjunction
| Conjunction '|' Disjunction
Conjunction -> NumTest
| NumTest '&' Conjunction
NumTest -> Exp '<' Exp | Exp '>' Exp | Exp '=' Exp | '[' Disjunction ']' | '!' '[' Disjunction ']'
Code example 1:
String expr = "pow(BASE,EXPONENT)*MULT";
HashMap symbols = new HashMap();
symbols.put("BASE", new Double(2));
symbols.put("EXPONENT", new Double(9));
symbols.put("MULT", new Double(0.1));
double result = MathematicalExpression.evaluate(expr, symbols);
System.out.println(expr + " and " + symbols + " = " + result);
Code Example 2 (utilizing a parse tree several times):
String expr = "pow(BASE,EXPONENT)";
MathematicalExpression.TreeNode tree = MathematicalExpression.parse(expr);
for (int i = -10; i <= 10; i++) {
HashMap symbols = new HashMap();
symbols.put("BASE", new Double(2));
symbols.put("EXPONENT", new Double(i));
double result = MathematicalExpression.evaluate(tree, symbols);
System.out.println(expr + " and " + symbols + " = " + result);
}
| Nested Class Summary | |
|---|---|
static class |
MathematicalExpression.Tokenizer
A tokenizer for MathematicalExpression |
static class |
MathematicalExpression.TreeNode
Tree Node of MathematicalExpression |
| Constructor Summary | |
|---|---|
MathematicalExpression()
|
|
| Method Summary | |
|---|---|
static double |
evaluate(MathematicalExpression.TreeNode parseTree,
java.util.Map symbols)
evalutes and returns the result of a mathematical expression, based on the given expression tree and values of the symbols. |
static double |
evaluate(java.lang.String expr,
java.util.Map symbols)
generates a new parse tree from the given expression and evalutes and returns the result of a mathematical expression, based on the given values of the symbols. |
static MathematicalExpression.TreeNode |
parse(java.lang.String exp)
Construct the Expression tree for a given String |
| Methods inherited from class java.lang.Object |
|---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public MathematicalExpression()
| Method Detail |
|---|
public static MathematicalExpression.TreeNode parse(java.lang.String exp)
throws java.lang.Exception
exp - the expression used to build the tree
java.lang.Exception - if EOF is not reached
public static double evaluate(java.lang.String expr,
java.util.Map symbols)
throws java.lang.Exception
expr - the expression to evaluatesymbols - the symbol/value mapping
java.lang.Exception - if something goes wrong
public static double evaluate(MathematicalExpression.TreeNode parseTree,
java.util.Map symbols)
throws java.lang.Exception
parseTree - fully generated expression treesymbols - the symbol/value mapping
java.lang.Exception - if something goes wrong
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||