The following code installs the JessListener
interface and sets the event mask. 


import jess.*;

public class try8
{
    public static void main(String args[])
         {
         try
            {
             Rete r=new Rete();
             r.addJessListener(new
ExMyEventHandler());
             r.setEventMask(r.getEventMask() |
JessEvent.DEFRULE_FIRED | JessEvent.CLEAR );  
             r.executeCommand("(clear)");
             r.executeCommand("(deftemplate node (slot
name) (slot type) (slot question) (slot yes-node)
(slot no-node) (slot answer))");                      
               
             r.executeCommand("(defrule initialize-1
(not (node (name root))) => (load-facts
\"examples/animal.dat\") (assert (current-node
root)))");
             r.executeCommand("(defrule initialize-2
(declare (salience 100)) ?fact <- (next-gensym-idx
?idx) => (retract ?fact) (setgen ?idx))");
             r.executeCommand("(defrule
ask-decision-node-question ?node <- (current-node
?name) (node (name ?name) (type decision) (question
?question)) (not (answer ?)) => (printout t ?question
\" (yes or no) \") (assert (answer (read))))");
             r.executeCommand("(defrule bad-answer
?answer <- (answer ~yes&~no) => (retract ?answer))");
             r.executeCommand("(defrule
proceed-to-yes-branch ?node <- (current-node ?name)
(node (name ?name) (type decision) (yes-node
?yes-branch)) ?answer <- (answer yes) => (retract
?node ?answer) (assert (current-node ?yes-branch)))");
             r.executeCommand("(defrule
proceed-to-no-branch ?node <- (current-node ?name)
(node (name ?name) (type decision) (no-node
?no-branch)) ?answer <- (answer no) => (retract ?node
?answer) (assert (current-node ?no-branch)))");
             r.executeCommand("(defrule
ask-if-answer-node-is-correct ?node <- (current-node
?name) (node (name ?name) (type answer) (answer
?value)) (not (answer ?)) => (printout t \"I guess it
is a \" ?value crlf) (printout t \"Am I correct? (yes
or no) \") (assert (answer (read))))");
             r.executeCommand("(defrule
answer-node-guess-is-correct ?node <- (current-node
?name) (node (name ?name) (type answer)) ?answer <-
(answer yes) => (assert (ask-try-again)) (retract
?node ?answer))");
             r.executeCommand("(defrule
answer-node-guess-is-incorrect ?node <- (current-node
?name) (node (name ?name) (type answer)) ?answer <-
(answer no) => (assert (replace-answer-node ?name))
(retract ?answer ?node))");
             r.executeCommand("(defrule ask-try-again
(ask-try-again) (not (answer ?)) => (printout t \"Try
again? (yes or no) \") (assert (answer (read))))");
             r.executeCommand("(defrule one-more-time
?phase <- (ask-try-again) ?answer <- (answer yes) =>
(retract ?phase ?answer) (assert (current-node
root)))");
             r.executeCommand("(defrule no-more ?phase
<- (ask-try-again) ?answer <- (answer no) => (retract
?phase ?answer) (bind ?g (gensym*)) (assert
(next-gensym-idx (sub-string 4 (str-length ?g) ?g)))
(save-facts \"examples/animal.dat\" node
next-gensym-idx))");
             r.executeCommand("(defrule
replace-answer-node ?phase <- (replace-answer-node
?name) ?data <- (node (name ?name) (type answer)
(answer ?value)) => (retract ?phase) (printout t
\"What is the animal? \") (bind ?new-animal (read))
(printout t \"What question when answered yes \")
(printout t \"will distinguish \" crlf \"   a \")
(printout t ?new-animal \" from a \" ?value \"? \")
(bind ?question (readline)) (printout t \"Now I can
guess \" ?new-animal crlf) (bind ?newnode1 (gensym*))
(bind ?newnode2 (gensym*)) (modify ?data (type
decision) (question ?question) (yes-node ?newnode1)
(no-node ?newnode2)) (assert (node (name ?newnode1)
(type answer) (answer ?new-animal))) (assert (node
(name ?newnode2) (type answer) (answer ?value)))
(assert (ask-try-again)))");
             r.executeCommand("(reset)");
             r.executeCommand("(run)");
            }
            catch(JessException je)
                {
                        System.err.println(je);
                }
        }
}


The following code implements the event handler. 


import jess.*;
public class ExMyEventHandler implements JessListener
{
public void eventHappened(JessEvent je)
         {
          System.out.println("jess event entered");
          int defaultMask = JessEvent.DEFRULE_FIRED |
JessEvent.CLEAR;
          System.out.println("after defmask");        
     
          int type = je.getType();
          System.out.println("after gettype");
          switch(type)
                 {
                  case JessEvent.CLEAR:               
                                      
System.out.println("in clear");
                                       Rete engine =
(Rete) je.getSource();
                                       int mask =
engine.getEventMask();
                                       mask |=
defaultMask;
                                      
engine.setEventMask(mask); break;
                  case JessEvent.DEFRULE_FIRED:
                                              
System.out.println("Rule Fired"); 
                                              
//String s=je.getName();            
                                               
Iterator i=new Iterator();
                                               
i.listActivations(); 
                                                break;
                  default:  //ignore
                  }
}                            
}
                  

    In the event handler code i have inserted
system.out.println statements so that we could track
the flow of the event handler.
    However the event handler does not seem to capture
the rule-firing event, since the corresponding output
statements are not getting printed.

   I am extremely sorry to send such a lengthy code.
But i had to send this code to explain my problem
clearly to you, as i have ran out of all options of
trying to debug it by myself.

   Hoping to be obliged.
 
   Monika   

__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.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