JESS: Modifying JavaBeans

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

JESS: Modifying JavaBeans

skrasnigor
Modifying JavaBeans

Sorry to bother everyone. I am trying to play around with JESS embedded in Java to get an understanding of JESS. I created a simple Java bean which contains two properties: range and qualRng. I create the fact within Jess utilizing defclass and definstance. Executing engine.facts() shows that the fact has been created in Jess's memory with the appropriate slots. I created a simple rule to check to see if range is below 20, if true, modify the qualRng slot to TRUE. I added code to main() decrement the initial value of range in order to fire the rule. Watching the results shows that the range slot decrements properly and the rule fires, but the qualRng field never gets updated to show TRUE. I am sure it is something simple, but I just don't see the problem. I've modified the RHS of the rule with all the different iterations I could think of but to no avail. The code is listed below (minus the java bean class). Thanks for your assistance.

import jess.*;

public class testFacts
{
        public static void main(String[] args)
        {
                Value fact;
                try
                {
                        Rete engine = new Rete();
                        engine.watchAll();

                        engine.defclass("testTrkClass", "testTrack", null);
                        testTrack trkOne = new testTrack(30, 45, 5);
                        fact = engine.definstance("testTrkClass", trkOne, true);
                       
                        // Load rules clip file
                        engine.executeCommand("(batch testTrk.clp)");
                        engine.run();
                       
                        // change range until it triggers a dummy doct rule

                        int i;
                        for(i = trkOne.getRange(); i > 5; i--)
                        {
                                trkOne.setRange(i);
                                try
                                {
                                        Thread.sleep(500);
                                }
                                catch (InterruptedException ie){}
                        }
                }
                catch (JessException je)
                {
                        System.out.print("An error occurred at line " + je.getLineNumber());
                        System.out.println(" which looks like " + je.getProgramText());
                        System.out.println("Message: " + je.getMessage());
                }
        }

}

The clp file:

(defrule dummy-range-doctrine
    ;; set qualRng to true if trk is in range
    ?fact <- (testTrkClass (range ?x&:(<= ?x 20)))
    =>
    (modify ?fact (qualRng TRUE)))



Scott L. Krasnigor
Principal Engineer/Scientist
Atlantic Science & Technology
Lockheed Martin - MS2
' 856-359-3094
* [hidden email]
* 770-2

Reply | Threaded
Open this post in threaded view
|

Re: JESS: Modifying JavaBeans

friedman_hill ernest j
Hi Scott,

Rules only fire during a call to run(). Your call to run() returns
immediately because when you call it, there are no rules eligible to
fire yet. Then you make some changes to the fact, and the
PropertyChangeEvents update the working memory, but you never call
run() again, so even when it is activated, the rule never fires.

You could call run() in your for loop, and then the rule would fire
and update the slot as expected. Note that it makes sense for this
rule to fire only once -- no reason to fire once the qualRng slot is
TRUE, so you might write the pattern like this:


 (defrule dummy-range-doctrine
     ;; set qualRng to true if trk is in range
     ?fact <- (testTrkClass (range ?x&:(<= ?x 20)) (qualRng ~TRUE))
     =>
     (modify ?fact (qualRng TRUE)))


I think Krasnigor, Scott L (N-AST) wrote:

> Sorry to bother everyone. I am trying to play around with JESS embedded
> in Java to get an understanding of JESS. I created a simple Java bean
> which contains two properties: range and qualRng. I create the fact
> within Jess utilizing defclass and definstance. Executing engine.facts()
> shows that the fact has been created in Jess's memory with the
> appropriate slots. I created a simple rule to check to see if range is
> below 20, if true, modify the qualRng slot to TRUE. I added code to
> main() decrement the initial value of range in order to fire the rule.
> Watching the results shows that the range slot decrements properly and
> the rule fires, but the qualRng field never gets updated to show TRUE. I
> am sure it is something simple, but I just don't see the problem. I've
> modified the RHS of the rule with all the different iterations I could
> think of but to no avail. The code is listed below (minus the java bean
> class). Thanks for your assistance.
>
> import jess.*;
>
> public class testFacts
> {
> public static void main(String[] args)
> {
> Value fact;
> try
> {
> Rete engine = new Rete();
> engine.watchAll();
>
> engine.defclass("testTrkClass", "testTrack",
> null);
> testTrack trkOne = new testTrack(30, 45, 5);
> fact = engine.definstance("testTrkClass",
> trkOne, true);
>
> // Load rules clip file
> engine.executeCommand("(batch testTrk.clp)");
> engine.run();
>
> // change range until it triggers a dummy doct
> rule
>
> int i;
> for(i = trkOne.getRange(); i > 5; i--)
> {
> trkOne.setRange(i);
> try
> {
> Thread.sleep(500);
> }
> catch (InterruptedException ie){}
> }
> }
> catch (JessException je)
> {
> System.out.print("An error occurred at line " +
> je.getLineNumber());
> System.out.println(" which looks like " +
> je.getProgramText());
> System.out.println("Message: " +
> je.getMessage());
> }
> }
>
> }
>
> The clp file:
>
> (defrule dummy-range-doctrine
>     ;; set qualRng to true if trk is in range
>     ?fact <- (testTrkClass (range ?x&:(<= ?x 20)))
>     =>
>     (modify ?fact (qualRng TRUE)))
>
>
>
> Scott L. Krasnigor
> Principal Engineer/Scientist
> Atlantic Science & Technology
> Lockheed Martin - MS2
> ' 856-359-3094
> * [hidden email]
> * 770-2
>



