hu.birot.OTKit.otBuildingBlocks
Class ConstraintExamples

java.lang.Object
  extended by hu.birot.OTKit.otBuildingBlocks.ConstraintExamples

public class ConstraintExamples
extends java.lang.Object

Offers a number of static methods that return frequently used constraints.

Regarding constraint arithmetic: All constraint violations are casted to double. If constraints are integer-valued, you may want to implement the constraint arithmetic yourself, to gain CPU time and precision. If constraints assign violations that belong to some "non-numeric" violation type, then be prepared to runtime exceptions.


Constructor Summary
ConstraintExamples()
           
 
Method Summary
static Constraint condition(java.lang.String name, Constraint cond, Constraint tru, Constraint fal)
          Returns a conditional constraint.
static Constraint condition(java.lang.String name, Constraint cond, Constraint pos, Constraint zero, Constraint neg)
          Returns a conditional constraint.
static Constraint constant(java.lang.String name, double val)
          Returns a constraint whose name is name, and which assign a constant FloatViolation to any candidate.
static Constraint constant(java.lang.String name, int val)
          Returns a constraint whose name is name, and which assign a constant IntegerViolation to any candidate.
static Constraint constant(java.lang.String name, Violation viol)
          Returns a constraint whose name is name, and which assign a constant violation to any candidate.
static Constraint contain(java.lang.String name, java.lang.String str, double val)
          Returns a constraint whose name is name, and which assigns a FloatViolation of value val to any candidate whose surface form contain the string str.
static Constraint counter1(java.lang.String name, double val)
          This static method returns a constraint whose name is name, and which assign a violation to any candidate that is equal to val times the field counter1 of the surface form of that candidate.
static Constraint counter1EqualTo(java.lang.String name, double e, double val)
          This static method returns a constraint whose name is name, and which assigns a violation of value val to any candidate whose surface form has a counter 1 that is exactly equal to e.
static Constraint counter1GreaterEqual(java.lang.String name, double threshold, double val)
          This static method returns a constraint whose name is name, and which assigns a violation of value val to any candidate whose surface form has a counter 1 that is greater than or equal to threshold.
static Constraint counter1GreaterThan(java.lang.String name, double threshold, double val)
          This static method returns a constraint whose name is name, and which assigns a violation of value val to any candidate whose surface form has a counter 1 that is greater than threshold.
static Constraint counter1LessEqual(java.lang.String name, double threshold, double val)
          This static method returns a constraint whose name is name, and which assigns a violation of value val to any candidate whose surface form has a counter 1 that is less than or equal to threshold.
static Constraint counter1LessThan(java.lang.String name, double threshold, double val)
          This static method returns a constraint whose name is name, and which assigns a violation of value val to any candidate whose surface form has a counter 1 that is less than threshold.
static Constraint counter2(java.lang.String name, double val)
          This static method returns a constraint whose name is name, and which assign a violation to any candidate that is equal to val times the field counter2 of the surface form of that candidate.
static Constraint counter2EqualTo(java.lang.String name, double e, double val)
          This static method returns a constraint whose name is name, and which assigns a violation of value val to any candidate whose surface form has a counter 2 that is exactly equal to e.
static Constraint counter2GreaterEqual(java.lang.String name, double threshold, double val)
          This static method returns a constraint whose name is name, and which assigns a violation of value val to any candidate whose surface form has a counter 2 that is greater than or equal to threshold.
static Constraint counter2GreaterThan(java.lang.String name, double threshold, double val)
          This static method returns a constraint whose name is name, and which assigns a violation of value val to any candidate whose surface form has a counter 2 that is greater than threshold.
static Constraint counter2LessEqual(java.lang.String name, double threshold, double val)
          This static method returns a constraint whose name is name, and which assigns a violation of value val to any candidate whose surface form has a counter 2 that is less than or equal to threshold.
static Constraint counter2LessThan(java.lang.String name, double threshold, double val)
          This static method returns a constraint whose name is name, and which assigns a violation of value val to any candidate whose surface form has a counter 2 that is less than threshold.
static Constraint counter3(java.lang.String name, double val)
          This static method returns a constraint whose name is name, and which assign a violation to any candidate that is equal to val times the field counter3 of the surface form of that candidate.
