【问题标题】:Drools rule engine flowDrools 规则引擎流程
【发布时间】:2014-01-03 10:16:29
【问题描述】:

我正在向 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

当我插入对象的次数少于我收到的输出数量时,我无法理解规则是如何被触发这么多次的。
如果我缺少有关流口水的一些知识,请提供帮助。在此先感谢

【问题讨论】:

    标签: java drools


    【解决方案1】:

    这是产生式规则系统的一个非常基本的特征:穷举搜索由规则模式定义的所有可能组合。

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

    您已分别插入 4 个,因此该规则将针对每个可能的配对触发。

    【讨论】:

      【解决方案2】:

      只是添加到@laune,根据您的情况,您可以例如匹配后收回每对事实:

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

      编辑

      是的,为了关联这些对,您可以使用 binding variable,然后在模式匹配中对此进行过滤:

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

      (假设在类Test 上有一个getCategoryId() getter。此外,以这种方式关联将减少规则匹配排列的数量)。如果事实以这种方式以相关对匹配,则您可能不需要撤回事实。

      【讨论】:

      • 如果是这样,应该有额外的约束(categoryid?)以保证正确的对被匹配和收回。
      猜你喜欢
      • 2012-12-12
      • 2013-08-23
      • 2011-06-20
      • 1970-01-01
      • 2014-04-17
      • 2017-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多