简体   繁体   English

流口水统治引擎流

[英]Drools rule engine flow

I am inserting data to drools rule engine but i can not understand how it processed inserted data. 我正在将数据插入Drools规则引擎,但我不明白它如何处理插入的数据。 The code for inserting data is: 插入数据的代码是:

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)

don't worry for syntax this is groovy file. 不用担心语法,这是很时髦的文件。 And the rule being: 规则是:

 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

The output i am getting is quite strange so I think i have less understanding of drools runtime. 我得到的输出非常奇怪,因此我认为我对Drools运行时的了解较少。 The output is : 输出为:

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

I can not understand how rule got fired so many times while i inserted objects less times than the number of outputs i receive. 我无法理解规则被多次触发,而我插入对象的次数却少于接收到的输出数量。
Please help if i am missing some knowledge about drools.Thanks in advance 如果我缺少有关流口水的知识,请提供帮助。

This is a very basic feature of production rule systems: the exhaustive search of all possible combinations as defined by the patterns of a rule. 这是生产规则系统的一个非常基本的特征:详尽搜索规则规则所定义的所有可能组合。

Categories()    // <= match with any object of class Categories
Test()          // <= match with any object of class Test

You have inserted 4 of each, so the rule will fire for each possible pairing. 您已插入4个,因此该规则将为每个可能的配对触发。

Just to add to @laune, depending on your scenario, you can eg retract each pair of facts after a match: 只需添加到@laune,这取决于您的情况,例如,您可以在比赛后撤回每对事实:

then
 System.out.println("@@Multiple "+$c.getName()+":"+$t.getPrice());
 retract($c);
 retract($t);

Edit 编辑

Yes, in order to correlate the pairs, you can use a binding variable and then filter on this in the pattern match: 是的,为了关联对,可以使用绑定变量 ,然后在模式匹配中对此进行过滤:

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());

(Assuming that there is a getCategoryId() getter on class Test . Also, correlating in this way will reduce the number of rule match permutations). (假设类Test上有一个getCategoryId() getter。此外,以这种方式进行关联将减少规则匹配排列的数量)。 You might not need to retract the facts if they are matched in correlated pairs this way. 如果事实以这种方式在相关对中匹配,则可能不需要撤回事实。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM