网上有很多关于如何加载 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();
}
}