---------------------------------------------------------
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: Modifying JavaBeans

skrasnigor
In reply to this post by skrasnigor
Thanks. Works like a champ. I had a feeling that it was something
simple.

-----Original Message-----
From: [hidden email] [mailto:[hidden email]]
On Behalf Of [hidden email]
Sent: Wednesday, August 31, 2005 1:31 PM
To: [hidden email]
Subject: Re: JESS: Modifying JavaBeans

Hi Scott,

Rules only fire during a call to run(). Your call to run() returns
immediately because when you call it, there are no rules eligible to
fire yet. Then you make some changes to the fact, and the
PropertyChangeEvents update the working memory, but you never call
run() again, so even when it is activated, the rule never fires.

You could call run() in your for loop, and then the rule would fire and
update the slot as expected. Note that it makes sense for this rule to
fire only once -- no reason to fire once the qualRng slot is TRUE, so
you might write the pattern like this:


 (defrule dummy-range-doctrine
     ;; set qualRng to true if trk is in range
     ?fact <- (testTrkClass (range ?x&:(<= ?x 20)) (qualRng ~TRUE))
     =>
     (modify ?fact (qualRng TRUE)))


I think Krasnigor, Scott L (N-AST) wrote:
> Sorry to bother everyone. I am trying to play around with JESS
> embedded in Java to get an understanding of JESS. I created a simple
> Java bean which contains two properties: range and qualRng. I create
> the fact within Jess utilizing defclass and definstance. Executing
> engine.facts() shows that the fact has been created in Jess's memory
> with the appropriate slots. I created a simple rule to check to see if

> range is below 20, if true, modify the qualRng slot to TRUE. I added
> code to
> main() decrement the initial value of range in order to fire the rule.
> Watching the results shows that the range slot decrements properly and

> the rule fires, but the qualRng field never gets updated to show TRUE.

> I am sure it is something simple, but I just don't see the problem.
> I've modified the RHS of the rule with all the different iterations I
> could think of but to no avail. The code is listed below (minus the
> java bean class). Thanks for your assistance.
>
> import jess.*;
>
> public class testFacts
> {
> public static void main(String[] args)
> {
> Value fact;
> try
> {
> Rete engine = new Rete();
> engine.watchAll();
>
> engine.defclass("testTrkClass", "testTrack",
null);
> testTrack trkOne = new testTrack(30, 45, 5);
> fact = engine.definstance("testTrkClass",
> trkOne, true);
>
> // Load rules clip file
> engine.executeCommand("(batch testTrk.clp)");
> engine.run();
>
> // change range until it triggers a dummy doct
rule

>
> int i;
> for(i = trkOne.getRange(); i > 5; i--)
> {
> trkOne.setRange(i);
> try
> {
> Thread.sleep(500);
> }
> catch (InterruptedException ie){}
> }
> }
> catch (JessException je)
> {
> System.out.print("An error occurred at line " +
> je.getLineNumber());
> System.out.println(" which looks like " +
je.getProgramText());

> System.out.println("Message: " +
> je.getMessage());
> }
> }
>
> }
>
> The clp file:
>
> (defrule dummy-range-doctrine
>     ;; set qualRng to true if trk is in range
>     ?fact <- (testTrkClass (range ?x&:(<= ?x 20)))
>     =>
>     (modify ?fact (qualRng TRUE)))
>
>
>
> Scott L. Krasnigor
> Principal Engineer/Scientist
> Atlantic Science & Technology
> Lockheed Martin - MS2
> ' 856-359-3094
> * [hidden email]
> * 770-2
>



---------------------------------------------------------
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].
--------------------------------------------------------------------

--------------------------------------------------------------------
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].
--------------------------------------------------------------------