JESS: Problem with interactive Java-Jess applications using store & fetch

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

JESS: Problem with interactive Java-Jess applications using store & fetch

Arti Pande
Hi,

I am trying to create an interactive (Q & A)
application using Java and Jess. Jess determines which
question needs to be asked, stores the question. Java
code then fetches the question and shows it to user.
User's answer is read by Java code and stored again so
Jess can fetch and use it.

However I am facing a problem in that - after the
first question is stored, Jess transfers control to
Java, and it never regains it (never activates any
rules further, even if I assert some facts to trigger
the activation).

Can you help me solve this problem?

------------ Here is the JESS code : ----------------

(defrule init-rule
  (initial-fact)
=>
  (printout t "Writing Question" crlf)
  (store Question "Is this a question?")
)

(defrule AssertAnswer
   (jess state resume)
=>
   (printout t "Asserting answer...." crlf)
   (assert (answer is (fetch Answer)))
)

(defrule EvaluateAnswer
   (answer is y|Y|Yes|yes)
=>
   (printout t "You are passed, JESS" crlf)
)


---------- Here is the Java code : --------------

import jess.*;

public class JessTrial
{
  public static void main(String args[])
  {
     try
     {
        Rete rete = new Rete();
        rete.clear();
        Context context = rete.getGlobalContext();

        rete.watch(WatchConstants.FACTS);
        rete.watch(WatchConstants.ACTIVATIONS);

        Value v1 = rete.batch("trial.clp");

        rete.reset();

        System.out.println("Running...");
        int v2 = rete.run();
        System.out.println("run() returned ..." + v2);
   
        String Question =
(rete.fetch("Question")).symbolValue(context);
        System.out.println("Java received :: " +
Question);
        String Answer = "Yes";
     
        Value stored = rete.store("Answer", Answer);
        System.out.println("Java Answered :: " +
stored);
 
        rete.assertFact(new Fact("jess state resume",
rete));   // This should activate "AssertAnswer" rule
- but it never does

        System.out.println("Done");
     }
     catch(Exception e) {
System.out.println("Exception " + e); }
   }
}

---------- Here is the output : --------------------

E:\Aarati>java JessTrial
 ==> f-0 (MAIN::initial-fact)
==> Activation: MAIN::init-rule :  f-0
Running...
Writing Question
run() returned ...1
Java received :: Is this a question?
Java Answered :: null
 ==> f-1 (MAIN::jess state resume)
Done
------------------ END ------------------------------



               
____________________________________________________
Start your day with Yahoo! - make it your home page
http://www.yahoo.com/r/hs 
 


--------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users [hidden email]'
in the BODY of a message to [hidden email], NOT to the list
(use your own address!) List problems? Notify [hidden email].
--------------------------------------------------------------------

Reply | Threaded
Open this post in threaded view
|

Re: JESS: Problem with interactive Java-Jess applications using store & fetch

Jason Morris
Hi Arti,

I think that the main problem is in your constructor for the new Fact
(JIA p.416).  The constructor for Fact takes a two arguments: the
first represents the name of an unordered deftemplate that the Rete
engine should attempt to use.  You are passing in "jess state resume"
and the Rete engine is not finding that template.  As a default, it
creates an ordered fact of its own.  Perhaps Ernest can give you the
gory details, but I changed your assertion (plus a few watch commands)
to:

     rete.executeCommand("(assert(jess state resume))");

and got the following trace:

MAIN::init-rule: +1+1+t
MAIN::AssertAnswer: +1+1+1+1+1+t
MAIN::EvaluateAnswer: +1+1+1+1+1+t
==> Focus MAIN
==> f-0 (MAIN::initial-fact)
==> Activation: MAIN::init-rule :  f-0
Running...
FIRE 1 MAIN::init-rule f-0
Writing Question
<== Focus MAIN
run() returned ...1
Java received :: Is this a question?
Java Answered :: null
==> f-1 (MAIN::jess state resume)
==> Activation: MAIN::AssertAnswer :  f-1
f-0   (MAIN::initial-fact)
f-1   (MAIN::jess state resume)
For a total of 2 facts in all modules.
FIRE 1 MAIN::AssertAnswer f-1
Asserting answer....
==> f-2 (MAIN::answer is <External-Address:java.lang.String>)
Done

.. which hopefully is what you were after.  :-D
Cheers,
Jason

-----------------------------------------------------
Morris Technical Solutions LLC
www.morristechnicalsolutions.com
[hidden email]
phone/fax: 503.692.1088


--------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users [hidden email]'
in the BODY of a message to [hidden email], NOT to the list
(use your own address!) List problems? Notify [hidden email].
--------------------------------------------------------------------

Reply | Threaded
Open this post in threaded view
|

Re: JESS: Problem with interactive Java-Jess applications using store & fetch

friedman_hill ernest j
In reply to this post by Arti Pande

>         rete.assertFact(new Fact("jess state resume",
> rete));   // This should activate "AssertAnswer" rule
> - but it never does


This isn't how you make Jess facts in Java. The manual describes
things in detail: see here for an example of creating an ordered fact
like yours:

