Please help me how to load multiple rules using JSR94 template. I saw the example also there loading only one DRL file using JSR94 template. Ideal case we have to maintain multiple DRL files each drl file contains corresponding functionality rules.
Printable View
Please help me how to load multiple rules using JSR94 template. I saw the example also there loading only one DRL file using JSR94 template. Ideal case we have to maintain multiple DRL files each drl file contains corresponding functionality rules.
Try this.
Sample Spring configCode: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;
}
}
This is used against a spreadsheet with multiple worksheets. But you could modify it to use different drl files.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>
E.g. Change the worksheets Map to Map<String, Resource> worksheets and the config to be:
... although I haven't tried this modification myself to see if it works.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>