Thanks Ernest!

Please pardon my ignorace, but I am having some
difficulty with runUntilHalt(). When I execute the
code with 'run()' it works, but when I use
'runUntilHalt()' it does not work. Here's the code:

=====================================================
CurrentState.java
=====================================================
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

public class CurrentState {

        public static final int OFFLINE               
                 = 0;
        public static final int HANDLE_UP             
                 = 1;
        public static final int HANDLE_DOWN           
                 = 2;
        private static final int MAX_STATE_ID         
                 = 3;

        private int currentState;
        private int previousState;
        private PropertyChangeSupport pcs;


        public CurrentState() {
                pcs = new PropertyChangeSupport(this);
                currentState = HANDLE_DOWN;
                previousState = OFFLINE;
        }

        public void setCurrentState(int _state) {
                System.out.println("CurrentState:
setCurrentState: passed integer is " + _state);
                if ((_state > 0) && (_state <=
MAX_STATE_ID)) {
                        previousState = currentState;
                        currentState = _state;
                       
pcs.firePropertyChange("CurrentState", new
Integer(previousState), new Integer(currentState));
                }

        }

        public int getCurrentState() {
                return currentState;
        }

        public void
addPropertyChangeListener(PropertyChangeListener _l) {
                pcs.addPropertyChangeListener(_l);
        }

        public void
removePropertyChangeListener(PropertyChangeListener
_l) {
                pcs.removePropertyChangeListener(_l);
        }
}

=====================================================
testJess.java
=====================================================
import jess.*;
import java.util.*;

public class testJess
{
  private Rete rete;
  private Context c;

  public testJess() {
        rete = new Rete();
        c = rete.getGlobalContext();
  }

  public static void main(String[] argv) {
        testJess tj = new testJess();
        tj.test();
  }

