我对Drools很新,我可能做的事情非常愚蠢。问题是由于某种原因,我的Drools规则没有被解雇。使用的drools版本是5.4
如果有帮助,当我转回StatelessKnowledgeSession
并使用execute方法触发规则时,规则会触发。但是,我需要使用AgendaFilter
来过滤规则,我们了解StatelessKnowledgeSession
尚不支持议程过滤器。作为第一步,我只是将会话翻转为StatefulKnowledgeSession
,插入事实并解除所有规则。
不得不谦卑地说,过去几天我一直在打破这个问题。太糟糕了,文档对于drools并不友好(我参考官方文档和用户指南。
下面引用了规则验证器代码和我从规则文件加载知识的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;
KnowledgeBaseFactory.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;
}
}
答案 0 :(得分:2)
感谢提示@Nim
终于想通了。我应该正确阅读API文档
显然,我在Iterable
中使用了execute
重载的StatelessKnowledgeSession
方法,该方法遍历了我的所有事实并对每个事实应用了规则。
然而,对于StatefulKnowledgeSession
,我应该事先迭代并逐个插入事实。然后,我可以解雇规则。
insert(Iterable)
中没有StatefulKnowledgeSession
方法的原因。