home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / programming / clips_2 / CLIPS / Examples / Animal next >
Encoding:
Text File  |  1993-06-02  |  14.2 KB  |  394 lines

  1. ;;;======================================================
  2. ;;;   Animal Identification Expert System
  3. ;;;
  4. ;;;     A simple expert system which attempts to identify
  5. ;;;     an animal based on its characteristics.
  6. ;;;     The knowledge base in this example is a 
  7. ;;;     collection of facts which represent backward
  8. ;;;     chaining rules. CLIPS forward chaining rules are
  9. ;;;     then used to simulate a backward chaining inference
  10. ;;;     engine.
  11. ;;;
  12. ;;;     CLIPS Version 6.0 Example
  13. ;;; 
  14. ;;;     To execute, merely load, reset, and run.
  15. ;;;     Answer questions yes or no.
  16. ;;;======================================================
  17.  
  18. ;;;***************************
  19. ;;;* DEFTEMPLATE DEFINITIONS *
  20. ;;;***************************
  21.  
  22. (deftemplate rule 
  23.    (multislot if)
  24.    (multislot then))
  25.  
  26. ;;;**************************
  27. ;;;* INFERENCE ENGINE RULES *
  28. ;;;**************************
  29.  
  30. (defrule propagate-goal ""
  31.    (goal is ?goal)
  32.    (rule (if ?variable $?)
  33.          (then ?goal ? ?value))
  34.    =>
  35.    (assert (goal is ?variable)))
  36.  
  37. (defrule goal-satified ""
  38.    (declare (salience 30))
  39.    ?f <- (goal is ?goal)
  40.    (variable ?goal ?value)
  41.    (answer ? ?text ?goal)
  42.    =>
  43.    (retract ?f)
  44.    (format t "%s%s%n" ?text ?value))
  45.  
  46. (defrule remove-rule-no-match ""
  47.    (declare (salience 20))
  48.    (variable ?variable ?value)
  49.    ?f <- (rule (if ?variable ? ~?value $?))
  50.    =>
  51.    (retract ?f))
  52.  
  53. (defrule modify-rule-match ""
  54.    (declare (salience 20))
  55.    (variable ?variable ?value)
  56.    ?f <- (rule (if ?variable ? ?value and $?rest))
  57.    =>
  58.    (modify ?f (if ?rest)))
  59.  
  60. (defrule rule-satisfied ""
  61.    (declare (salience 20))
  62.    (variable ?variable ?value)
  63.    ?f <- (rule (if ?variable ? ?value)
  64.                (then ?goal ? ?goal-value))
  65.    =>
  66.    (retract ?f)
  67.    (assert (variable ?goal ?goal-value)))
  68.  
  69. (defrule ask-question-no-legalvalues ""
  70.    (declare (salience 10))
  71.    (not (legalanswers $?))
  72.    ?f1 <- (goal is ?variable)
  73.    ?f2 <- (question ?variable ? ?text)
  74.    =>
  75.    (retract ?f1 ?f2)
  76.    (format t "%s " ?text)
  77.    (assert (variable ?variable (read))))
  78.  
  79. (defrule ask-question-legalvalues ""
  80.    (declare (salience 10))
  81.    (legalanswers ? $?answers)
  82.    ?f1 <- (goal is ?variable)
  83.    ?f2 <- (question ?variable ? ?text)
  84.    =>
  85.    (retract ?f1)
  86.    (format t "%s " ?text)
  87.    (printout t ?answers " ")
  88.    (bind ?reply (read))
  89.    (if (member (lowcase ?reply) ?answers) 
  90.      then (assert (variable ?variable ?reply))
  91.           (retract ?f2)
  92.      else (assert (goal is ?variable))))
  93.  
  94. ;;;***************************
  95. ;;;* DEFFACTS KNOWLEDGE BASE *
  96. ;;;***************************
  97.  
  98. (deffacts knowledge-base 
  99.    (goal is type.animal)
  100.    (legalanswers are yes no)
  101.    (rule (if backbone is yes) 
  102.          (then superphylum is backbone))
  103.    (rule (if backbone is no) 
  104.          (then superphylum is jellyback))
  105.    (question backbone is "Does your animal have a backbone?")
  106.    (rule (if superphylum is backbone and
  107.           warm.blooded is yes) 
  108.          (then phylum is warm))
  109.    (rule (if superphylum is backbone and
  110.           warm.blooded is no) 
  111.          (then phylum is cold))
  112.    (question warm.blooded is "Is the animal warm blooded?")
  113.    (rule (if superphylum is jellyback and
  114.           live.prime.in.soil is yes) 
  115.          (then phylum is soil))
  116.    (rule (if superphylum is jellyback and
  117.           live.prime.in.soil is no) 
  118.          (then phylum is elsewhere))
  119.    (question live.prime.in.soil is "Does your animal live primarily in soil?")
  120.    (rule (if phylum is warm and
  121.           has.breasts is yes) 
  122.          (then class is breasts))
  123.    (rule (if phylum is warm and
  124.           has.breasts is no) 
  125.          (then type.animal is bird/penguin))
  126.    (question has.breasts is "Normally, does the female of your animal nurse its young with milk?")
  127.    (rule (if phylum is cold and
  128.           always.in.water is yes) 
  129.          (then class is water))
  130.    (rule (if phylum is cold and
  131.           always.in.water is no) 
  132.          (then class is dry))
  133.    (question always.in.water is "Is your animal always in water?")
  134.    (rule (if phylum is soil and
  135.           flat.bodied is yes) 
  136.          (then type.animal is flatworm))
  137.    (rule (if phylum is soil and
  138.           flat.bodied is no) 
  139.          (then type.animal is worm/leech))
  140.    (question flat.bodied is "Does your animal have a flat body?")
  141.    (rule (if phylum is elsewhere and
  142.           body.in.segments is yes) 
  143.          (then class is segments))
  144.    (rule (if phylum is elsewhere and
  145.           body.in.segments is no) 
  146.          (then class is unified))
  147.    (question body.in.segments is "Is the animals body in segments?")
  148.    (rule (if class is breasts and
  149.           can.eat.meat is yes) 
  150.          (then order is meat))
  151.    (rule (if class is breasts and
  152.           can.eat.meat is no) 
  153.          (then order is vegy))
  154.    (question can.eat.meat is "Does your animal eat red meat?")
  155.    (rule (if class is water and
  156.           boney is yes) 
  157.          (then type.animal is fish))
  158.    (rule (if class is water and
  159.           boney is no) 
  160.          (then type.animal is shark/ray))
  161.    (question boney is "Does your animal have a boney skeleton?")
  162.    (rule (if class is dry and
  163.           scally is yes) 
  164.          (then order is scales))
  165.    (rule (if class is dry and
  166.           scally is no) 
  167.          (then order is soft))
  168.    (question scally is "Is your animal covered with scaled skin?")
  169.    (rule (if class is segments and
  170.           shell is yes) 
  171.          (then order is shell))
  172.    (rule (if class is segments and
  173.           shell is no) 
  174.          (then type.animal is centipede/millipede/insect))
  175.    (question shell is "Does your animal have a shell?")
  176.    (rule (if class is unified and
  177.           digest.cells is yes) 
  178.          (then order is cells))
  179.    (rule (if class is unified and
  180.           digest.cells is no) 
  181.          (then order is stomach))
  182.    (question digest.cells is "Does your animal use many cells to digest it's food instead of a stomach?")
  183.    (rule (if order is meat and
  184.           fly is yes) 
  185.          (then type.animal is bat))
  186.    (rule (if order is meat and
  187.           fly is no) 
  188.          (then family is nowings))
  189.    (question fly is "Can your animal fly?")
  190.    (rule (if order is vegy and
  191.           hooves is yes) 
  192.          (then family is hooves))
  193.    (rule (if order is vegy and
  194.           hooves is no) 
  195.          (then family is feet))
  196.    (question hooves is "Does your animal have hooves?")
  197.    (rule (if order is scales and
  198.           rounded.shell is yes) 
  199.          (then type.animal is turtle))
  200.    (rule (if order is scales and
  201.           rounded.shell is no) 
  202.          (then family is noshell))
  203.    (question rounded.shell is "Does the animal have a rounded shell?")
  204.    (rule (if order is soft and
  205.           jump is yes) 
  206.          (then type.animal is frog))
  207.    (rule (if order is soft and
  208.           jump is no) 
  209.          (then type.animal is salamander))
  210.    (question jump is "Does your animal jump?")
  211.    (rule (if order is shell and
  212.           tail is yes) 
  213.          (then type.animal is lobster))
  214.    (rule (if order is shell and
  215.           tail is no) 
  216.          (then type.animal is crab))
  217.    (question tail is "Does your animal have a tail?")
  218.    (rule (if order is cells and
  219.           stationary is yes) 
  220.          (then family is stationary))
  221.    (rule (if order is cells and
  222.           stationary is no) 
  223.          (then type.animal is jellyfish))
  224.    (question stationary is "Is your animal attached permanently to an object?")
  225.    (rule (if order is stomach and
  226.           multicelled is yes) 
  227.          (then family is multicelled))
  228.    (rule (if order is stomach and
  229.           multicelled is no) 
  230.          (then type.animal is protozoa))
  231.    (question multicelled is "Is your animal made up of more than one cell?")
  232.    (rule (if family is nowings and
  233.           opposing.thumb is yes) 
  234.          (then genus is thumb))
  235.    (rule (if family is nowings and
  236.           opposing.thumb is no) 
  237.          (then genus is nothumb))
  238.    (question opposing.thumb is "Does your animal have an opposing thumb?")
  239.    (rule (if family is hooves and
  240.           two.toes is yes) 
  241.          (then genus is twotoes))
  242.    (rule (if family is hooves and
  243.           two.toes is no) 
  244.          (then genus is onetoe))
  245.    (question two.toes is "Does your animal stand on two toes/hooves per foot?")
  246.    (rule (if family is feet and
  247.           live.in.water is yes) 
  248.          (then genus is water))
  249.    (rule (if family is feet and
  250.           live.in.water is no) 
  251.          (then genus is dry))
  252.    (question live.in.water is "Does your animal live in water?")
  253.    (rule (if family is noshell and
  254.           limbs is yes) 
  255.          (then type.animal is crocodile/alligator))
  256.    (rule (if family is noshell and
  257.           limbs is no) 
  258.          (then type.animal is snake))
  259.    (question limbs is "Does your animal have limbs?")
  260.    (rule (if family is stationary and
  261.           spikes is yes) 
  262.          (then type.animal is sea.anemone))
  263.    (rule (if family is stationary and
  264.           spikes is no) 
  265.          (then type.animal is coral/sponge))
  266.    (question spikes is "Does your animal normally have spikes radiating from it's body?")
  267.    (rule (if family is multicelled and
  268.           spiral.shell is yes) 
  269.          (then type.animal is snail))
  270.    (rule (if family is multicelled and
  271.           spiral.shell is no) 
  272.          (then genus is noshell))
  273.    (question spiral.shell is "Does your animal have a spiral-shaped shell?")
  274.    (rule (if genus is thumb and
  275.           prehensile.tail is yes) 
  276.          (then type.animal is monkey))
  277.    (rule (if genus is thumb and
  278.           prehensile.tail is no) 
  279.          (then species is notail))
  280.    (question prehensile.tail is "Does your animal have a prehensile tail?")
  281.    (rule (if genus is nothumb and
  282.           over.400 is yes) 
  283.          (then species is 400))
  284.    (rule (if genus is nothumb and
  285.           over.400 is no) 
  286.          (then species is under400))
  287.    (question over.400 is "Does an adult normally weigh over 400 pounds?")
  288.    (rule (if genus is twotoes and
  289.           horns is yes) 
  290.          (then species is horns))
  291.    (rule (if genus is twotoes and
  292.           horns is no) 
  293.          (then species is nohorns))
  294.    (question horns is "Does your animal have horns?")
  295.    (rule (if genus is onetoe and
  296.           plating is yes) 
  297.          (then type.animal is rhinoceros))
  298.    (rule (if genus is onetoe and
  299.           plating is no) 
  300.          (then type.animal is horse/zebra))
  301.    (question plating is "Is your animal covered with a protective plating?")
  302.    (rule (if genus is water and
  303.           hunted is yes) 
  304.          (then type.animal is whale))
  305.    (rule (if genus is water and
  306.           hunted is no) 
  307.          (then type.animal is dolphin/porpoise))
  308.    (question hunted is "Is your animal, unfortunately, commercially hunted?")
  309.    (rule (if genus is dry and
  310.           front.teeth is yes) 
  311.          (then species is teeth))
  312.    (rule (if genus is dry and
  313.           front.teeth is no) 
  314.          (then species is noteeth))
  315.    (question front.teeth is "Does your animal have large front teeth?")
  316.    (rule (if genus is noshell and
  317.           bivalve is yes) 
  318.          (then type.animal is clam/oyster))
  319.    (rule (if genus is noshell and
  320.           bivalve is no) 
  321.          (then type.animal is squid/octopus))
  322.    (question bivalve is "Is your animal protected by two half-shells?")
  323.    (rule (if species is notail and
  324.           nearly.hairless is yes) 
  325.          (then type.animal is man))
  326.    (rule (if species is notail and
  327.           nearly.hairless is no) 
  328.          (then subspecies is hair))
  329.    (question nearly.hairless is "Is your animal nearly hairless?")
  330.    (rule (if species is 400 and
  331.           land.based is yes) 
  332.          (then type.animal is bear/tiger/lion))
  333.    (rule (if species is 400 and
  334.           land.based is no) 
  335.          (then type.animal is walrus))
  336.    (question land.based is "Is your animal land based?")
  337.    (rule (if species is under400 and
  338.           thintail is yes) 
  339.          (then type.animal is cat))
  340.    (rule (if species is under400 and
  341.           thintail is no) 
  342.          (then type.animal is coyote/wolf/fox/dog))
  343.    (question thintail is "Does your animal have a thin tail?")
  344.    (rule (if species is horns and
  345.           one.horn is yes) 
  346.          (then type.animal is hippopotamus))
  347.    (rule (if species is horns and
  348.           one.horn is no) 
  349.          (then subspecies is nohorn))
  350.    (question one.horn is "Does your animal have one horn?")
  351.    (rule (if species is nohorns and
  352.           lives.in.desert is yes) 
  353.          (then type.animal is camel))
  354.    (rule (if species is nohorns and
  355.           lives.in.desert is no) 
  356.          (then type.animal is giraffe))
  357.    (question lives.in.desert is "Does your animal normally live in the desert?")
  358.    (rule (if species is teeth and
  359.           large.ears is yes) 
  360.          (then type.animal is rabbit))
  361.    (rule (if species is teeth and
  362.           large.ears is no the type.animal is rat/mouse/squirrel/beaver/porcupine))
  363.    (question large.ears is "Does your animal have large ears?")
  364.    (rule (if species is noteeth and
  365.           pouch is yes) 
  366.          (then type.animal is "kangaroo/koala bear"))
  367.    (rule (if species is noteeth and
  368.           pouch is no) 
  369.          (then type.animal is mole/shrew/elephant))
  370.    (question pouch is "Does your animal have a pouch?")
  371.    (rule (if subspecies is hair and
  372.           long.powerful.arms is yes) 
  373.          (then type.animal is orangutan/gorilla/chimpanzie))
  374.    (rule (if subspecies is hair and
  375.           long.powerful.arms is no) 
  376.          (then type.animal is baboon))
  377.    (question long.powerful.arms is "Does your animal have long, powerful arms?")
  378.    (rule (if subspecies is nohorn and
  379.           fleece is yes) 
  380.          (then type.animal is sheep/goat))
  381.    (rule (if subspecies is nohorn and
  382.           fleece is no) 
  383.          (then subsubspecies is nofleece))
  384.    (question fleece is "Does your animal have fleece?")
  385.    (rule (if subsubspecies is nofleece and
  386.           domesticated is yes) 
  387.          (then type.animal is cow))
  388.    (rule (if subsubspecies is nofleece and
  389.           domesticated is no) 
  390.          (then type.animal is deer/moose/antelope))
  391.    (question domesticated is "Is your animal domesticated?")
  392.    (answer is "I think your animal is a " type.animal))
  393.  
  394.