static Constraint counter3EqualTo(java.lang.String name, double e, double val)
          This static method returns a constraint whose name is name, and which assigns a violation of value val to any candidate whose surface form has a counter 3 that is exactly equal to e.
static Constraint counter3GreaterEqual(java.lang.String name, double threshold, double val)
          This static method returns a constraint whose name is name, and which assigns a violation of value val to any candidate whose surface form has a counter 3 that is greater than or equal to threshold.
static Constraint counter3GreaterThan(java.lang.String name, double threshold, double val)
          This static method returns a constraint whose name is name, and which assigns a violation of value val to any candidate whose surface form has a counter 3 that is greater than threshold.
static Constraint counter3LessEqual(java.lang.String name, double threshold, double val)
          This static method returns a constraint whose name is name, and which assigns a violation of value val to any candidate whose surface form has a counter 3 that is less than or equal to threshold.
static Constraint counter3LessThan(java.lang.String name, double threshold, double val)
          This static method returns a constraint whose name is name, and which assigns a violation of value val to any candidate whose surface form has a counter 3 that is less than threshold.
static Constraint equal(java.lang.String name, java.lang.String str, double val)
          Returns a constraint whose name is name, and which assigns a FloatViolation of value val to any candidate whose surface form is the string str.
static Constraint linearCombination(java.lang.String name, Constraint x, double a, Constraint y, double b)
          Returns the linear combination of two constraints.
static Constraint max(java.lang.String name, Constraint con1, Constraint con2)
          Returns the maximum of two constraints.
static Constraint min(java.lang.String name, Constraint con1, Constraint con2)
          Returns the minimum of two constraints.
static Constraint negate(java.lang.String name, Constraint con)
          Returns the negation of a constraint.
static Constraint product(java.lang.String name, Constraint[] con)
          Returns the product of constraints.
static Constraint product(java.lang.String name, Constraint con1, Constraint con2)
          Returns the product of two constraints.
static Constraint product(java.lang.String name, Constraint con, double coeff)
          Returns the product of a constant and a constraint.
static Constraint ratio(java.lang.String name, Constraint con1, Constraint con2)
          Returns the ratio of two constraints.
static Constraint ratio(java.lang.String name, double constant, Constraint constraint)
          Returns the ratio of a constant and a constraint.
static Constraint regex(java.lang.String name, java.lang.String regex, double val)
          Returns a constraint whose name is name, and which assign a FloatViolation of value val to any candidate whose string in its surface form matches the regular expression regex.
static Constraint substring(java.lang.String name, java.lang.String str, double val)
          Returns a constraint whose name is name, and which assigns a FloatViolation of value val to each instances of the substring str.
static Constraint sum(java.lang.String name, Constraint[] con)
          Returns the sum of constraints.
static Constraint sum(java.lang.String name, Constraint con1, Constraint con2)
          Returns the sum of two constraints.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ConstraintExamples

public ConstraintExamples()
Method Detail

equal

public static Constraint equal(java.lang.String name,
                               java.lang.String str,
                               double val)

Returns a constraint whose name is name, and which assigns a FloatViolation of value val to any candidate whose surface form is the string str. All other candidates are assigned a zero Violation (namely, FloatViolation.nullViolation).

Parameters:
name - The name of the constraint to be constructed.
str - The surface form that is assigned a violation.
val - The violation level to be assigned.
Returns:
A constraint, as described above.

contain

public static Constraint contain(java.lang.String name,
                                 java.lang.String str,
                                 double val)

Returns a constraint whose name is name, and which assigns a FloatViolation of value val to any candidate whose surface form contain the string str. Note that the number of times str appears in the surface form does not influence the number of violation marks.

All other candidates are assigned a zero Violation (namely, FloatViolation.nullViolation).

Parameters:
name - The name of the constraint to be constructed.
str - The string that causes the violation violation.
val - The violation level to be assigned.
Returns:
A constraint, as described above.

substring

public static Constraint substring(java.lang.String name,
                                   java.lang.String str,
                                   double val)

Returns a constraint whose name is name, and which assigns a FloatViolation of value val to each instances of the substring str. Hence, a candidate whose surface form contains the substring str n times (including overlapping instances!) is assigned a violation level of n*val.

