clips

Upload: ivan-dario-sanchez-moreno

Post on 07-Jan-2016

3 views

Category:

Documents


0 download

DESCRIPTION

presenttancion clips

TRANSCRIPT

The CLIPS Expert System Shell

The CLIPS Expert System ShellDr Nicholas [email protected] CLIPS Expert System ShellA framework for building expert systemsDont need to build basic infrastructure every time you build an expert systemCommonly used and well-understoodFirst developed at NASA JSC in mid-1980sProvides the following facilities:Representation of facts and rulesSearch mechanism for identifying and firing matching rules

Facts in CLIPSRepresented in CLIPS by s-expressions:(likes fred sally)likes, fred and sally are symbolsTwo types of CLIPS factOrdered (as above)Unordered, specified using deftemplate (well come to this later)Starting and stopping CLIPSRun CLIPS from the UNIX command line:

CLIPS>To quit CLIPS, type (exit)Asserting factsAdd a new fact to the knowledge base with assert:CLIPS> (assert (likes fred mary))

CLIPS>Check for asserted facts with facts:CLIPS> (facts)f-0 (likes fred mary)For a total of 1 fact.CLIPS> The 0 in f-0 and Fact-0 is the fact-index of the new factRetracting factsRemove a fact from the knowledge base with retract:CLIPS> (assert (likes fred mary))

CLIPS> (facts)f-0 (likes fred mary)For a total of 1 fact.CLIPS> (retract 0)CLIPS> (facts)CLIPS>Note that you need to know the fact-index to retract a factRemove all facts from the knowledge base using (clear)Watching the knowledge baseObserve the activity in the knowledge base using watch:CLIPS> (watch facts)CLIPS> (assert (likes fred mary))==> f-0 (likes fred mary)

CLIPS> (retract 0) ==> indicates that a fact is being added

(assert (loved mary)))The name of the ruleThe rule conditionThe rule actionRules in general(defrule optional comment...)Everything before => is known as the left-hand side (LHS), everything after the right-hand side (RHS)The rule is fired if all of the conditions in the LHS matchWhen the rule fires, all of the actions are carried outThe AgendaThe list of rules which are waiting to be fired (the conflict set) is known as the agendaThis can be examined using (agenda):CLIPS> (agenda)0 mary-loved: f-0For a total of 1 activation.CLIPS>Running your rulesStart executing your rules with run:CLIPS> (run)==> f-1 (loved mary)CLIPS>Execution continues until there are no rules left to be fired (the agenda is empty)Can control number of rule firings with (run ) VariablesWe can have variables in the conditions of our rules:(defrule loved(likes ?sub ?obj) =>(assert (loved ?obj)))VariablesCLIPS> (assert (likes fred mary))==> f-0 (likes fred mary)

CLIPS> (assert (likes fred sue))==> f-1 (likes fred sue)

CLIPS> (agenda)0 loved: f-10 mary-loved: f-00 loved: f-0For a total of 3 activations.CLIPS> (run)==> f-2 (loved sue)==> f-3 (loved mary)CLIPS> Loading from filesIf your rules are defined in a file called myrules.clpAt the shell command line:

$ clips -f myrules.clpAt the CLIPS command line:

CLIPS> (load myrules.clp)Retracting facts(defrule remove-liked?fact (retract ?fact))

(printout t Route from A to B via ?towns crlf))Multifield variablesWe have a fact:(route a b c d e f g h)In how many ways can this be matched by this pattern?(route $?start ?item $?end)Multifield variables$?start = (), ?item = a, $?end = (b c d e f g h)$?start = (a), ?item = b, $?end = (c d e f g h)$?start = (a b), ?item = c, $?end = (d e f g h)$?start = (a b c), ?item = d, $?end = (e f g h)$?start = (a b c d), ?item = e, $?end = (f g h)$?start = (a b c d e), ?item = f, $?end = (g h)$?start = (a b c d e f), ?item = g, $?end = (h)$?start = (a b c d e f g), ?item = h, $?end = ()Field ConstraintsWe can further constraint the values taken by variables ~symbol matches anything but symbol symbol1|symbol2 matches either symbol1 or symbol2 symbol1&symbol2 matches both symbols ?var&constraint matches anything that satisfies the constraint, and assigns it to ?var

Test ConditionsLHS of rule can contain other sorts of constraints on variable values (see BPG p. 47)For example:(defrule my-rule(age fred ?var1)(age sally ?var2)(test (> ?var1 ?var2))=>)

FunctionsCLIPS has a number of built-in functions which can be invoked like their Scheme look-a-likes:(+ 1 2)Complete list of built-in functions starting at p.149 of the CLIPS reference manual (Basic Programming Guide) Defining functionsWe can define a custom function using deffunction:(deffunction square(?x)(* ?x ?x))Defining functions(deffunction function-name(?arg1 ?arg2 ... $?args)(action1 action2 ... actionn))Value returned by function is that of last actionFinal argument may be a multifield variableBinding variablesWe can bind the result of a function to a variable:(bind ?sum (+ ?x ?y))

Example: Route FindingECBDA574283Example: Route FindingRepresent routes as facts of the form:(route start end distance)For example:(route town-a town-b town-d town-e 16)How can we generate all of the shortest routes from every town to every other town?Writing outputWe can write to stdout using printout:(defrule print-likes(likes ?x ?y)=>(printout t ?x likes ?y crlf))prints:john likes sallyReading input(defrule read-colour(initial-fact)=>(printout t Name a colour crlf)(assert (colour (read))))(read) reads a single symbol, integer or string(readline) reads until the next carriage returnTemplated factsThe ordered lists of terms weve seen so far are only one kind of factWe can also create facts containing unordered (but named) terms:(car (colour red) (doors 4) (maker vw))is the same as:(car (doors 4) (maker vw) (colour red))Templated factsWe introduce a new type of templated fact as follows:(deftemplate car(slot registration(type SYMBOL))(slot color(type SYMBOL)(allowed-symbols white red green blue))(slot doors(type INTEGER)(default 4))(slot maker(type SYMBOL)))Templated factsTemplated facts are asserted like any other fact:

(assert (car (registration g885tnj)(doors 4)(maker vw)(colour green)))Example: ClassificationWe wish to build an expert system to classify the following farmyard animals:DogCowsChickensDucksPigsExample: ClassificationPossible means for classification:Fur/feathers/skinNumber of legsSwims/doesnt swimEats meat/grass/grainExample: Classification(deftemplate observation(slot name(type SYMBOL))(slot value))(deftemplate classification(slot name(type SYMBOL)))