CLIPS mainly used primitives

Primitive list and dialogs with the interpreter examples.

 

SYNTAX

CLIPS is case-sensitive
A string is delimited with "" -> Example "a string"
A comment line starts with a semi-colon ; -> Example ; a comment

In this page a word in capital letters represented in < > means that you must replace it (and the < >) by the thing described by the word itself, for example if I explain how to buid a simple sentence I would say : <SUBJECT> <VERB>, and you would write 'the cat eats'.

 

FACTS

Add a fact in the Facts Base

(assert (<FACT>))
(assert (<FACT-1>) (<FACT-2>) ...)

Ask about the FB content

(facts)

Delete the FB

(clear) ou (reset)

A fact can hav several attributes and the values are not only boolean

(assert (duck nil))
(assert (distance 1.5))

To remove one fact from the FB (Use with care)

(retract <FACT-NUMBER>)
(retract *) ; like (clear)

Saving the FB

(save-facts "<FILE-NAME>")

Loading a FB

(load-facts "<FILE-NAME>")

Fact definition

(deffacts <NAME> (<FACT-1>) (<FACT-2>) ...)

Removing facts defined as a group in a 'deffacts'

(undeffacts <NAME>)

 

TRACE - DEBUG

Tracing facts changes

(watch facts)

Stop tracing

(unwatch facts)

Other traces

(watch facts)
(watch instances) ; used with objects
(watch slots) ; used with objects
(watch rules)
(watch activations)
(watch messages) ; used with objects
(watch message-handlers) ; used with objects
(watch generic-functions)
(watch methods) ; used with objects
(watch deffunctions)
(watch compilations) ; on by default
(watch statistics)
(watch globals)
(watch focus)
(watch all) ; watch everything

 

RULES

Rule definition

IF Premisses THEN Conclusion

That is to say that all rules will have the same syntax and action : It checks if one facts in the fact base has a certain value (in the example below Fact-1 is checked with Value-X) and if it's true, it changes the vlaue of another fact of the fact base (here Fact-2 takes the value Value-Y).

IF FACT1 ?= VALUEX THEN FACT2 <- VALUEY

(defrule (FAIT-1) => (assert (FAIT-2)))

Examples

(defrule duck (animal-is duck) => (assert (sound-is quack)))
(defrule dog (animal-is dog) => (assert (sound-is ouaouh)))
(defrule maj (age-john 18) => (assert (majority-john yes)))

Rules that can be used

(agenda)

Starting the inference engine

(run)

Saving rules

(save"FILE-NAME.clp")

Printing a rule

(ppdefrule NOM-REGLE)

Rules in the rule base

(rules)

To re-use a rule

(refresh NOM-REGLE)

Waiting premisses for a rule

(matches NOM-REGLE)

 

VARIABLES

A variable is an anonyme symbol begining with a ?

Exemple

(defrule make-quack

(duck-sound ?sound) => (assert (sound-is ?sound)))

A variable can also be manually linked (unified) or unliked.

(defrule get-married

?duck <- (bachelor Dopey)
=>
(printout t "Dopey is now happily married " ?duck crlf)
(retract ?duck)
)

Example

CLIPS> (clear)
CLIPS> (defrule marriage
?duck <- (bachelor ?name)
=>
(printout t ?name " is now happily married" crlf)
(retract ?duck))
CLIPS> (deffacts good-prospects
(bachelor Dopey)
(bachelor Dorky)
(bachelor Dicky))
CLIPS> (reset)
CLIPS> (run)
Dicky is now happily married
Dorky is now happily married
Dopey is now happily married

 

ARISTOTLE SYLLOGISM

Let us try to make our inference ingine deduce that Socrates is mortal.

You can copy/paste all the lines of the dialog below (after the 'CLIPS>' prompt, of course!)


CLIPS> (clear)
==> f-0 (initial-fact)

CLIPS> (defrule soc (man ?x) => (assert (mortal ?x)))

CLIPS> (assert (man socrate))
==> f-1 (man socrate)
<Fact-1>

CLIPS> (agenda)
0 soc: f-1
For a total of 1 activation.

CLIPS> (run)
==> f-2 (mortal socrate)

CLIPS> (facts)
f-0 (initial-fact)
f-1 (man socrate)
f-2 (mortal socrate)
For a total of 3 facts.

 

FUNCTIONS

You can define your own functions, if necessary

CLIPS> (deffunction pms (?a ?b) (- (* ?a ?b) (+ ?a ?b)))
CLIPS> (pms 3 4)
5

 

INPUTS - OUTPUTS

Affichage de texte dans la conclusion d'une rËgle
(printout t "TEXTE-A-AFFICHER" crlf)

