2010-03-17 88 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。