Package com.singularsys.extensions.xjep
Class TreeUtils
- java.lang.Object
-
- com.singularsys.extensions.xjep.TreeUtils
-
- All Implemented Interfaces:
JepComponent,java.io.Serializable
- Direct Known Subclasses:
FieldTreeUtils
public class TreeUtils extends java.lang.Object implements JepComponent
A set of Utility functions for working with Jep expression trees. Main methods are-
isConstanttest if its a constant. Many other is... methods.
- Author:
- rich
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Modifier Constructor Description TreeUtils()Constructor when used as a Jep component.TreeUtils(NumberFactory nf)Stand alone constructor with supplied NumberFactoryprotectedTreeUtils(java.lang.Object zero, java.lang.Object one, java.lang.Object minus_one, java.lang.Object two, java.lang.Object pinf, java.lang.Object ninf, java.lang.Object nan)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ComplexcomplexValue(java.lang.Object val)static Node[]getChildrenAsArray(Node node)returns the children of a node as an array of nodes.java.lang.ObjectgetCI()Complex imaginary unitjava.lang.ObjectgetCMinusI()Complex minus ijava.lang.ObjectgetCMinusOne()Complex minus onejava.lang.ObjectgetCOne()Complex onejava.lang.ObjectgetCZero()Complex zerojava.lang.ObjectgetFalse()The value used to represent a False ASTconstant value.intgetIntValue(java.lang.Object val)Value of an ASTConstant node cast to an Integer should only be used ifisIntegerVal(Object)returns true.JepComponentgetLightWeightInstance()Gets a light-weight instance suitable for using in multiple threads.java.lang.ObjectgetMinusOne()Number representing -1java.lang.ObjectgetNAN()Number representing NaNjava.lang.ObjectgetNegativeInfinity()Number representing +Inftyjava.lang.ObjectgetOne()Number representing onejava.lang.ObjectgetPositiveInfinity()Number representing +Inftyjava.lang.ObjectgetTrue()The value used to represent a True ASTconstant value.java.lang.ObjectgetTwo()Number representing twojava.lang.ObjectgetZero()Number representing zerobooleanhasInfinity()Whether we have a working value for +/- InfinitybooleanhasNaN()Whether we have a working value for NaNvoidinit(Jep jep)Initialize the component.intintValue(Node node)Value of an ASTConstant node cast to an Integer should only be used if isInteger returns true.booleanisBinaryOperator(Node node)Returns true if node represents a binary operatorbooleanisComplex(Node node)returns true if node is an ASTConstant of type ComplexbooleanisComplexVal(java.lang.Object val)Test a value for being complex.booleanisConstant(Node node)returns true if node is an ASTConstantbooleanisConstantVariable(Node node)returns true if node is an ASTVarNode with a constant variablebooleanisFunction(Node node)returns true if node is an ASTFunNodebooleanisInfinity(Node node)returns true if node is a ASTConstant with a Infinite component TODO do proper treatment of signed infinitybooleanisInfinityVal(java.lang.Object val)Test a value for being infinite.booleanisInteger(Node node)return true is the node is an ASTConstant with a value which is integral like 1.0.booleanisIntegerVal(java.lang.Object val)Test a value for being an integer.booleanisMinusOne(Node node)returns true if node is a ASTConstant with value Double(-1) or Complex(-1,0)booleanisMinusOneVal(java.lang.Object val)Test a value for being minus one.booleanisNaN(Node node)returns true if node is a ASTConstant with a NaN componentbooleanisNaNVal(java.lang.Object val)Test a value for being NaN.booleanisNegative(Node node)returns true if node is an ASTConstant with a negative valueisNegativeVal(Object)booleanisNegativeVal(java.lang.Object val)Test a value for being negative.booleanisOne(Node node)returns true if node is a ASTConstant with value Double(1) or Complex(1,0)booleanisOneVal(java.lang.Object val)Test a value for being one.booleanisOperator(Node node)returns true if node is an ASTOpNodebooleanisPositive(Node node)returns true if node is an ASTConstant with a positive Number valuebooleanisPositiveVal(java.lang.Object val)Test a value for being positive.booleanisReal(Node node)returns true if node is a ASTConstant with Number valuebooleanisRealVal(java.lang.Object val)booleanisUnaryOperator(Node node)Returns true if node represents a unary operatorbooleanisVariable(Node node)returns true if node is an ASTVarNodebooleanisZero(Node node)returns true if node is a ASTConstant with value Double(0) or Complex(0,0)booleanisZeroVal(java.lang.Object val)Test a value for being zero.static NodesetChildrenIfNeeded(Node node, Node[] children)Sets the children of a node if they have changed for it current children.
-
-
-
Constructor Detail
-
TreeUtils
public TreeUtils(NumberFactory nf)
Stand alone constructor with supplied NumberFactory- Parameters:
nf-
-
TreeUtils
public TreeUtils()
Constructor when used as a Jep component. The NumberFactory is set using theinit(Jep)method
-
TreeUtils
protected TreeUtils(java.lang.Object zero, java.lang.Object one, java.lang.Object minus_one, java.lang.Object two, java.lang.Object pinf, java.lang.Object ninf, java.lang.Object nan)
-
-
Method Detail
-
isReal
public boolean isReal(Node node)
returns true if node is a ASTConstant with Number value
-
isRealVal
public boolean isRealVal(java.lang.Object val)
-
isZero
public boolean isZero(Node node)
returns true if node is a ASTConstant with value Double(0) or Complex(0,0)
-
isZeroVal
public boolean isZeroVal(java.lang.Object val)
Test a value for being zero. This implementation test for Double 0 using ==, or Complex 0.- Parameters:
val- value to test- Returns:
- true in value is zero false otherwise
-
isOne
public boolean isOne(Node node)
returns true if node is a ASTConstant with value Double(1) or Complex(1,0)
-
isOneVal
public boolean isOneVal(java.lang.Object val)
Test a value for being one. This implementation test for Double 1 or Complex 1.- Parameters:
val- value to test- Returns:
- true in value is one false otherwise
-
isMinusOne
public boolean isMinusOne(Node node)
returns true if node is a ASTConstant with value Double(-1) or Complex(-1,0)
-
isMinusOneVal
public boolean isMinusOneVal(java.lang.Object val)
Test a value for being minus one. This implementation test for Double -1 or Complex -1.- Parameters:
val- value to test- Returns:
- true in value is -1 false otherwise
-
isInfinity
public boolean isInfinity(Node node)
returns true if node is a ASTConstant with a Infinite component TODO do proper treatment of signed infinity
-
isInfinityVal
public boolean isInfinityVal(java.lang.Object val)
Test a value for being infinite. This implementation test for Double infinite or Complex infinite- Parameters:
val- value to test- Returns:
- true in value is infinite false otherwise
-
isNaN
public boolean isNaN(Node node)
returns true if node is a ASTConstant with a NaN component
-
isNaNVal
public boolean isNaNVal(java.lang.Object val)
Test a value for being NaN. This implementation test for Double NaN or Complex NaN.- Parameters:
val- value to test- Returns:
- true in value is zero false otherwise
-
isNegative
public boolean isNegative(Node node)
returns true if node is an ASTConstant with a negative valueisNegativeVal(Object)
-
isNegativeVal
public boolean isNegativeVal(java.lang.Object val)
Test a value for being negative. This implementation test for Numbers. Complex values of the form(x + i0)withx<0are considered negative- Parameters:
val- value to test- Returns:
- true in value is negative false otherwise
-
isPositive
public boolean isPositive(Node node)
returns true if node is an ASTConstant with a positive Number value
-
isPositiveVal
public boolean isPositiveVal(java.lang.Object val)
Test a value for being positive. This implementation test for Numbers. Complex numbers of the form(x + i0)wherex>0are considered positive.- Parameters:
val- value to test- Returns:
- true in value is positive false otherwise
-
isComplex
public boolean isComplex(Node node)
returns true if node is an ASTConstant of type Complex
-
isComplexVal
public boolean isComplexVal(java.lang.Object val)
Test a value for being complex. This implementation test for value being of type Complex.- Parameters:
val- value to test- Returns:
- true in value is positive false otherwise
-
complexValue
public Complex complexValue(java.lang.Object val) throws EvaluationException
- Throws:
EvaluationException
-
isInteger
public boolean isInteger(Node node)
return true is the node is an ASTConstant with a value which is integral like 1.0.
-
isIntegerVal
public boolean isIntegerVal(java.lang.Object val)
Test a value for being an integer. This implementation test for Numbers whose double value and int value are equal. Complex values of the form (n + i 0) are considered to be integers.- Parameters:
val- value to test- Returns:
- true in value is positive false otherwise
-
intValue
public int intValue(Node node) throws EvaluationException
Value of an ASTConstant node cast to an Integer should only be used if isInteger returns true.- Parameters:
node-- Returns:
- an integer value identical to the nodes constant
- Throws:
EvaluationException- if the number cannot be converted exactly to an integer
-
getIntValue
public int getIntValue(java.lang.Object val) throws EvaluationExceptionValue of an ASTConstant node cast to an Integer should only be used ifisIntegerVal(Object)returns true.- Throws:
EvaluationException- if the number cannot be converted exactly to an integer
-
getTrue
public java.lang.Object getTrue()
The value used to represent a True ASTconstant value.- Returns:
- Boolean.True
-
getFalse
public java.lang.Object getFalse()
The value used to represent a False ASTconstant value.- Returns:
- Boolean.False
-
isConstant
public boolean isConstant(Node node)
returns true if node is an ASTConstant
-
isVariable
public boolean isVariable(Node node)
returns true if node is an ASTVarNode
-
isConstantVariable
public boolean isConstantVariable(Node node)
returns true if node is an ASTVarNode with a constant variable
-
isOperator
public boolean isOperator(Node node)
returns true if node is an ASTOpNode
-
isBinaryOperator
public boolean isBinaryOperator(Node node)
Returns true if node represents a binary operator
-
isUnaryOperator
public boolean isUnaryOperator(Node node)
Returns true if node represents a unary operator
-
isFunction
public boolean isFunction(Node node)
returns true if node is an ASTFunNode
-
setChildrenIfNeeded
public static Node setChildrenIfNeeded(Node node, Node[] children) throws ParseException
Sets the children of a node if they have changed for it current children.- Throws:
ParseException
-
getChildrenAsArray
public static Node[] getChildrenAsArray(Node node)
returns the children of a node as an array of nodes.
-
getCI
public java.lang.Object getCI()
Complex imaginary unit- Returns:
- Complex(0,1)
-
getCMinusI
public java.lang.Object getCMinusI()
Complex minus i- Returns:
- Complex(0,-1)
-
getCMinusOne
public java.lang.Object getCMinusOne()
Complex minus one- Returns:
- Complex(-1,0)
-
getCOne
public java.lang.Object getCOne()
Complex one- Returns:
- Complex(1,0)
-
getCZero
public java.lang.Object getCZero()
Complex zero- Returns:
- Complex(0,0)
-
getZero
public java.lang.Object getZero()
Number representing zero- Returns:
- 0.0d
-
getOne
public java.lang.Object getOne()
Number representing one- Returns:
- 1.0d
-
getMinusOne
public java.lang.Object getMinusOne()
Number representing -1- Returns:
- -1.0d
-
getTwo
public java.lang.Object getTwo()
Number representing two- Returns:
- 2.0d
-
hasNaN
public boolean hasNaN()
Whether we have a working value for NaN- Returns:
-
hasInfinity
public boolean hasInfinity()
Whether we have a working value for +/- Infinity- Returns:
-
getNAN
public java.lang.Object getNAN()
Number representing NaN- Returns:
- Double.NaN or null if
hasNaN()returns false
-
getPositiveInfinity
public java.lang.Object getPositiveInfinity()
Number representing +Infty- Returns:
- Double.POSITIVE_INFINITY or null if
hasNaN()returns false
-
getNegativeInfinity
public java.lang.Object getNegativeInfinity()
Number representing +Infty- Returns:
- Double.NEGATIVE_INFINITY or null if
hasNaN()returns false
-
getLightWeightInstance
public JepComponent getLightWeightInstance()
Description copied from interface:JepComponentGets a light-weight instance suitable for using in multiple threads.- Specified by:
getLightWeightInstancein interfaceJepComponent- Returns:
- either an new instance, null or 'this'.
-
init
public void init(Jep jep)
Description copied from interface:JepComponentInitialize the component. This methods is called whenever a component is added to Jep. Hence it allows components to keep track of the other components they may rely on.- Specified by:
initin interfaceJepComponent- Parameters:
jep- the current Jep instance
-
-