hu.birot.OTKit.dataType.violation
Class Violation

java.lang.Object
  extended by hu.birot.OTKit.dataType.violation.Violation
Direct Known Subclasses:
BooleanViolation, FloatViolation, IntegerViolation

public abstract class Violation
extends java.lang.Object

Realizes a violation instance: the level at which a certain candidate violates a certain constraint (none, once, more times..., and variations of this theme).

This class is abstract. Its most frequently used implementations are IntegerViolation (corresponding to the "number of stars" in a tableau), and FloatViolation (allowing any real number of "stars").

In general, a constraint is a function mapping candidates unto violations. The range of such a function is the set of objects that are constructed using a particular implementation of the abstract class Violation. For instance, most constraints in linguistics map onto the set of non-negative integers, for which the class IntegerViolation can be used (although the latter also allows negative integers). Yet, the range of constraint HNuc in the analysis of Imdlawn Tashlhiyt Berber syllabification by Prince and Smolensky (1993/2002/2004) is a phoneme. What matters is that the range be ordered: two stars is better than four stars, and [l] is more sonorous than [t].

A number of methods are abstract:

  1. The method value() returns the value of the violation: in general, an Object.
  2. The method betterEqual(Violation v) defines the order relation on the range of the constraint. For integer or real (float) values, this relation is simply the arithmetic "less than or equal to" relation. In an implementation of the Berber syllabification, one needs to implement here the sonority scale. In general, this method allows using ordinal scales as the range of the constraints.
  3. The method difference(Violation v) defines the difference this-v on pairs of violation as real numbers (doubles). It is used, for instance, in simulated annealing and in comparing candidates in Harmonic Grammars. This method allows generalizing these inherently "floating point-based" models to constraints with "affine-like" ranges (corresponding to an "interval scale" in statistics). It is expected that v1.betterEqual(v2) if and only if v1.difference(v2) <= 0; that is informally, v1 is a violation better than or equivalent to violation v2, if and only if "v1 - v2" <= 0.
  4. The method equals(Object o) overrides the general equals method. Not necessarily, but usually, violations v1 and v2 are equal for this method (v1.equals(v2)), if and only if v1.betterEqual(v2) && v2.betterEqual(v1), that is, if and only if Violation.isEqual(v1, v2) == true.
  5. The method clone() overrides the general clone() method. For practical reasons, its implementation is required.
  6. The method toString() overrides the general toString() method. For practical reasons, its implementation is required.

An important convention is that although the value method of the class Constraint returns a Violation object in general, nevertheless a specific constraint must always return instances of the same subclass of Violation (for instance, always IntegerViolation, or always FloatViolation). As explained above, the range of a constraint is the set defined by a specific implementation of the abstract class Violation.

Consequently, when implementing the abstract methods betterEqual(Violation v) and difference(Violation v), you can suppose that v belongs to the same subclass. In theory, violations assigned by two different constraints are never compared, and their difference should be never used.

Biro (2006:76) requires that the set of violations assigned by a constraint to the elements of a candidate set be a well-ordered set (a totally ordered set in which any subset has a lower bound contained by that subset). Note that the subclasses IntegerViolation and FloatViolation correspond to the set of integer numbers and (vaguely speaking) real numbers, respectively, which are not well-ordered. Yet, constraints usually employ only a subset of these sets: for instance, the non-negative integers only.

In Harmonic Grammar, it is usually supposed that the violations must be real numbers. Yet, for comparing two candidates (and hence, to define the best candidate in a well-ordered set), it is enough to have violation with an "interval scale". In other words, each constraint C must have an "affine-like" range: the violations need not be real numbers, but the difference of two violations (assigned by the same constraint to any candidate) must be a real number. Namely, the comparison of two candidates depends only on the sign of the weighted sum of these differences.

Here is an example of how to implement this class:

public class BooleanViolation extends Violation {
private boolean b;
public BooleanViolation(boolean v) {
   b = v;
}
public Boolean value() {
   return b;
}
public BooleanViolation clone() {
   return new BooleanViolation(this.b);
}
public boolean betterEqual(Violation v) {
   if (!(v instanceof BooleanViolation)) return false;
   return this.b || !((Boolean) v.value());
}
public double difference(Violation v) {
   boolean b2 = (Boolean) v.value() ;
   if (this.b == b2) return 0;
   else if (this.b && !b2) return -1; // that is, this.b == true && b2 == false
   else return 1; // !this.b && b2; that is, this.b == false && b2 == true
}
public boolean equals(Object o) {
   if (this == o) {
      return true;
   }
   else if (o!= null && (o instanceof BooleanViolation) ) {
      return ((BooleanViolation) o).b == this.b;
   }
   else {
      return false;
   }
}
public String toString() {
   return ""+this.b;
}
}

See Also:
FloatViolation, IntegerViolation, BooleanViolation, Constraint

Field Summary
protected  java.lang.Class<? extends Violation> thisclass
           
 
Constructor Summary
Violation()
           
 
Method Summary
abstract  boolean betterEqual(Violation v)
          This method compares the present Violation to Violation v, and returns true if and only if this violation is better than or equal to v.
abstract  Violation clone()
          Creates a clone of the Violation: construct a new violation with the same value.
