Package org.oristool.lello.ast
Class Expression
java.lang.Object
org.oristool.lello.ast.Expression
- Direct Known Subclasses:
BinaryExpression
,Brackets
,Constant
,FunctionCall
,UnaryExpression
,Variable
The base class for every node in Lello AST (Abstract Syntax Tree). This class
provides several services to the algorithms involved in manipulating the AST,
both in the form of static methods and instance methods, which can eventually
be overridden in subclasses.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final double
The tolerance beyond which two numeric values are considered the same. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionabstract Expression
copy()
Creates a copy of the object.abstract Value
Evaluates this expression using the specified variable bindings.format
(FormatVisitor visitor) Formats this expression.abstract Truth
isOne()
Checks whether this expression is equal to 1 (with respect to the EPSILON tolerance).static final boolean
isOneNumber
(double x) Checks whether a double is equal to 1 (with respect to the EPSILON tolerance).static final boolean
isOneNumber
(Value x) Checks whether a value is equal to 1 (with respect to the EPSILON tolerance).abstract Truth
isZero()
Checks whether this expression is equal to 0 (with respect to the EPSILON tolerance).static final boolean
isZeroNumber
(double x) Checks whether a double is equal to 0 (with respect to the EPSILON tolerance).static final boolean
Checks whether a value is equal to 0 (with respect to the EPSILON tolerance).abstract Expression
simplify
(Bindings bindings, SymbolicVisitor visitor) Tries to simplify this expression.toString()
Retrieves the names of all the variables appearing in this expression.
-
Field Details
-
EPSILON
public static final double EPSILONThe tolerance beyond which two numeric values are considered the same.- See Also:
-
-
Constructor Details
-
Expression
public Expression()
-
-
Method Details
-
isZeroNumber
Checks whether a value is equal to 0 (with respect to the EPSILON tolerance).- Parameters:
x
- The value to be tested.- Returns:
- true if x is equal to 0, false otherwise.
-
isZeroNumber
public static final boolean isZeroNumber(double x) Checks whether a double is equal to 0 (with respect to the EPSILON tolerance). This method is internally called by isZeroNumber(Value) but it is also exposed so that the same identical rule to address the tolerance issue is available to programmers.- Parameters:
x
- The double to be tested.- Returns:
- true if x is equal to 0, false otherwise.
-
isOneNumber
Checks whether a value is equal to 1 (with respect to the EPSILON tolerance).- Parameters:
x
- The value to be tested.- Returns:
- true if x is equal to 1, false otherwise.
-
isOneNumber
public static final boolean isOneNumber(double x) Checks whether a double is equal to 1 (with respect to the EPSILON tolerance). This method is internally called by isOneNumber(Value) but it is also exposed so that the same identical rule to address the tolerance issue is available to programmers.- Parameters:
x
- The double to be tested.- Returns:
- true if x is equal to 1, false otherwise.
-
eval
Evaluates this expression using the specified variable bindings. Calling this method does not alter this expression.It is important to note that this method will not attempt any kind of simplification on the expression, which means that all the variables appearing in it must be either bound or correspond to Java static fields defined somewhere; if this is not the case an exception will be thrown.
- Parameters:
bindings
- Variable bindings.- Returns:
- The value of the expression.
-
simplify
Tries to simplify this expression. Calling this method does not alter this expression.This method will not throw an exception if some variables appearing in the expression are not bound. However it may still be able to cancel a variable, if an appropriate rule is found.
- Parameters:
bindings
- Variable bindings.visitor
- The visitor implementing the simplification rules.- Returns:
- A simplified expression if an appropriate rule was found, the same expression otherwise.
-
isZero
Checks whether this expression is equal to 0 (with respect to the EPSILON tolerance).To decrease the probability that this method will return DONTKNOW, simplify or eval should be preventively called on this expression.
- Returns:
- YES if this expression is equal to 0, NO if it is not, DONTKNOW if it is not known.
-
isOne
Checks whether this expression is equal to 1 (with respect to the EPSILON tolerance).To decrease the probability that this method will return DONTKNOW, simplify or eval should be preventively called on this expression.
- Returns:
- YES if this expression is equal to 1, NO if it is not, DONTKNOW if it is not known.
-
variables
Retrieves the names of all the variables appearing in this expression.- Returns:
- set of variables
-
copy
Creates a copy of the object.- Returns:
- a copy
-
format
Formats this expression.- Parameters:
visitor
- The visitor implementing the formatting rules.- Returns:
- The formatted string.
-
toString
-