- JavaDoc

# JEP

- Introduction
- Basic Usage
- Variables
- Data types
- Operators
- Functions
- BigDecimal
- FAQ
- Migration
- Version History

# Advanced Features

- Manipulating Expressions
- Components
- Configurable Parser
- Serialization
- Grammar

# Advanced Features

## Manipulating expressions

It is possible to manipulate expressions by manipulating the corresponding expression tree created by JEP. The parse() and getLastRootNode() method can be used to obtain the expression tree of an expression. This will be useful if you want to do more than just evaluate the expressions you parse. For example, you may want to determine the derivative of an expression. In order to be able to this, you need direct access to the expression tree.

The expression tree consists of nodes. Each of the nodes in the parse tree is an object of one of the following types:

- ASTFunNode - Functions
- ASTOpNode - Operators +, -, *, /
- ASTConstant - Constants
- ASTVarNode - Variables

### Node methods

There are several methods of Node and its sub-classes which can be used to interegate the node.

All nodes
  - `node.jjtGetNumChildren()` returns the number of children of the node. For constants and variable nodes this is 0, for functions and operators this is the number of arguments.
  - `node.jjtGetChild(i)` returns the i-th child of the node.
Constant nodes
  - `((ASTConstant) node).getValue()` returns the value of a constant node.
Variable nodes
  - `((ASTVarNode) node).getVar()` returns the variable object associated with a variable node.
  - `((ASTVarNode) node).getName()` returns the name of the variable.
  - `((ASTVarNode) node).getValue()` returns the value of the variable.
Function nodes
  - `((ASTFunNode) node).getName()` returns the name of the function.
  - `((ASTFunNode) node).getPFMC()` returns PostfixMathCommandI for the function.
Operator nodes
  - `((ASTOpNode) node).getOperator()` returns the operator.
  - `((ASTOpNode) node).getPFMC()` returns PostfixMathCommandI for the function.

### Constructing expressions

The com.singularsys.jep.NodeFactory class contains various methods for creating different types of node to make an expression. For example the following produces an expression "5+cos(x)".

    NodeFactory nf = jep.getNodeFactory();
    Node node = nf.buildOperatorNode(
    	  		  jep.getOperatorTable().getAdd(),
    			  nf.buildConstantNode(new Double(5)),
    			  nf.buildFunctionNode(
    			  	"cos",
    			    nf.buildVaraibleNode("x")
    			  );
    			);

### Traversing expressions

There are several methods of traversing the expression tree. The most commonly used method is based on the Visitor pattern. ParserVisitor defines an interface for classes which wish to use this.

    public interface ParserVisitor {
    	public Object visit(ASTConstant node,Object data) throws JepException;
    	public Object visit(ASTFunNode node,Object data) throws JepException;
    	public Object visit(ASTVarNode node,Object data) throws JepException;
    	public Object visit(ASTOpNode node,Object data) throws JepException;
    }

A class which wishes to traverse the tree would need to implement each method, the `data` argument can be used to pass in arbitrary data and they can return arbitrary values. To start the transversal call `Node.jjtAccept(ParserVisitor pv,Object data)` for the top node of the expression, the visitor pattern will take care of calling the correct method.

The `visit(ASTFunNode)` and `visit(ASTOpNode)` methods will often be similar in functionality as both have a PFMC and a number of child nodes. They should generally call `Node.jjtAccept` for each child.

    public Object visit(ASTFunNode node,Object data) throws JepException
    {
    	int nChild = node.jjtGetNumChildren();
    	Object[] results = new Object[nChild];
    	for(int i=0;i<nChild;++i)
    		results[i] = node.jjtGetChild(i).jjtAccept(this,data);
    	// now process this node
    	return res;
    }
    // Just call the ASTFunNode method
    public Object visit(ASTOpNode node,Object data) throws JepException
    {
    	return visit((ASTFunNode) node,data);
    }

For visitors which manipulate expressions and return a modified parse tree the DoNothingVisitor and DeepCopyVisitor can be used as base classes. DoNothingVisitor visits each node in turn and does nothing, it provides the `Node[] visitChildren(Node node,Object data)` methods which simplifies the process of visiting the child nodes. DeepCopyVisitor produces a deep copy of the expression.

For very large equations with 10,000+ nodes the standard ParserVisitor class can encounter problems with stack overflows due to recursions. The PostfixTreeWalker offers a base class for a transversal strategy which minimizes the number of stack frames used and the PostfixEvaluator is an evaluator which uses this strategy. These methods visit each node in postfix fashion hence for `1+cos(x)` the nodes are visited in the order 1, x, cos, +.