如何使用JSR94使用Drools加载基于Excel的决策表?

时间:2010-03-17 20:35:05

标签: drools

网上有很多关于如何加载Drools DRL规则集的例子。但是,我似乎无法找到任何有关如何使用JSR94 API以Excel格式加载决策表的说明或示例。

有谁知道怎么做?如果是这样,你能提供一个简单的代码示例吗?

以下是我正在使用的一段代码示例。我已经标记了我怀疑某些属性需要设置并作为createRuleExectuionSet()的第二个参数传入的区域(虽然这可能不是解决方案)。

package com.sample;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.rules.RuleRuntime;
import javax.rules.RuleServiceProvider;
import javax.rules.RuleServiceProviderManager;
import javax.rules.StatelessRuleSession;
import javax.rules.admin.LocalRuleExecutionSetProvider;
import javax.rules.admin.RuleAdministrator;
import javax.rules.admin.RuleExecutionSet;

import org.drools.jsr94.rules.RuleServiceProviderImpl;

/**
 * This is a sample class to launch a decision table.
 */
public class DecisionTableTestJsr94 {

    // URL to the Decision Table file (via the classpath)
    private static final String DECISION_TABLE_PATH = "/rules/Sample.xls";

    // An arbitrary URI to identify the rule set
    private static final String BIND_URI = "uri://fake/bind/uri";

    public DecisionTableTestJsr94() throws Exception{
        // Initialize the needed services
        RuleServiceProviderManager.registerRuleServiceProvider(RuleServiceProviderImpl.RULE_SERVICE_PROVIDER, RuleServiceProviderImpl.class);
        RuleServiceProvider ruleServiceProvider = RuleServiceProviderManager.getRuleServiceProvider(RuleServiceProviderImpl.RULE_SERVICE_PROVIDER);
        RuleAdministrator ruleAdmin = ruleServiceProvider.getRuleAdministrator();
        LocalRuleExecutionSetProvider ruleExecutionSetProvider = ruleAdmin.getLocalRuleExecutionSetProvider(null);

        // Read the decision table
        InputStream rules = this.getClass().getResourceAsStream(DECISION_TABLE_PATH);
        Map ruleProperties = new HashMap();

        // ** (probably something needs to happen hear with a properties Map, but what? **

        RuleExecutionSet ruleExecutionSet = ruleExecutionSetProvider.createRuleExecutionSet(rules, null);

        // Add the rules
        ruleAdmin.registerRuleExecutionSet(BIND_URI, ruleExecutionSet, null);

        // Start the rule session
        StatelessRuleSession ruleSession = null;
        ruleSession = (StatelessRuleSession) ruleServiceProvider.getRuleRuntime().createRuleSession(BIND_URI, null, RuleRuntime.STATELESS_SESSION_TYPE);

        // Create a domain object for the test
        Message message = new Message();
        message.setStatus(Message.HELLO);
        System.out.println("Message is: '" + message.getMessage() + "'"); // should be null

        // Run the object through the rules
        List<Message> inputList = new ArrayList<Message>();
        inputList.add(message);
        ruleSession.executeRules(inputList);

        // See if the rules modified the object
        System.out.println("Message is: '" + message.getMessage() + "'"); // should have the appropriate message
    }

    public static final void main(String[] args) throws Exception {
        new DecisionTableTestJsr94();
    }
}

2 个答案:

答案 0 :(得分:4)

我认为JSR-94提供程序尚未提供Decision表实现 - 您需要使用决策表API将XLS转换为drl格式,然后您可以将其传递给上面的代码。

因此,如果你使用可以为你做的SpreadsheetCompiler(org.drools.decisiontables包) - 不幸的是,这意味着你必须导入一个drools类(不是纯JSR-94),这样可能会破坏目的。

在任何情况下,很少有JSR-94 api非常有用 - 这就是为什么它没有作为API规范发展的原因。你可以用少量代码来实现几个主要规则引擎的“存根”,而不是使用JSR-94(我已经完成了!)。

对我来说有用的一次是当我编写一个适用于JRules和Drools的测试工具时(在这种情况下它非常有用,因为我只处理数据 - 而不是规则本身 - 在上面的代码中 - 不同规则引擎的JSR-94“可插拔性”是没有用的 - 如果你要切换到别的东西,你的规则就必须重写了。

祝你好运!

答案 1 :(得分:1)

我不知道JSr,但肯定你可以在JBPM中使用drools决策表。 我有一个类文件可以帮助您在代码中添加决策表excel表。希望如此。

package com.sample;

import java.util.*;

import org.drools.*;

import org.jbpm.*;

public class ProcessRuleTest {

    public static final void main(String[] args) {
        try {
            // load up the knowledge base
            KnowledgeBase kbase = readKnowledgeBase();
            StatefulKnowledgeSession ksession = createSession(kbase);
            KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory
                    .newFileLogger(ksession, "test");

            // set the parameters
            Map<String, Object> params = new HashMap<String, Object>();
            HelloProcessModel hpm = new HelloProcessModel();
            hpm.setCount(new Integer("3"));
            hpm.setUserlocation("NewYorkUser");
            params.put("hpm", hpm);
            ksession.startProcess("looptest777",params);

            ksession.fireAllRules();
            logger.close();
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    private static KnowledgeBase readKnowledgeBase() throws Exception {
        ProcessBuilderFactory
                .setProcessBuilderFactoryService(new ProcessBuilderFactoryServiceImpl());
        ProcessMarshallerFactory
                .setProcessMarshallerFactoryService(new ProcessMarshallerFactoryServiceImpl());
        ProcessRuntimeFactory
                .setProcessRuntimeFactoryService(new ProcessRuntimeFactoryServiceImpl());
        BPMN2ProcessFactory
                .setBPMN2ProcessProvider(new BPMN2ProcessProviderImpl());
        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory
                .newKnowledgeBuilder();
        kbuilder.add(ResourceFactory.newClassPathResource("processRuleslooptest777.bpmn"),
                ResourceType.BPMN2);

        DecisionTableConfiguration config = KnowledgeBuilderFactory.newDecisionTableConfiguration();
        config.setInputType(DecisionTableInputType.XLS);
        kbuilder.add(ResourceFactory.newClassPathResource("LoopConditionRules.xls"), ResourceType.DTABLE, config);


        /*
         * Add drl file
         */
        //kbuilder.add(ResourceFactory.newClassPathResource("LoopConditionRules.drl"), ResourceType.DRL);

        return kbuilder.newKnowledgeBase();
    }

    private static StatefulKnowledgeSession createSession(KnowledgeBase kbase) {
        Properties properties = new Properties();
        properties
                .put("drools.processInstanceManagerFactory",
                        "org.jbpm.process.instance.impl.DefaultProcessInstanceManagerFactory");
        properties.put("drools.processSignalManagerFactory",
                "org.jbpm.process.instance.event.DefaultSignalManagerFactory");
        KnowledgeSessionConfiguration config = KnowledgeBaseFactory
                .newKnowledgeSessionConfiguration(properties);
        return kbase.newStatefulKnowledgeSession(config,
                EnvironmentFactory.newEnvironment());
    }
}

kbuilder.add(ResourceFactory.newClassPathResource(“LoopConditionRules.xls”),在此代码中是在项目中添加drl文件的方式,可能正在看这个你可以得到的提示你的jsr项目。 一切都好。