java-如何在流口水中激活Ruleflow-Group

您能否帮助我了解如何在Drools 6 Final中激发一组特定的规则?

我总共有100多个规则.我已经使用Ruleflow-group对规则进行了分组,但是我不知道如何激活Ruleflow-Group.我需要做这样的事情:

if (a == x) fireRuleflowOne
if (a == y) fireRuleFlowTwo

我正在使用StatefulKnowledgeSession,并且api中没有任何东西可以用来触发/激活特定的规则组.在调用fireAllRules之前/之时,我想将其告知fireGroupOfRules.

StatefulKnowledgeSession session = knowledgeBase.newStatefulKnowledgeSession();
session.insert(facts);
session.fireAllRules();

如果您需要更多详细信息,请告诉我.提前致谢

解决方法:

尽管是旧线程,但有人可能会遇到这个问题,因此这里是一个示例,请注意,由于它是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;
        }

    }
}

高温超导

上一篇:linux修改文件所有者和文件所在组


下一篇:Linux下hadoop和spark的基础环境配置准备