Jess Information

Jess Home
Jess 7 Features
Download Now!
Online Demo

Documentation
FAQ
Manual
Mailing List
Jess Wiki

More information Related Web Sites
User Contributions
JSR94 Info
Developer's Log
About This Site

JESS ®, the Rule Engine for the JavaTM Platform

Jess Wiki: Multi Rule Program

Aim

To demonstrate one rule triggering another, and using both explicit and implicit deftemplates when using facts (asserts).

Method

Create 2 rules. One which is triggered on initialisation, looks for a car that is newer than 1991. It makes an assertion (newcar) which is picked up by the next rule which prints out 'that new car smell'

Explanation

  • (reset). Clears working memory. Not necessary, but docs seem to indicate it is good practice.

  • (deftemplate). Define a car type. It's only slot is year.

  • (defrule). This is triggered if a car is found with a year after 1991. If it finds one it asserts newcar. This is a fact that doesn't have an associated template. It has an implicit deftemplate.

  • (defrule). This is triggered when it finds a newcar fact. It merely prints out that new car smell

  • (deffunction) init. I like to initialise data in a single specified place, which I do here. I assert that there are 2 cars with different years. One of these will trigger rule_1. Can you guess which one?

  • (init). Call my initialisation function.

snippet
(reset)

;define the template (equiv. of class) 
(deftemplate car (slot year (type INTEGER)))

;define a rule to find fast cars
(defrule rule_1
    (car {year > 1991})
    =>
    (printout t "there is a new car" crlf)
    (assert (newcar))
    )

;define a rule to find fords
(defrule rule_2
    (newcar)
    =>
    (printout t "that new car smell!!!" crlf)
    )

;initialise the data
(deffunction init ()
    (assert (car (year 1992)))
    (assert (car (year 1990)))
    )

(init)
(run)

;should always be called
(run)
output
there is a new car
that new car smell!!!


Front Page | Sandbox | Recent Changes | Powered by Friki | Last Edited: 09 November 2008