早上,原谅这个愚蠢的问题,但我正在忙着建立一个专家系统,就像“21问题”游戏一样,它使用向用户提出的问题,以便为他们确定合适的狗。专家系统用CLIPS / .CPS语言编码,我希望包括的一个要求是,当询问用户是/否问题时,他们需要输入“y”或“n”。
在我们所教授的所有资源中,我们只进行了艰难的数字验证,而不是特定的字符验证,我也找不到任何资源。
这是我为确保他们在我的一个问题上输入有效数字而进行的数字验证的示例
(defrule test-integer
(number-in ?number&:(integerp ?number))
=>
(printout t ?number "is valid"
(defrule test-non-int
?number-address <- (number-in ?number&:(not (integerp ?number)))
=>
(printout t ?number " not valid int" crlf)
(retract ?number-address))
答案 0 :(得分:0)
这是您使用规则的方式:
CLIPS>
(defrule test-response
(response-in ?response&y|n)
=>
(printout t ?response " is valid" crlf))
CLIPS>
(defrule test-non-response
?response-address <- (response-in ?response&~y&~n)
=>
(printout t ?response " not valid response" crlf)
(retract ?response-address))
CLIPS> (assert (response-in xyz))
<Fact-1>
CLIPS> (run)
xyz not valid response
CLIPS> (assert (response-in n))
<Fact-2>
CLIPS> (run)
n is valid
CLIPS>
我建议使用只接受正确答案的函数:
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 "Continue? " y n yes no)
Continue? k
Continue? l
Continue? ye
Continue? YeS
yes
CLIPS>
答案 1 :(得分:0)
我想到的是首先将一个规则的答案与另一个规则的答案相关联,以检查答案是否有效,如果答案是有效的,将其链接到正确的规则那么将继续进行下一个问题。
代码来自我自己的专家系统:
(defrule Small-CoatType-Full
(Small-Coat f)
(person (name ?name))
=>
(open "result.txt" result "a")
(printout result ?name " Likes Smaller, Fury Dogs" crlf)
(close result)
(printout t "Would you like a low energetic(l) or high energetic(h) breed?" crlf)
(assert (Small-Energy-Level(lowcase(read)))))
(defrule Small-Energy-Level-Wrong
(Small-Energy-Level ?var &~l&~h)
=>
(printout t crlf "Plesae Only Choose (l) or (h)")
(assert (Small-Energy-Level (lowcase(read)))))`