網絡上有很多關於如何加載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();
}
}