Package ru.ispras.fortress.expression
Class ExprUtils
- java.lang.Object
-
- ru.ispras.fortress.expression.ExprUtils
-
public final class ExprUtils extends java.lang.Object
TheExprUtils
class provides utility methods to work with logical expressions.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static boolean
areCompatible(Node... exprs)
Checks whether the specified logical conditions are compatible(getConjunction(exprs[0], ..., exprs[n-1]) is SAT)
.static boolean
areComplete(Node... exprs)
Checks whether the specified logical conditions are complete!(getComplement(exprs[0], ..., exprs[n-1]) is SAT)
.static Node
getComplement(Node... exprs)
Performs logical complement (negation)!(getDisjunction(exprs[0], ..., exprs[n-1])
of the specified expressions combined with disjunction and returns the resulting expression.static Node
getConjunction(Node... exprs)
Performs logical conjunction(exprs[0] && ... && exprs[n-1])
of the specified expressions and returns the resulting expression.static Node
getDisjunction(Node... exprs)
Performs logical disjunction(exprs[0] || ... || exprs[n-1])
of the specified expressions and returns the resulting expression.static Node
getNegation(Node... exprs)
Performs logical negation(!getConjunction(exprs[0], ..., exprs[n-1]))
of the specified expressions combined with conjunction and returns the resulting expression.static java.util.Collection<NodeVariable>
getVariables(java.lang.Iterable<Node> exprs)
Returns all variables used in the specified expressions.static java.util.Collection<NodeVariable>
getVariables(Node expr)
Returns all variables used in the specified expression.static boolean
hasBindings(Node expr)
Checks whether the specified expression tree contains bindings (nodes of type BINDING).static boolean
isAtomicCondition(Node expr)
Checks whether the specified expression is an atomic logical expression (can be evaluated to boolean and does not include logical operations to combine expressions such as: AND, OR, NOT, XOR and IMPL).static boolean
isBinding(Node expr)
Checks whether the specified expression is represented by a binding.static boolean
isCondition(Node expr)
Checks whether the specified expression is a logical expression (can be evaluated to boolean).static boolean
isConstant(Node expr)
Checks whether the given expression is a constant expression (can be evaluated to a constant value).static boolean
isKind(Node.Kind kind, Node... exprs)
Checks whether all of the specified expressions are of the specified kind (seeNode.Kind
).static boolean
isOperation(Node expr)
Checks whether the expression is represented by an operation.static boolean
isOperation(Node expr, java.lang.Enum<?>... opIds)
Checks whether the expression is represented by one of the specified operations.static <T extends java.lang.Enum<? extends T>>
booleanisOperation(Node expr, java.util.Collection<T> opIds)
Checks whether the expression is represented by one of the specified operations.static <T extends java.lang.Enum<? extends T>>
booleanisOperation(Node expr, T opId)
Checks whether the expression is represented by the specified operation.static boolean
isSat(Node expr)
Checks whether the specified expression is satisfiable.static boolean
isType(DataTypeId typeId, Node... exprs)
Checks whether all of the specified expressions have the specified type (types are compared on theDataTypeId
level).static boolean
isType(DataType type, Node... exprs)
Checks whether all of the specified expressions have the specified type (types are compared on theDataType
level).static boolean
isValue(Node expr)
Checks whether the specified expression is represented by a constant value.static boolean
isVariable(Node expr)
Checks whether the specified expression is represented by a variable.
-
-
-
Method Detail
-
isType
public static boolean isType(DataTypeId typeId, Node... exprs)
Checks whether all of the specified expressions have the specified type (types are compared on theDataTypeId
level).- Parameters:
typeId
- Expected data type identifier.exprs
- Expressions to be checked.- Returns:
true
if all expression types match the type specified by thetypeId
argument orfalse
otherwise.- Throws:
java.lang.IllegalArgumentException
- if the expression array is empty; if any expression in the array isnull
.
-
isType
public static boolean isType(DataType type, Node... exprs)
Checks whether all of the specified expressions have the specified type (types are compared on theDataType
level).- Parameters:
type
- Expected data type.exprs
- Expressions to be checked.- Returns:
true
if all expression types match the type specified by thetype
argument orfalse
otherwise.- Throws:
java.lang.IllegalArgumentException
- if the expression array is empty; if any expression in the array isnull
.
-
isKind
public static boolean isKind(Node.Kind kind, Node... exprs)
Checks whether all of the specified expressions are of the specified kind (seeNode.Kind
).- Parameters:
kind
- Expected expression kind.exprs
- Expressions to be checked.- Returns:
true
if all expressions are of the specified kind orfalse
otherwise.- Throws:
java.lang.IllegalArgumentException
- if the expression array is empty; if any expression in the array isnull
.
-
isOperation
public static boolean isOperation(Node expr)
Checks whether the expression is represented by an operation.- Parameters:
expr
- Expression to be checked.- Returns:
true
if the expression is represented by an operation orfalse
otherwise.- Throws:
java.lang.IllegalArgumentException
- if the argument isnull
.
-
isOperation
public static <T extends java.lang.Enum<? extends T>> boolean isOperation(Node expr, T opId)
Checks whether the expression is represented by the specified operation.- Type Parameters:
T
- Operation identifier type.- Parameters:
expr
- Expression to be checked.opId
- Operation identifier.- Returns:
true
if the expression is represented by the specified operation orfalse
otherwise.- Throws:
java.lang.IllegalArgumentException
- if any of the parameters isnull
.
-
isOperation
public static <T extends java.lang.Enum<? extends T>> boolean isOperation(Node expr, java.util.Collection<T> opIds)
Checks whether the expression is represented by one of the specified operations.- Type Parameters:
T
- Operation identifier type.- Parameters:
expr
- Expression to be checked.opIds
- Collection of operation identifiers.- Returns:
true
if the expression is represented by one of the specified operations orfalse
otherwise.- Throws:
java.lang.IllegalArgumentException
- if any of the parameters isnull
; if the list of operation identifiers is empty.
-
isOperation
public static boolean isOperation(Node expr, java.lang.Enum<?>... opIds)
Checks whether the expression is represented by one of the specified operations.- Parameters:
expr
- Expression to be checked.opIds
- List of operation identifiers.- Returns:
true
if the expression is represented by one of the specified operations orfalse
otherwise.- Throws:
java.lang.IllegalArgumentException
- if any of the parameters isnull
; if the list of operation identifiers is empty.
-
isValue
public static boolean isValue(Node expr)
Checks whether the specified expression is represented by a constant value.- Parameters:
expr
- Expression to be checked.- Returns:
true
if the expression is a value orfalse
otherwise.- Throws:
java.lang.IllegalArgumentException
- if the parameter isnull
.
-
isVariable
public static boolean isVariable(Node expr)
Checks whether the specified expression is represented by a variable.- Parameters:
expr
- Expression to be checked.- Returns:
true
if the expression is a variable orfalse
otherwise.- Throws:
java.lang.IllegalArgumentException
- if the parameter isnull
.
-
isBinding
public static boolean isBinding(Node expr)
Checks whether the specified expression is represented by a binding.- Parameters:
expr
- Expression to be checked.- Returns:
true
if the expression is a binding orfalse
otherwise.- Throws:
java.lang.IllegalArgumentException
- if the parameter isnull
.
-
isCondition
public static boolean isCondition(Node expr)
Checks whether the specified expression is a logical expression (can be evaluated to boolean).- Parameters:
expr
- Expression to be checked.- Returns:
true
if the expression is logical (can be evaluated to boolean) orfalse
otherwise.- Throws:
java.lang.IllegalArgumentException
- if the parameter isnull
.
-
isAtomicCondition
public static boolean isAtomicCondition(Node expr)
Checks whether the specified expression is an atomic logical expression (can be evaluated to boolean and does not include logical operations to combine expressions such as: AND, OR, NOT, XOR and IMPL).- Parameters:
expr
- Expression to be checked.- Returns:
true
if the expression is an atomic logical expression orfalse
otherwise.- Throws:
java.lang.IllegalArgumentException
- if the parameter isnull
.
-
hasBindings
public static boolean hasBindings(Node expr)
Checks whether the specified expression tree contains bindings (nodes of type BINDING).- Parameters:
expr
- Expression to be checked.- Returns:
true
if the expression tree contains bindings (nodes of type BINDING) orfalse
otherwise.- Throws:
java.lang.IllegalArgumentException
- if the parameter isnull
.
-
isConstant
public static boolean isConstant(Node expr)
Checks whether the given expression is a constant expression (can be evaluated to a constant value). An expression is considered constant as long as it does not contain unassigned variables (bindings are taken into consideration).- Parameters:
expr
- Expression to be checked.- Returns:
true
if the expression is a constant expression orfalse
otherwise.- Throws:
java.lang.IllegalArgumentException
- if the parameter isnull
.
-
getConjunction
public static Node getConjunction(Node... exprs)
Performs logical conjunction(exprs[0] && ... && exprs[n-1])
of the specified expressions and returns the resulting expression.- Parameters:
exprs
- Expressions to be combined.- Returns:
- A logical conjunction of the specified expressions.
- Throws:
java.lang.IllegalArgumentException
- if any argument in the array isnull
; if no arguments are provided; if an argument is not a logical expression.
-
getDisjunction
public static Node getDisjunction(Node... exprs)
Performs logical disjunction(exprs[0] || ... || exprs[n-1])
of the specified expressions and returns the resulting expression.- Parameters:
exprs
- Expressions to be combined.- Returns:
- A logical disjunction of the specified expressions.
- Throws:
java.lang.IllegalArgumentException
- if any argument in the array isnull
; if no arguments are provided; if an argument is not a logical expression.
-
getNegation
public static Node getNegation(Node... exprs)
Performs logical negation(!getConjunction(exprs[0], ..., exprs[n-1]))
of the specified expressions combined with conjunction and returns the resulting expression.- Parameters:
exprs
- Expressions to be combined.- Returns:
- A logical negation of the specified expressions.
- Throws:
java.lang.IllegalArgumentException
- if any argument in the array isnull
; if no arguments are provided; if an argument is not a logical expression.
-
getComplement
public static Node getComplement(Node... exprs)
Performs logical complement (negation)!(getDisjunction(exprs[0], ..., exprs[n-1])
of the specified expressions combined with disjunction and returns the resulting expression.- Parameters:
exprs
- Expressions to be combined.- Returns:
- A logical complement of the specified expressions.
- Throws:
java.lang.IllegalArgumentException
- if any argument in the array isnull
; if no arguments are provided; if an argument is not a logical expression.
-
areComplete
public static boolean areComplete(Node... exprs)
Checks whether the specified logical conditions are complete!(getComplement(exprs[0], ..., exprs[n-1]) is SAT)
. N.B. The method uses the default constraint solver to perform the check.- Parameters:
exprs
- Conditions (logical expressions) to be checked.- Returns:
true
if the conditions are complete orfalse
otherwise.- Throws:
java.lang.IllegalArgumentException
- if any argument in the array isnull
; if no arguments are provided; if an argument is not a logical expression.
-
areCompatible
public static boolean areCompatible(Node... exprs)
Checks whether the specified logical conditions are compatible(getConjunction(exprs[0], ..., exprs[n-1]) is SAT)
. N.B. The method uses the default constraint solver to perform the check.- Parameters:
exprs
- Conditions (logical expressions) to be checked.- Returns:
true
if the conditions are compatible orfalse
otherwise.- Throws:
java.lang.IllegalArgumentException
- if any argument in the array isnull
; if no arguments are provided; if an argument is not a logical expression.
-
isSat
public static boolean isSat(Node expr)
Checks whether the specified expression is satisfiable. N.B. The method uses the default constraint solver to perform the check.- Parameters:
expr
- Expression to be checked.- Returns:
true
if the expression is satisfiable orfalse
otherwise.- Throws:
java.lang.IllegalArgumentException
- (1) if the parameter isnull
; (2) if the expression description contains errors that prevent the solver engine from solving it in a correct way; (3) if the solver is unable to solve a constraint based on the given expression due to limitations of its implementation.java.lang.IllegalStateException
- if the solver engine returned results with an unknown status.
-
getVariables
public static java.util.Collection<NodeVariable> getVariables(Node expr)
Returns all variables used in the specified expression.- Parameters:
expr
- Expression to be processed.- Returns:
- A collection of all variables used in the specified expression.
- Throws:
java.lang.IllegalArgumentException
- if the parameter isnull
.java.lang.IllegalStateException
- if the method finds nodes that refer to different variables that have the same name.
-
getVariables
public static java.util.Collection<NodeVariable> getVariables(java.lang.Iterable<Node> exprs)
Returns all variables used in the specified expressions.- Parameters:
exprs
- Collection of expressions to be processed.- Returns:
- A collection of all variables used in the specified expressions.
- Throws:
java.lang.IllegalArgumentException
- if the parameter isnull
.java.lang.IllegalStateException
- if the method finds nodes that refer to different variables that have the same name.
-
-