误解CLIPS功能。无法找到正确的答案

时间:2015-09-27 04:08:39

标签: declarative clips

我很难理解CLIPS的工作原理。我需要得到一个类似于" mooo - >牛&#34 ;.这就是我的代码。

(deftemplate animal (slot name)(slot sound))

(deffacts Input_animal 
    (animal(name cow)(sound mooo))
    (animal(name dog)(sound barf))
    (animal(name cat)(sound meuw))
    (animal(name sheep)(sound me-e-e))
    (animal(name duck)(sound cuack))
    )

(defrule sound_animal 
    (sound ?x)
    (animal(name ?animal)(sound ?x))
    =>
    (printout t ?animal crlf)
)

(defrule no_sound_animal 
    (sound ?x)
    (not(animal(name ?animal)(sound ?x)))
    =>
    (printout t ?x => "the animal doesn't exist" crlf)
)
.

然后我把它放在控制台上:     (观看规则)     (观察事实)     (观看调用)     (重启)     (跑)     (sound_animal(sound mooo))

我得到了这个答案:     [EXPRNPSR3]声音的Miising函数声明

好吧......我正在画一些喜欢"动物 - >牛" 任何人都可以解雇我吗?我知道这应该很简单,但我卡住了......谢谢!!

1 个答案:

答案 0 :(得分:2)

您还没有定义名为sound_animal或sound的函数,因此尝试调用这些函数会产生错误。

CLIPS> (sound_animal (sound mooo))

[EXPRNPSR3] Missing function declaration for sound_animal.
CLIPS> (deffunction sound_animal ())
CLIPS> (sound_animal (sound mooo))

[EXPRNPSR3] Missing function declaration for sound.
CLIPS>

使用assert命令创建一个声音事实来触发sound_animal规则:(断言(声音moo))。 assert命令是一种特殊形式,因为断言函数名后面的括号用于分隔事实关系及其槽,而不是用moo参数表示对声音函数的函数调用。

CLIPS> 
(deftemplate animal (slot name)(slot sound))
CLIPS> 
(deffacts Input_animal 
    (animal(name cow)(sound mooo))
    (animal(name dog)(sound barf))
    (animal(name cat)(sound meuw))
    (animal(name sheep)(sound me-e-e))
    (animal(name duck)(sound cuack))
    )
CLIPS> 
(defrule sound_animal 
    (sound ?x)
    (animal(name ?animal)(sound ?x))
    =>
    (printout t ?animal crlf))
CLIPS> 
(defrule no_sound_animal 
    (sound ?x)
    (not(animal(name ?animal)(sound ?x)))
    =>
    (printout t ?x => "the animal doesn't exist" crlf))
CLIPS> (watch rules)
CLIPS> (watch facts)
CLIPS> (reset)
<== f-0     (initial-fact)
==> f-0     (initial-fact)
==> f-1     (animal (name cow) (sound mooo))
==> f-2     (animal (name dog) (sound barf))
==> f-3     (animal (name cat) (sound meuw))
==> f-4     (animal (name sheep) (sound me-e-e))
==> f-5     (animal (name duck) (sound cuack))
CLIPS> (assert (sound mooo))
==> f-6     (sound mooo)
<Fact-6>
CLIPS> (run)
FIRE    1 sound_animal: f-6,f-1
cow
CLIPS>