All other candidates are assigned a zero Violation (namely, FloatViolation.nullViolation).

Parameters:
name - The name of the constraint to be constructed.
str - The substring that is assigned a violation.
val - The violation level to be assigned to each occurrence of this substring.
Returns:
A constraint, as described above.

regex

public static Constraint regex(java.lang.String name,
                               java.lang.String regex,
                               double val)

Returns a constraint whose name is name, and which assign a FloatViolation of value val to any candidate whose string in its surface form matches the regular expression regex. String regex is a standard Java regular expression.

All other candidates are assigned a zero Violation (namely, FloatViolation.nullViolation).

Parameters:
name - The name of the constraint to be constructed.
regex - Regular expression to be matched against the string of the surface form.
val - The violation level to be assigned candidates matching the regex.
Returns:
A constraint, as described above.

counter1

public static Constraint counter1(java.lang.String name,
                                  double val)
This static method returns a constraint whose name is name, and which assign a violation to any candidate that is equal to val times the field counter1 of the surface form of that candidate.

Parameters:
name - The name of the constraint to be constructed.
val - The multiplier of counter1.
Returns:
A constraint as described above.

counter2

public static Constraint counter2(java.lang.String name,
                                  double val)
This static method returns a constraint whose name is name, and which assign a violation to any candidate that is equal to val times the field counter2 of the surface form of that candidate.

Parameters:
name - The name of the constraint to be constructed.
val - The multiplier of counter2.
Returns:
A constraint as described above.

counter3

public static Constraint counter3(java.lang.String name,
                                  double val)
This static method returns a constraint whose name is name, and which assign a violation to any candidate that is equal to val times the field counter3 of the surface form of that candidate.

Parameters:
name - The name of the constraint to be constructed.
val - The multiplier of counter3.
Returns:
A constraint as described above.

counter1LessThan

public static Constraint counter1LessThan(java.lang.String name,
                                          double threshold,
                                          double val)
This static method returns a constraint whose name is name, and which assigns a violation of value val to any candidate whose surface form has a counter 1 that is less than threshold.

Parameters:
name - The name of the constraint to be constructed.
threshold - The value of counter 1 below which the constraint is violated.
val - Level of violation to be assigned.
Returns:
A constraint as described above.

counter1LessEqual

public static Constraint counter1LessEqual(java.lang.String name,
                                           double threshold,
                                           double val)
This static method returns a constraint whose name is name, and which assigns a violation of value val to any candidate whose surface form has a counter 1 that is less than or equal to threshold.

Parameters:
name - The name of the constraint to be constructed.
threshold - The value of counter 1 below which and at which the constraint is violated.
val - Level of violation to be assigned.
Returns:
A constraint as described above.

counter1GreaterThan

public static Constraint counter1GreaterThan(java.lang.String name,
                                             double threshold,
                                             double val)
This static method returns a constraint whose name is name, and which assigns a violation of value val to any candidate whose surface form has a counter 1 that is greater than threshold.

Parameters:
name - The name of the constraint to be constructed.
threshold - The value of counter 1 above which the constraint is violated.
val - Level of violation to be assigned.
Returns:
A constraint as described above.

counter1GreaterEqual

public static Constraint counter1GreaterEqual(java.lang.String name,
                                              double threshold,
                                              double val)
This static method returns a constraint whose name is name, and which assigns a violation of value val to any candidate whose surface form has a counter 1 that is greater than or equal to threshold.

Parameters:
name - The name of the constraint to be constructed.
threshold - The value of counter 1 above which and at which the constraint is violated.
val - Level of violation to be assigned.
Returns:
A constraint as described above.

counter1EqualTo

public static Constraint counter1EqualTo(java.lang.String name,
                                         double e,
                                         double val)
This static method returns a constraint whose name is name, and which assigns a violation of value val to any candidate whose surface form has a counter 1 that is exactly equal to e.

Parameters:
name - The name of the constraint to be constructed.
e - The value of counter 1 at which the constraint is violated.
val - Level of violation to be assigned.
Returns:
A constraint as described above.

counter2LessThan

