JESS: [EXTERNAL] Dynamic rule-base analysis

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

JESS: [EXTERNAL] Dynamic rule-base analysis

Przemyslaw Woznowski
Dear Jess users.

What I am trying to do is to dynamically list all the defrules together with their LHS and RHS facts excluding any conditions. Basically what I mean is that, for example, if there are Person and Car facts on the rule's LHS and the rule asserts an Owner fact, deletes the Car fact and modifies the Person fact. I would like to create a data structure that would, for each rule, carry it's name and the facts it needs to fire (excluding any conditions just deftemplate's name) and any deftemplates that appear on the rule's RHS. So for the example above, something like:
Rule_1
 needs: Person, Car
 asserts: Owner
 modifies: Person
 deletes: Car

I can get a list of all defrules via the listDefrules() method called on the Rete object but then it gets tricky. The listNodes() method called on the HasLHS object gives a textual description of the rule's LHS but I cannot find a method that would simply return name of facts on the LHS. Some text processing would solve the rule's LHS problem as I can look for occurrences of "::" to find facts. However, the rule's RHS is more problematic as I cannot find a method that would give me what I want.

I would appreciate your help with this one.

Best regards,
Pete

--
Przemyslaw (Pete) Woznowski, PhD candidate & RA
Email: [hidden email]
Room C/2.06, Desk 8
Cardiff School of Computer Science & Informatics, 
Cardiff University,
Queen's Buildings,
5 The Parade, Roath,
Cardiff, CF24 3AA, UK
& School of Healthcare Studies,
Cardiff University
Cardigan House
Heath Park Campus
Cardiff, CF14 4XN, UK
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: JESS: [EXTERNAL] Dynamic rule-base analysis

Wolfgang Laun-2
Rete has listDefrules and Defrule has getConditionalElements, which
lets you retrieve its constituents, depending on it being a Pattern or
Accumulate; if it is a Group you must recurse down... It's like
walking an AST

RHS is similar. But note that fact modifications are possible in
functions, so if one of these is called on the RHS, you'll have to
investigate in depth.

Expect to have some fun ;-)

Cheers
Wolfgang


On 14/08/2013, Przemyslaw Woznowski <[hidden email]> wrote:

> Dear Jess users.
>
> What I am trying to do is to dynamically list all the defrules together
> with their LHS and RHS facts excluding any conditions. Basically what I
> mean is that, for example, if there are Person and Car facts on the rule's
> LHS and the rule asserts an Owner fact, deletes the Car fact and modifies
> the Person fact. I would like to create a data structure that would, for
> each rule, carry it's name and the facts it needs to fire (excluding any
> conditions just deftemplate's name) and any deftemplates that appear on the
> rule's RHS. So for the example above, something like:
> Rule_1
>  needs: Person, Car
>  asserts: Owner
>  modifies: Person
>  deletes: Car
>
> I can get a list of all defrules via the listDefrules() method called on
> the Rete object but then it gets tricky. The listNodes() method called on
> the HasLHS object gives a textual description of the rule's LHS but I
> cannot find a method that would simply return name of facts on the LHS.
> Some text processing would solve the rule's LHS problem as I can look for
> occurrences of "::" to find facts. However, the rule's RHS is more
> problematic as I cannot find a method that would give me what I want.
>
> I would appreciate your help with this one.
>
> Best regards,
> Pete
>
> --
> Przemyslaw (Pete) Woznowski, PhD candidate & RA
> Email: [hidden email]
> Room C/2.06, Desk 8
> Cardiff School of Computer Science & Informatics,
> Cardiff University,
> Queen's Buildings,
> 5 The Parade, Roath,
> Cardiff, CF24 3AA, UK
> & School of Healthcare Studies,
> Cardiff University
> Cardigan House
> Heath Park Campus
> Cardiff, CF14 4XN, UK
>
--------------------------------------------------------------------
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
|  
Report Content as Inappropriate

Re: JESS: [EXTERNAL] Dynamic rule-base analysis

Przemyslaw Woznowski
Thanks Wolfgang, this helps a lot! I must have missed the getConditionalElements in the API. The analysis of the rule's LHS is working great and it's RHS is more complex but do-able.

Once again, thanks for the advice,
Pete


On Thu, Aug 15, 2013 at 6:51 PM, Wolfgang Laun <[hidden email]> wrote:
Rete has listDefrules and Defrule has getConditionalElements, which
lets you retrieve its constituents, depending on it being a Pattern or
Accumulate; if it is a Group you must recurse down... It's like
walking an AST

RHS is similar. But note that fact modifications are possible in
functions, so if one of these is called on the RHS, you'll have to
investigate in depth.

