Announcement Announcement Module
Collapse

Spring Modules forum decommissioned in favor of Spring Extensions

As the Spring Modules project has been replaced by the Spring Extensions (http://www.springsource.org/extensions) project, this forum has been decommissioned in favour of Spring Extensions one at:
http://forum.springsource.org/forumdisplay.php?f=44

Please see the Spring Extensions home page for a complete list of current projects in Java, .NET and ActionScript. You can also propose one if you want.

Cheers,
Costin Leau
SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
http://twitter.com/costinl
See more
See less
How to load multiple DRL Rule Files using JSR 94 Template Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to load multiple DRL Rule Files using JSR 94 Template

    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.

  • #2
    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.
    Last edited by dean; Oct 7th, 2009, 01:37 AM.

    Comment

    Working...
    X