public static Constraint counter2LessThan(java.lang.String name,
                                          double threshold,
                                          double val)
This static method returns a constraint whose name is name, and which assigns a violation of value val to any candidate whose surface form has a counter 2 that is less than threshold.

Parameters:
name - The name of the constraint to be constructed.
threshold - The value of counter 2 below which the constraint is violated.
val - Level of violation to be assigned.
Returns:
A constraint as described above.

counter2LessEqual

public static Constraint counter2LessEqual(java.lang.String name,
                                           double threshold,
                                           double val)
This static method returns a constraint whose name is name, and which assigns a violation of value val to any candidate whose surface form has a counter 2 that is less than or equal to threshold.

Parameters:
name - The name of the constraint to be constructed.
threshold - The value of counter 2 below which and at which the constraint is violated.
val - Level of violation to be assigned.
Returns:
A constraint as described above.

counter2GreaterThan

public static Constraint counter2GreaterThan(java.lang.String name,
                                             double threshold,
                                             double val)
This static method returns a constraint whose name is name, and which assigns a violation of value val to any candidate whose surface form has a counter 2 that is greater than threshold.

Parameters:
name - The name of the constraint to be constructed.
threshold - The value of counter 2 above which the constraint is violated.
val - Level of violation to be assigned.
Returns:
A constraint as described above.

counter2GreaterEqual

public static Constraint counter2GreaterEqual(java.lang.String name,
                                              double threshold,
                                              double val)
This static method returns a constraint whose name is name, and which assigns a violation of value val to any candidate whose surface form has a counter 2 that is greater than or equal to threshold.

Parameters:
name - The name of the constraint to be constructed.
threshold - The value of counter 2 above which and at which the constraint is violated.
val - Level of violation to be assigned.
Returns:
A constraint as described above.

counter2EqualTo

public static Constraint counter2EqualTo(java.lang.String name,
                                         double e,
                                         double val)
This static method returns a constraint whose name is name, and which assigns a violation of value val to any candidate whose surface form has a counter 2 that is exactly equal to e.

Parameters:
name - The name of the constraint to be constructed.
e - The value of counter 2 at which the constraint is violated.
val - Level of violation to be assigned.
Returns:
A constraint as described above.

counter3LessThan

public static Constraint counter3LessThan(java.lang.String name,
                                          double threshold,
                                          double val)
This static method returns a constraint whose name is name, and which assigns a violation of value val to any candidate whose surface form has a counter 3 that is less than threshold.

Parameters:
name - The name of the constraint to be constructed.
threshold - The value of counter 3 below which the constraint is violated.
val - Level of violation to be assigned.
Returns:
A constraint as described above.

counter3LessEqual

public static Constraint counter3LessEqual(java.lang.String name,
                                           double threshold,
                                           double val)
This static method returns a constraint whose name is name, and which assigns a violation of value val to any candidate whose surface form has a counter 3 that is less than or equal to threshold.

Parameters:
name - The name of the constraint to be constructed.
threshold - The value of counter 3 below which and at which the constraint is violated.
val - Level of violation to be assigned.
Returns:
A constraint as described above.

counter3GreaterThan

public static Constraint counter3GreaterThan(java.lang.String name,
                                             double threshold,
                                             double val)
This static method returns a constraint whose name is name, and which assigns a violation of value val to any candidate whose surface form has a counter 3 that is greater than threshold.

Parameters:
name - The name of the constraint to be constructed.
threshold - The value of counter 3 above which the constraint is violated.
val - Level of violation to be assigned.
Returns:
A constraint as described above.

counter3GreaterEqual

public static Constraint counter3GreaterEqual(java.lang.String name,
                                              double threshold,
                                              double val)
This static method returns a constraint whose name is name, and which assigns a violation of value val to any candidate whose surface form has a counter 3 that is greater than or equal to threshold.

Parameters:
name - The name of the constraint to be constructed.
threshold - The value of counter 3 above which and at which the constraint is violated.
val - Level of violation to be assigned.
Returns:
A constraint as described above.

counter3EqualTo

public static Constraint counter3EqualTo(java.lang.String name,
                                         double e,
                                         double val)
This static method returns a constraint whose name is name, and which assigns a violation of value val to any candidate whose surface form has a counter 3 that is exactly equal to e.

