public class Constraint
extends java.lang.Object
Constraints
are conjunctions of atomic constraints (class AConstraint
).AConstraint
Modifier and Type | Field | Description |
---|---|---|
static int |
MAX_ARGUMENTS_PER_CONSTRAINT |
The maximum number of arguments that can be passed to the constructor of a constraint (excluding name and code).
|
Constructor | Description |
---|---|
Constraint() |
Creates an empty constraint conjunction, i.e. the conjunction of zero atomic constraints.
|
Constraint(java.lang.String name,
java.lang.Object... arguments) |
Creates a constraint conjunction of a single atomic constraint, identified by
name
with at most Constraint.MAX_ARGUMENTS_PER_CONSTRAINT arguments in arguments . |
Modifier and Type | Method | Description |
---|---|---|
java.lang.Object |
accept(jsetl.Visitor visitor) |
Needed for the visitor design pattern.
|
Constraint |
add(Constraint constraint) |
Adds each atomic constraint in
constraint to this constraint conjunction then returns this constraint conjunction. |
static Constraint |
allDifferent(java.lang.Object... objects) |
Returns a constraint that is satisfied if and only if all the elements in
objects
are different from each other. |
static Constraint |
allDifferent(java.util.List<?> objects) |
Returns a constraint that is satisfied if and only if all the elements in
objects
are different from each other. |
Constraint |
and(Constraint constraint) |
Logical "AND" between this constraint conjunction and the parameter
constraint . |
boolean |
check() |
Solves this constraint using a local solver;
|
Constraint |
clone() |
Creates and returns a clone of this constraint (the same as
new Constraint(this) . |
boolean |
equals(java.lang.Object other) |
Checks whether this object is equal to
other . |
void |
fail() |
Raises an exception
Fail . |
static Constraint |
falsec() |
Returns a constraint that is never satisfiable.
|
boolean |
firstCall() |
Checks whether the first atomic constraint is solved for the first time or not.
|
static <T> Constraint |
forAll(java.lang.Iterable<T> iterable,
LObject dummy,
Constraint constraint) |
Creates and returns a constraint that is the conjunction of the result of the substitutions of
dummy with each
element in iterable (one at a time) in constraint . |
int |
getAlternative() |
Gets the next alternative of the first constraint in the conjunction if it is the only one.
|
java.lang.Object |
getArg(int i) |
Gets the specified argument of the constraints if the constraint conjunction contains only one atomic constraint.
|
java.lang.String |
getName() |
Gets the name identifying the constraint.
|
Constraint |
impliesTest(Constraint constraint) |
Deterministic "implication test" (A implies B) between this constraint
conjunction and the parameter constraint.
|
boolean |
isGround() |
Checks whether this constraint is ground (i.e. everything it contains is ground) or not.
|
void |
notFirstCall() |
Sets the first atomic constraint
firstCall field value to false . |
void |
notSolved() |
Sets the solved flag of each atomic constraint in the conjunction to
false . |
Constraint |
notTest() |
Deterministic "negation test" not(A) over this constraint conjunction
|
Constraint |
or(Constraint constraint) |
Logical and NON-DETERMINISTIC "OR" between this constraint conjunction and the parameter constraint.
|
Constraint |
orTest(Constraint constraint) |
Deterministic "or test" between this constraint conjunction and the parameter
constraint . |
LSet |
setof(LVar lVar) |
Computes and returns the set of all possible values for
lVar which satisfy this constraint conjunction. |
void |
solve() |
Solves this constraint using a local solver.
|
boolean |
test() |
Solves this constraint using a local solver but leaving the constraint and all logical variables unchanged;
|
java.lang.String |
toString() |
Gives a string representation of the constraint conjunction like "_x = 1 + 2 AND _x.label()"
|
java.lang.String |
toStringInternals() |
Similar to the method
Constraint#toString() but gives a different string representation: one of the form
"constraint(constraint_name, arg_1, arg_2, arg_3, arg_4) - alternative: index_of_alternative, solved? |
static Constraint |
truec() |
Returns a constraint that is always satisfiable.
|
public static final int MAX_ARGUMENTS_PER_CONSTRAINT
AConstraint.MAX_ACONSTRAINT_ARGUMENTS (4)
.AConstraint.MAX_ACONSTRAINT_ARGUMENTS
,
Constant Field Valuespublic Constraint()
public Constraint(@NotNull java.lang.String name, @NotNull java.lang.Object... arguments)
name
with at most Constraint.MAX_ARGUMENTS_PER_CONSTRAINT
arguments in arguments
.name
- the name identifying the kind of atomic constraint to create.arguments
- sequence of arguments (max Constraint.MAX_ARGUMENTS_PER_CONSTRAINT
).java.lang.IllegalArgumentException
- if too many arguments are provided or if the name is "_and"
and the number of non-null object arguments is not 2
or if their types are not correct.@NotNull public static Constraint allDifferent(@NotNull java.lang.Object... objects)
objects
are different from each other.objects
- variable arguments of type Object
. None of them can be null
.java.lang.NullPointerException
- if one of objects
is null
.@NotNull public static Constraint allDifferent(@NotNull java.util.List<?> objects)
objects
are different from each other.objects
- list of objects. None of them can be null
.java.lang.NullPointerException
- if objects
contains null
values.@NotNull public static <T> Constraint forAll(java.lang.Iterable<T> iterable, LObject dummy, Constraint constraint)
dummy
with each
element in iterable
(one at a time) in constraint
. For example: if constraint
is x.neq(1)
, the dummy
variable is x
and the objects in iterable
are "1,2,3,4", the constraint returned is "1 neq 1 AND 2 neq 1 AND 3 neq 1 AND 4 neq 1".T
- a generic type. It should be compatible with the type of dummy
.iterable
- an iterable containing the substitutions for dummy
. Note that the objects in iterable
should be compatible with
the type of dummy
(e.g. if dummy
is an LSet
then the objects in iterable
should be LSet
s or Set
s
but these conditions are not checked by the method.dummy
- dummy variable in constraint
.constraint
- the constraint to apply to each element of iterable
.@NotNull public static Constraint truec()
@NotNull public static Constraint falsec()
public boolean check()
true
if the constraint is satisfiable, false
otherwise.@NotNull public Constraint clone()
new Constraint(this)
.public boolean equals(@Nullable java.lang.Object other)
other
.equals
in class java.lang.Object
other
- the other argument of equality.true
if this object is equal to other
, false
otherwise.@Nullable public java.lang.Object getArg(int i)
null
.
If it contains more than one atomic constraint in conjunction then it returns a new constraint conjunction made
of only the first atomic constraint in conjunction if i
is 1, it returns a conjunction made of
all the atomic constraint except the first if i
is 2, otherwise returns null
.i
- index of the argument.null
.
If it contains more than one atomic constraint in conjunction then it returns a new constraint conjunction made
of only the first atomic constraint in conjunction if i
is 1, it returns a conjunction made of
all the atomic constraint except the first if i
is 2, otherwise returns null
.@NotNull public java.lang.String getName()
public boolean isGround()
true
if the constraint is ground, false
otherwise.@NotNull public LSet setof(@NotNull LVar lVar)
lVar
which satisfy this constraint conjunction.
This method uses a local solver.lVar
- The logical variable which values are collected.LSet
containing all possible solutions for lVar
that satisfy this constraint.public void solve() throws Failure
Failure
- if the constraint is not satisfiable.public boolean test()
true
if the constraint is satisfiable, otherwise it returns false
.solve()
@NotNull public java.lang.String toString()
toString
in class java.lang.Object
String
representing the constraint conjunction.@NotNull public java.lang.String toStringInternals()
Constraint#toString()
but gives a different string representation: one of the form
"constraint(constraint_name, arg_1, arg_2, arg_3, arg_4) - alternative: index_of_alternative, solved?: boolean_value".
Used for debugging purposes.String
representation for the constraint conjunction.@Nullable public java.lang.Object accept(@NotNull jsetl.Visitor visitor)
visitor
- the visitor that wants to visit the object.visitor.visit(this)
.public void fail()
Fail
. This exception is handled by the Solver
which
will backtrack if there are open choice points or throw an exception Failure
otherwise.Fail
- always.public boolean firstCall()
true
if the first atomic constraint is solved for the first time, false
otherwise.public void notFirstCall()
firstCall
field value to false
.
It will return false
when checked with firstCall()
.firstCall()
public int getAlternative()
0
if the size is at least 2
, raises an error otherwise.public void notSolved()
false
.@NotNull public Constraint and(@NotNull Constraint constraint)
constraint
. Does not modify this object.constraint
- the constraint conjunction which is the second argument of the logical "AND".constraint
.@NotNull public Constraint add(@NotNull Constraint constraint)
constraint
to this constraint conjunction then returns this constraint conjunction.constraint
- the constraint conjunction which atomic constraints are to be added to this constraint conjunction.constraint
.@NotNull public Constraint impliesTest(@NotNull Constraint constraint)
constraint
- the second argument of the "implication test".constraint
.@NotNull public Constraint notTest()
@NotNull public Constraint or(@NotNull Constraint constraint)
constraint
- the constraint conjunction which is the second argument of the non-deterministic "OR".constraint
.@NotNull public Constraint orTest(@NotNull Constraint constraint)
constraint
.constraint
- the second argument of the "or test".