使用 Jess 作为规则引擎,我们可以断言某个证人在某个地方见过一个人,并且与时间相关:
(deffacts witnesses
(witness Batman Gotham 18)
(witness Hulk NYC 19)
(witness Batman Gotham 2)
(witness Superman Chicago 22)
(witness Batman Gotham 10)
)
有一个规则,我想知道几个证人是否在同一个地方见过同一个人,而不考虑时间。
在 Jess 文档中,我们得到了这个示例,用于计算年薪超过 10 万的员工:
(defrule count-highly-paid-employees
?c <- (accumulate (bind ?count 0) ;; initializer
(bind ?count (+ ?count 1)) ;; action
?count ;; result
(employee (salary ?s&:(> ?s 100000)))) ;; CE
=>
(printout t ?c " employees make more than $100000/year." crlf))
所以我的代码基于前面的例子:
(defrule count-witnesses
(is-lost ?plost)
(witness ?pseen ?place ?time)
?c <- (accumulate (bind ?count 0)
(bind ?count (+ ?count 1))
?count
(test ()) ; conditional element of accumulate
(test (= ?plost ?pseen))
(test (>= ?count 3))
=>
(assert (place-seen ?place))
)
使用上面提供的“(deffacts)”指令和规则,引擎应该断言事实
(place-seen Gotham)
因为我们在哥谭见过蝙蝠侠三遍。
我不知道如何使用“累积”的条件元素(CE)部分。我可以使用“测试”来保留同一个人和地点的事实吗?
知道如何实现这一目标吗?
谢谢!
注意:'accumulate' 的语法是
(accumulate <initializer> <action> <result> <conditional element>)