有没有办法在 CLIPS 中制作一个受控循环?

Is there a way to make a controlled loop in CLIPS?

你好 Stack overflow 社区,我向你寻求帮助。我正在尝试在 CLIPS 中做一些事情,有点像解析器,但我遇到了一些麻烦。总而言之,我正在尝试制作一个接受用户输入的程序,这是一个短语,例如 "I read a book" 并根据我定义的一些规则给出输出,假设 "I read" 由规则 G1,"a book" 由规则 G2 识别(如果我们需要处理输入 "A book I read","A book" 可以由规则 G3 识别)。

"I read a book" 的输出应该是 "YES G1 G2",因为根据用户输入和规则,程序确定了用于识别用户提供的输入中所写内容的规则。我希望我解释得很好,这基本上是我的第一个问题。到目前为止,我已经设法创建了规则,并且考虑了字符串可以放在输入开头的情况(参见上面提到的 G3 规则)。

也许这可以使用 explode$ 和 $ 等通配符来解决?但我真的不知道如何开始。

到目前为止,这是我的代码,我知道它不是很多:

(defrule G1
    =>
    (assert (regula G1 "I read"))
)

(defrule G2
    =>
    (assert (regula G2 "I read."))
)

(defrule G3
    =>
    (assert (regula G3 "a book."))
)

(defrule G4
    =>
    (assert (regula G4 "A book"))
)

感谢所有的帮助和回答。

您的问题陈述与您目前生成的代码不完全匹配("a book" 未被规则 G2 识别)。此外,您可以使用 defacts 语句代替无条件断言事实的规则。

这是解决问题的一种方法:

         CLIPS (6.31 6/12/19)
CLIPS> 
(deffacts regulas
   (regula G1 I read)
   (regula G2 I read.)
   (regula G3 a book.)
   (regula G4 A book))
CLIPS> 
(deftemplate translation
   (multislot text)
   (multislot tokens)
   (multislot output)
   (slot complete (default NO)))
CLIPS>       
(defrule get-input
   =>
   (printout t "Input: ")
   (bind ?text (readline))
   (assert (translation (text ?text)
                        (tokens (explode$ ?text)))))
CLIPS>                         
(defrule parse
   ?t <- (translation (tokens $?tokens $?rest)
                      (output $?output))
   (regula ?replacement $?tokens)
   =>
   (modify ?t (tokens $?rest)
              (output $?output ?replacement)))
CLIPS> 
(defrule success
   ?t <- (translation (tokens)
                      (complete NO))
   =>
   (modify ?t (complete YES)))
CLIPS>    
(defrule print-results
   (declare (salience -10))
   ?t <- (translation (complete ?complete)
                      (tokens $?tokens)
                      (output $?output))

   =>
   (printout t ?complete " " (implode$ (create$ ?output ?tokens)) crlf))
CLIPS> (reset)
CLIPS> (run)
Input: I read a book.
YES G1 G3
CLIPS> (reset)
CLIPS> (run)
Input: A book I read.
YES G4 G2
CLIPS> (reset)
CLIPS> (run)
Input: A book you read.
NO G4 you read.
CLIPS> (reset)
CLIPS> (run)
Input: You read a book.
NO You read a book.
CLIPS>