I think that Eric Oliphant wrote:
>>can defclasses be backchain-reactive ?

Hi Eric,

Let me share with you my approach to answering your question.  I don't do
this to patronize -- I think this is an instructive example -- and my main
goal as  a Jess consultant is teaching people "how to fish" rather than just
"giving them a fish".  Let's assume that I don't know the answer and that
Dr.  Friedman-Hill is unavailable.  All I have are the Jess documents, maybe
a copy of "Jess In Action" (JIA), and some coffee.

Just poking around the docs, I note ample evidence that Jess treats
(defclass) facts just like (deftemplate) facts (See section 8.2. Definstance
facts in  Jess docs and JIA section 6.5).  I reason that if this is true,
then it is logical to assume that (defclass) facts should be backward
chainable since I know  that regular facts are backward chainable (See
section 7.4 in JIA and section 6.20 in the Jess docs).  Cool!  I have a
working hypothesis!

Now, how do I test it?

* I need to understand how a backward chaining scenario would work with
regular, plain vanilla facts. Section 6.20 gives a good enough example.
Understanding how the mechanism works here is the key.

* I need to be clear on how to create (defclass) facts.  Once I can write a
JavaBean class and (definstance) it, I should be able to substitute the
definstance for a regular fact assertion.

* Finally, I'll write some rules that backward chain  in other words, they
will assert one or more pieces of evidence provided that one or more pieces
of  evidence are available to them.

-----------------------------------------------------------
Step 1.  Write some generic JavaBeans (nothing fancy)
-----------------------------------------------------------
package testJess7b06;

import java.io.Serializable;

public class MyBean implements Serializable {
        private String name;
        private double datum;
        public double getDatum() {
                return datum;
        }
        public void setDatum(double datum) {
                this.datum = datum;
        }
        public String getName() {
                return name;
        }
        public void setName(String name) {
                this.name = name;
        }

}

package testJess7b06;
import java.io.Serializable;

public class AnotherBean implements Serializable {
        private int id;
        private double value;
        public int getId() {
                return id;
        }
        public void setId(int id) {
                this.id = id;
        }
        public double getValue() {
                return value;
        }
        public void setValue(double value) {
                this.value = value;
        }

}

-----------------------------------------------------------
Step 2. Write some Jess code with backward chaining rules.
-----------------------------------------------------------

;; Init program
(clear)
(watch all)

;; Let Jess know about my beans
(import testJess7b06.MyBean)
(import testJess7b06.AnotherBean)

;; Define my bean templates
(defclass beanA MyBean)
(defclass beanB AnotherBean)

;; Docs say that I have to declare these before I use the rules ...
(do-backward-chaining beanA)
(do-backward-chaining beanB)

;; Note how final-link needs a (beanB) fact
(defrule final-link
    ;; other patterns here
    (beanB)
    =>
    (printout t "All done!" crlf)
    )

;; second-link will supply a (beanB) if needed
;; but it needs a (beanA) to start the process
(defrule second-link
    ;; other patterns here
    (beanA)
    (need-beanB)
    =>
;; This is where I assume that
;; (deftemplate) fact = (defclass) fact
    (bind ?bean2 (new AnotherBean))
    (definstance beanB ?bean2 static)
    )

;; first-link will supply a (beanA) for free!
(defrule first-link
    ;; other patterns here
    (need-beanA)
    =>
    (bind ?bean1 (new MyBean))
    (definstance beanA ?bean1 static))

(reset)
(run)

-----------------------------------------------------------
Step 3.  Run the program and observe the results.
-----------------------------------------------------------
Jess, the Rule Engine for the Java Platform
Copyright (C) 2005 Sandia Corporation
Jess Version 7.0b6 3/13/2006

MAIN::final-link: +1+1+1+2+t
MAIN::sescond-link: =1=1+1+2+1+2=1+2+t
MAIN::first-link: +1+1=1+2+t
 ==> Focus MAIN
 ==> f-0 (MAIN::initial-fact)
 ==> f-1 (MAIN::need-beanB (class nil) (id nil) (value nil) (OBJECT nil))
 ==> f-2 (MAIN::need-beanA (class nil) (datum nil) (name nil) (OBJECT nil))
==> Activation: MAIN::first-link :  f-2,
FIRE 1 MAIN::first-link f-2,
 ==> f-3 (MAIN::beanA (class <Java-Object:java.lang.Class>) (datum 0.0)
(name nil) (OBJECT <Java-Object:testJess7b06.MyBean>))
==> Activation: MAIN::sescond-link :  f-0, f-3, f-1,
FIRE 2 MAIN::second-link f-0, f-3, f-1,
 ==> f-4 (MAIN::beanB (class <Java-Object:java.lang.Class>) (id 0) (value
0.0) (OBJECT <Java-Object:testJess7b06.AnotherBean>))
==> Activation: MAIN::final-link :  f-0, f-4
FIRE 3 MAIN::final-link f-0, f-4
All done!
 <== Focus MAIN

-----------------------------------------------------------
Step 4.  Make my educated conclusions.
-----------------------------------------------------------
Looking at the FIRE traces, it does indeed look like I can do backward
chaining with (defclass) facts!  No Dr. Friedman-Hill needed!  :-D

I learn so much more when I hold myself to this discipline  it's not easy,
but it's infinitely more rewarding.  It also gives me the peace of mind from
the  conviction that if I do ask for help then I'm REALLY stuck.

Other things that I could test might be:

a) Does changing the pattern order affect how the rules fire?

b) What if I only want the final-link rule to fire if there isn't a (beanA)
fact in working memory?

Hope this helps... especially all new Jess users out there.
Experiment!  Experiment! Experiment!  And have fun!

Cheers,
Jason


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