CLIPS 验证文本输入 [英] CLIPS Validate Text Entry

查看:38
本文介绍了CLIPS 验证文本输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好,请原谅这个愚蠢的问题,但我正忙于构建一个专家系统,就像21 个问题"游戏一样,它使用向用户提出的问题来确定适合他们的狗.专家系统是用 CLIPS/.CPS 语言编码的,我希望包括的要求之一是,当用户被问到是/否问题时,他们需要输入y"或n".

Morning, Excuse the silly question but I am busy building a expert system much like the "21 Questions" game that uses questions asked to the user in order to determine the right dog for them. The expert system is coded in CLIPS / .CPS language and one of the requirements I am looking to include is that when the user is asked a yes/no question they are required to input "y" or "n".

在我们被教导的所有资源中,我们只进行了严格的数字验证,而不是特定的字符验证,我也找不到任何可以做到这一点的资源.

In all the resources we have been taught we have only been tough number validation and not a specific character validation and I cannot find any resources that do this either.

这是我为确保他们在我的一个问题中输入有效数字而进行的数字验证示例

This is an example of the number validation I did in order to ensure they input a valid number on one of my questions

(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))

推荐答案

这是使用规则的方式:

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>

我建议使用只接受正确响应的函数:

I'd suggest using a function that only accepts correct responses:

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> 

这篇关于CLIPS 验证文本输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