你能帮我理解如何在drools 6 final中解决一组特定的规则吗?
我总共有超过100条规则。我使用ruleflow-group对规则进行了分组,但我不知道如何激活规则流组。我需要做这样的事情:
if (a == x) fireRuleflowOne
if (a == y) fireRuleFlowTwo
我正在使用StatefulKnowledgeSession,并且api中没有任何内容可用于触发/激活特定规则组。在调用fireAllRules之前/之后我想告诉fireGroupOfRules。
StatefulKnowledgeSession session = knowledgeBase.newStatefulKnowledgeSession();
session.insert(facts);
session.fireAllRules();
如果您需要更多详细信息,请与我们联系。提前谢谢
答案 0 :(得分:3)
虽然是一个老线程,有人可能会碰到这个问题,所以这里有一个例子,请注意,因为它是Spring Boot项目的一部分,所以drools是自动连接的!
注意:@danidemi是正确的。
package com.sample.services;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.drools.core.command.impl.GenericCommand;
import org.drools.core.command.impl.KnowledgeCommandContext;
import org.drools.core.common.InternalAgenda;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.ObjectFilter;
import org.kie.api.runtime.rule.FactHandle;
import org.kie.internal.command.Context;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sample.controllers.CollectResponse;
import com.sample.entities.BusPass;
import com.sample.entities.SalesDroolsProcessParams;
import com.sample.entities.VFactDbDaily;
import com.sample.repositories.VFactDailySalesRepository;
@Service
public class SalesService {
private static Logger log = LoggerFactory.getLogger(SalesService.class);
@Autowired
private KieContainer kieContainer;
@Autowired
VFactDailySalesRepository salesRepository;
public SalesService() {
}
public CollectResponse getCollected() {
Iterable<VFactDbDaily> salesList = salesRepository.findAll();
ArrayList<Object> collected = new ArrayList<>();
SalesDroolsProcessParams params = new SalesDroolsProcessParams("yearly");
KieSession kSession = kieContainer.newKieSession("testKS");
kSession.setGlobal("log", LoggerFactory.getLogger("com.sample.rules.test"));
kSession.setGlobal("collected", collected);
kSession.insert(params);
ActivateRuleFlowCommand stepOneCmd = new ActivateRuleFlowCommand("step1");
for (VFactDbDaily fact : salesList) {
// add the fact to working memory
kSession.insert(fact);
// fire all rules
kSession.fireAllRules();
kSession.execute(stepOneCmd);
}
ActivateRuleFlowCommand stepTwoCmd = new ActivateRuleFlowCommand("step2");
for (VFactDbDaily fact : salesList) {
// add the fact to working memory
kSession.insert(fact);
// fire all rules
kSession.fireAllRules();
// execute ruleflow
kSession.execute(stepTwoCmd);
}
kSession.dispose();
return new CollectResponse();
}
// ruleflow-group implementation
public class ActivateRuleFlowCommand implements GenericCommand<Object> {
private static final long serialVersionUID = 1L;
private String ruleFlowGroupName;
public ActivateRuleFlowCommand(String ruleFlowGroupName) {
this.ruleFlowGroupName = ruleFlowGroupName;
}
public Void execute(Context context) {
KieSession ksession = ((KnowledgeCommandContext) context).getKieSession();
((InternalAgenda) ksession.getAgenda()).activateRuleFlowGroup(ruleFlowGroupName);
return null;
}
}
}
HTH