org.activemath.exercises.openmath
Class OpenMath

java.lang.Object
  extended by org.activemath.exercises.openmath.OpenMath
Direct Known Subclasses:
CasParser, Evaluator, FeedbackGenerator, FractionDRService, HTWService, IDEASDRService, IntegralDRService, InteractionGenerator, OMDUtil

public class OpenMath
extends Object

OpenMath utility class. Provides some utility functions for classes working with OpenMath expressions such as expression evaluators, interaction generators, OpenMath services, etc.

Version:
$Revision: 1.6 $ $Date: 2009/12/16 22:32:44 $

Field Summary
static org.jdom.Namespace openmathNamespace
           
 
Constructor Summary
OpenMath()
           
 
Method Summary
static org.jdom.Element applyBinding(org.jdom.Element binding, List variableValuesList, int index)
          Apply a variable substitution.
static org.jdom.Element getOmobjContents(org.jdom.Element element)
          Get the contents of the OMOBJ element.
static org.jdom.Element oma(org.jdom.Element[] content)
           
static org.jdom.Element oma(org.jdom.Element operator, org.jdom.Element argument1)
           
static org.jdom.Element oma(org.jdom.Element operator, org.jdom.Element argument1, org.jdom.Element argument2)
           
static org.jdom.Element oma(org.jdom.Element operator, org.jdom.Element argument1, org.jdom.Element argument2, org.jdom.Element argument3)
           
static org.jdom.Element oma(List content)
           
static org.jdom.Element ombind(org.jdom.Element[] content)
           
static org.jdom.Element ombind(org.jdom.Element operator, org.jdom.Element bvar, org.jdom.Element argument1)
           
static org.jdom.Element ombind(org.jdom.Element operator, org.jdom.Element bvar, org.jdom.Element argument1, org.jdom.Element argument2)
           
static org.jdom.Element ombind(org.jdom.Element operator, org.jdom.Element bvar, org.jdom.Element argument1, org.jdom.Element argument2, org.jdom.Element argument3)
           
static org.jdom.Element ombind(List content)
           
static org.jdom.Element ombvar(org.jdom.Element variable1)
           
static org.jdom.Element ombvar(org.jdom.Element[] variables)
           
static org.jdom.Element ombvar(org.jdom.Element variable1, org.jdom.Element variable2)
           
static org.jdom.Element ombvar(org.jdom.Element variable1, org.jdom.Element variable2, org.jdom.Element variable3)
           
static org.jdom.Element ome(org.jdom.Element symbol)
           
static org.jdom.Element ome(org.jdom.Element[] content)
           
static org.jdom.Element ome(org.jdom.Element symbol, org.jdom.Element message)
           
static org.jdom.Element ome(List content)
           
static org.jdom.Element omf(double number)
           
static org.jdom.Element omf(String number)
           
static org.jdom.Element omi(int number)
           
static org.jdom.Element omi(String number)
           
static org.jdom.Element omobj(org.jdom.Element expressionRoot)
           
static org.jdom.Element oms(String cd, String name)
           
static org.jdom.Element omstr(String string)
           
static org.jdom.Element omv(String name)
           
static void output(org.jdom.Element om, OutputStream outputStream)
           
static void setOmNamespace(org.jdom.Element om)
          The answer from an external system would normally be plain OpenMath, which means that when parsing it back as XML, the elements don't have any namespace associated to them.
static boolean substituteVariables(Map variables, org.jdom.Element om)
          Substitute variables in the given OpenMath element.
static void tidyForOpenMath1(org.jdom.Element om)
          External OM systems are not required to handle namespaces or extra attributes, so we need to remove them to ensure that the expression will be parsed correctly.
static double toDouble(org.jdom.Element element)
          Parse an OpenMath element into a double float number.
static int toInt(org.jdom.Element element)
          Parse an OpenMath integer element into an integer number.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

openmathNamespace

public static final org.jdom.Namespace openmathNamespace
Constructor Detail

OpenMath

public OpenMath()
Method Detail

toDouble

public static double toDouble(org.jdom.Element element)
Parse an OpenMath element into a double float number. Integers are also parsed into double floats, which limits their values. This function is not intended for general OM support, but rather just for the numbers that a user could have to give as replies for exercises. Rational numbers are also evaluated. A rational number is either an application of the symbol "nums1:rational", or an application of the symbol "arith1:divide", to a pair of integers. If any of the arguments is something else, even a float, it is not evaluated.

Parameters:
element - XML element.
Returns:
double float value from parsing the element, or zero on error.

toInt

public static int toInt(org.jdom.Element element)
Parse an OpenMath integer element into an integer number. Integers are parsed into Java ints, which limits their values, but this function is not intended for general OM support, but rather just for the numbers that a user could have to give as replies for exercises.

Parameters:
element - XML element.
Returns:
int value from parsing the element, or zero on error.

substituteVariables

public static boolean substituteVariables(Map variables,
                                          org.jdom.Element om)
