КЛИПЫ - Как я могу предложить пользователю, пока он не введет правильный ввод - PullRequest
1 голос
/ 03 апреля 2019

У меня возникли проблемы с этой проблемой, и я был бы признателен, если бы кто-нибудь мог мне помочь с этим! Мой код запрашивает у пользователя характеристики драгоценного камня (твердость, плотность и цвета) и возвращает его имя. Тем не менее, пользователь должен получать запрос, пока он или она не ответит одним из перечисленных здесь цветов (красный, розовый, желтый, коричневый, зеленый, синий, фиолетовый, черный, белый, бесцветный), и я не знаю, как это сделать. даже потратив на это часы ...

(deftemplate characteristics
(slot name)
(slot hardness)
(slot density)
(multislot colors)


(deffacts gems
(characteristics (name Diamond) (hardness 10) (density 3.52) (colors yellow brown green blue white colorless))   
(characteristics (name Corundum) (hardness 9) (density 4) (colors red pink yellow brown green blue violet black white colorless))    
(characteristics (name Chrysoberyl) (hardness 8.5) (density 3.72) (colors yellow brown green))   
(characteristics (name Spinel) (hardness 8) (density 3.6) (colors red pink yellow brown green blue violet white colorless))    
(characteristics (name Topaz) (hardness 8) (density 3.52) (colors red pink yellow brown blue violet white colorless))    
(characteristics (name Beryl) (hardness 7.5) (density 2.7) (colors red pink yellow brown green blue white colorless))    
(characteristics (name Zircon) (hardness 6) (density 4.7) (colors yellow brown green violet white colorless))    
(characteristics (name Quartz) (hardness 7) (density 2.65) (colors yellow brown green blue violet white black colorless))    
(characteristics (name Tourmaline) (hardness 7) (density 3.1) (colors red pink yellow brown green blue white black colorless))    
(characteristics (name Peridot) (hardness 6.5) (density 3.3) (colors yellow brown green))    
(characteristics (name Jadeite) (hardness 6.5) (density 3.3) (colors red pink yellow brown green blue violet white black colorless))    
(characteristics (name Opal) (hardness 5.5) (density 2) (colors red pink yellow brown white black colorless))   
(characteristics (name Nephrite) (hardness 5) (density 2.9) (colors green white black colorless))  
(characteristics (name Turquoise) (hardness 5) (density 2.7) (colors blue)))


(defrule get-input
=>
(printout t "What is the hardness?")
(assert (hardness (read)))
(printout t "What is the density?")
(assert (density (read))) 
(printout t "What is/are the color(s)?")
(assert (colors (read))))

(defrule what-gem-is-this
(hardness ?hardness)(density ?density)(colors ?colors)
(characteristics(name ?name1)(hardness ?hardness1)(density ?density1)(colors $?colors1))
(test (= ?hardness ?hardness1))
(test (= ?density ?density1))
(test (member$ ?colors ?colors1))
=> 
(printout t "The gem described is a: " ?name1 crlf))

1 Ответ

0 голосов
/ 04 апреля 2019

Определите функцию, в которую вы можете передать вопрос и набор допустимых значений:

         CLIPS (6.31 4/1/19)
CLIPS> 
(deffunction ask-question
   (?question $?allowed-values)
   (printout t ?question)
   (bind ?answer (read))
   (if (lexemep ?answer)
      then
      (bind ?answer (lowcase ?answer)))
   (while (not (member ?answer ?allowed-values)) do
      (printout t ?question)
      (bind ?answer (read))
      (if (lexemep ?answer)
         then
         (bind ?answer (lowcase ?answer))))
   ?answer)
CLIPS> (ask-question "What is the color? " yellow brown green blue white colorless red pink violet black)
What is the color? orange
What is the color? purple
What is the color? red
red
CLIPS> 

Вот как вы можете проверить значения, используя правила:

CLIPS> 
(defrule get-color
   (not (color ?))
   =>
   (printout t "What is the color? ")
   (assert (color (read))))
CLIPS> 
(defrule bad-color
   ?f <-  (color ~yellow&~brown&~green&~blue&~white&~colorless&~red&~pink&~violet&~black)
   =>
   (retract ?f))
CLIPS> 
(defrule get-hardness
   (not (hardness ?))
   =>
   (printout t "What is the hardness? ")
   (assert (hardness (read))))
CLIPS> 
(defrule bad-hardness
   ?f <-  (hardness ?h)
   (test (or (not (numberp ?h))
             (< ?h 1)
             (> ?h 10)))
   =>
   (retract ?f))
CLIPS> (run)
What is the hardness? very
What is the hardness? 20
What is the hardness? 10
What is the color? orange
What is the color? blue
CLIPS> 
...