English 中文(简体)
How do I load an Excel-based Decision Table with Drools using JSR94?
原标题:
  • 时间:2010-03-17 20:35:05
  •  标签:
  • drools

There s a bunch of examples on the web of how to load a Drools DRL rule set. However, I can t seem to find any instructions or examples of how to load a Decision Table in Excel format using the JSR94 API.

Does anyone know how to do this? If so, could you provide a simple code example?

Here s a sample piece of code I m working with below. I ve marked the area that I suspect that some properties need to get setup and passed in as the second parameter to createRuleExectuionSet() (Although that may not be the solution).

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();
    }
}
最佳回答

I do not think the JSR-94 provider provides a Decision table implementation as yet - you would need to use the decision table API to convert the XLS to the drl format, which you could then pass to the above code.

So if you use SpreadsheetCompiler (org.drools.decisiontables package) that could do it for you - unfortunately that means you have to import a drools class (not pure JSR-94) so that may defeat the purpose.

In any case, it is rare that the JSR-94 api is very useful - there is a reason why it has not progressed as an API spec. You could arguably implement "stubs" for a few major rule engines in less lines of code than using JSR-94 (I have done it !).

The one time it was useful for me was when I was writing a testing tool which worked for both JRules and Drools (it was useful in that case because I was only dealing with data - not rules themselves - in your above code - the JSR-94 "pluggability" of a different rule engine is no use - if you were to switch to something else your rules would have to be rewritten anyway).

Good luck !

问题回答

Well i don t know for JSr but surely you can use drools decision table in JBPM . I have one class file which can help you to add your decision table excel sheet in your code hopefully.

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());
    }
}

The line kbuilder.add(ResourceFactory.newClassPathResource("LoopConditionRules.xls"), in this code is the way to add drl file in project,may be looking at this you can get hint for your jsr project. All the Best.





相关问题
Drools Rules: How can I use a method on "when" section?

I need to execute a method on "when" section of a DSLR file and I´m not sure if it´s possible. Example: rule "WNPRules_10" when $reminder:Reminder(source == "HMI") $user:User(isInAgeRange("...

Extract DRL from DSLR

How can I access the DRL generated within a DSLR file in Drools?

Drools, spring and mule

Has anyone combined these technologies? Could you share lessons learnt?

drools persistence with mysql, can t get process instance

I m stumped. I m trying to implement persistence with Drools-flow, and I d like to grab the value of a property on a workitem / processinstance, but everytime I try to get the workitem or process ...

Using Maven to download drools sources

I m new to maven and drools, so this could be obvious... I m trying to work off of the drools snapshot repository using maven/eclipse. I d like to be able to link the sources/javadoc when updating ...

Spring-Drools integration?

There used to be spring-modules project that provides this integration; however, that project is deprecated now. Does anyone now if there is any continued support for this integration? Thanks.

热门标签