Substitute variables in the given OpenMath element.
Values are substituted in place, so the element is modified.
Recursive substitution can be done easily with the following idiom:
 while (substituteVariables(vars, omobj));
 
However this does not terminate if there are loops in the variable table.
It is better in general to put a hard limit of n iterations:
 for (int n=10; n>0 && substituteVariables(vars, omobj); --n);
 
This code calls the function at most 10 times, or less if all possible substitutions are done before.

Parameters:
variables - table of variable values indexed by their names. It can be null or empty, in which case the expression is not modified.
om - OpenMath expression. If it is itself a variable, it does not get substituted.
Returns:
true if there was at least one substitution, false otherwise.

omobj

public static org.jdom.Element omobj(org.jdom.Element expressionRoot)

omi

public static org.jdom.Element omi(int number)

omi

public static org.jdom.Element omi(String number)

omf

public static org.jdom.Element omf(double number)

omf

public static org.jdom.Element omf(String number)

omv

public static org.jdom.Element omv(String name)

oms

public static org.jdom.Element oms(String cd,
                                   String name)

omstr

public static org.jdom.Element omstr(String string)

oma

public static org.jdom.Element oma(org.jdom.Element operator,
                                   org.jdom.Element argument1)

oma

public static org.jdom.Element oma(org.jdom.Element operator,
                                   org.jdom.Element argument1,
                                   org.jdom.Element argument2)

oma

public static org.jdom.Element oma(org.jdom.Element operator,
                                   org.jdom.Element argument1,
                                   org.jdom.Element argument2,
                                   org.jdom.Element argument3)

oma

public static org.jdom.Element oma(List content)

oma

public static org.jdom.Element oma(org.jdom.Element[] content)

ombind

public static org.jdom.Element ombind(org.jdom.Element operator,
                                      org.jdom.Element bvar,
                                      org.jdom.Element argument1)

ombind

public static org.jdom.Element ombind(org.jdom.Element operator,
                                      org.jdom.Element bvar,
                                      org.jdom.Element argument1,
                                      org.jdom.Element argument2)

ombind

public static org.jdom.Element ombind(org.jdom.Element operator,
                                      org.jdom.Element bvar,
                                      org.jdom.Element argument1,
                                      org.jdom.Element argument2,
                                      org.jdom.Element argument3)

ombvar

public static org.jdom.Element ombvar(org.jdom.Element variable1)

ombvar

public static org.jdom.Element ombvar(org.jdom.Element variable1,
                                      org.jdom.Element variable2)

ombvar

public static org.jdom.Element ombvar(org.jdom.Element variable1,
                                      org.jdom.Element variable2,
                                      org.jdom.Element variable3)

ombvar

public static org.jdom.Element ombvar(org.jdom.Element[] variables)

ombind

public static org.jdom.Element ombind(List content)

ombind

public static org.jdom.Element ombind(org.jdom.Element[] content)

ome

public static org.jdom.Element ome(org.jdom.Element symbol)

ome

public static org.jdom.Element ome(org.jdom.Element symbol,
                                   org.jdom.Element message)

ome

public static org.jdom.Element ome(List content)

ome

public static org.jdom.Element ome(org.jdom.Element[] content)

getOmobjContents

public static org.jdom.Element getOmobjContents(org.jdom.Element element)
Get the contents of the OMOBJ element. If the element is not an OMOBJ, it is returned unmodified, since we assume that it is the content that has been already extracted. This is where the OpenMath 2 OMOBJ xrefs would be resolved once we decide to support them. Local xrefs (inside the same OMOBJ) would be searched for here, but global xrefs would require MBase queries. Keep in mind that the result is not detached from its container, so if you want to insert it somewhere else, you will have to do it yourself. Similarly, the returned element is the original, so remember to clone it if you want to have a modified copy without altering the original.

Parameters:
element - OMOBJ element or contents root. Can be null.
Returns:
OMOBJ contents root. Can be null.

applyBinding

public static org.jdom.Element applyBinding(org.jdom.Element binding,
                                            List variableValuesList,
                                            int index)
Apply a variable substitution. Any extra bound variables after the first one are ignored.

Parameters:
binding - OpenMath fns1:lambda binding, without any enclosing OMOBJ.
variableValuesList - values for the bound variables.
index - index of the value for the first bound variable.
Returns:
a copy of the expression in the binding, with the variable substituted.

setOmNamespace

public static void setOmNamespace(org.jdom.Element om)
The answer from an external system would normally be plain OpenMath, which means that when parsing it back as XML, the elements don't have any namespace associated to them. However, some parts of ActiveMath need them to be in the OpenMath namespace, so we need to put the expression there after we parse it as a JDom tree.

Parameters:
om - OpenMath expression as a JDom tree. This should be the OMOBJ.

tidyForOpenMath1

public static void tidyForOpenMath1(org.jdom.Element om)
External OM systems are not required to handle namespaces or extra attributes, so we need to remove them to ensure that the expression will be parsed correctly.

Parameters:
om - the OpenMath expression to clean up.

output

public static void output(org.jdom.Element om,
                          OutputStream outputStream)