2010-03-17 36 views
5

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

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

下面是我正在使用的示例代码片段。我标记了一些区域,我怀疑某些属性需要设置并作为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(); 
    } 
} 

回答

4

我不认为JSR-94供应商提供了一个决策表实现尚未 - 你需要使用的决策表API到XLS转换为DRL格式,然后你可以通过上述码。所以如果你使用SpreadsheetCompiler(org.drools.decisiontables包)可以为你做 - 不幸的是这意味着你必须导入一个drools类(不是纯粹的JSR-94),这样可能会破坏目的。

在任何情况下,JSR-94 api都非常有用 - 这是为什么它没有像API规范那样发展的原因。在使用JSR-94(我已经完成了!)的代码行中,您可以为几个主要的规则引擎实现“存根”。

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

祝你好运!

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 project。 All the Best。