CLIPS
- Uses a Lisp-like syntax, in which everything(?) is
preceded and followed by parentheses.
- When you reset, the list of facts is set to only contain
(initial-fact)
- A rule’s LHS has 0 or more patterns, all of
which must match the current facts in order for the rule to fire
- If the rule has 0 patterns, it matches (initial-fact)
- The rule’s LHS is separated from its RHS by an arrow =>
- When a rule is ready to execute (all patterns match), the
rule is activated and put on the agenda.
- The rule to be fired is selected from the agenda.
- If more than one rule is on the agenda, the one to fire
is chosen based on “salience” (“Conflict resolution”)
- Salience can range from -10,000 to 10,000. Default
salience is 0.
- Lower salience rules will still get a chance after higher
salience rules
- If the agenda is empty, then the program stops execution.
- OR represented by creating a separate rule for the same
conclusion
- A rule’s RHS has 0 or more actions, all of which will be
carried out when the rule fires. Frequently these involve asserting or
retracting facts.
- Besides asserting facts, actions can include displaying
info, using the printout function
- A rule will not fire again on the exact same fact
- But if the fact is retracted and reasserted, it will fire
again)
-
Strategies
- rules on the agenda do not have to be executed in order of
salience as discussed above
- under Execution > Options can choose “Strategy” from
pull down choices (depth, breadth, complexity, simplicity, random, LEX,
MEA)
- Strategy influences when salience equal
- Depth – (rules with equal salience) – new activations are
placed on the agenda AHEAD of anything already on the agenda (with equal
salience)
Deffacts
- Can establish facts that will be set when a reset is done
(Deffacts aname
factsinalistofcourseeachsurroundedbyparens )
Watches
- Under Execution > Watches can select a variety of
things to watch. Some basic useful things include: Compilations, Facts,
Rules
Traces
- Following Lisp, called “dribble”
- Under File, can choose Turn Dribble On, dialog box allows
you to choose the file name.
- Later, under File, can choose Turn Dribble Off
Debugging
- Can single step (Execution > Step )
- While stopped can check matching of a rule to current
facts (matches rulename)
A Step Up – Variables
- Variables are indicated with a ? before the name
- Variables are “bound” to a value locally within
the rule during a given execution of the rule
- See print-answer rule of simple zoo forward chaining
example
- Variable gives flexibility in matching facts to LHS, RHS
then uses the bound value
- If the rule contains more than one instance of the same
variable on the LHS, the same value must be bound to it in both places
(defrule houseshape
(triangle ?tri)
(square ?sq)
(supports ?sq ?tri)
;;; only matches if
there can be found facts in which the square supports the triangle, NOT with
the following facts:
(triangle A)
(square B)
(square C)
(supports B C)
- Variables may also refer to a fact via address so that the
fact can be retracted
- See partially commented out mammal1 rule in zoofw
(non-simple)
(defrule mammal1
?f1 <- (hair)
=>
(assert (catg mammal))
(retract ?f1) ;
sometimes might want to retract a fact after using it
)
- ?f1 binds to the address of the fact that matches (hair)
(because of the <-
- RHS uses that variable to do the retraction
- This would be a weird example because we might need the
(hair) for other rules as well. More normal of the execution of the rule
changes some status (e.g. in diagnosis a fact could include that we need
to check the gas tank)
- Anonymous variables may be used to match as wildcards
(defrule color
(color black ?)
=>
;;; matches if
there is any fact involving two colors, the first being black
;;; would not match (color black)
or (color black white red) but would match
;;; (color black white)
- Multifield Wildcard – matches 0 or more fields
- $?
- See p48 of User’s Guide
Another Step Up – Control Facts (an idiom)
- Assert a fact that explicitly influences future processing
- (other rules look for that fact as part of their LHSs)
- See p62 of User’s Guide
Templates – allow you to define a structure for facts and
rules
- Similar to records/structures/
- A deftemplate is a list of named fields, called slots
- Deftemplate allows access by name rather than by order of
fields
- A slot is a single-slot (1 value) or multi-slot (0 or
more values)
(Deftemplate nameoftemplate
“ optional comment “
(slot slotname1
(type
SYMBOL)
(default
avalue) )
(slot slotname2
(type
SYMBOL) ;;; or NUMBER or STRING or
(default
avalue) )
…
)
- See template avh in zebrapuzzle problem
- Can also specify allowed values (allowed-symbols,
allowed-strings, allowed-numbers, allowed-integers, …)
- Main benefits is to impose order on facts and rules
- (also allows you to not have to worry about order)