CLIPS - 如何在用户输入有效输入之前提示用户

问题描述 投票:1回答:1

我遇到了这个问题的一些麻烦,如果有人能帮助我,我会很感激!我的代码询问用户宝石的特征(硬度,密度和颜色)并返回其名称。但是,应该提示用户,直到他或她回复此处列出的颜色之一(红色,粉红色,黄色,棕色,绿色,蓝色,紫罗兰色,黑色,白色,无色),我不知道该怎么做即使在花了好几个小时后......

(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))
clips
1个回答
0
投票

定义一个可以传递问题和一组允许值的函数:

         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> 
© www.soinside.com 2019 - 2024. All rights reserved.