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: Simple Count Program

Aim

To demonstrate a simple counter that would output a series of numbers.

Method

Write a rule that would trigger when a counter was less than a cetain value, which increment that counter and output the number.

Explanation

  • (reset) this cleans out all facts from working memory. Not required, but seems to be recommended in the docs.

  • deftemplate define a type, with one slot, a counter. The default isn't required, but for documentation purposes, I put it in.

  • defrule consists of facts (check current assertions, in LHS (Left Hand Side)) and then, on the RHS (Right Hand Side).

  • bind this statement increments the count slot in the counter. A bit verbose considering how common incr is.

  • deffunction. I like to have all initialising done in a single call, which I do here. Not really necessary, but good practice, I reckon.

  • (init). Initialise by calling the init function previously defined.

  • (run). Run the program.

snippet - cut and paste this, and run it in your environment
 (reset) 

 ; define the counter
 (deftemplate counter (slot count (type integer) (default 0)))

 ; define the rule
 (defrule switchme
     ?p <- (counter {count < 10})
     =>
     (printout t ?p.count crlf)
    (bind ?p.count (+ ?p.count 1))
     )

 ; initialise counter
 (deffunction init ()
     (assert (counter (count 1)))
    )

 ;initialise program
 (init)

 ;run program
 (run)
and your output will be
1
2
3
4
5
6
7
8
9


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