babase.ranker.ranking
Class RankingConstraintMultiset

java.lang.Object
  extended by babase.ranker.ranking.RankingConstraintMultiset
All Implemented Interfaces:
RankingToBeChangedListener

public class RankingConstraintMultiset
extends java.lang.Object
implements RankingToBeChangedListener

A multiset of constraints on rankings, each of the form: individual i must be ranked above individual j. Individuals are identified by integers from 0 to n-1, where n is the total number of individuals. This multiset of constaints watches changes made to a Ranking object, and raises exceptions whenever a constraint is violated. The same constraint can appear multiple times in the multiset; the constraint will be enforced as long as one copy exists,

Author:
junyang

Field Summary
protected  int[][] _constraints
          The multiset of constraints, encoded as follows: There is a contraint dictating that individual i must be ranked above j if and only _constraints[i][j] is positive.
protected  Ranking _ranks
          The ranking to which this multiset of constraints applies.
 
Constructor Summary
RankingConstraintMultiset(Ranking ranks)
          Constructs an empty multiset of ranking constraints for the given ranking.
 
Method Summary
(package private)  void addConstraint(int higher, int lower)
          Add to the multiset (one instance of) a constriant that individual higher must be ranked above individual lower.
(package private)  void addConstraints(int[] sequence)
          Add a sequence of constraints (one instance of each constraint) to the multiset.
protected  boolean depthFirstTraversal(int src, int dst, boolean[] visited)
          Perform a depth-first traversal on the directed graph defined by _constraints starting with node src.
 boolean isConstraintImplied(int higher, int lower)
           
 void rankingToBeChanged(RankingChangeEvent e)
          This method will be invoked on the listerner object immediately before the Ranking object being listened to changes.
(package private)  void removeConstraint(int higher, int lower)
          Remove from the multiset (just one instance of) a constriant that individual higher must be ranked above individual lower.
(package private)  void removeConstraints(int[] sequence)
          Remove a sequence of constraints (one instance of each constraint) from the multiset.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_ranks

protected final Ranking _ranks
The ranking to which this multiset of constraints applies.


_constraints

protected int[][] _constraints
The multiset of constraints, encoded as follows: There is a contraint dictating that individual i must be ranked above j if and only _constraints[i][j] is positive. The actual value of _constraints[i][j] counts the number of times this constraint appears in the multiset.

Constructor Detail

RankingConstraintMultiset

public RankingConstraintMultiset(Ranking ranks)
Constructs an empty multiset of ranking constraints for the given ranking.

Parameters:
ranks -
Method Detail

isConstraintImplied

public boolean isConstraintImplied(int higher,
                                   int lower)
Parameters:
higher -
lower -
Returns:
Whether the constraint that higher is ranked higher than lower follows from the constraints in the multiset.

depthFirstTraversal

protected boolean depthFirstTraversal(int src,
                                      int dst,
                                      boolean[] visited)
Perform a depth-first traversal on the directed graph defined by _constraints starting with node src. There is a directed edge from node i to node j if and only if _constraints[i][j] > 0.

Parameters:
src - The source node where the traversal starts.
dst - The destination node, which, once encountered, will terminate the traversal and return true. Use the special value of -1 to ensure that the traversal covers all reachable nodes.
visited - If visited[i] is already true, then node i will not be visited again. On the other hand, if the procedure visits node i, visited[i] will be set to true.
Returns:
As soon as dst is encountered, the traversal stops and returns true. If all nodes reachable from src have been traversed and dst is not one of them, return false.

addConstraint

void addConstraint(int higher,
                   int lower)
             throws RankingConstraintException.Inconsistency
Add to the multiset (one instance of) a constriant that individual higher must be ranked above individual lower.

Parameters:
higher -
lower -
Throws:
RankingConstraintException.Inconsistency - Thrown if the contraint to be added does not make sense or is inconsistent with existing ones in the multiset (i.e., they already imply that lower is ranked above higher .

removeConstraint

void removeConstraint(int higher,
                      int lower)
Remove from the multiset (just one instance of) a constriant that individual higher must be ranked above individual lower. This method silently ignores the case where the constraint to be removed does not exist in the multiset.

Parameters:
higher -
lower -

addConstraints

void addConstraints(int[] sequence)
              throws RankingConstraintException.Inconsistency
Add a sequence of constraints (one instance of each constraint) to the multiset. The operation is atomic in the sense that if an exception is thrown due to inconsistency, none of the constraints in the sequence will be added.

Parameters:
sequence - An array specifying a sequence of constraints: individual sequence[0] must be ranked higher than sequence[1], which must be ranked higher than sequence[2], etc.
Throws:
RankingConstraintException.Inconsistency

removeConstraints

void removeConstraints(int[] sequence)
Remove a sequence of constraints (one instance of each constraint) from the multiset.

Parameters:
sequence - An array specifying a sequence of constraints: individual sequence[0] must be ranked higher than sequence[1], which must be ranked higher than sequence[2], etc.

rankingToBeChanged

public void rankingToBeChanged(RankingChangeEvent e)
                        throws java.lang.Exception
Description copied from interface: RankingToBeChangedListener
This method will be invoked on the listerner object immediately before the Ranking object being listened to changes.

Specified by:
rankingToBeChanged in interface RankingToBeChangedListener
Throws:
java.lang.Exception