http://www.eteks.com

com.eteks.parser
Class MathMLInterpreter

java.lang.Object
  |
  +--com.eteks.parser.MathMLInterpreter
All Implemented Interfaces:
Interpreter

public class MathMLInterpreter
extends java.lang.Object
implements Interpreter

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 :
 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>
This interpreter supports conditions but doesn't support recursive compiled functions.
The methods of this class are thread safe.

Since:
Jeks 1.0
Version:
1.0
Author:
Emmanuel Puybaret
See Also:
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

MathMLInterpreter

public MathMLInterpreter()
Method Detail

getLiteralValue

public java.lang.Object getLiteralValue(java.lang.Object literal)
Returns the MathML element of the literal literal interpreted as a MathML tag. literal must be an instance of Number.
Specified by:
getLiteralValue in interface Interpreter
Parameters:
literal - an instance of Number.
Returns:
the MathML element of the literal.
Throws:
java.lang.IllegalArgumentException - if literal isn't an instance of Number.

getParameterValue

public java.lang.Object getParameterValue(java.lang.Object parameter)
Returns the MathML element of the parameter parameter. parameter must be an instance of Number, String or Character.
Specified by:
getParameterValue in interface Interpreter
Parameters:
parameter - an instance of Number, String or Character.
Returns:
the MathML element of the parameter.
Throws:
java.lang.IllegalArgumentException - if parameter isn't an instance of Number, String or Character.

getConstantValue

public java.lang.Object getConstantValue(java.lang.Object constantKey)
Returns the MathML element of the constant constantKey. constantKey may be the key of a constant of Syntax (one of CONSTANT_PI, CONSTANT_E, CONSTANT_FALSE, CONSTANT_TRUE).
Specified by:
getConstantValue in interface Interpreter
Parameters:
constantKey - the key of a constant of Syntax.
Returns:
the MathML element of the constant.
Throws:
java.lang.IllegalArgumentException - if constantKey isn't a key of a constant of Syntax.

getUnaryOperatorValue

public 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. unaryOperatorKey must be the key of an unary operator of Syntax (one of OPERATOR_POSITIVE, OPERATOR_OPPOSITE, OPERATOR_LOGICAL_NOT, OPERATOR_BITWISE_NOT).
Specified by:
getUnaryOperatorValue in interface Interpreter
Parameters:
unaryOperatorKey - the key of an unary operator of Syntax.
operand - the operand (a MathML element).
Returns:
the MathML element matching the operation.
Throws:
java.lang.IllegalArgumentException - if unaryOperatorKey isn't the key of an unary operator of Syntax.

getBinaryOperatorValue

public 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. binaryOperatorKey must be the key of a binary operator of Syntax (one of OPERATOR_ADD, OPERATOR_SUBSTRACT, OPERATOR_MULTIPLY, OPERATOR_DIVIDE,...).
Specified by:
getBinaryOperatorValue in interface Interpreter
Parameters:
binaryOperatorKey - the key of a binary operator of Syntax.
operand1 - the first operand (a MathML element).
operand2 - the second operand (a MathML element).
Returns:
the MathML element matching the operation. The returned value is an instance of Double, Long, Boolean or String.
Throws:
java.lang.IllegalArgumentException - if binaryOperatorKey isn't the key of a binary operator of Syntax.

getCommonFunctionValue

public 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. commonFunctionKey must be the key of a commomon function of Syntax (one of FUNCTION_LN, FUNCTION_LOG, FUNCTION_EXP, FUNCTION_SQR,...).
Specified by:
getCommonFunctionValue in interface Interpreter
Parameters:
commonFunctionKey - the key of a common function of Syntax.
param - the parameter of the function (a MathML element).
Returns:
the MathML element of the function.
Throws:
java.lang.IllegalArgumentException - if commonFunctionKey isn't the key of a commomon function of Syntax.

getConditionValue

public 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.
Specified by:
getConditionValue in interface Interpreter
Parameters:
paramIf - the condition (a MathML element).
paramThen - the true condition value (a MathML element).
paramElse - the false condition value (a MathML element).
Returns:
the MathML element of the condition.

isTrue

public boolean isTrue(java.lang.Object param)
Throws an IllegalArgumentException exception because this interpreter can't evaluate values. This method won't be called internally because supportsRecursiveCall () returns false.
Specified by:
isTrue in interface Interpreter
Parameters:
condition - the value to test.
Throws:
java.lang.IllegalArgumentException - because this interpreter can't evaluate values values.

supportsRecursiveCall

public boolean supportsRecursiveCall()
Returns false.
Specified by:
supportsRecursiveCall in interface Interpreter
Returns:
false.

getFunctionValue

public 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. 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.
Specified by:
getFunctionValue in interface Interpreter
Parameters:
function - 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.
Returns:
the MathML element of the function call.

&cp; 1998-2003 eTeks - All rights reserved