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-
isConstant
test 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 NumberFactoryprotected
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 Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Complex
complexValue(java.lang.Object val)
static Node[]
getChildrenAsArray(Node node)
returns the children of a node as an array of nodes.java.lang.Object
getCI()
Complex imaginary unitjava.lang.Object
getCMinusI()
Complex minus ijava.lang.Object
getCMinusOne()
Complex minus onejava.lang.Object
getCOne()
Complex onejava.lang.Object
getCZero()
Complex zerojava.lang.Object
getFalse()
The value used to represent a False ASTconstant value.int
getIntValue(java.lang.Object val)
Value of an ASTConstant node cast to an Integer should only be used ifisIntegerVal(Object)
returns true.JepComponent
getLightWeightInstance()
Gets a light-weight instance suitable for using in multiple threads.java.lang.Object
getMinusOne()
Number representing -1java.lang.Object
getNAN()
Number representing NaNjava.lang.Object
getNegativeInfinity()
Number representing +Inftyjava.lang.Object
getOne()
Number representing onejava.lang.Object
getPositiveInfinity()
Number representing +Inftyjava.lang.Object
getTrue()
The value used to represent a True ASTconstant value.java.lang.Object
getTwo()
Number representing twojava.lang.Object
getZero()
Number representing zeroboolean
hasInfinity()
Whether we have a working value for +/- Infinityboolean
hasNaN()
Whether we have a working value for NaNvoid
init(Jep jep)
Initialize the component.int
intValue(Node node)
Value of an ASTConstant node cast to an Integer should only be used if isInteger returns true.boolean
isBinaryOperator(Node node)
Returns true if node represents a binary operatorboolean
isComplex(Node node)
returns true if node is an ASTConstant of type Complexboolean
isComplexVal(java.lang.Object val)
Test a value for being complex.boolean
isConstant(Node node)
returns true if node is an ASTConstantboolean
isConstantVariable(Node node)
returns true if node is an ASTVarNode with a constant variableboolean
isFunction(Node node)
returns true if node is an ASTFunNodeboolean
isInfinity(Node node)
returns true if node is a ASTConstant with a Infinite component TODO do proper treatment of signed infinityboolean
isInfinityVal(java.lang.Object val)
Test a value for being infinite.boolean
isInteger(Node node)
return true is the node is an ASTConstant with a value which is integral like 1.0.boolean
isIntegerVal(java.lang.Object val)
Test a value for being an integer.boolean
isMinusOne(Node node)
returns true if node is a ASTConstant with value Double(-1) or Complex(-1,0)boolean
isMinusOneVal(java.lang.Object val)
Test a value for being minus one.boolean
isNaN(Node node)
returns true if node is a ASTConstant with a NaN componentboolean
isNaNVal(java.lang.Object val)
Test a value for being NaN.boolean
isNegative(Node node)
returns true if node is an ASTConstant with a negative valueisNegativeVal(Object)
boolean
isNegativeVal(java.lang.Object val)
Test a value for being negative.boolean
isOne(Node node)
returns true if node is a ASTConstant with value Double(1) or Complex(1,0)boolean
isOneVal(java.lang.Object val)
Test a value for being one.boolean
isOperator(Node node)
returns true if node is an ASTOpNodeboolean
isPositive(Node node)
returns true if node is an ASTConstant with a positive Number valueboolean
isPositiveVal(java.lang.Object val)
Test a value for being positive.boolean
isReal(Node node)
returns true if node is a ASTConstant with Number valueboolean
isRealVal(java.lang.Object val)
boolean
isUnaryOperator(Node node)
Returns true if node represents a unary operatorboolean
isVariable(Node node)
returns true if node is an ASTVarNodeboolean
isZero(Node node)
returns true if node is a ASTConstant with value Double(0) or Complex(0,0)boolean
isZeroVal(java.lang.Object val)
Test a value for being zero.static Node
setChildrenIfNeeded(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<0
are 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>0
are 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 EvaluationException
Value 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:JepComponent
Gets a light-weight instance suitable for using in multiple threads.- Specified by:
getLightWeightInstance
in interfaceJepComponent
- Returns:
- either an new instance, null or 'this'.
-
init
public void init(Jep jep)
Description copied from interface:JepComponent
Initialize 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:
init
in interfaceJepComponent
- Parameters:
jep
- the current Jep instance
-
-