Try this.
Code:
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.rmi.RemoteException;
import java.util.Map;
import javax.rules.admin.RuleExecutionSet;
import javax.rules.admin.RuleExecutionSetCreateException;
import javax.rules.admin.RuleExecutionSetRegisterException;
import org.drools.decisiontable.SpreadsheetCompiler;
import org.springframework.core.io.Resource;
import org.springmodules.jsr94.rulesource.AbstractRuleSource;
/**
*
*
*
*/
public class MultipleWorksheetDecisionTableRuleSource extends AbstractRuleSource {
private Resource source;
private Map providerProperties;
private Map rulesetProperties;
private Map registrationProperties;
/**
* Map<String workssheet name, String bind uri>
*/
private Map<String, String> worksheets;
public void setProviderProperties(Map providerProperties) {
this.providerProperties = providerProperties;
}
public void setRegistrationProperties(Map registrationProperties) {
this.registrationProperties = registrationProperties;
}
public void setRulesetProperties(Map rulesetProperties) {
this.rulesetProperties = rulesetProperties;
}
public void setSource(Resource source) {
this.source = source;
}
public void setWorksheets(Map<String, String> worksheets) {
this.worksheets = worksheets;
}
protected void registerRuleExecutionSets()
throws RuleExecutionSetCreateException, RemoteException,
IOException, RuleExecutionSetRegisterException {
if (logger.isDebugEnabled()) {
logger.debug("Registering " + this.worksheets.size() + " worksheets.");
}
SpreadsheetCompiler compiler = new SpreadsheetCompiler();
for (String worksheet: this.worksheets.keySet()) {
String drl = compiler.compile(getSpreadsheetStream(), worksheet);
RuleExecutionSet ruleExecutionSet = ruleAdministrator.getLocalRuleExecutionSetProvider(providerProperties).createRuleExecutionSet(new StringReader(drl), rulesetProperties);
String bindUri = this.worksheets.get(worksheet);
if (logger.isDebugEnabled()) {
logger.debug("Rules for worksheet '" + worksheet + "' created. Binding to uri '" + bindUri + "'.");
}
ruleAdministrator.registerRuleExecutionSet(bindUri, ruleExecutionSet, registrationProperties);
}
if (logger.isDebugEnabled()) {
logger.debug("Registered all worksheets.");
}
}
private InputStream getSpreadsheetStream() throws IOException {
InputStream stream = null;
try {
if (logger.isDebugEnabled()) {
try {
logger.debug("Rule source:" + (source.getURL() != null ? " URL = " + source.getURL() : (source.getFilename() != null ? "File name = " + source.getFilename() : "")));
}
catch (Exception e) {
}
}
stream = source.getInputStream();
} catch (IOException e) {
logger.error("Problem loading resource: " + (source.getURL() != null ? source.getURL() : source.getFilename()));
throw e;
}
return stream;
}
}
Sample Spring config
Code:
<bean id="ruleServiceProvider"
class="org.springmodules.jsr94.factory.DefaultRuleServiceProviderFactoryBean">
<property name="provider"><value>http://drools.org/</value></property>
<property name="providerClass">
<value>org.drools.jsr94.rules.RuleServiceProviderImpl</value>
</property>
</bean>
<bean id="ruleRuntime" class="org.springmodules.jsr94.factory.RuleRuntimeFactoryBean">
<property name="serviceProvider"><ref local="ruleServiceProvider"/></property>
</bean>
<bean id="ruleAdministrator" class="org.springmodules.jsr94.factory.RuleAdministratorFactoryBean">
<property name="serviceProvider"><ref local="ruleServiceProvider"/></property>
</bean>
<bean id="ruleSource" class="au.com.bupa.services.facade.rules.utils.MultipleWorksheetDecisionTableRuleSource">
<property name="ruleRuntime"><ref local="ruleRuntime"/></property>
<property name="ruleAdministrator"><ref local="ruleAdministrator"/></property>
<property name="source"><value>classpath:/spreadsheet.xls</value></property>
<property name="worksheets">
<map>
<entry key="worksheet1" value="Rule_Uri_1"/>
<entry key="worksheet2" value="Rule_Uri_2"/>
</map>
</property>
</bean>
<!-- Jsr94Template -->
<bean id="jsr94Template" class="org.springmodules.jsr94.core.Jsr94Template">
<property name="ruleSource">
<ref local="ruleSource"/>
</property>
</bean>
This is used against a spreadsheet with multiple worksheets. But you could modify it to use different drl files.
E.g. Change the worksheets Map to Map<String, Resource> worksheets and the config to be:
Code:
<map>
<entry key="Bind_Uri_1" value="classpath:/path/to/file_1.drl"/>
<entry key="Bind_Uri_2" value="classpath:/path?to/file_2.drl"/>
</map>
... although I haven't tried this modification myself to see if it works.