home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / programming / clips_2 / CLIPS / Examples / Zebra < prev   
Encoding:
Text File  |  1993-06-02  |  7.6 KB  |  210 lines

  1.  
  2. ;;;======================================================
  3. ;;;   Who Drinks Water? And Who owns the Zebra?
  4. ;;;     
  5. ;;;     Another puzzle problem in which there are five 
  6. ;;;     houses, each of a different color, inhabited by 
  7. ;;;     men of different nationalities, with different 
  8. ;;;     pets, drinks, and cigarettes. Given the initial
  9. ;;;     set of conditions, it must be determined which
  10. ;;;     attributes are assigned to each man.
  11. ;;;
  12. ;;;     CLIPS Version 6.0 Example
  13. ;;;
  14. ;;;     To execute, merely load, reset and run.
  15. ;;;======================================================
  16.     
  17. (deftemplate avh (field a) (field v) (field h))
  18.  
  19. (defrule find-solution
  20.   ; The Englishman lives in the red house.
  21.  
  22.   (avh (a nationality) (v englishman) (h ?n1))
  23.   (avh (a color) (v red) (h ?c1&?n1))
  24.  
  25.   ; The Spaniard owns the dog.
  26.  
  27.   (avh (a nationality) (v spaniard) (h ?n2&~?n1))
  28.   (avh (a pet) (v dog) (h ?p1&?n2))
  29.  
  30.   ; The ivory house is immediately to the left of the green house,
  31.   ; where the coffee drinker lives.
  32.  
  33.   (avh (a color) (v ivory) (h ?c2&~?c1))
  34.   (avh (a color) (v green) (h ?c3&~?c2&~?c1&=(+ ?c2 1)))
  35.   (avh (a drink) (v coffee) (h ?d1&?c3))
  36.  
  37.   ; The milk drinker lives in the middle house.
  38.  
  39.   (avh (a drink) (v milk) (h ?d2&~?d1&3))
  40.  
  41.   ; The man who smokes Old Golds also keeps snails.
  42.  
  43.   (avh (a smokes) (v old-golds) (h ?s1))
  44.   (avh (a pet) (v snails) (h ?p2&~?p1&?s1))
  45.  
  46.   ; The Ukrainian drinks tea.
  47.  
  48.   (avh (a nationality) (v ukrainian) (h ?n3&~?n2&~?n1))
  49.   (avh (a drink) (v tea) (h ?d3&~?d2&~?d1&?n3))
  50.  
  51.   ; The Norwegian resides in the first house on the left.
  52.  
  53.   (avh (a nationality) (v norwegian) (h ?n4&~?n3&~?n2&~?n1&1))
  54.  
  55.   ; Chesterfields smoker lives next door to the fox owner.
  56.  
  57.   (avh (a smokes) (v chesterfields) (h ?s2&~?s1))
  58.   (avh (a pet) (v fox) (h ?p3&~?p2&~?p1&:(or (= ?s2 (- ?p3 1)) (= ?s2 (+ ?p3 1)))))
  59.  
  60.   ; The Lucky Strike smoker drinks orange juice.
  61.  
  62.   (avh (a smokes) (v lucky-strikes) (h ?s3&~?s2&~?s1))
  63.   (avh (a drink) (v orange-juice) (h ?d4&~?d3&~?d2&~?d1&?s3)) 
  64.  
  65.   ; The Japanese smokes Parliaments
  66.  
  67.   (avh (a nationality) (v japanese) (h ?n5&~?n4&~?n3&~?n2&~?n1))
  68.   (avh (a smokes) (v parliaments) (h ?s4&~?s3&~?s2&~?s1&?n5))
  69.  
  70.   ; The horse owner lives next to the Kools smoker, 
  71.   ; whose house is yellow.
  72.  
  73.   (avh (a pet) (v horse) (h ?p4&~?p3&~?p2&~?p1))
  74.   (avh (a smokes) (v kools) (h ?s5&~?s4&~?s3&~?s2&~?s1&:(or (= ?p4 (- ?s5 1)) (= ?p4 (+ ?s5 1)))))
  75.   (avh (a color) (v yellow) (h ?c4&~?c3&~?c2&~?c1&?s5))
  76.  
  77.   ; The Norwegian lives next to the blue house.
  78.  
  79.   (avh (a color) (v blue) (h ?c5&~?c4&~?c3&~?c2&~?c1&:(or (= ?c5 (- ?n4 1)) (= ?c5 (+ ?n4 1)))))
  80.   
  81.   ; Who drinks water?  And Who owns the zebra?
  82.  
  83.   (avh (a drink) (v water) (h ?d5&~?d4&~?d3&~?d2&~?d1))
  84.   (avh (a pet) (v zebra) (h ?p5&~?p4&~?p3&~?p2&~?p1))
  85.  
  86.   => 
  87.   (assert (solution nationality englishman ?n1)
  88.           (solution color red ?c1)
  89.           (solution nationality spaniard ?n2)
  90.           (solution pet dog ?p1)
  91.           (solution color ivory ?c2)
  92.           (solution color green ?c3)
  93.           (solution drink coffee ?d1)
  94.           (solution drink milk ?d2) 
  95.           (solution smokes old-golds ?s1)
  96.           (solution pet snails ?p2)
  97.           (solution nationality ukrainian ?n3)
  98.           (solution drink tea ?d3)
  99.           (solution nationality norwegian ?n4)
  100.           (solution smokes chesterfields ?s2)
  101.           (solution pet fox ?p3)
  102.           (solution smokes lucky-strikes ?s3)
  103.           (solution drink orange-juice ?d4) 
  104.           (solution nationality japanese ?n5)
  105.           (solution smokes parliaments ?s4)
  106.           (solution pet horse ?p4) 
  107.           (solution smokes kools ?s5)
  108.           (solution color yellow ?c4)
  109.           (solution color blue ?c5)
  110.           (solution drink water ?d5)
  111.           (solution pet zebra ?p5))
  112.   )
  113.  
  114. (defrule print-solution
  115.   ?f1 <- (solution nationality ?n1 1)
  116.   ?f2 <- (solution color ?c1 1)
  117.   ?f3 <- (solution pet ?p1 1)
  118.   ?f4 <- (solution drink ?d1 1)
  119.   ?f5 <- (solution smokes ?s1 1)
  120.   ?f6 <- (solution nationality ?n2 2)
  121.   ?f7 <- (solution color ?c2 2)
  122.   ?f8 <- (solution pet ?p2 2)
  123.   ?f9 <- (solution drink ?d2 2)
  124.   ?f10 <- (solution smokes ?s2 2)
  125.   ?f11 <- (solution nationality ?n3 3)
  126.   ?f12 <- (solution color ?c3 3)
  127.   ?f13 <- (solution pet ?p3 3)
  128.   ?f14 <- (solution drink ?d3 3)
  129.   ?f15 <- (solution smokes ?s3 3)
  130.   ?f16 <- (solution nationality ?n4 4)
  131.   ?f17 <- (solution color ?c4 4)
  132.   ?f18 <- (solution pet ?p4 4)
  133.   ?f19 <- (solution drink ?d4 4)
  134.   ?f20 <- (solution smokes ?s4 4)
  135.   ?f21 <- (solution nationality ?n5 5)
  136.   ?f22 <- (solution color ?c5 5)
  137.   ?f23 <- (solution pet ?p5 5)
  138.   ?f24 <- (solution drink ?d5 5)
  139.   ?f25 <- (solution smokes ?s5 5)
  140.   =>
  141.   (retract ?f1 ?f2 ?f3 ?f4 ?f5 ?f6 ?f7 ?f8 ?f9 ?f10 ?f11 ?f12 ?f13 ?f14
  142.            ?f15 ?f16 ?f17 ?f18 ?f19 ?f20 ?f21 ?f22 ?f23 ?f24 ?f25)
  143.   (format t "HOUSE | %-11s | %-6s | %-6s | %-12s | %-13s%n" 
  144.             Nationality Color Pet Drink Smokes)
  145.   (format t "--------------------------------------------------------------------%n")
  146.   (format t "  1   | %-11s | %-6s | %-6s | %-12s | %-13s%n" ?n1 ?c1 ?p1 ?d1 ?s1)
  147.   (format t "  2   | %-11s | %-6s | %-6s | %-12s | %-13s%n" ?n2 ?c2 ?p2 ?d2 ?s2)
  148.   (format t "  3   | %-11s | %-6s | %-6s | %-12s | %-13s%n" ?n3 ?c3 ?p3 ?d3 ?s3)
  149.   (format t "  4   | %-11s | %-6s | %-6s | %-12s | %-13s%n" ?n4 ?c4 ?p4 ?d4 ?s4)
  150.   (format t "  5   | %-11s | %-6s | %-6s | %-12s | %-13s%n" ?n5 ?c5 ?p5 ?d5 ?s5)
  151.   (printout t crlf crlf))
  152.  
  153. (defrule startup
  154.    =>
  155.    (printout t
  156.     "There are five houses, each of a different color, inhabited by men of"  crlf
  157.     "different nationalities, with different pets, drinks, and cigarettes."  crlf
  158.     crlf
  159.     "The Englishman lives in the red house.  The Spaniard owns the dog."     crlf
  160.     "The ivory house is immediately to the left of the green house, where"   crlf
  161.     "the coffee drinker lives.  The milk drinker lives in the middle house." crlf
  162.     "The man who smokes Old Golds also keeps snails.  The Ukrainian drinks"  crlf
  163.     "tea.  The Norwegian resides in the first house on the left.  The"       crlf)
  164.    (printout t
  165.     "Chesterfields smoker lives next door to the fox owner.  The Lucky"      crlf
  166.     "Strike smoker drinks orange juice.  The Japanese smokes Parliaments."   crlf
  167.     "The horse owner lives next to the Kools smoker, whose house is yellow." crlf
  168.     "The Norwegian lives next to the blue house."                 t
  169.     crlf
  170.     "Now, who drinks water?  And who owns the zebra?" crlf crlf)
  171.    (assert (value color red) 
  172.            (value color green) 
  173.            (value color ivory)
  174.            (value color yellow)
  175.            (value color blue)
  176.            (value nationality englishman)
  177.            (value nationality spaniard)
  178.            (value nationality ukrainian) 
  179.            (value nationality norwegian)
  180.            (value nationality japanese)
  181.            (value pet dog)
  182.            (value pet snails)
  183.            (value pet fox)
  184.            (value pet horse)
  185.            (value pet zebra)
  186.            (value drink water)
  187.            (value drink coffee)
  188.            (value drink milk)
  189.            (value drink orange-juice)
  190.            (value drink tea)
  191.            (value smokes old-golds)
  192.            (value smokes kools)
  193.            (value smokes chesterfields)
  194.            (value smokes lucky-strikes)
  195.            (value smokes parliaments)) 
  196.    )
  197.  
  198. (defrule generate-combinations
  199.    ?f <- (value ?s ?e)
  200.    =>
  201.    (retract ?f)
  202.    (assert (avh (a ?s) (v ?e) (h 1))
  203.            (avh (a ?s) (v ?e) (h 2))
  204.            (avh (a ?s) (v ?e) (h 3))
  205.            (avh (a ?s) (v ?e) (h 4))
  206.            (avh (a ?s) (v ?e) (h 5))))
  207.  
  208.  
  209.  
  210.