  public void test() {
    try {


        // Read in packages
        rete.addUserpackage(new PredFunctions());
        rete.addUserpackage(new MiscFunctions());


        // Read files
        System.out.println("Reading batch files:
scriptlib.clp, test.clp.");
        rete.executeCommand("(batch scriptlib.clp)");
        rete.executeCommand("(batch test.clp)");
        rete.reset();

        // Launch a separate thread that runs forever
until the halt is encountered
/*
        Thread thr = new Thread() {
                public void run() {
                        this.yield();
                        try {
                               
testJess.this.rete.runUntilHalt();
                        } catch (JessException e) {
                                e.printStackTrace();
                        }
                }
        };
        thr.start();
        System.out.println("Thread started with
(run-until-halt)");
*/

        // Assert handle-up
        System.out.println("Asserting fact
'handle-up'...");
        Fact f = ((Value) rete.executeCommand("(assert
(handle-up))")).factValue(c);
        System.out.println("Facts after the asserting
'handle-up'");
        listFacts(rete);

        rete.executeCommand("(run)");

        // Get Current State
        CurrentState cs = (CurrentState)
rete.getGlobalContext().getVariable("*cState*").externalAddressValue(c);
        System.out.println("Retrieved CurrentState
object from JESS, current state = " +
cs.getCurrentState());
        listFacts(rete);

        // Retract handle-up
        int id = f.getFactId();
        System.out.println("Retracting 'handle-up'");
        rete.executeCommand("(retract (fact-id " + id
+ "))");
        System.out.println("Facts after the retract");
        listFacts(rete);

        // Set current state to 1
        cs.setCurrentState(1);
        System.out.println("Set currentState = 1 from
the reference to the Java object");

        // Assert handle-up again
        System.out.println("Asserting fact
'handle-up'...");
        Fact fact = ((Value)
rete.executeCommand("(assert
(handle-up))")).factValue(c);
        System.out.println("Facts after the asserting
'handle-up'");
        listFacts(rete);

        rete.executeCommand("(run)");

        // Get Current State again
        cs = (CurrentState)
rete.getGlobalContext().getVariable("*cState*").externalAddressValue(c);
        System.out.println("Retrieved CurrentState
object from JESS, current state = " +
cs.getCurrentState());
        listFacts(rete);

        // Retract handle-up again
        int fid = fact.getFactId();
        System.out.println("Retracting 'handle-up'");
        rete.executeCommand("(retract (fact-id " + fid
+ "))");
        System.out.println("Facts after the retract");
        listFacts(rete);

        if (1 == 1) {
                System.out.println("");
                System.out.println("Issuing
'stop-exec'");
                rete.executeCommand("(assert
(stop-exec))");
        }


      } catch (JessException re) {
        re.printStackTrace();
      } catch (Exception e) {
        e.printStackTrace();
      }

  }

        public static void listFacts(Rete _rete) {
                System.out.println("");
                System.out.println("Listing
facts...");
                for (Enumeration e =
_rete.listFacts(); e.hasMoreElements();) {
                        Object t = e.nextElement();
                       
System.out.println(t.toString());
                }
        }
}

=====================================================
test.clp
=====================================================
(watch all)

(set-reset-globals nil)
(defglobal ?*cState* = (new CurrentState))


(defrule handle-up-rule
(handle-up)
=>
(printout t "Executing handle-up-rule..." crlf)
(call ?*cState* setCurrentState 2)
)

(defrule end-exec
(stop-exec)
=>
(halt)
)

=====================================================
Output
=====================================================
Reading batch files: scriptlib.clp, test.clp.
handle-up-rule: +1+1+1+t
end-exec: +1+1+1+t
 ==> f-0 (initial-fact)
Asserting fact 'handle-up'...
 ==> f-1 (handle-up)
==> Activation: handle-up-rule :  f-1
Facts after the asserting 'handle-up'

Listing facts...
(initial-fact)
(handle-up)
FIRE 1 handle-up-rule f-1
Executing handle-up-rule...
CurrentState: setCurrentState: passed integer is 2
Retrieved CurrentState object from JESS, current state
= 2

Listing facts...
(initial-fact)
(handle-up)
Retracting 'handle-up'
 <== f-1 (handle-up)
Facts after the retract

Listing facts...
(initial-fact)
CurrentState: setCurrentState: passed integer is 1
Set currentState = 1 from the reference to the Java
object
Asserting fact 'handle-up'...
 ==> f-2 (handle-up)
==> Activation: handle-up-rule :  f-2
Facts after the asserting 'handle-up'

Listing facts...
(initial-fact)
(handle-up)
FIRE 1 handle-up-rule f-2
Executing handle-up-rule...
CurrentState: setCurrentState: passed integer is 2
Retrieved CurrentState object from JESS, current state
= 2

Listing facts...
(initial-fact)
(handle-up)
Retracting 'handle-up'
 <== f-2 (handle-up)
Facts after the retract

Listing facts...
(initial-fact)

Issuing 'stop-exec'
 ==> f-3 (stop-exec)
==> Activation: end-exec :  f-3

=====================================================
My Question:
=====================================================
Why does this code not work when I replace the
multiple 'run()' commands with the currently commented
out section that starts a separate Thread. This Thread
calls the 'runUntilHalt()' function. When run with the
separate Thread, the 'CurrentState' never changes (see
setCurrentState() method). However, it does execute
the 'end-exec' rule! (??)

=====================================================
Output with Thread (runUntilHalt()) instead of 
multiple run() commands
=====================================================
Reading batch files: scriptlib.clp, test.clp.
handle-up-rule: +1+1+1+t
end-exec: +1+1+1+t
 ==> f-0 (initial-fact)
Thread started with (run-until-halt)
Asserting fact 'handle-up'...
 ==> f-1 (handle-up)
==> Activation: handle-up-rule :  f-1
Facts after the asserting 'handle-up'

Listing facts...
(initial-fact)
(handle-up)
Retrieved CurrentState object from JESS, current state
= 3

Listing facts...
(initial-fact)
FIRE 1 handle-up-rule f-1
(handle-up)
Executing handle-up-rule...
CurrentState: setCurrentState: passed integer is 2
Retracting 'handle-up'
 <== f-1 (handle-up)
<== Activation: handle-up-rule :  f-1
Facts after the retract

Listing facts...
(initial-fact)
CurrentState: setCurrentState: passed integer is 1
Set currentState = 1 from the reference to the Java
object
Asserting fact 'handle-up'...
 ==> f-2 (handle-up)
==> Activation: handle-up-rule :  f-2
Facts after the asserting 'handle-up'

Listing facts...
(initial-fact)
(handle-up)
Retrieved CurrentState object from JESS, current state
= 1

Listing facts...
(initial-fact)
(handle-up)
Retracting 'handle-up'
 <== f-2 (handle-up)
<== Activation: handle-up-rule :  f-2
Facts after the retract

Listing facts...
(initial-fact)

Issuing 'stop-exec'
 ==> f-3 (stop-exec)
==> Activation: end-exec :  f-3
FIRE 2 end-exec f-3

=====================================================
Any help is greatly appreciated!!!!!!!!!!!!!!!!

Thanks,
- Ashish

--- friedman_hill ernest j
<[EMAIL PROTECTED]> wrote:
> I think Ashish Majmundar wrote:
> > Hi,
> > 
> > 
> >         Rete rete = new Rete();
> >         Context c = rete.getGlobalContext();
> >         rete.executeCommand("(reset)");
> ....
> >         rete.reset();
> 
> 
> These last two calls have -exactly- the same effect,
> but the first one
> is inefficient.
> 
> >         rete.executeCommand("(batch test.clp)");
> ....
> >         rete.executeCommand("(assert
> (run-until-halt))");
> 
> This is the problem.
> 
> 1) Here you've confused executing a function with
> asserting a
> fact. (run-until-halt) is a function; what you've
> done here is assert
> a fact (run-until-halt) which, despite the similar
> apeparance, is
> absolutely unrelated to what you want to do.
> 
> 2) The function you meant to call, (run-until-halt),
> does not return
> until (halt) is called - i.e., the below code will
> never be
> executed. If you want to call (run-until-halt) and
> then make more Java
> calls from the same method, you need to make the
> (run-until-halt) call
> in a separate Thread.
> 
> 
> >
> ===================================================
> > test.clp
> >
> ===================================================
> > (set-reset-globals nil)
> 
> You've got no global variables in this program, so
> this has no effect.
> 
> > My question is: why doesn't the 'Current State
> after
> > assert on handle-up' say 'handle-up'?
> > 
> > Thanks in advance.
> 
> 
> The rest of your code is correct, as far as I can
> see.  It generally
> helps when developing this sort of code to 1) Get
> the Jess code
> working by itself, first, interactively. Only when
> it does what you
> expect should you then embed it in a Java
> application. 2) use the Jess
> command (watch all) so that the engine tells you
> what it's doing; this
> would have let you know that your rules were never
> firing because no
> form of (run) was being called.
> 
> 
> 
> 
>
---------------------------------------------------------
> Ernest Friedman-Hill  
> Distributed Systems Research        Phone: (925)
> 294-2154
> Sandia National Labs                FAX:   (925)
> 294-2234
> Org. 8920, MS 9012                 
> [EMAIL PROTECTED]
> PO Box 969                 
> http://herzberg.ca.sandia.gov
> Livermore, CA 94550


__________________________________________________
Do You Yahoo!?
Send instant messages with Yahoo! Messenger.
http://im.yahoo.com/
---------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users [EMAIL PROTECTED]'
in the BODY of a message to [EMAIL PROTECTED], NOT to the
list (use your own address!) List problems? Notify [EMAIL PROTECTED]
---------------------------------------------------------------------

Reply via email to