在CLIPS中检查多个事实

时间:2019-01-19 12:07:48

标签: clips

比方说,我有这样一些事实(我不知道有多少个事实):灯x熄灭。我轻柔地逐步打开所有灯,这样每个事实都会打开:x灯亮着。我如何检查所有点亮的指示灯。我知道如果有三盏灯我可以写:

(defrule checkAllLamps
    (lamp 1 is on)
    (lamp 2 is on)
    (lamp 3 is on)
    =>
    (printout t "All lamps are on now")
)

但是对于x灯? 谢谢!

2 个答案:

答案 0 :(得分:1)

您可以为此使用事实集查询功能(Basic Programming Guide的第{{1}章)。

12.9.12

答案 1 :(得分:1)

在这里,您可以检查所有指示灯是否都亮着。 checkAllLamps规则将所有灯都点亮的情况下根本没有灯,而checkAllLampsAtLeastOne规则要求至少有一个灯点亮。

         CLIPS (6.31 2/3/18)
CLIPS> 
(defrule checkAllLamps
   (not (lamp ? is off))
   =>
   (printout t "All lamps are on now" crlf))
CLIPS> 
(defrule checkAllLampsAtLeastOne
   (exists (lamp ? is on))
   (not (lamp ? is off))
   =>
   (printout t "All lamps are on now" crlf))
CLIPS> (agenda)
0      checkAllLamps: *
For a total of 1 activation.
CLIPS> (assert (lamp 1 is on))
<Fact-1>
CLIPS> (agenda)
0      checkAllLampsAtLeastOne: *,*
0      checkAllLamps: *
For a total of 2 activations.
CLIPS> (assert (lamp 2 is off))
<Fact-2>
CLIPS> (agenda)
CLIPS> (retract 2)
CLIPS> (assert (lamp 2 is on))
<Fact-3>
CLIPS> (agenda)
0      checkAllLampsAtLeastOne: *,*
0      checkAllLamps: *
For a total of 2 activations.
CLIPS>