EntrÈe clavier
(read)

Exemple
CLIPS> (defrule read-input
=>
(printout t "Name a primary color" crlf)
(assert (color (read))))

 

PREDEFINED FUNCTIONS

Logical

not : Boolean not
and : Boolean and
or : Boolean or

Arithmetic

/ division
* multiplication
+ addition
- subtraction

Comparison

eq equal (any type). Compares type and magnitude
neq not equal (any type)
= equal (numeric type). Compares magnitude
<> not equal (numeric type)
>= greater than or equal to
> greater than
<= less than or equal to
< less than

 

OBJECT ORIENTED PROGRAMMING

Class

Root class: OBJECT
Subclass of OBJECT for users: USER

Class definition
(defclass <CLASS-NAME> (is-a <MOTHER-CLASS-NAME>))

Examples
(defclass ANIMAL (is-a USER))
(defclass DUCK (is-a ANIMAL)
(slot sound)
(slot age))

Class list
(list-defclasses)

Class hierarchy from given class
(browse-classes OBJECT)

Class description (inspect)
(describe-class <CLASS-NAME>)

Instances

To convert a symbol to an instance name

CLIPS> (symbol-to-instance-name Dorky_Duck)
[Dorky_Duck]

And the opposite function

CLIPS> (instance-name-to-symbol [Dorky_Duck])
Dorky_Duck

The make-instance function is used to make an instance object

(make-instance [<INSTANCE-NAME>] of <CLASS-NAME> <SLOT-OVERRIDE>)

Instances list

(instances)

Define instances after a reset
(definstances DORKY_OBJECTS
(Dorky of DUCK)
(Spot of DOG))
puis faire un
(reset)
pour voir les instances apparaitre

To delete one or all instances
(unmake-instance *)
(send [<instance-name>] delete)

Message sending
(send [<instance-name>] <message>)

Any slot name has an associed method named put-<SLOT-NAME>
To change the value of a slot in a rule, simply enter
(send <INSTANCE-NAME> put-<SLOT-NAME> <VALUE>)
Example
(send [Donald] put-sound quack)

Instance view
(send [<INSTANCE-NAME>] print)

Example of use

CLIPS> (defclass ANIMAL (is-a USER) (slot age))

CLIPS> (browse-classes ANIMAL)
ANIMAL

CLIPS> (defclass DUCK (is-a ANIMAL) (slot sound))

CLIPS> (describe-class DUCK)
================================================================================
********************************************************************************
Concrete: direct instances of this class can be created.
Reactive: direct instances of this class can match defrule patterns.

Direct Superclasses: ANIMAL
Inheritance Precedence: DUCK ANIMAL USER OBJECT
Direct Subclasses:
--------------------------------------------------------------------------------
SLOTS : FLD DEF PRP ACC STO MCH SRC VIS CRT OVRD-MSG SOURCE(S)
age : SGL STC INH RW LCL RCT EXC PRV RW put-age ANIMAL
sound : SGL STC INH RW LCL RCT EXC PRV RW put-sound DUCK

Constraint information for slots:

SLOTS : SYM STR INN INA EXA FTA INT FLT
age : + + + + + + + + RNG:[-oo..+oo]
sound : + + + + + + + + RNG:[-oo..+oo]
--------------------------------------------------------------------------------

...

********************************************************************************
================================================================================

CLIPS> (make-instance [Donald] of DUCK (age 42) (sound quack))
[Donald]

CLIPS>

 

Example

Everybody who is older than 18 is an adult

CLIPS> (defclass HOMME (is-a USER) (slot age) (slot majority))
CLIPS> (make-instance [Jean] of HOMME (age 19))
[Jean]
CLIPS> (make-instance [Luc] of HOMME (age 17))
[Luc]
CLIPS> (make-instance [Paul] of HOMME (age 12))
[Paul]
CLIPS> (make-instance [Pierre] of HOMME (age 65))
[Pierre]

CLIPS> (instances)
[initial-object] of INITIAL-OBJECT
[Donald] of DUCK
[Jean] of HOMME
[Luc] of HOMME
[Paul] of HOMME
[Pierre] of HOMME
For a total of 6 instances.

CLIPS> (defrule majority
?H <- (object (is-a HOMME)
(age ?A))
(test (> ?A 18))
=>
(send ?H put-majority True))

CLIPS> (send [Jean] print)
[Jean] of HOMME
(age 19)
(majority nil)

CLIPS> (run)

CLIPS> (send [Jean] print)
[Jean] of HOMME
(age 19)
(majority True)

Screen :

 


ICAM Toulouse - Last update: 30-Sep-2013