我有以下代码:
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与基于身份的断言模式。所以我的相关问题是:
someFact
在内存中的位置可能已在Do something else with someFact, insert it into some db
阶段发生变化,是否会对问题产生影响?private long id
对象上提交了someFact
,注明了@Id
。KieContainer
和KieBase
,如下所示:```
@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);
}
```
如果我必须切换到基于相等的断言,我在我的对象上实现了equals
和hashCode
方法,但找不到定义KieBase
断言的方法为EQUALITY
。我怎么能这样做?
答案 0 :(得分:0)
someFact
中Do something else with someFact, insert it into some db
在@Id
中更改内存的位置才有效,因此Drools无法使用身份模式进行比较。KieBaseConfiguration kieBaseConfiguration = kieServices().newKieBaseConfiguration();
kieBaseConfiguration.setOption(EqualityBehaviorOption.EQUALITY);
return kieContainer().newKieBase(kieBaseConfiguration);
注释。以下是将引擎置于等式模式的方法:
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