abstract  double difference(Violation v)
          This method returns the difference of this Violation and of Violation v (in this order: "this-v").
 boolean equals(java.lang.Object o)
          This method overrides java.lang.Object.equals.
 int hashCode()
          A hash code corresponding to the Violation.
static boolean isBetter(Violation v1, Violation v2)
          Returns true if and only if v1 is better than v2.
static boolean isEqual(Violation v1, Violation v2)
          Returns true if and only if v1 is equal to v2.
static boolean notComparable(Violation v1, Violation v2)
          Returns true if and only if v1 and v2 are not comparable.
abstract  java.lang.String toString()
          Transforms a Violation into a String, overriding the method java.lang.Object.toString.
abstract  java.lang.Object value()
          Return the value of the violation.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

thisclass

protected final java.lang.Class<? extends Violation> thisclass
Constructor Detail

Violation

public Violation()
Method Detail

value

public abstract java.lang.Object value()

Return the value of the violation. In general, that is an Object. For specific subclasses, the return value is usually something more concrete, for instance Integer (for the implementation IntegerViolation) or Double (for the implementation FloatViolation).

Returns:
the value of the violation.
See Also:
IntegerViolation.value(), FloatViolation.value()

betterEqual

public abstract boolean betterEqual(Violation v)

This method compares the present Violation to Violation v, and returns true if and only if this violation is better than or equal to v.

Value false must be returned, whenever v belongs to a different subclass of Violation.

In the case of real-valued violations, "better than" means "less than". In the case of non-real valued violations, "equals to" can be replaced by "equivalent to" (which allows for different but equivalent values).

All the other comparison methods are derived from this method:

isEqual (v1, v2) = v1.betterEqual(v2) && v2.betterEqual(v1)
isBetter (v1, v2) = v1.betterEqual(v2) && !v2.betterEqual(v1)
notComparable (v1, v2) = !( v1.betterEqual(v2) || v2.betterEqual(v1) )

Parameters:
v - : the second violation.
Returns:
true if this violation is better than or equal to v; otherwise false.

isBetter

public static boolean isBetter(Violation v1,
                               Violation v2)
Returns true if and only if v1 is better than v2.
isBetter (v1, v2) = v1.betterEqual(v2) && !v2.betterEqual(v1)

Parameters:
v1 - First violation to be compared.
v2 - Second violation to be compared
Returns:
whether v1 is better than v2, as a function of the betterEqual(Violation) method of the two violations.
See Also:
betterEqual(Violation)

isEqual

public static boolean isEqual(Violation v1,
                              Violation v2)

Returns true if and only if v1 is equal to v2.

If v1 == v2, then returns true, to speed up the computations. (That is why one is encouraged to use the static method v(value) defined in the subclasses of the Violation class.)

Otherwise, the implementation of the abstract method betterEqual(Violation) is used in the following way:

isEqual (v1, v2) = v1.betterEqual(v2) && v2.betterEqual(v1)

Parameters:
v1 - First violation to be compared.
v2 - Second violation to be compared
Returns:
whether v1 is equal to v2, as a function of the betterEqual(Violation) method of the two violations. Always true if the two parameters refer to the same object.
See Also:
betterEqual(Violation)

notComparable

public static boolean notComparable(Violation v1,
                                    Violation v2)
Returns true if and only if v1 and v2 are not comparable.
notComparable (v1, v2) = !( v1.betterEqual(v2) || v2.betterEqual(v1) )

Parameters:
v1 - First violation to be compared.
v2 - Second violation to be compared
Returns:
whether v1 cannot be compared to v2, as a function of the betterEqual(Violation) method of the two violations.
See Also:
betterEqual(Violation)

difference

public abstract double difference(Violation v)

This method returns the difference of this Violation and of Violation v (in this order: "this-v").

In the case of IntegerViolations of FloatViolations, the difference is simply this.value() - v.value(), on condition that they belong to the same subclass of Violation.

Usually, v1.difference(v2) < 0 if and only if isBetter(v1,v2). Similarly, v1.difference(v2) == 0 often means isEqual(v1,v2). For more explanation, see the introduction to the class Violation.

The static method difference(v1,v2) is defined as v1.difference(v2).

Parameters:
v - The subtrahend violation.
Returns:
The difference of the two violations (this – v).

equals

public boolean equals(java.lang.Object o)
This method overrides java.lang.Object.equals. Two violations are equal, if and only if (1) they belong to exactly the same subclasses, and (2) their values are equal. It may be useful to override this class.

Overrides:
equals in class java.lang.Object

clone

public abstract Violation clone()
Creates a clone of the Violation: construct a new violation with the same value. This method overrides java.lang.Object.clone.

Overrides:
clone in class java.lang.Object

toString

public abstract java.lang.String toString()

Transforms a Violation into a String, overriding the method java.lang.Object.toString.

Overrides:
toString in class java.lang.Object
Returns:
A string representation of the Violation.

hashCode

public int hashCode()

A hash code corresponding to the Violation. It is simply the hash code calculated from the output of the toString() method.

If you use this method frequently, you may want to override it. For instance, if the value is always an integer, you may simply return the value.

Overrides:
hashCode in class java.lang.Object
Returns:
The hash code corresponding to the Violation