org.lsmp.djep.matrixJep.MatrixNodeFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jep Show documentation
Show all versions of jep Show documentation
JEP is a Java library for parsing and evaluating mathematical expressions.
The newest version!
/* @author rich
* Created on 16-Nov-2003
*/
package org.lsmp.djep.matrixJep;
import org.nfunk.jep.*;
import org.nfunk.jep.function.*;
import org.lsmp.djep.matrixJep.nodeTypes.*;
import org.lsmp.djep.vectorJep.*;
import org.lsmp.djep.vectorJep.function.*;
import org.lsmp.djep.xjep.*;
/**
* This class is used to create nodes of specified types.
* It can be sub-classed to change the nature of how nodes
* are constructed. Generally there are two methods for creating
* nodes, methods which take an existing node and methods which
* take the components.
*
* @author Rich Morris
* Created on 16-Nov-2003
*/
public class MatrixNodeFactory extends NodeFactory {
public MatrixNodeFactory(XJep xj)
{
super(xj);
}
/** Creates an ASTConstant node with specified value. **/
public ASTConstant buildConstantNode(Object value) throws ParseException
{
ASTMConstant node = new ASTMConstant(ParserTreeConstants.JJTCONSTANT);
node.setValue(value);
return node;
}
/** Creates a ASTVariable node with specified value. **/
public ASTVarNode buildVariableNode(Variable var) throws ParseException
{
ASTMVarNode node = new ASTMVarNode(ParserTreeConstants.JJTVARNODE);
node.setVar(var);
return node;
}
/**
* Builds a function with n arguments
* @param name of function.
* @param pfmc PostfixMathCommand for function.
* @param arguments the arguments to the function.
* @return top Node of expression
*/
public ASTFunNode buildFunctionNode(String name,PostfixMathCommandI pfmc,Node[] arguments) throws ParseException
{
ASTMFunNode res = new ASTMFunNode(ParserTreeConstants.JJTFUNNODE);
res.setFunction(name,pfmc);
copyChildren(res,arguments);
res.setDim(calcDim(name,pfmc,arguments));
return res;
}
/** Calculates the dimension of node using the dimensions
* of the children. Does not recurse down the tree.
*/
public Dimensions calcDim(String name,PostfixMathCommandI pfmc,Node arguments[])
throws ParseException
{
MatrixNodeI children[] = new MatrixNodeI[arguments.length];
for(int i=0;i