Parameters:
name - The name of the constraint to be constructed.
e - The value of counter 3 at which the constraint is violated.
val - Level of violation to be assigned.
Returns:
A constraint as described above.

constant

public static Constraint constant(java.lang.String name,
                                  double val)
Returns a constraint whose name is name, and which assign a constant FloatViolation to any candidate.

Parameters:
name - The name of the constraint to be constructed.
val - The real value of the constant violation to be assigned to any candidate.
Returns:
A constraint as described above, always returning FloatViolation(val).

constant

public static Constraint constant(java.lang.String name,
                                  int val)
Returns a constraint whose name is name, and which assign a constant IntegerViolation to any candidate.

Parameters:
name - The name of the constraint to be constructed.
val - The integer value of the constant violation to be assigned to any candidate.
Returns:
A constraint as described above, always returning IntegerViolation(val).

constant

public static Constraint constant(java.lang.String name,
                                  Violation viol)

Returns a constraint whose name is name, and which assign a constant violation to any candidate.

NB: This constraint is not immutable, if Violation viol is defined by a mutable object (and not by a number or a string, for instance). To avoid problems, use at least a shallow cloning viol.clone() as the second argument.

Parameters:
name - The name of the constraint to be constructed.
viol - The constant violation to be assigned to any candidate.
Returns:
A constraint as described above.

sum

public static Constraint sum(java.lang.String name,
                             Constraint con1,
                             Constraint con2)
Returns the sum of two constraints. The sum is a constraint whose name is name, and which assigns to any candidate c the sum of the (real valued) violations assigned to c by constraints con1 and con2 (casted to double).

Parameters:
name - The name of the constraint to be constructed.
con1 - First addend.
con2 - Second addend.
Returns:
A constraint that is the sum of con1 and con2.

sum

public static Constraint sum(java.lang.String name,
                             Constraint[] con)
Returns the sum of constraints. The sum is a constraint whose name is name, and which assigns to any candidate c the sum of the (real valued) violations assigned to c by the constraints in the array of constraints con[]. Constant 0 is returned, if the array is empty.

Parameters:
name - The name of the constraint to be constructed.
con - Array of constraints to be summed up.
Returns:
A constraint that is the sum of the constraints in con[].

max

public static Constraint max(java.lang.String name,
                             Constraint con1,
                             Constraint con2)
Returns the maximum of two constraints. The maximum is a constraint whose name is name, and which assigns to any candidate c the largest of the violations (casted to double) assigned to c by constraints con1 and con2. For instance, if both constraints are binary (assigning values 0 or 1), then their maximum is their disjunction (logical "or").

Parameters:
name - The name of the constraint to be constructed.
con1 - First constraint.
con2 - Second constraint.
Returns:
A constraint that is the max of con1 and con2.

min

public static Constraint min(java.lang.String name,
                             Constraint con1,
                             Constraint con2)
Returns the minimum of two constraints. The minimum is a constraint whose name is name, and which assigns to any candidate c the smallest of the violations (casted to double) assigned to c by constraints con1 and con2. For instance, if both constraints are binary (assigning values 0 or 1), then their minimum is their conjunction (logical "and").

Parameters:
name - The name of the constraint to be constructed.
con1 - First constraint.
con2 - Second constraint.
Returns:
A constraint that is the min of con1 and con2.

negate

public static Constraint negate(java.lang.String name,
                                Constraint con)
Returns the negation of a constraint. The negation is a constraint whose name is name, and which assigns to any candidate c a (real valued) violation of 1 minus the violation assigned to c by constraint con:
negation(c) = 1 - con(c).

This operation is called negation because if constraint con is a binary constraint assigning either 0 (standing for false) or 1 (standing for true) to any candidate, then its negation will also be a binary constraint assigning the negated values.

Parameters:
name - The name of the constraint to be constructed.
con - Constraint to be negated.
Returns:
A constraint that is the "negation" of constraint con, as defined above.

product

public static Constraint product(java.lang.String name,
                                 Constraint con,
                                 double coeff)
Returns the product of a constant and a constraint. The product is a constraint whose name is name, and which assigns to any candidate c the multiplication of coeff times the (real valued) violation assigned to c by constraint con.

