Algernon Tutorial 3.c
Forward-chaining rules


previous   next

These exercises and examples use the newspaper project included in the examples folder of your Protege installation.

:ADD-RULE

Adds a rule that is triggered when changes occur in a specified frame or one of its children. You can copy this Algernon code to a file and execute it by running Algernon standalone. Or else you can enter it in the Algernon tab inside Protege.
;; this line is unnecessary if you are working inside Protege
;; and you have the newspaper example loaded.
(tell ((:USE-KB :KB-1 Protege "/Applications/Protege-2000/examples/newspaper/newspaper.pprj")))

;;  It is a booming economy, so assign every Editor to the Business section.
;; instance_00048 is the Business section.

(tell  ((:ADD-RULE Editor
          ((date_hired ?editor ?date)
           ->
           (sections ?editor instance_00048)))))

;; Test the rule --------------------------------------

;; Should fail.
;; instance_00068 is "Ms Gardiner"
(ask ((sections instance_00068 ?section)))

;; This should trigger the rule.
(tell ((date_hired instance_00068 "08-Feb-2003")))

;; This should now succeed.
(ask  ((sections instance_00068 ?section)))

Notes

You can add more than one rule at a time. :ADD-RULE accepts multiple rules in the same form.

Chaining

The consequent of a forward chaining rule is executed in assert (tell) mode, so it may assert new facts. These in turn can cause more forward chaining rules to fire.

A more complex example

When laying out a newspaper, every rectangle must be
   between 3 and 15 square inches.

(tell ((:ADD-RULE Rectangle
        ((width  ?rectangle ?width)
         ->
         (height ?rectangle ?height)
         (:bind ?area (:LISP (* ?width ?height)))
         (:fail (:test (:LISP (<= 3 ?area 15))))
         (name ?rectangle ?name)
         (:PRINTLN "Warning: rectangle " ?name " has an out of limits area of " ?area)))))

(tell (;;(:trace :verbose)
       (:add-instance (?r1 Rectangle) (:name ?r1 "Rect1") (name ?r1 "Rect1") (width ?r1 1.0) (height ?r1 5.0))
       (:add-instance (?r2 Rectangle) (:name ?r2 "Rect2") (name ?r2 "Rect2") (width ?r2 2.0) (height ?r2 1.0))
       (:add-instance (?r3 Rectangle) (:name ?r3 "Rect3") (name ?r3 "Rect3") (width ?r3 3.0) (height ?r3 5.0))
       (:add-instance (?r4 Rectangle) (:name ?r4 "Rect4") (name ?r4 "Rect4") (width ?r4 4.0) (height ?r4 3.0))
       (:add-instance (?r5 Rectangle) (:name ?r5 "Rect5") (name ?r5 "Rect5") (width ?r5 6.3) (height ?r5 1.6))
       (:add-instance (?r6 Rectangle) (:name ?r6 "Rect6") (name ?r6 "Rect6") (width ?r6 6.0) (height ?r6 3.8))
       ))
Notes:
previous   next
Author: Micheal S. Hewett
Email: hewett@cs.stanford.edu
Last Updated: Saturday, February 8, 2003