Prolog Cafe v1.2

jp.ac.kobe_u.cs.prolog.lang
Class PrologControl

java.lang.Object
  extended by jp.ac.kobe_u.cs.prolog.lang.PrologControl
All Implemented Interfaces:
java.io.Serializable, java.lang.Runnable

public class PrologControl
extends java.lang.Object
implements java.lang.Runnable, java.io.Serializable

Prolog thread.
The PrologControl class is an implementation of Prolog Box Control Flow Model.
This PrologControl provides methods for both sequential and parallel execution.

 // An example of sequential execution
 // calls a goal father(abraham, X) and get all solutions.
 PrologControl p = new PrologControl();
 Predicate code = new PRED_father_2();
 Term a1 = SymbolTerm.makeSymbol("abraham");
 Term a2 = new VariableTerm();
 Term[] args = {a1, a2};
 p.setPredicate(code, args);
 for (boolean r = p.call(); r; r = p.redo()) {
     System.out.println(a2.toString());
 }
 
 // To get only one solution.
 PrologControl p = new PrologControl();
 Predicate code = new PRED_father_2();
 Term a1 = SymbolTerm.makeSymbol("abraham");
 Term a2 = new VariableTerm();
 Term[] args = {a1, a2};
 if (p.execute(code, args))
     System.out.println(a2.toString());
 else 
     System.out.println("fail");
 
 // An example of parallel execution
 // calls queens(4,X) and queens(8,Y) in parallel.
 // Usage:
 //   % plcafe -cp queens.jar T
 // 
 import jp.ac.kobe_u.cs.prolog.lang.*;
 public class T {
     public static void main(String args[]) {
     long t = System.currentTimeMillis();
     boolean r1 = true;
     boolean r2 = true;
     Term a1[] = {new IntegerTerm(4), new VariableTerm()};
     Term a2[] = {new IntegerTerm(8), new VariableTerm()};

     PrologControl e1 = new PrologControl();
     PrologControl e2 = new PrologControl();
     Term v1 = new VariableTerm();
     Term v2 = new VariableTerm();
     e1.setPredicate(new PRED_queens_2(), a1);
     e2.setPredicate(new PRED_queens_2(), a2);
     System.out.println("Start");
     e1.start();
     e2.start();
     while (r1 || r2) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {}
            if (r1 && e1.ready()) {
                r1 = e1.next();
                if (r1) {
                    System.out.println("Success1 = " + a1[1]);
                    e1.cont();
                } else {
                    System.out.println("Fail1");
                }
            } else if (r2 && e2.ready()) {
                r2 = e2.next();
                if (r2) {
                    System.out.println("Success2 = " + a2[1]);
                    e2.cont();
                } else {
                    System.out.println("Fail2");
                }
            } else {
                System.out.println("Waiting");
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {}
            }
        }
        System.out.println("End");
        long t1 = System.currentTimeMillis();
        long t2 = t1 - t;
        System.out.println("time = " + t2 + "msec.");
    }
 }
 

See Also:
Serialized Form

Field Summary
 Predicate code
          Holds a Prolog goal to be executed.
 Prolog engine
          Holds a Prolog engine.
 boolean result
          A flag that indicates whether the result of goal is true or false.
 boolean resultReady
          A flag that indicates whether the result of goal is ready or not.
 java.lang.Thread thread
          A volatile instance variable holding a thread.
 
Constructor Summary
PrologControl()
          Constructs a new PrologControl.
 
Method Summary
 boolean call()
          Returns true if the system succeeds to find a first solution of the goal, false otherwise.
 void cont()
          Forces the thread to continue the execution.
 boolean execute(Predicate p, Term[] args)
          Returns true if the system succeeds to find a first solution of the given goal, false otherwise.
protected  void fail()
          Is invoked after failure of all trials.
 boolean in_failure()
          Returns true if the result of goal is ready and false, otherwise false.
 boolean in_success()
          Returns true if the result of goal is ready and true, otherwise false.
 void join()
          Waits for this thread to die.
 boolean next()
          Wait until the system finds a next solution, and then returns the result as boolean.
 boolean ready()
          Returns true if the result of goal is ready, false otherwise.
 boolean redo()
          Returns true if the system succeeds to find a next solution of the goal, false otherwise.
 void run()
          Executes the goal.
 void setPredicate(Predicate p, Term[] args)
          Sets a goal and its arguments to this Prolog thread.
 void setPredicate(Term t)
          Sets a goal call(t) to this Prolog thread.
 void start()
          Forces the thread to start the execution.
 void stop()
          Forces the thread to stop.
