|
http://www.eteks.com | ||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Object | +--com.eteks.parser.MathMLInterpreter
Runtime interpreter that outputs a MathML 2.0 string.
MathML is an application of XML for describing mathematics and
is a W3C recommendation available at http://www.w3.org/Math.
This class interprets the contants, operators and functions
defined in Syntax with their matching elements of MathML.
The value of parameters passed to compute an instance of
CompiledFunction with this interpreter may be either strings
or numbers instances of Number. If they are strings the MathML
element <ci> will be used, otherwise the MathML <cn> will be used as these
parameters were literals.
Examples of output with a MathMLInterpreter :
// Create a parser using DefaultSyntax
FunctionParser parser = new FunctionParser ();
// Compile functions
CompiledFunction function1 = parser.compileFunction("f(x) = x ^ 2 + 2 * x + 1");
// Add function1 to syntax to be able to call it elsewhere
((DefaultSyntax)parser.getSyntax ()).addFunction (function1);
CompiledFunction function2 = parser.compileFunction("g(x) = x * f(x)");
// Interpret these functions
Interpreter interpreter = new MathMLInterpreter ();
System.out.println ("f(2.2) =>\n" + function1.computeFunction (interpreter, new Object [] {new Double (2.2)}));
System.out.println ("f(x) =>\n" + function1.computeFunction (interpreter, function1.getParameters ()));
System.out.println ("g(y) =>\n" + function2.computeFunction (interpreter, new Object [] {"y"}));
The output of this examples is :
This interpreter supports conditions but doesn't support recursive compiled functions.f(2.2) => <apply> <plus/> <apply> <plus/> <apply> <power/> <cn>2.2</cn> <cn type="integer">2</cn> </apply> <apply> <times/> <cn type="integer">2</cn> <cn>2.2</cn> </apply> </apply> <cn type="integer">1</cn> </apply> f(x) => <apply> <plus/> <apply> <plus/> <apply> <power/> <ci>x</ci> <cn type="integer">2</cn> </apply> <apply> <times/> <cn type="integer">2</cn> <ci>x</ci> </apply> </apply> <cn type="integer">1</cn> </apply> g(y) => <apply> <times/> <ci>y</ci> <apply> <plus/> <apply> <plus/> <apply> <power/> <ci>y</ci> <cn type="integer">2</cn> </apply> <apply> <times/> <cn type="integer">2</cn> <ci>y</ci> </apply> </apply> <cn type="integer">1</cn> </apply> </apply>
Syntax,
CompiledFunction| Constructor Summary | |
MathMLInterpreter()
|
|
| Method Summary | |
java.lang.Object |
getBinaryOperatorValue(java.lang.Object binaryOperatorKey,
java.lang.Object operand1,
java.lang.Object operand2)
Returns the MathML element describing the binary operator binaryOperatorKey applied on
the two operands operand1 and operand2. |
java.lang.Object |
getCommonFunctionValue(java.lang.Object commonFunctionKey,
java.lang.Object param)
Returns the MathML element describing the common function commonFunctionKey with
the parameter param. |
java.lang.Object |
getConditionValue(java.lang.Object paramIf,
java.lang.Object paramThen,
java.lang.Object paramElse)
Returns the MathML element describing the condition defined by the parameters paramIf, paramThen or paramElse. |
java.lang.Object |
getConstantValue(java.lang.Object constantKey)
Returns the MathML element of the constant constantKey. |
java.lang.Object |
getFunctionValue(Function function,
java.lang.Object[] parametersValue,
boolean recursiveCall)
Returns the MathML element describing the function function call with its
parameters parametersValue.
|
java.lang.Object |
getLiteralValue(java.lang.Object literal)
Returns the MathML element of the literal literal interpreted as a MathML tag.
|
java.lang.Object |
getParameterValue(java.lang.Object parameter)
Returns the MathML element of the parameter parameter.
|
java.lang.Object |
getUnaryOperatorValue(java.lang.Object unaryOperatorKey,
java.lang.Object operand)
Returns the MathML element describing the unary operator unaryOperatorKey applied
on the operand operand. |
boolean |
isTrue(java.lang.Object param)
Throws an IllegalArgumentException exception because this interpreter can't
evaluate values. |
boolean |
supportsRecursiveCall()
Returns false. |
| Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
public MathMLInterpreter()
| Method Detail |
public java.lang.Object getLiteralValue(java.lang.Object literal)
literal interpreted as a MathML tag.
literal must be an instance of Number.getLiteralValue in interface Interpreterliteral - an instance of Number.java.lang.IllegalArgumentException - if literal isn't an instance of Number.public java.lang.Object getParameterValue(java.lang.Object parameter)
parameter.
parameter must be an instance of Number, String or Character.getParameterValue in interface Interpreterparameter - an instance of Number, String or Character.java.lang.IllegalArgumentException - if parameter isn't an instance of Number,
String or Character.public java.lang.Object getConstantValue(java.lang.Object constantKey)
constantKey. constantKey
may be the key of a constant of Syntax (one of CONSTANT_PI,
CONSTANT_E, CONSTANT_FALSE, CONSTANT_TRUE).getConstantValue in interface InterpreterconstantKey - the key of a constant of Syntax.java.lang.IllegalArgumentException - if constantKey isn't a key of a constant of
Syntax.
public java.lang.Object getUnaryOperatorValue(java.lang.Object unaryOperatorKey,
java.lang.Object operand)
unaryOperatorKey applied
on the operand operand. unaryOperatorKey must be the key
of an unary operator of Syntax (one of OPERATOR_POSITIVE,
OPERATOR_OPPOSITE, OPERATOR_LOGICAL_NOT, OPERATOR_BITWISE_NOT).getUnaryOperatorValue in interface InterpreterunaryOperatorKey - the key of an unary operator of Syntax.operand - the operand (a MathML element).java.lang.IllegalArgumentException - if unaryOperatorKey isn't the key of an unary
operator of Syntax.
public java.lang.Object getBinaryOperatorValue(java.lang.Object binaryOperatorKey,
java.lang.Object operand1,
java.lang.Object operand2)
binaryOperatorKey applied on
the two operands operand1 and operand2. binaryOperatorKey
must be the key of a binary operator of Syntax (one of OPERATOR_ADD,
OPERATOR_SUBSTRACT, OPERATOR_MULTIPLY, OPERATOR_DIVIDE,...).getBinaryOperatorValue in interface InterpreterbinaryOperatorKey - the key of a binary operator of Syntax.operand1 - the first operand (a MathML element).operand2 - the second operand (a MathML element).Double,
Long, Boolean or String.java.lang.IllegalArgumentException - if binaryOperatorKey isn't the key
of a binary operator of Syntax.
public java.lang.Object getCommonFunctionValue(java.lang.Object commonFunctionKey,
java.lang.Object param)
commonFunctionKey with
the parameter param. commonFunctionKey must be the key
of a commomon function of Syntax (one of FUNCTION_LN,
FUNCTION_LOG, FUNCTION_EXP, FUNCTION_SQR,...).getCommonFunctionValue in interface InterpretercommonFunctionKey - the key of a common function of Syntax.param - the parameter of the function (a MathML element).java.lang.IllegalArgumentException - if commonFunctionKey isn't the key of a
commomon function of Syntax.
public java.lang.Object getConditionValue(java.lang.Object paramIf,
java.lang.Object paramThen,
java.lang.Object paramElse)
paramIf, paramThen or paramElse.getConditionValue in interface InterpreterparamIf - the condition (a MathML element).paramThen - the true condition value (a MathML element).paramElse - the false condition value (a MathML element).public boolean isTrue(java.lang.Object param)
IllegalArgumentException exception because this interpreter can't
evaluate values. This method won't be called internally because supportsRecursiveCall () returns
false.isTrue in interface Interpretercondition - the value to test.java.lang.IllegalArgumentException - because this interpreter can't evaluate values values.public boolean supportsRecursiveCall()
false.supportsRecursiveCall in interface Interpreterfalse.
public java.lang.Object getFunctionValue(Function function,
java.lang.Object[] parametersValue,
boolean recursiveCall)
function call with its
parameters parametersValue.
As this method returns function.computeFunction (this, parametersValue),
if function is an instance of CompiledFunction it will return the MathML
element matching its expression and if function is a Java written function
it will return the MathML element returned by the function if the function is able to use this
interpreter.getFunctionValue in interface Interpreterfunction - the function to evaluate.parametersValue - the value of function's parameters (MathML elements).recursiveCall - true if the call to this function is a recursive call, meaning that
the current evaluated function calls itself.
|
|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||