getFactHandle方法为Drools

时间:2017-02-16 08:10:47

标签: java spring-boot drools kie

我有以下代码:

KieSession kSession = JPAKnowledgeService.loadStatefulKnowledgeSession(someId, kieBase, null, env)

for (FactHandle f : kSession.getFactHandles()) {
    Object sessionObject = kSession.getObject(f);
    logger.info(String.format("Fact:  %s", sessionObject.toString()));
}

kSession.insert(someFact);

// Let's loop again to see if our fact is indeed there
for (FactHandle f : kSession.getFactHandles()) {
    Object sessionObject = kSession.getObject(f);
    logger.info(String.format("Fact:  %s", sessionObject.toString()));
}

// Do something else with someFact, insert it into some db

// factHandle is null
FactHandle factHandle = kSession.getFactHandle(someFact);

基本上,我正在加载我的持久性KieSession,向它添加一个事实,循环查看事实以检查新添加的事实确实存在,但是当我尝试将其恢复时,factHandle object为null。我试图使用Drools与基于身份的断言模式。所以我的相关问题是:

  1. 为什么我的事实处理空? someFact在内存中的位置可能已在Do something else with someFact, insert it into some db阶段发生变化,是否会对问题产生影响?
  2. Drools如何比较身份?我在private long id对象上提交了someFact,注明了@Id
  3. 我正在创建我的KieContainerKieBase,如下所示:
  4. ```

    @Bean
    public KieServices kieServices() {
        return KieServices.Factory.get();
    }
    
    @Bean
    public KieContainer kieContainer() throws IOException {
    
        final KieRepository kieRepository = kieServices().getRepository();
    
        kieRepository.addKieModule(new KieModule() {
            @Override
            public ReleaseId getReleaseId() {
                return kieRepository.getDefaultReleaseId();
            }
        });
    
        KieFileSystem kfs = kieServices().newKieFileSystem();
        PathMatchingResourcePatternResolver pmrs = new PathMatchingResourcePatternResolver();
        Resource[] files = pmrs.getResources("classpath*:com/company/**/rules/*.drl");
    
        for(Resource file : files) {
            String myString = IOUtils.toString(file.getInputStream(), "UTF-8");
            kfs.write("src/main/resources/"+ file.getFilename(), myString);
        }
    
        KieBuilder kieBuilder = kieServices().newKieBuilder(kfs);
        kieBuilder.buildAll();
        return kieServices().newKieContainer(kieRepository.getDefaultReleaseId());
    }
    
    @Bean
    public KieBase kieBase() throws IOException {
        KieBaseConfiguration kieBaseConfiguration = kieServices().newKieBaseConfiguration();
        kieBaseConfiguration.setProperty("assertBehaviour", "equality");
        return KnowledgeBaseFactory.newKnowledgeBase(kieBaseConfiguration);
    }
    

    ```

    如果我必须切换到基于相等的断言,我在我的对象上实现了equalshashCode方法,但找不到定义KieBase断言的方法为EQUALITY。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

  1. 只有someFactDo something else with someFact, insert it into some db@Id中更改内存的位置才有效,因此Drools无法使用身份模式进行比较。
  2. 内存中的位置。不是Javax KieBaseConfiguration kieBaseConfiguration = kieServices().newKieBaseConfiguration(); kieBaseConfiguration.setOption(EqualityBehaviorOption.EQUALITY); return kieContainer().newKieBase(kieBaseConfiguration); 注释。
  3. 以下是将引擎置于等式模式的方法:

    insert into yourTable ( ...)      
    with names as (
        select 'A' as name from dual union all
        select 'B' as name from dual union all
        select 'C' as name from dual 
    ),
    dates as (
              select date' 2017-01-01' + level -1 as yourDate
              from dual
              connect by date' 2016-01-01' + level -1 <= date '2017-02-20'
             )
    select rownum, name, yourDate
    from names
           cross join dates