剪辑:匹配(或不匹配空字符串)

Clips: Matching (or not matching an empty string)

我尝试一段一段地翻译字符串,所以我知道当原始字符串为空时,我们就完成了。问题是,CLIPS 如何知道 "input" 字符串中什么都没有?

(defrule check-if-empty
    ?phase <- (phase CONVERT)
    (input "code here possibly")
    =>
    (retract ?phase ?input)
    (assert (phase PRINT))
    (return))

CLIPS 有一个名为 str-length 的内置函数。你必须检查长度是否为0.

CLIPS> (defrule check-if-empty
    ?phase <- (phase CONVERT)
    ?input <- (input ?inputstr)
    (test (= (str-length ?inputstr) 0))
=>
    (retract ?phase ?input)
    (printout t "Empty string" crlf)
    (return)
)

CLIPS> (assert (phase CONVERT))
<Fact-1>
CLIPS> (run)
CLIPS> (assert (input "foo"))
<Fact-2>
CLIPS> (run)
CLIPS> (assert (input ""))
<Fact-3>
CLIPS> (run)
Empty string

你只需要把我的printout改成你的assert

CLIPS 中的空字符串是"",所以只需将"code here possibly" 替换为""。也不需要在规则末尾放置 return ,除非您正在使用模块并希望在具有当前焦点的模块中结束规则的执行。

(defrule check-if-empty
    ?phase <- (phase CONVERT)
    ?input <- (input "")
    =>
    (retract ?phase ?input)
    (assert (phase PRINT)))