Class TreeUtils

java.lang.Object
com.singularsys.extensions.xjep.TreeUtils
All Implemented Interfaces:
JepComponent, Serializable
Direct Known Subclasses:
FieldTreeUtils

public class TreeUtils extends 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:
  • Constructor Details

    • TreeUtils

      public TreeUtils(NumberFactory nf)
      Standalone constructor with supplied NumberFactory
      Parameters:
      nf -
    • TreeUtils

      public TreeUtils()
      Constructor when used as a Jep component. The NumberFactory is set using the init(Jep) method
    • TreeUtils

      protected TreeUtils(Object zero, Object one, Object minus_one, Object two, Object pinf, Object ninf, Object nan)
  • Method Details

    • isReal

      public boolean isReal(Node node)
      returns true if node is a ASTConstant with Number value
    • isRealVal

      public boolean isRealVal(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(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(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(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
    • isInfinityVal

      public boolean isInfinityVal(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(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 value isNegativeVal(Object)
    • isNegativeVal

      public boolean isNegativeVal(Object val)
      Test a value for being negative. This implementation test for Numbers. Complex values of the form (x + i0) with x<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(Object val)
      Test a value for being positive. This implementation test for Numbers. Complex numbers of the form (x + i0) where x>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(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(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(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(Object val) throws EvaluationException
      Value of an ASTConstant node cast to an Integer should only be used if isIntegerVal(Object) returns true.
      Throws:
      EvaluationException - if the number cannot be converted exactly to an integer
    • getTrue

      public Object getTrue()
      The value used to represent a True ASTconstant value.
      Returns:
      Boolean.True
    • getFalse

      public 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 its current children.
      Throws:
      ParseException
    • getChildrenAsArray

      public static Node[] getChildrenAsArray(Node node)
      returns the children of a node as an array of nodes.
    • getCI

      public Object getCI()
      Complex imaginary unit
      Returns:
      Complex(0,1)
    • getCMinusI

      public Object getCMinusI()
      Complex minus i
      Returns:
      Complex(0,-1)
    • getCMinusOne

      public Object getCMinusOne()
      Complex minus one
      Returns:
      Complex(-1,0)
    • getCOne

      public Object getCOne()
      Complex one
      Returns:
      Complex(1,0)
    • getCZero

      public Object getCZero()
      Complex zero
      Returns:
      Complex(0,0)
    • getZero

      public Object getZero()
      Number representing zero
      Returns:
      0.0d
    • getOne

      public Object getOne()
      Number representing one
      Returns:
      1.0d
    • getMinusOne

      public Object getMinusOne()
      Number representing -1
      Returns:
      -1.0d
    • getTwo

      public 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 Object getNAN()
      Number representing NaN
      Returns:
      Double.NaN or null if hasNaN() returns false
    • getPositiveInfinity

      public Object getPositiveInfinity()
      Number representing +Infty
      Returns:
      Double.POSITIVE_INFINITY or null if hasNaN() returns false
    • getNegativeInfinity

      public 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 interface JepComponent
      Returns:
      either a new instance, null or 'this'.
    • init

      public void init(Jep jep)
      Description copied from interface: JepComponent
      Initialize the component. This method 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 interface JepComponent
      Parameters:
      jep - the current Jep instance