我们可以在 Jess 中计算具有相同值的事实吗?
使用 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 <initializer> <action> <result> <conditional element>)
Using Jess as the rule engine, we can assert a fact that some witness has seen a person, at some place, and with time associated:
(deffacts witnesses
(witness Batman Gotham 18)
(witness Hulk NYC 19)
(witness Batman Gotham 2)
(witness Superman Chicago 22)
(witness Batman Gotham 10)
)
With a rule, I want to know if several witnesses have seen the same person at the same place, not considering time.
In Jess documentation we got this example for counting employees making salraries of 100K and more:
(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))
So I based my code on previous example:
(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))
)
With the '(deffacts)' instruction provided above and the rule, the engine should assert the fact
(place-seen Gotham)
because we have Batman seen three times in Gotham.
I have no idea how to use the conditional element (CE) part of 'accumulate'. Can I use a 'test' to retain facts with same person and place?
Any idea how to achieve this?
Thank you!
Note : synthax of 'accumulate' is
(accumulate <initializer> <action> <result> <conditional element>)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将省略有关
?plos
的内容,因为您没有准确解释那是什么;如果需要,您可以自己将其添加回来。(几乎)做你想要的事情的基本规则如下。您没有得到的 CE 部分只是我们想要积累的一个模式;在这里,它会将事实与在同一地点见证的同一个人与第一个人匹配的事实相匹配:
现在,此规则的唯一问题是它会针对您的
deffacts
触发三次,每一个蝙蝠侠/哥谭事实都一次。我们可以通过更改第一个模式来仅匹配某个人在给定地点的最早出现来阻止这种情况:I'm going to leave out the stuff about
?plost
, as you don't explain what that is, exactly; you can add it back in yourself if you need to.A basic rule that (almost) does what you want is as follows. The CE part you weren't getting is just a pattern that we want to accumulate over; here, it matches facts with the same person witnessed at the same place as the fact matched by the first person:
Now, the only problem with this rule is that it'll fire three times for your
deffacts
, once for every one of the Batman/Gotham facts. We can stop this by changing the first pattern to match only the earliest sighting of a person at a given place: