190 likes | 430 Views
CLIPS. CLIPS is a rule-based forward reasoning knowledge based systems shell based on Rete pattern matching algorithm used originally in OPS5. CLIPS stands for: C Language Implementation Production System Developed by The AI section of the Johnson Space Center of NASA CLIPS supports
E N D
CLIPS • CLIPS is a rule-based forward reasoning knowledge based systems shell based on Rete pattern matching algorithm used originally in OPS5. • CLIPS stands for: • C Language Implementation Production System • Developed by • The AI section of the Johnson Space Center of NASA • CLIPS supports • Rule-based • Object oriented (COOL: CLIPS Object Oriented Language) • Procedural programming paaradigms.
Attributes of CLIPS High Portability Low Cost High Degree of Integration with External Programs To start working with CLIPS you issue the corresponding command depending on the machine used. CLIPS prompt will appear usually in a window: CLIPS> (assert (a) (b) (c)) <Fact-3> CLIPS> (facts) f-0(initial-fact) f-1(a) f-2(b) f-3(c) For a total of 4 facts. CLIPS>(exit) ;ctrl Q CLIPS CLIPS>(reset) CLIPS>(facts) f-0 (initial-fact) For a total of 1 fact. CLIPS>
Facts A fact consistes of a relation name followed by one or more slots. Here is an example of a fact (plays john mary netball) One way of entering facts into CLIPS is (assert (play john mary netball)) (assert (likes john mary)) CLIPS> (reset) CLIPS> (facts) f-0 (initial-fact) For a total of 1 fact. CLIPS> (assert (play john mary netball)) <Fact-1> CLIPS> (assert (likes john mary)) <Fact-2> CLIPS> Returned from CLIPS
Fact Index Fact Identifier CLIPS>(facts) f-0 (initial-fact) f-1 (play john mary netball) f-2 (likes john mary) For a total of 3 facts. CLIPS> To delete facts from CLIPS fact list: CLIPS> (retract 1) CLIPS> (facts) f-0 (initial-fact) f-2 (likes john mary) For a total of 2 facts. CLIPS>
To enter a fact with multiple slots you must declare its template using the deftemplate construct. We use this construct to decalre the structure of a group of facts. The general form of the deftemplate construct is: (deftemplate <relation-name> [<optional-comment>] <slot-definitions>*) Slots are defined as: (slot <slot-name>) | (multislot <slot-name>) CLIPS> (deftemplate person "an example deftemplate" (slot name) (slot age) (slot eye-color) (slot hair-color)) CLIPS> (assert (person (name "John Durkin")(age 43) (eye-color blue)(hair-color brown))) <Fact-3> CLIPS>
CLIPS> (facts) f-2 (likes john mary) f-3 (person (name "John Durkin") (age 43) (eye-color blue) (hair-color brown)) For a total of 2 facts. CLIPS> deftemplate can be implicit when we use ordered facts (used earlier): (numbers 1 2 3) (likes john mary)
Some CLIPS Commands • (exit) to exit from CLIPS • (clear) to clear the environment from active definitions • (reset) to set the fact base to its initial state clears all and sets (initial-fact). Use (reset) before run command • (load “filename.clp”) to load a program from the file namedfilename.clp into CLIPS. • (run) executes a program loaded into CLIPS • (facts) to display the fact list • (rules) to display the rules in the rule base.
An Example • Using an editor create a file containing the following rule: • (defrule my-first • (initial-fact) • => • (printout t "First CLIPS program executed" crlf) • ) CLIPS CLIPS> (reset) CLIPS> (facts) f-0 (initial-fact) For a total of 1 fact. CLIPS> (load "test.clp") Defining defrule: my-first +j TRUE CLIPS> (run) First CLIPS program executed CLIPS>
Rules If the emergency is a fire Thenthe response is to activate the sprinkler system Lets define this rule. Before defining the rule we use deftemplates to define emergencies and responses: (deftemplate emergency (slot type)) (deftemplate response (slot action)) ; Rule header (defrule fire-emergency “an example rule” ; pattern (emergency (type fire)) ; arrow => ; action (assert (response (action activate-sprinkler-system))))
General format of rules (defrule <rule-name> [<comment>] <patterns>* ; LHS of the rule => <actions>* ) ; RHS of the rule A rule may have no conditional element (in which case initial fact is its conditional element) or no action part. Or they may have multiple patterns or actions: • (defrule is-it-a-duck (animal-has webbed-feet) • (animal-has feathers) • => • (assert (animal-is duck))) • (defrule duck (animal-is duck) • => • (assert (sound-is quack)) • (printout t "it’s a duck" crlf))
The run command is used to run a CLIPS program (run [ <limit>] ) ; limit is the maximum number of rules we want to fire Rules that match facts are put on the agenda and fire according to their priority. We may display the agena: (agenda) CLIPS> (reset) CLIPS> (deftemplate emergency (slot type)) CLIPS> (deftemplate response (slot action)) CLIPS> (assert (emergency (type fire))) <Fact-2> CLIPS> (defrule fire-emergency "an example rule" ; pattern (emergency (type fire)) =>; arrow (assert (response (action activate-sprinkler-system)))); action CLIPS> (agenda) 0 fire-emergency: f-2 For a total of 1 activation. CLIPS> agenda agenda CLIPS>
CLIPS> (run) CLIPS> (facts) f-0 (initial-fact) f-1 (emergency (type fire)) f-2 (response (action activate-sprinkler-system)) For a total of 3 facts. Rules have a property called refraction which means that they will fire only once for the same set of facts. If run is issued again the rule won’t fire again. We can however force this to happen by retracting and asserting the fact again or by using refresh to refresh the rule: (refresh fire-emergency)
Variables ?name ?color ?status ?value ?x A variable may be bound to a value or even a fact address. CLIPS> (clear) CLIPS> (deftemplate person (slot name) (slot eyes) (slot hair))
CLIPS> (defrule find-blue-eyes (person (name ?name) (eyes blue)) => (printout t ?name " has blue eyes." crlf)) CLIPS> (deffacts people (person (name Jane) (eyes blue) (hair red)) (person (name Joe) (eyes green) (hair brown)) (person (name Jack) (eyes blue) (hair black)) (person (name Jeff) (eyes green) (hair brown))) CLIPS> (reset) CLIPS> (run) Jack has blue eyes. Jane has blue eyes. CLIPS>
CLIPS> (defrule list-animals (animal ?name) => (printout t ?name " found" crlf)) CLIPS>(run) haddock found duck found dog found CLIPS>
CLIPS> (defrule mammal (animal ?name) (warm-blooded ?name) (not (lays-eggs ?name)) => (assert (mammal ?name)) (printout t ?name " is a mammal" crlf)) CLIPS> (deffacts startup (animal dog) (animal duck) (animal haddock)(animal cat)) CLIPS> (deffacts warm-blooded (warm-blooded cat) (warm-blooded dog)) CLIPS> (reset) CLIPS> (run) cat is a mammal dog is a mammal
Functions and expressions CLIPS> (+ 2 3) 5 CLIPS> (+ 2 3.0 4) 9.0 CLIPS> (/ 6 2) 3.0 CLIPS> (/ 6 2 3) 1.0 CLIPS> (clear) CLIPS> (assert (answer (+ 2 2))) <Fact-0> CLIPS> (facts) f-0 (answer 4) For a total of 1 fact. CLIPS> (Y2 – y1) / (x2 – x1) > 0 (> (/ (- y2 y1) (- x2 x1)) 0)
I/O CLIPS> (defrule what-is-child (animal ?name) (not (child-of ?name ?)) => (printout t "What do you call the child of a " ?name "?") (assert (child-of ?name (read)))) CLIPS> (run) What do you call the child of a haddock?Hoddy What do you call the child of a duck?haddock In an Automobile Diagnosis Expert System CLIPS> (defrule are-lights-working (not (lights-working ?)) => (printout t "Are the car's lights working (yes or no)?") (assert (lights-working (read))))
Functions (deffunction yes-or-no-p (?question) (bind ?response (ask-question ?question yes no y n)) (if (or (eq ?response yes) (eq ?response y)) then TRUE else FALSE))