Package org.xcsp.common.predicates
Class XNode<V extends IVar>
- java.lang.Object
-
- org.xcsp.common.predicates.XNode<V>
-
- All Implemented Interfaces:
Comparable<XNode<V>>
- Direct Known Subclasses:
XNodeLeaf
,XNodeParent
public abstract class XNode<V extends IVar> extends Object implements Comparable<XNode<V>>
This class is used for representing a node of a syntactic tree, which is built for functional expressions, and used especially with element<intension>
. Subclasses of this class allow us to manage parent and leaf nodes.- Author:
- Christophe Lecoutre
-
-
Field Summary
Fields Modifier and Type Field Description XNode<V>[]
sons
The sons (children) of the node.Types.TypeExpr
type
The type of the node.
-
Constructor Summary
Constructors Modifier Constructor Description protected
XNode(Types.TypeExpr type, XNode<V>[] sons)
Builds a node for a syntactic tree, with the specified type and the specified sons (children).
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract XNode<V>
abstraction(List<Object> args, boolean abstractIntegers, boolean multiOccurrences)
Returns a new tree representing an abstraction of the tree rooted by this node.LinkedList<XNode<V>>
allNodesSuchThat(java.util.function.Predicate<XNode<V>> p)
Returns a list containing all nodes accepted by the specified predicate in the tree rooted by this node.abstract LinkedList<XNode<V>>
allNodesSuchThat(java.util.function.Predicate<XNode<V>> p, LinkedList<XNode<V>> list)
Adds to the specified list all nodes accepted by the specified predicate in the tree rooted by this node.Types.TypeArithmeticOperator
ariop(int i)
int
arity()
Returns the arity of this node, i.e., the number of sons.int[]
arrayOfVals()
Returns the list of values (integers) in the tree rooted by this node, in the order they are encountered, ornull
if there is none.V[]
arrayOfVars()
Returns the list of variables in the tree rooted by this node, in the order they are encountered, ornull
if there is none.abstract XNode<V>
canonization()
Returns a new tree, equivalent to the tree rooted by this node, and in canonical form.LinkedHashSet<V>
collectVarsToSet(LinkedHashSet<V> set)
abstract XNode<V>
concretization(Object[] args)
Returns a new tree representing a concretization of the tree rooted by this node.boolean
exactlyVars(V[] t)
Returntrue
iff the sequence of variables (without duplicates) encountered in the tree rooted by this node is exactly the specified array.abstract XNode<V>
firstNodeSuchThat(java.util.function.Predicate<XNode<V>> p)
Returns the first node accepted by the specified predicate in the tree rooted by this node, ornull
otherwise.Types.TypeExpr
getType()
Returns the type of the node.LinkedList<Long>
listOfVals()
Builds a list with the sequence of values (long integers) encountered during a depth-first exploration of the tree rooted by this node.LinkedList<V>
listOfVars()
Builds a list with the sequence of variables encountered during a depth-first exploration of the tree rooted by this node.Types.TypeLogicalOperator
logop(int i)
static <V extends IVar>
XNodeLeaf<V>longLeaf(long value)
abstract int
maxParameterNumber()
Returns the maximum value of a parameter number in the tree rooted by this node, or -1 if there is none.static <V extends IVar>
XNodeParent<V>node(MatcherInterface.AbstractOperation type, XNode<V> son)
static <V extends IVar>
XNodeParent<V>node(MatcherInterface.AbstractOperation type, XNode<V> left, XNode<V> right)
static <V extends IVar>
XNodeParent<V>node(Types.TypeExpr type, List<XNode<V>> sons)
static <V extends IVar>
XNodeParent<V>node(Types.TypeExpr type, java.util.stream.Stream<XNode<V>> sons)
static <V extends IVar>
XNodeParent<V>node(Types.TypeExpr type, XNode<V> son)
static <V extends IVar>
XNodeParent<V>node(Types.TypeExpr type, XNode<V>[] sons)
static <V extends IVar>
XNodeParent<V>node(Types.TypeExpr type, XNode<V> left, XNode<V> right)
Object
possibleValues()
Types.TypeConditionOperatorRel
relop(int i)
abstract XNode<V>
replaceLeafValues(java.util.function.Function<Object,Object> f)
a new tree, obtained from the tree rooted by this node by replacing values of leaves, as defined by the specified functionabstract XNode<V>
replacePartiallyParameters(Object[] valueParameters)
abstract XNode<V>
replaceSymbols(Map<String,Integer> mapOfSymbols)
Returns a new tree, obtained from the tree rooted by this node by replacing symbols with integers, as defined by the specified map.abstract int
size()
Returns the size of the tree rooted by this node, i.e., the number of nodes it contains.static <V extends IVar>
XNodeLeaf<V>specialLeaf(String value)
abstract String
toFunctionalExpression(Object[] argsForConcretization)
Returns a string denoting the functional expression of the tree rooted by this node.abstract String
toPostfixExpression(IVar[] scopeForAbstraction)
Returns a string denoting the post-fixed expression of the tree rooted by this node.String
toString()
Types.TypeUnaryArithmeticOperator
unalop(int i)
Integer
val(int i)
Returns the (i+1)th value encountered while traversing (in a depth-first manner) the tree rooted by this node, ornull
if such value does not exist.V
var(int i)
Returns the (i+1)th variable encountered while traversing (in a depth-first manner) the tree rooted by this node, ornull
if such variable does not exist.V[]
vars()
Returns the set of variables in the tree rooted by this node, in the order they are collected, ornull
if there is none.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.lang.Comparable
compareTo
-
-
-
-
Field Detail
-
type
public final Types.TypeExpr type
The type of the node. For example, it can beADD
,NOT
, orLONG
.
-
-
Constructor Detail
-
XNode
protected XNode(Types.TypeExpr type, XNode<V>[] sons)
Builds a node for a syntactic tree, with the specified type and the specified sons (children).- Parameters:
type
- the type of the nodesons
- the sons (children) of the node
-
-
Method Detail
-
node
public static <V extends IVar> XNodeParent<V> node(Types.TypeExpr type, XNode<V> left, XNode<V> right)
-
node
public static <V extends IVar> XNodeParent<V> node(MatcherInterface.AbstractOperation type, XNode<V> left, XNode<V> right)
-
node
public static <V extends IVar> XNodeParent<V> node(Types.TypeExpr type, XNode<V> son)
-
node
public static <V extends IVar> XNodeParent<V> node(MatcherInterface.AbstractOperation type, XNode<V> son)
-
node
public static <V extends IVar> XNodeParent<V> node(Types.TypeExpr type, XNode<V>[] sons)
-
node
public static <V extends IVar> XNodeParent<V> node(Types.TypeExpr type, List<XNode<V>> sons)
-
node
public static <V extends IVar> XNodeParent<V> node(Types.TypeExpr type, java.util.stream.Stream<XNode<V>> sons)
-
getType
public final Types.TypeExpr getType()
Returns the type of the node. For exampleADD
,NOT
, orLONG
. Note that we need this method for language Scala.- Returns:
- the type of the node
-
arity
public final int arity()
Returns the arity of this node, i.e., the number of sons.- Returns:
- the arity of this node
-
size
public abstract int size()
Returns the size of the tree rooted by this node, i.e., the number of nodes it contains.- Returns:
- the size of the tree rooted by this node
-
maxParameterNumber
public abstract int maxParameterNumber()
Returns the maximum value of a parameter number in the tree rooted by this node, or -1 if there is none.- Returns:
- the maximum value of a parameter number in the tree rooted by this node, or -1
-
firstNodeSuchThat
public abstract XNode<V> firstNodeSuchThat(java.util.function.Predicate<XNode<V>> p)
Returns the first node accepted by the specified predicate in the tree rooted by this node, ornull
otherwise.- Parameters:
p
- a predicate to be applied on nodes- Returns:
- the first node accepted by the specified predicate
-
allNodesSuchThat
public abstract LinkedList<XNode<V>> allNodesSuchThat(java.util.function.Predicate<XNode<V>> p, LinkedList<XNode<V>> list)
Adds to the specified list all nodes accepted by the specified predicate in the tree rooted by this node. The specifies list is returned.- Parameters:
p
- a predicate to be applied on nodeslist
- a list in which nodes are added- Returns:
- a list with all nodes accepted by the specified predicate in the tree rooted by this node
-
allNodesSuchThat
public LinkedList<XNode<V>> allNodesSuchThat(java.util.function.Predicate<XNode<V>> p)
Returns a list containing all nodes accepted by the specified predicate in the tree rooted by this node. Nodes are added in infix manner.- Parameters:
p
- a predicate to be applied on nodes- Returns:
- a list with all nodes accepted by the specified predicate in the tree rooted by this node
-
listOfVars
public final LinkedList<V> listOfVars()
Builds a list with the sequence of variables encountered during a depth-first exploration of the tree rooted by this node. Multiple occurrences of the same variables are possible.- Returns:
- the list of encountered variables during a depth-first exploration
-
listOfVals
public final LinkedList<Long> listOfVals()
Builds a list with the sequence of values (long integers) encountered during a depth-first exploration of the tree rooted by this node. Multiple occurrences of the same values are possible.- Returns:
- the list of encountered values (integers) during a depth-first exploration
-
var
public final V var(int i)
Returns the (i+1)th variable encountered while traversing (in a depth-first manner) the tree rooted by this node, ornull
if such variable does not exist.- Parameters:
i
- the index, starting at 0, of a variable- Returns:
- the (i+1)th variable encountered in the tree rooted by this node
-
val
public final Integer val(int i)
Returns the (i+1)th value encountered while traversing (in a depth-first manner) the tree rooted by this node, ornull
if such value does not exist.- Parameters:
i
- the index, starting at 0, of a value- Returns:
- the (i+1)th value encountered in the tree rooted by this node
-
relop
public final Types.TypeConditionOperatorRel relop(int i)
-
unalop
public final Types.TypeUnaryArithmeticOperator unalop(int i)
-
ariop
public final Types.TypeArithmeticOperator ariop(int i)
-
logop
public final Types.TypeLogicalOperator logop(int i)
-
arrayOfVars
public final V[] arrayOfVars()
Returns the list of variables in the tree rooted by this node, in the order they are encountered, ornull
if there is none. Contrary to vars(), the same variables may occur several times.- Returns:
- the list of variables in the order they are encountered
-
arrayOfVals
public final int[] arrayOfVals()
Returns the list of values (integers) in the tree rooted by this node, in the order they are encountered, ornull
if there is none. Of course, the same values may occur several times.- Returns:
- the list of values (integers) in the order they are encountered
-
vars
public final V[] vars()
Returns the set of variables in the tree rooted by this node, in the order they are collected, ornull
if there is none.- Returns:
- the set of variables in the tree rooted by this node, or
null
-
exactlyVars
public final boolean exactlyVars(V[] t)
Returntrue
iff the sequence of variables (without duplicates) encountered in the tree rooted by this node is exactly the specified array.- Parameters:
t
- an array of variables- Returns:
true
iff the sequence of variables encountered in the tree rooted by this node is exactly the specified array
-
collectVarsToSet
public final LinkedHashSet<V> collectVarsToSet(LinkedHashSet<V> set)
-
replaceSymbols
public abstract XNode<V> replaceSymbols(Map<String,Integer> mapOfSymbols)
Returns a new tree, obtained from the tree rooted by this node by replacing symbols with integers, as defined by the specified map.- Parameters:
mapOfSymbols
- a map associating integers with strings (symbols)- Returns:
- a new tree, obtained by replacing symbols with integers, as defined by the specified map
-
replaceLeafValues
public abstract XNode<V> replaceLeafValues(java.util.function.Function<Object,Object> f)
a new tree, obtained from the tree rooted by this node by replacing values of leaves, as defined by the specified function- Parameters:
f
- a function mapping objects to objects- Returns:
- a new tree, obtained by replacing values of leaves, as defined by the specified function
-
replacePartiallyParameters
public abstract XNode<V> replacePartiallyParameters(Object[] valueParameters)
-
canonization
public abstract XNode<V> canonization()
Returns a new tree, equivalent to the tree rooted by this node, and in canonical form. For example, commutative operators will take variables before integers as operands; actually, the total ordinal order over constants inTypeExpr
is used. Some simplifications are also performed; for example,not(eq(x,y))
becomesne(x,y)
.- Returns:
- a new tree, equivalent to the tree rooted by this node, and in canonical form
-
abstraction
public abstract XNode<V> abstraction(List<Object> args, boolean abstractIntegers, boolean multiOccurrences)
Returns a new tree representing an abstraction of the tree rooted by this node. Variables are replaced by parameters, and integers are also replaced by parameters (if the first specified Boolean is true). Occurrences of the same variables are replaced by the same parameter (if the second specified Boolean is true). Values replaced by parameters are added to the specified list.- Parameters:
args
- a list that is updated by adding the objects (variables, and possibly integers) that are abstracted (replaced by parameters)abstractIntegers
- iftrue
, encountered integers are also abstractedmultiOccurrences
- iftrue
, occurrences of the same variables are replaced by the same parameter- Returns:
- a new tree representing an abstraction of the tree rooted by this node
-
concretization
public abstract XNode<V> concretization(Object[] args)
Returns a new tree representing a concretization of the tree rooted by this node. Any parameter of value i is replaced by the ith object in the specified list of arguments.- Parameters:
args
- the list of arguments to be used as values for the parameters that are present in the tree rooted by this node- Returns:
- a new tree representing a concretization of the tree rooted by this node
-
toPostfixExpression
public abstract String toPostfixExpression(IVar[] scopeForAbstraction)
Returns a string denoting the post-fixed expression of the tree rooted by this node. If the specified array is notnull
, variables that are present in the tree are replaced by their parameterized forms%i
.- Parameters:
scopeForAbstraction
- if notnull
, the scope on which an abstract post-fixed expression is built- Returns:
- a string denoting the post-fixed expression of the tree rooted by this node.
-
toFunctionalExpression
public abstract String toFunctionalExpression(Object[] argsForConcretization)
Returns a string denoting the functional expression of the tree rooted by this node. If the specified array is notnull
, parameters that are present in the tree are replaced by their corresponding arguments.- Parameters:
argsForConcretization
- if notnull
, the list of arguments to be used as values for the parameters that are present in the tree rooted by this node- Returns:
- a string denoting the functional expression of the tree rooted by this node
-
possibleValues
public Object possibleValues()
-
-