RulesWithSpringFramework

For folks using the spring framework, the following tips may be handy.

 

Consider taking a look at:

 

https://springmodules.dev.java.net/

https://springmodules.dev.java.net/docs/reference/0.5/html/

 

 

Or, feel free to use the following FactoryBean from Geoffrey De Smit:

 

package net.sf.tabudrool.evaluation;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.compiler.PackageBuilder;
import org.drools.compiler.PackageBuilderConfiguration;
import java.util.List;
import java.io.InputStreamReader;

/**
 * @author Geoffrey De Smet
 */
public class RuleBaseBeanFactory implements FactoryBean, InitializingBean {

    private List<Resource> drlResourceList;

    private RuleBase ruleBase;

    private  PackageBuilderConfiguration packageBuilderConfiguration;

    public void setDrlResourceList(List<Resource> drlResourceList) {
        this.drlResourceList = drlResourceList;
    }

    public void afterPropertiesSet() throws Exception {
        Assert.notEmpty(drlResourceList, "drlResourceList must not be
empty");
        PackageBuilder builder = (packageBuilderConfiguration != null ? new PackageBuilder(packageBuilderConfiguration) : new PackageBuilder());
        for (Resource resource : drlResourceList) {
            builder.addPackageFromDrl(new
InputStreamReader(resource.getInputStream())); // TODO will use default
encoding which might not be wanted
        }
        ruleBase = RuleBaseFactory.newRuleBase();
        ruleBase.addPackage(builder.getPackage());
    }


    public RuleBase getObject() {
        return ruleBase;
    }

    public Class getObjectType() {
        return RuleBase.class;
    }

    public boolean isSingleton() {
        return true;
    }

    public void setPackageBuilderConfiguration(PackageBuilderConfiguration packageBuilderConfiguration) {
        this.packageBuilderConfiguration = packageBuilderConfiguration;
    }
}

 

and the configuration:

 


    <bean id="ruleBase"
class="net.sf.tabudrool.evaluation.RuleBaseBeanFactory">
        <property name="drlResourceList">
            <list>
                <value
type="org.springframework.core.io.Resource">classpath:/net/sf/tabudrool/evaluation/rules.drl</value>
            </list>
        </property>
        <property name="packageBuilderConfiguration">
          <bean class="org.drools.compiler.PackageBuilderConfiguration">
            <property name="javaLanguageLevel" value="1.5"></property>
          </bean>
        </property>
    </bean>

    <bean id="workflowFrame"
class="net.sf.tabudrool.swingui.WorkflowFrame" singleton="false">
...
        <property name="ruleBase" ref="ruleBase"></property>
    </bean>

 

And this is how you use it:

 


    private void assertScheduleInWorkingMemory() {
        if (workingMemory != null) {
            workingMemory.dispose();
        }
        workingMemory = ruleBase.newWorkingMemory();
        Schedule schedule = schedulePanel.getSchedule();
        for (Timeslot timeslot : schedule.getTimeslotList()) {
            workingMemory.assertObject(timeslot);
        }
...
        workingMemory.fireAllRules();
    }