我对 Drools 很陌生,可能正在做一些非常愚蠢的事情。问题是由于某种原因我的 Drools 规则没有被解雇。使用的 drools 版本是 5.4
如果有帮助,当我切换回 aStatelessKnowledgeSession
并使用 execute 方法触发规则时,规则就会触发。但是,我需要使用AgendaFilter
来过滤规则,并且我知道尚不支持议程过滤器StatelessKnowledgeSession
。作为第一步,我只是将会话翻转为 a StatefulKnowledgeSession
,插入事实并触发所有规则。
不得不谦虚地说,在过去的几天里,我为此感到头疼。太糟糕了,文档对流口水不太友好(我参考了官方文档和用户指南。
下面引用了我从规则文件加载知识的规则验证器代码和 Spring 工厂 bean。我个人认为FactoryBean 没有任何问题。任何指针都会有很大帮助。
RuleValidator.java
StatefulKnowledgeSession ksession = knowledgeBase.newStatefulKnowledgeSession();
ksession.setGlobal("validationReport", validationReport);
ksession.setGlobal("simpleCache", simpleCache);
ksession.setGlobal("ruleValidator", this);
ksession.insert(allClientDetails); //this is a list of ClientDetails objects (facts)
ksession.fireAllRules();
ksession.dispose();
return validationReport;
知识库工厂.java
import java.io.IOException;
import java.util.Map;
import org.apache.log4j.Logger;
import org.drools.KnowledgeBase;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.Resource;
import org.drools.io.ResourceFactory;
import org.springframework.beans.factory.FactoryBean;
public class KnowledgeBaseFactory implements FactoryBean<KnowledgeBase> {
private static Logger logger=Logger.getLogger(KnowledgeBaseFactory.class);
private KnowledgeBase knowledgeBase;
public KnowledgeBaseFactory(Map<String,ResourceType> resourceMap) throws IOException {
logger.debug("Resource Map : "+resourceMap);
final KnowledgeBuilder knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
for (Map.Entry<String, ResourceType> eachRuleResource:resourceMap.entrySet()){
knowledgeBuilder.add(ResourceFactory.newClassPathResource(eachRuleResource.getKey()),eachRuleResource.getValue());
}
if (knowledgeBuilder.hasErrors()){
logger.error(knowledgeBuilder.getErrors().toString());
throw new RuntimeException(knowledgeBuilder.getErrors().toString());
}
knowledgeBase = knowledgeBuilder.newKnowledgeBase();
knowledgeBase.addKnowledgePackages(knowledgeBuilder.getKnowledgePackages());
}
@Override
public KnowledgeBase getObject() throws Exception {
return knowledgeBase;
}
@Override
public Class<?> getObjectType() {
return knowledgeBase.getClass();
}
@Override
public boolean isSingleton() {
return true;
}
}