protected  void success()
          Is invoked when the system succeeds to find a solution.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

thread

public volatile java.lang.Thread thread
A volatile instance variable holding a thread.


engine

public Prolog engine
Holds a Prolog engine.


code

public Predicate code
Holds a Prolog goal to be executed.


result

public boolean result
A flag that indicates whether the result of goal is true or false.


resultReady

public boolean resultReady
A flag that indicates whether the result of goal is ready or not.

Constructor Detail

PrologControl

public PrologControl()
Constructs a new PrologControl.

Method Detail

setPredicate

public void setPredicate(Predicate p,
                         Term[] args)
Sets a goal and its arguments to this Prolog thread. An initial continuation goal (a Success object) is set to the cont field of goal p as continuation.


setPredicate

public void setPredicate(Term t)
Sets a goal call(t) to this Prolog thread. An initial continuation goal (a Success object) is set to the cont field of call(t) as continuation.


execute

public boolean execute(Predicate p,
                       Term[] args)
Returns true if the system succeeds to find a first solution of the given goal, false otherwise.
This method is useful to find only one solution.
This method first initilizes the Prolog engine by invoking engine.init(), allocates a new Thread object, and start the execution of the given goal. And then it stops the thread and returns true if the goal succeeds, false otherwise.

See Also:
run()

call

public boolean call()
Returns true if the system succeeds to find a first solution of the goal, false otherwise.
This method first invokes the start() method that initilizes the Prolog engine, allocates a new Thread object, and start the execution. And then it returns the boolean whose value is next().

See Also:
start(), next()

redo

public boolean redo()
Returns true if the system succeeds to find a next solution of the goal, false otherwise.
This method first invokes the cont() method that sets the resultReady to false and wakes up all threads that are waiting on this object's monitor. And then it returns the boolean whose value is next().

See Also:
cont(), next()

success

protected void success()
Is invoked when the system succeeds to find a solution.
This method is invoked from the initial continuation goal (a Success object).
This method first sets the resultReady and result to true. And then it wakes up all threads that are waiting by notifyAll(). Finally, while the thread is not null and the resultReady is true, it waits until another thread invokes the notify() method or the notifyAll() method for this object.

See Also:
resultReady, result, thread

fail

protected void fail()
Is invoked after failure of all trials.
This method is invoked from the run method.
This method first sets the resultReady and result to true and false respectively. And then it wakes up all threads that are waiting by notifyAll(). Finally, while the thread is not null and the resultReady is true, it waits until another thread invokes the notify() method or the notifyAll() method for this object.

See Also:
resultReady, result, thread

join

public void join()
Waits for this thread to die.


stop

public void stop()
Forces the thread to stop.
This method first sets the resultReady and thread to false and null respectively. And then it wakes up all threads that are waiting by notifyAll().

See Also:
resultReady, thread

start

public void start()
Forces the thread to start the execution.
This method initilizes the Prolog engine by invoking engine.init(), allocates a new Thread object, and start the execution. The Java Virtual Machine calls the run method of this thread.

See Also:
run()

cont

public void cont()
Forces the thread to continue the execution.
This method sets the resultReady to false, and then wakes up all threads that are waiting by notifyAll().

See Also:
resultReady

ready

public boolean ready()
Returns true if the result of goal is ready, false otherwise.

Returns:
a boolean whose value is resultReady.
See Also:
resultReady

in_success

public boolean in_success()
Returns true if the result of goal is ready and true, otherwise false.

Returns:
a boolean whose value is (ready() && result).
See Also:
ready(), result

in_failure

public boolean in_failure()
Returns true if the result of goal is ready and false, otherwise false.

Returns:
a boolean whose value is (ready() && !result).
See Also:
ready(), result

next

public boolean next()
Wait until the system finds a next solution, and then returns the result as boolean.
This method first waits until another thread invokes the notify() method or the notifyAll() method for this object, while the thread is not null and the resultReady is false. And then invokes the stop() if the result is false. Finally, returns the result.

See Also:
resultReady, result, thread

run

public void run()
Executes the goal.
Every time finding a solution, the success method is invoked. And then the fail method is invoked after failure of all trials. Finally, the stop method is invoked at the end of this run.

Specified by:
run in interface java.lang.Runnable
See Also:
success(), fail(), stop()

Prolog Cafe v1.2

Copyright (C) 1997-2008 M.BANBARA and N.TAMURA