Lab 2 - Inference Chaining with JESS

This lab is worth 10 points.
Due: Friday January 18, 2008 at 5pm

Part 1: Forward Chaining

This lab will build upon the second part of Lab 1 where you coded the first rule of the book's inference chaining example. If you have not already, create a subdirectory off your home directory called cs356. Change to cs356 and create a file called lab2.clp. Copy rule 1 from Lab 1 into the file. Now add two new rules into the file for rules 2 and 3, which are as follows:
Rule 2: If X and B and E then Y
Rule 3: If A then X
Be sure to add printout functions in the consequence of each rule that tells you it has fired, just as was done for Rule 1.

Also code in the initial facts using JESS's deffacts construct. As in the book, the initial facts are A, B, C, D and E. The deffacts should look as follows:

(deffacts starting-facts "Initial facts"
   (A) (B) (C) (D) (E) 
)
You must put parentheses around each letter to indicate it is an individual fact and not part of a larger unordered fact.

When you have finished writing the rules and the deffacts construct, change to /ai/jess. Start up JESS using:

java jess.Main
Load your lab2.clp file by giving the following command:
(batch <your_home_dir>/cs356/lab2.clp)
Replace <your_home_dir> with the absolute path to your home directory. If you don't know the absolute path, type pwd while in your home directory and it will tell you. The $HOME environmental variable does not work from within JESS.

You will now be at the JESS prompt. Type (reset) to load your facts. (reset) will trigger the deffacts construct. If you type (facts) you should see the 5 initial facts. If you type (rules) you should see the 3 rules. If you type (agenda), you should see that Rule 3 is satisfied and waiting to fire. Now type (run) to start the inference engine. You should see that Rule 3, then Rule 2 then Rule 1 fired. If you type (facts) again, you should see X, Y and Z added to the fact base.

Part 2: Backwards Chaining

Copy lab2.clp to lab2b.clp. At the top of the file, add the following lines to tell JESS to do backwards chaining:
(do-backward-chaining Z)
(do-backward-chaining Y)
(do-backward-chaining X)
You will also need to add a new rule that has the following structure:
If Z then print Z has been asserted
Without such a rule, JESS would not know where to start for the backwards chaining.

Next, at the start of rules 1-3, you will need to add one more antecedent for backwards chaining in JESS to work. This special antecedent starts with the keyword need-. It tells JESS what facts the rule is going to assert. For example, Rule 1 asserts Z, so it would tell JESS that by adding (need-Z) to the left hand side of the rule like so:

(defrule rule1 "Rule 1"
   (need-Z)
   (Y)
   (D)
   =>
   (printout t "Rule 1 fired" crlf)
   (assert (Z))
)
Do the same for rules 2 and 3.

Save this file and switch back to /ai/jess. Start JESS and use the batch command to load this new file. Again, give the (reset) command to load the initial facts. Now type (facts) again. You should see three facts you didn't see with forward chaining: need-Z, need-Y and need-X. If you do not see these facts, then you did not properly add the new rule for "if Z" as directed above. Now when you type (run) you should again see rule 3, then rule 2 then rule 1 fire. Additionally, you should see the output of the "if Z" rule.

Lab Submissions

Email your lab2.clp and lab2b.clp files to me for your lab submission.