Expect to have some fun ;-)

Cheers
Wolfgang


On 14/08/2013, Przemyslaw Woznowski <[hidden email]> wrote:
> Dear Jess users.
>
> What I am trying to do is to dynamically list all the defrules together
> with their LHS and RHS facts excluding any conditions. Basically what I
> mean is that, for example, if there are Person and Car facts on the rule's
> LHS and the rule asserts an Owner fact, deletes the Car fact and modifies
> the Person fact. I would like to create a data structure that would, for
> each rule, carry it's name and the facts it needs to fire (excluding any
> conditions just deftemplate's name) and any deftemplates that appear on the
> rule's RHS. So for the example above, something like:
> Rule_1
>  needs: Person, Car
>  asserts: Owner
>  modifies: Person
>  deletes: Car
>
> I can get a list of all defrules via the listDefrules() method called on
> the Rete object but then it gets tricky. The listNodes() method called on
> the HasLHS object gives a textual description of the rule's LHS but I
> cannot find a method that would simply return name of facts on the LHS.
> Some text processing would solve the rule's LHS problem as I can look for
> occurrences of "::" to find facts. However, the rule's RHS is more
> problematic as I cannot find a method that would give me what I want.
>
> I would appreciate your help with this one.
>
> Best regards,
> Pete
>
> --
> Przemyslaw (Pete) Woznowski, PhD candidate & RA
> Email: [hidden email]
> Room C/2.06, Desk 8
> Cardiff School of Computer Science & Informatics,
> Cardiff University,
> Queen's Buildings,
> 5 The Parade, Roath,
> Cardiff, CF24 3AA, UK
> & School of Healthcare Studies,
> Cardiff University
> Cardigan House
> Heath Park Campus
> Cardiff, CF14 4XN, UK
>
--------------------------------------------------------------------
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].
--------------------------------------------------------------------




--
Przemyslaw (Pete) Woznowski, PhD candidate & RA
Email: [hidden email]
Room C/2.06, Desk 8
Cardiff School of Computer Science & Informatics, 
Cardiff University,
Queen's Buildings,
5 The Parade, Roath,
Cardiff, CF24 3AA, UK
& School of Healthcare Studies,
Cardiff University
Cardigan House
Heath Park Campus
Cardiff, CF14 4XN, UK
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

JESS: [EXTERNAL] bsave fails after defquery execution [was: Dynamic rule-base analysis]

Jonathan Sewall-2
In reply to this post by Przemyslaw Woznowski
[Apologies for the incorrect Subject: on this report yesterday, 9/29.]

The trouble with bsave after executing a defquery appears to happen when
serializing the Map Context.m_variables in Rete.m_globalContext: at this
point in the script, the map holds the QueryResult variable ?qr:

     (bind ?qr (run-query* all-cars))

Hence, if I execute

     (bind ?qr "not a QueryResult")

before the 2nd bsave, then the script runs without error. Could
QueryResult be made Serializable? Revised script and results below.
Thanks again,

                        Jonathan Sewall

------- Original Message --------
Subject: Re: JESS: [EXTERNAL] Dynamic rule-base analysis
Date: Sun, 29 Sep 2013 18:04:11 -0400
From: Jonathan Sewall <[hidden email]>
Reply-To: [hidden email], [hidden email]
To: [hidden email]

...

Contents of script.clp:

(deftemplate car (slot make) (slot model))
(defquery all-cars "Get all cars." ?car <- (car))
(defquery cars-of-make "Get cars of the given make." (declare (variables
?want-make)) ?car <- (car (make ?m&:(= ?m ?want-make))))
(reset)
(bind ?pickup (assert (car (make "Ford") (model "F100"))))
(bind ?sedan (assert (car (make "Honda") (model "Civic"))))
(bind ?suv (assert (car (make "Ford") (model "Explorer"))))
(printout t "*** Before 1st bsave ***" crlf)
(printout t "globalContext " (((engine) getGlobalContext) toString) crlf)
(bsave "s1.bsave")
(bind ?qr (run-query* all-cars))
(while (?qr next) (bind ?car (?qr getObject "car")) (printout t (?car
toString) crlf))
(?qr close)
(printout t "*** Before 2nd bsave ***" crlf)
(printout t "globalContext " (((engine) getGlobalContext) toString) crlf)
;; (bind ?qr "not a QueryResult") ;; uncomment to make script run ok
(bsave "s2.bsave")

Results:

$ java -cp lib/jess.jar jess.Main

Jess, the Rule Engine for the Java Platform
Copyright (C) 2008 Sandia Corporation
Jess Version 7.1p2 11/5/2008