http://herzberg.ca.sandia.gov/jess/docs/61/library.html#ordered_fact 

When you assert the fact correctly, the rule will activate but not
fire -- you'll need to call run() again for that.





---------------------------------------------------------
Ernest Friedman-Hill  
Advanced Software Research          Phone: (925) 294-2154
Sandia National Labs                FAX:   (925) 294-2234
PO Box 969, MS 9012                 [hidden email]
Livermore, CA 94550         http://herzberg.ca.sandia.gov

--------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users [hidden email]'
in the BODY of a message to [hidden email], NOT to the list
(use your own address!) List problems? Notify [hidden email].
--------------------------------------------------------------------

Reply | Threaded
Open this post in threaded view
|

Re: JESS: Problem with interactive Java-Jess applications using store & fetch

Arti Pande
Thanks a lot for your immediate reply. I corrected and
it worked.

--- [hidden email] wrote:

>
> >         rete.assertFact(new Fact("jess state
> resume",
> > rete));   // This should activate "AssertAnswer"
> rule
> > - but it never does
>
>
> This isn't how you make Jess facts in Java. The
> manual describes
> things in detail: see here for an example of
> creating an ordered fact
> like yours:
>
>
http://herzberg.ca.sandia.gov/jess/docs/61/library.html#ordered_fact

>
>
> When you assert the fact correctly, the rule will
> activate but not
> fire -- you'll need to call run() again for that.
>
>
>
>
>
>
---------------------------------------------------------

> Ernest Friedman-Hill  
> Advanced Software Research          Phone: (925)
> 294-2154
> Sandia National Labs                FAX:   (925)
> 294-2234
> PO Box 969, MS 9012                
> [hidden email]
> Livermore, CA 94550        
> http://herzberg.ca.sandia.gov
>
>
--------------------------------------------------------------------
> To unsubscribe, send the words 'unsubscribe
> jess-users [hidden email]'
> in the BODY of a message to [hidden email],
> NOT to the list
> (use your own address!) List problems? Notify
> [hidden email].
>
--------------------------------------------------------------------
>
>



               
____________________________________________________
Start your day with Yahoo! - make it your home page
http://www.yahoo.com/r/hs 
 


--------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users [hidden email]'
in the BODY of a message to [hidden email], NOT to the list
(use your own address!) List problems? Notify [hidden email].
--------------------------------------------------------------------

Reply | Threaded
Open this post in threaded view
|

Re: JESS: Problem with interactive Java-Jess applications using store & fetch

Arti Pande
In reply to this post by Jason Morris
Hi, Thanks for immediate reply. I got it working after
this correction.

--- Jason Morris <[hidden email]> wrote:

> Hi Arti,
>
> I think that the main problem is in your constructor
> for the new Fact
> (JIA p.416).  The constructor for Fact takes a two
> arguments: the
> first represents the name of an unordered
> deftemplate that the Rete
> engine should attempt to use.  You are passing in
> "jess state resume"
> and the Rete engine is not finding that template.
> As a default, it
> creates an ordered fact of its own.  Perhaps Ernest
> can give you the
> gory details, but I changed your assertion (plus a
> few watch commands)
> to:
>
>      rete.executeCommand("(assert(jess state
> resume))");
>
> and got the following trace:
>
> MAIN::init-rule: +1+1+t
> MAIN::AssertAnswer: +1+1+1+1+1+t
> MAIN::EvaluateAnswer: +1+1+1+1+1+t
> ==> Focus MAIN
> ==> f-0 (MAIN::initial-fact)
> ==> Activation: MAIN::init-rule :  f-0
> Running...
> FIRE 1 MAIN::init-rule f-0
> Writing Question
> <== Focus MAIN
> run() returned ...1
> Java received :: Is this a question?
> Java Answered :: null
> ==> f-1 (MAIN::jess state resume)
> ==> Activation: MAIN::AssertAnswer :  f-1
> f-0   (MAIN::initial-fact)
> f-1   (MAIN::jess state resume)
> For a total of 2 facts in all modules.
> FIRE 1 MAIN::AssertAnswer f-1
> Asserting answer....
> ==> f-2 (MAIN::answer is
> <External-Address:java.lang.String>)
> Done
>
> .. which hopefully is what you were after.  :-D
> Cheers,
> Jason
>
>
-----------------------------------------------------
> Morris Technical Solutions LLC
> www.morristechnicalsolutions.com
> [hidden email]
> phone/fax: 503.692.1088
>
>
>
--------------------------------------------------------------------
> To unsubscribe, send the words 'unsubscribe
> jess-users [hidden email]'
> in the BODY of a message to [hidden email],
> NOT to the list
> (use your own address!) List problems? Notify
> [hidden email].
>
--------------------------------------------------------------------
>
>



               
____________________________________________________
Start your day with Yahoo! - make it your home page
http://www.yahoo.com/r/hs 
 


--------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users [hidden email]'
in the BODY of a message to [hidden email], NOT to the list
(use your own address!) List problems? Notify [hidden email].
--------------------------------------------------------------------