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