Parameters:
name - The name of the constraint to be constructed.
con - Constraint to be multiplied.
coeff - Coefficient (factor).
Returns:
A constraint that is coeff * con.

product

public static Constraint product(java.lang.String name,
                                 Constraint con1,
                                 Constraint con2)
Returns the product of two constraints. The product is a constraint whose name is name, and which assigns to any candidate c the multiplication of the violations (casted to double) assigned to c by constraints con1 and con2. For instance, if both constraints are binary, assigning values 0 or 1, then their product is their conjunction (logical "and").

Parameters:
name - The name of the constraint to be constructed.
con1 - First factor.
con2 - Second factor.
Returns:
A constraint that is the product (multiplication) of con1 and con2.

product

public static Constraint product(java.lang.String name,
                                 Constraint[] con)
Returns the product of constraints. The product is a constraint whose name is name, and which assigns to any candidate c the product of the (real valued) violations assigned to c by the constraints in the array of constraints con[]. Constant 1 is returned, if the array is empty.

Parameters:
name - The name of the constraint to be constructed.
con - Array of constraints to be multiplied.
Returns:
A constraint that is the product of the constraints in con[].

ratio

public static Constraint ratio(java.lang.String name,
                               Constraint con1,
                               Constraint con2)
Returns the ratio of two constraints. The ratio is a constraint whose name is name, and which assigns to any candidate c the the (real valued) violations assigned to c by constraint con1 divided by the violations assigned to c by constraint con2. It is your task to avoid division by zero.

Parameters:
name - The name of the constraint to be constructed.
con1 - Numerator.
con2 - Denominator.
Returns:
A constraint that is con1 divided by con2.

ratio

public static Constraint ratio(java.lang.String name,
                               double constant,
                               Constraint constraint)
Returns the ratio of a constant and a constraint. The ratio is a constraint whose name is name, and which assigns to any candidate c the value constant divided by the violation assigned to c by constraint constraint. It is your task to avoid division by zero.

Parameters:
name - The name of the constraint to be constructed.
constant - Numerator.
constraint - Denominator.
Returns:
A constraint that is con1 divided by con2.

linearCombination

public static Constraint linearCombination(java.lang.String name,
                                           Constraint x,
                                           double a,
                                           Constraint y,
                                           double b)
Returns the linear combination of two constraints. The linear combination is a constraint whose name is name, and which assigns to any candidate c the sum of the violations (casted to double) assigned to c by constraint x multiplied by a, plus the violation assigned to c by y multiplied by b. In short:
linearCombination(c)= a*x(c) + b*y(c)
.

Parameters:
name - The name of the constraint to be constructed.
x - First addend.
a - First coefficient.
y - Second addend.
b - Second coefficient.
Returns:
A constraint that is the linear combination of x and y, with factors a and b respectively.

condition

public static Constraint condition(java.lang.String name,
                                   Constraint cond,
                                   Constraint pos,
                                   Constraint zero,
                                   Constraint neg)
Returns a conditional constraint. The constraint first evaluates candidate c for constraint cond, which must return a violation with a number (a float or an integer) as the value of the violation. The constraint constructed returns pos.value(c), zero.value(c) or neg.value(c) depending on the sign of cond.value(c).

Parameters:
name - Name of the constraint being built.
cond - Constraint implementing the condition.
pos - Constraint determining the return value if the condition gives a positive value.
zero - Constraint determining the return value if the condition gives a zero value.
neg - Constraint determining the return value if the condition gives a negative value.
Returns:
A constraint implementing a condition, with name name.

condition

public static Constraint condition(java.lang.String name,
                                   Constraint cond,
                                   Constraint tru,
                                   Constraint fal)
Returns a conditional constraint. The constraint first evaluates candidate c for constraint cond, which must return a violation with a boolean as the value of the violation. The constraint constructed returns tru.value(c), or fal.value(c) depending on the truth value of cond.value(c).

Parameters:
name - Name of the constraint being built.
cond - Constraint implementing the condition.
tru - Constraint determining the return value if the condition gives true.
fal - Constraint determining the return value if the condition gives false.
Returns:
A constraint implementing a condition, with name name.