Controling The Angenda
Controling The Angenda
The declare(salience) command allows you to have control over which rules are put on the
agenda but you need to use it carefully. Salience is a kind of rule priority.
Activated rules of the highest salience will fire first, followed by rules of lower salience. You
include the salience declaration in the definition of the rule.
The example below defines 3 rules with varying salience to fire when the object light-is has a
value red. Play around with your traffic-light example.
Jess> (agenda)
[Activation: MAIN::traffic-light1 f-1 ; time=2 ; salience=150]
[Activation: MAIN::traffic-light3 f-1 ; time=2 ; salience=120]
[Activation: MAIN::traffic-light f-1 ; time=2 ; salience=100]
For a total of 3 activations.
Jess> (run)
FIRE 1 MAIN::traffic-light1 f-1
STOP NOW!!!!
FIRE 2 MAIN::traffic-light3 f-1
STOP STOP
FIRE 1 MAIN::traffic-light f-1
STOP
3
2. Conflict Resolution
Jess offers two modes of conflict resolution : depth (last in first out) and breadth (first in first
out).
When depth strategy is in effect, this is the default, more recently activated rules will be fired
before less recently activated rules of the same salience.
When breadth strategy is active, rules of the same salience fire in the order in which they
were activated.
The command set-strategy allows you specify the conflict resolution strategy Jess uses.
Usage
(set-strategy (depth))
(set-strategy (breadth))
4. wildcards
A question mark ?
matches any single field within a fact
multi-field wildcard $?
matches zero or more fields in a fact
5. Constraints
not constraint ~
the field can take any value except the one specified
or constraint |
specifies alternative values, one of which must match
and constraint &
the value of the field must match all specified values
mostly used to place constraints on the binding of a variable
basic operators (+,-,*,/) and many functions (trigonometric, logarithmic, exponential) are
supported
Jess>
(defrule silly-eye-hair-match
(person (name ?name1)
(eye-color ?eyes1&blue|green)
(hair-color ?hair1&~black))
(person (name ?name2&~?name1)
(eye-color ?eyes2&~eyes1)
(hair-color ?hair2&brown|hair1))
=>
(printout t ?name1 " has "?eyes1 " eyes and " ?hair1 " hair." crlf)
(printout t ?name2 " has "?eyes2 " eyes and " ?hair2 " hair." crlf))
Jess> (run)
Tony has green eyes and red hair.
Dave has brown eyes and brown hair.
Tom has blue eyes and blonde hair.
Dave has brown eyes and brown hair.
(if previous rule also exists also get)
Tom has blue eyes.
Carol has blue eyes.