1 / 11

Rules

Rules. If the emergency is a fire Then the 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

Download Presentation

Rules

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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))))

  2. 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))

  3. 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>

  4. 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)

  5. 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))

  6. 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>

  7. CLIPS> (defrule list-animals (animal ?name) => (printout t ?name " found" crlf)) CLIPS>(run) haddock found duck found dog found CLIPS>

  8. 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

  9. 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)

  10. 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))))

  11. 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))

More Related