Jess> (batch "script.clp")
*** Before 1st bsave ***
globalContext [Context, 3 variables:
pickup=<Fact-1>;sedan=<Fact-2>;suv=<Fact-3>;]
(MAIN::car (make "Ford") (model "F100"))
(MAIN::car (make "Honda") (model "Civic"))
(MAIN::car (make "Ford") (model "Explorer"))
*** Before 2nd bsave ***
globalContext [Context, 5 variables:
car=<Java-Object:jess.Fact>;pickup=<Fact-1>;sedan=<Fact-2>;suv=<Fact-3>;qr=<Java-Object:jess.QueryResult>;]
Jess reported an error in routine bsave
        while executing (bsave "s2.bsave")
        while executing (batch "script.clp").
   Message: IO Exception.
   Program text: ( bsave "s2.bsave" )  at line 17 in file script.clp.

Nested exception is:
jess.QueryResult
Jess>
--------------------------------------------------------------------
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
|  
Report Content as Inappropriate

RE: JESS: [EXTERNAL] bsave fails after defquery execution [was: Dynamic rule-base analysis]

Friedman-Hill, Ernest
It would certainly make sense for this class to be Serializable, as most other classes in Jess are. The current implementation in practice contains an instance of java.util.ArrayList.Itr, which is not Seriaizable, so a certain amount of coding would be involved in making this change. Worth doing, though, so I'll see if we can work it in.

-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of Jonathan Sewall
Sent: Monday, September 30, 2013 9:49 AM
To: jess-users
Subject: JESS: [EXTERNAL] bsave fails after defquery execution [was: Dynamic rule-base analysis]

[Apologies for the incorrect Subject: on this report yesterday, 9/29.]

The trouble with bsave after executing a defquery appears to happen when serializing the Map Context.m_variables in Rete.m_globalContext: at this point in the script, the map holds the QueryResult variable ?qr:

     (bind ?qr (run-query* all-cars))

Hence, if I execute

     (bind ?qr "not a QueryResult")

before the 2nd bsave, then the script runs without error. Could QueryResult be made Serializable? Revised script and results below.
Thanks again,

                        Jonathan Sewall

------- Original Message --------
Subject: Re: JESS: [EXTERNAL] Dynamic rule-base analysis
Date: Sun, 29 Sep 2013 18:04:11 -0400
From: Jonathan Sewall <[hidden email]>
Reply-To: [hidden email], [hidden email]
To: [hidden email]

...

Contents of script.clp:

(deftemplate car (slot make) (slot model)) (defquery all-cars "Get all cars." ?car <- (car)) (defquery cars-of-make "Get cars of the given make." (declare (variables
?want-make)) ?car <- (car (make ?m&:(= ?m ?want-make))))
(reset)
(bind ?pickup (assert (car (make "Ford") (model "F100")))) (bind ?sedan (assert (car (make "Honda") (model "Civic")))) (bind ?suv (assert (car (make "Ford") (model "Explorer")))) (printout t "*** Before 1st bsave ***" crlf) (printout t "globalContext " (((engine) getGlobalContext) toString) crlf) (bsave "s1.bsave") (bind ?qr (run-query* all-cars)) (while (?qr next) (bind ?car (?qr getObject "car")) (printout t (?car
toString) crlf))
(?qr close)
(printout t "*** Before 2nd bsave ***" crlf) (printout t "globalContext " (((engine) getGlobalContext) toString) crlf) ;; (bind ?qr "not a QueryResult") ;; uncomment to make script run ok (bsave "s2.bsave")

Results:

$ java -cp lib/jess.jar jess.Main

Jess, the Rule Engine for the Java Platform Copyright (C) 2008 Sandia Corporation Jess Version 7.1p2 11/5/2008

Jess> (batch "script.clp")
*** Before 1st bsave ***
globalContext [Context, 3 variables:
pickup=<Fact-1>;sedan=<Fact-2>;suv=<Fact-3>;]
(MAIN::car (make "Ford") (model "F100")) (MAIN::car (make "Honda") (model "Civic")) (MAIN::car (make "Ford") (model "Explorer"))
*** Before 2nd bsave ***
globalContext [Context, 5 variables:
car=<Java-Object:jess.Fact>;pickup=<Fact-1>;sedan=<Fact-2>;suv=<Fact-3>;qr=<Java-Object:jess.QueryResult>;]
Jess reported an error in routine bsave
        while executing (bsave "s2.bsave")
        while executing (batch "script.clp").
   Message: IO Exception.
   Program text: ( bsave "s2.bsave" )  at line 17 in file script.clp.

Nested exception is:
jess.QueryResult
Jess>
--------------------------------------------------------------------
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].
--------------------------------------------------------------------
Loading...