net.sf.jauvm.util
Class Ambivalence<T>

java.lang.Object
  extended by net.sf.jauvm.util.Ambivalence<T>

public final class Ambivalence<T>
extends Object

A class to facilitate non-deterministic programing with back-tracking.

This class helps in implementing non-deterministic programing algorithms doing a deep-first search on problem space solutions.


Nested Class Summary
 class Ambivalence.NoSolutionException
          Thrown to indicate that there are no solutions in this Ambivalence object's problem space.
 
Constructor Summary
Ambivalence()
           
 
Method Summary
 T choose()
          Indicates there is no choice of values to return at this point.
 T choose(T... values)
          Ambiguously chooses one of its arguments to return.
 T choose(T value)
          Indicates there is only one possible value to return at this point.
 void cover(Runnable runnable)
          Covers all solutions defined by a Runnable object's run() method.
 void fail()
          Causes the computation to fail at this point.
 void require(boolean condition)
          Requires a certain condition to be true failing otherwise.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Ambivalence

public Ambivalence()
Method Detail

choose

public T choose()
Indicates there is no choice of values to return at this point.

This is the same as calling fail() with a return type. It never returns, though.

Throws:
Ambivalence.NoSolutionException - if there are no alternative choices in the problem space to back-track to.
See Also:
fail()

choose

public T choose(T value)
Indicates there is only one possible value to return at this point.

Allways returns value. The identity function.

Parameters:
value - the value to return
Returns:
value

choose

public T choose(T... values)
Ambiguously chooses one of its arguments to return.

Invoked with one argument this is the same as calling choose(T value); invoked with no arguments, its the same as choose(), or a fail() with a return type.

Parameters:
values - the values from which to choose from
Returns:
the chosen value
Throws:
Ambivalence.NoSolutionException - if no choice provides a solution and there are no alternative choices in the problem space to back-track to.

fail

public void fail()
Causes the computation to fail at this point.

Calling fail forces this Ambivalence object to back-track to previous choice points and try different values for those.

Throws:
Ambivalence.NoSolutionException - if there are no alternative choices in the problem space to back-track to.

require

public void require(boolean condition)
Requires a certain condition to be true failing otherwise.

Throws:
Ambivalence.NoSolutionException - if there are no more sollutions in the problem space to try

cover

public void cover(Runnable runnable)
Covers all solutions defined by a Runnable object's run() method.

This method takes a Runnable object whose run() method defines a problem space with this Ambivalence object. This method will run that run() method till it's normal termination for every solution defined in the problem space.