我是插入数据到drools规则引擎,但我无法理解它如何处理插入的数据。插入数据的代码是:
final StatefulKnowledgeSession session = getSession()
new Thread() {
@Override public void run() {
Thread.currentThread().setName("RuleEngineThread")
println 'engine starting fire'+Thread.currentThread().getName()
session.fireUntilHalt();
}
}.start();
WorkingMemoryEntryPoint entrypoint=session.getWorkingMemoryEntryPoint("Multiple Stream")
entrypoint.insert(new Categories([categoryid:120,name:"catN1"]))
entrypoint.insert(new Test(100,120))
entrypoint.insert(new Categories([categoryid:121,name:"catN2"]))
entrypoint.insert(new Test(100,121))
entrypoint.insert(new Categories([categoryid:1220,name:"catN3"]))
entrypoint.insert(new Test(100,1220))
entrypoint.insert(new Categories([categoryid:1202,name:"catN4"]))
entrypoint.insert(new Test(100,1202))
println "Thread sleeeping for 3 secs"
Thread.currentThread().sleep(3000)不要担心语法,这是groovy文件。规则是:
rule "multiple-opt"
//duration 120
no-loop true
when
$c: Categories() from entry-point "Multiple Stream"
$t: Test() from entry-point "Multiple Stream"
then
System.out.println("@@Multiple "+$c.getName()+":"+$t.getPrice());
end我得到的输出非常奇怪,所以我认为我对drools运行时的理解较少。产出如下:
engine starting fireRuleEngineThread
Thread sleeeping for 3 secs
@@Multiple catN1:100
@@Multiple catN4:100
@@Multiple catN3:100
@@Multiple catN2:100
@@Multiple catN1:100
@@Multiple catN4:100
@@Multiple catN3:100
@@Multiple catN2:100
@@Multiple catN1:100
@@Multiple catN3:100
@@Multiple catN2:100
@@Multiple catN1:100
@@Multiple catN2:100我不明白为什么规则被触发了这么多次,而我插入对象的次数少于我收到的输出数。
如果我事先遗漏了一些关于drools.Thanks的知识,请提供帮助
发布于 2014-01-03 11:31:21
这是生产规则系统的一个非常基本的特性:对规则的模式所定义的所有可能的组合进行彻底的搜索。
Categories() // <= match with any object of class Categories
Test() // <= match with any object of class Test您已经每个插入了4个,因此规则将为每个可能的配对触发。
发布于 2014-01-03 13:07:40
为了添加到@laune,根据您的场景,您可以在匹配之后撤回每一对事实:
then
System.out.println("@@Multiple "+$c.getName()+":"+$t.getPrice());
retract($c);
retract($t);编辑
是的,为了将这些对关联起来,您可以使用一个绑定变量,然后在模式匹配中对其进行筛选:
when
$c: Categories($catid : categoryid ) from entry-point "Multiple Stream"
$t: Test(categoryid == $catid) from entry-point "Multiple Stream"
then
System.out.println("@@Multiple "+$c.getName()+":"+$t.getPrice());(假设在类getCategoryId()上有一个Test getter。同样,以这种方式进行关联将减少规则匹配排列的数量)。如果事实是以这种方式匹配的,那么您可能不需要撤回事实。
https://stackoverflow.com/questions/20901297
复制相似问题