知识库中跳过规则

时间:2016-05-31 10:54:10

标签: drools

我们正在使用drools 5.5最终版本。我们有数千个对象和两个规则,因此我们明智地获取块(100大小)的对象并为每个块创建知识库并触发规则。因为知识库的创建是昂贵的我们我们正在创建性能问题。所以我们创建了一次知识库并在这种情况下使用该知识库,在这个案例中,从第6个块开始执行4到5个块后,虽然匹配在那里但规则没有被解雇。请指出可以是什么完成。

示例代码

public static KnowledgeBase getPackageKnowledgeBase(PackageDescr pkg){

        KnowledgeBuilderConfiguration builderConf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();

        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(builderConf);
        kbuilder.add(ResourceFactory.newDescrResource(pkg), ResourceType.DESCR);
        Collection<KnowledgePackage> kpkgs = kbuilder.getKnowledgePackages();
        if(kbuilder.hasErrors()){
            LOGGER.error(kbuilder.getErrors());
        }
        KnowledgePackage knowledgePackage = kpkgs.iterator().next();
        KnowledgeBase kbase= KnowledgeBaseFactory.newKnowledgeBase();
        kbase.addKnowledgePackages(Collections.singletonList(knowledgePackage));
        return kbase;
}

使用方法

        chunkSize=100;
        int start = 0; 
        Count = -1;
                KnowledgeBase kbase=getPackageKnowledgeBase(pkgdscr)//pkgdscr contails all rules got from db

                        while(Count!=0 && Count <= chunkSize ){
                        LOGGER.debug("Deduction not getting "+mappedCustomerId);
                        Objects inputObjects = handler.getPaginatedInputObjects(start);
                        Count = inputObjects.size();
                          start=start+chunkSize;

                    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
                        for(Object object:inputObjects){
                                ksession.insert(object);
                            }

                        ksession.fireAllRules();

                         ksession.dispose();

                        }

1 个答案:

答案 0 :(得分:0)

以下是循环的重要部分。在我看来,一旦Count超过chunkSize(100),这个循环就会终止。你确定这不会发生吗?

  while(Count!=0 && Count <= chunkSize ){             
      Objects inputObjects = ...;
      Count = inputObjects.size();
      ...
      StatefulKnowledgeSession ksession = ...;
      for(Object object:inputObjects){
          ksession.insert(object);
      }
      ksession.fireAllRules();
      ...
  }