[rules-dev] Drools + Spring

glasscat aburasheed at studentmasjid.com
Thu Apr 2 20:14:32 EDT 2009


Thank you very much Steven, I really appreciate it.



Steven Williams-5 wrote:
> 
> Here's what I do:
> 
> We inject KnowledgeBases into our app using Spring. It was a simple matter
> of just implementing a builder class that takes a bunch of drools
> Resources
> and adding a build() method to create the KnowledgeBase and then creating
> a
> class that implements Spring FactoryBean and InitializingBean to construct
> the builder and convert Spring resources to Drools ones and calling the
> builder.build() method in the afterPropertiesSet().
> 
> There is one currently open bug in drools that means we have to cast to a
> class from the old api to add accumulate functions.
> 
> cheers
> Steve
> 
> 
> 2009/4/3 Abu Rasheed <aburasheed at studentmasjid.com>
> 
>> Are there any plans to have Spring support in drools-api? The last mail
>> that I saw in searching the topic is dated last year, and there are no
>> new
>> postings related to this.
>>
>> Does anyone have any success in injecting KnowledgeBase into their apps
>> using Spring? Is there any examples out there that I can refer to?
>>
>> Thanks
>>
>>
>> _______________________________________________
>> rules-dev mailing list
>> rules-dev at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/rules-dev
>>
>>
> 
> package xxxxx;
> 
> import java.io.IOException;
> import java.util.Map;
> 
> import org.apache.commons.logging.Log;
> import org.apache.commons.logging.LogFactory;
> import org.drools.KnowledgeBase;
> import org.drools.KnowledgeBaseFactory;
> import org.drools.builder.KnowledgeBuilder;
> import org.drools.builder.KnowledgeBuilderConfiguration;
> import org.drools.builder.KnowledgeBuilderError;
> import org.drools.builder.KnowledgeBuilderErrors;
> import org.drools.builder.KnowledgeBuilderFactory;
> import org.drools.builder.ResourceType;
> import org.drools.compiler.PackageBuilderConfiguration;
> import org.drools.io.Resource;
> 
> /**
>  * Given the appropriate resources builds the rule base. Expected usage:
> <code> 
>  * RuleBaseBuilder builder = new RuleBaseBuilder();
>  * builder.set*List(arg); 
>  * RuleBase b = builder.build();
>  * </code> Since the build() method creates a new RuleBase each
> invocation, the builder instance can be reused as long
>  * as the lists are set/reset.
>  */
> public class KnowledgeBaseBuilder
> {
>    private static final Log LOG =
> LogFactory.getLog(KnowledgeBaseBuilder.class);
>    private Map<Resource, ResourceType> resourceList;
>    private Map<String, String> accumulateFunctionMap;
> 
>    /**
>     * List of rule resources that should be used in the Rule Base.
>     * 
>     * @param aResourceList
>     */
>    public void setResourceList(final Map<Resource, ResourceType>
> aResourceList)
>    {
>       this.resourceList = aResourceList;
>    }
> 
>    /**
>     * Builds the rule base.
>     * 
>     * @return the configured RuleBase object.
>     * @throws Exception
>     *         on error, e.g. no rule lists configured, or bad lists.
>     */
>    public KnowledgeBase build() throws Exception
>    {
>       // create default configuration.
>       KnowledgeBuilderConfiguration configuration =
> KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
>       // config.setJavaLanguageLevel("1.5");
>       // config.setCompiler(PackageBuilderConfiguration.ECLIPSE);
>       return build(configuration);
>    }
> 
>    /**
>     * Builds the rule base with the provided configuration for the
> builder.
>     * 
>     * @return the configured RuleBase object.
>     * @throws Exception
>     *         on error, e.g. no rule lists configured, or bad lists. if
> config is null
>     */
>    public KnowledgeBase build(final KnowledgeBuilderConfiguration config)
> throws Exception
>    {
>       verifyBuilder();
>       addAccumulateFunctions(config);
>       KnowledgeBuilder builder =
> KnowledgeBuilderFactory.newKnowledgeBuilder(config);
>       addPackagesFromList(builder);
>       KnowledgeBuilderErrors errors = builder.getErrors();
>       if (errors.size() > 0)
>       {
>          for (KnowledgeBuilderError error : errors)
>          {
>             LOG.error(error);
>          }
>          throw new IllegalArgumentException("Could not parse knowledge.");
>       }
>       KnowledgeBase knowledgeBase =
> KnowledgeBaseFactory.newKnowledgeBase();
>       knowledgeBase.addKnowledgePackages(builder.getKnowledgePackages());
> 
>       return knowledgeBase;
>    }
> 
>    private void verifyBuilder()
>    {
>       if (!rulesExist())
>       {
>          throw new IllegalStateException("No rule lists provided!");
>       }
>    }
> 
>    private void addAccumulateFunctions(final KnowledgeBuilderConfiguration
> config)
>    {
>       if (accumulateFunctionMap != null)
>       {
>          for (Map.Entry<String, String> entry :
> accumulateFunctionMap.entrySet())
>          {
> //            TODO: uncomment code below when adding of accumlate function
> bug is fixed in drools
> //            config.setProperty("drools.accumulate.function." +
> entry.getKey(), entry.getValue());
>             ((PackageBuilderConfiguration)
> config).addAccumulateFunction(entry.getKey(), entry.getValue());
>          }
>       }
>    }
> 
>    private void addPackagesFromList(final KnowledgeBuilder builder) throws
> IOException
>    {
>       for (Map.Entry<Resource, ResourceType> entry :
> resourceList.entrySet())
>       {
>          builder.add(entry.getKey(), entry.getValue());
>       }
>    }
> 
>    /**
>     * Checks that at least one rule resource list is not empty.
>     * 
>     * @return true if at least one rule list is populated, false if all
> empty.
>     */
>    public boolean rulesExist()
>    {
>       return (this.resourceList != null && !this.resourceList.isEmpty());
>    }
> 
>    public void setAccumulateFunctionMap(Map<String, String>
> aAccumulateFunctionMap)
>    {
>       this.accumulateFunctionMap = aAccumulateFunctionMap;
>    }
> }
> 
> package xxxxx;
> 
> import java.util.LinkedHashMap;
> import java.util.Map;
> 
> import org.drools.KnowledgeBase;
> import org.drools.builder.ResourceType;
> import org.drools.io.ResourceFactory;
> import org.springframework.beans.factory.FactoryBean;
> import org.springframework.beans.factory.InitializingBean;
> import org.springframework.core.io.Resource;
> 
> /**
>  * Factory for Drools KnowledgeBase.
>  * @author Steve
>  *
>  */
> public class KnowledgeBaseFactoryBean implements FactoryBean,
> InitializingBean
> {
>    private KnowledgeBaseBuilder builder = new KnowledgeBaseBuilder();
>    private KnowledgeBase knowledgeBase;
>    
>    /**
>     * List of DRL rules to include in the rule base.
>     * @param drlResourceList
>     */
>    public void setResourceList(Map<Resource, ResourceType> resourceList)
> throws Exception
>    {
>       Map<org.drools.io.Resource, ResourceType> resources = new
> LinkedHashMap<org.drools.io.Resource, ResourceType>();
>       for (Map.Entry<Resource, ResourceType> entry :
> resourceList.entrySet())
>       {
>         
> resources.put(ResourceFactory.newInputStreamResource(entry.getKey().getInputStream()),
> entry.getValue());
>       }
>       builder.setResourceList(resources);
>    }
>    
>    /**
>     * @param accumulateFunctionMap
>     */
>    public void setAccumulateFunctionMap(Map<String, String>
> accumulateFunctionMap)
>    {
>       builder.setAccumulateFunctionMap(accumulateFunctionMap);
>    }
> 
>    /**
>     * @{inheritDoc}
>     */
>    @Override
>    public Object getObject() throws Exception
>    {
>       return knowledgeBase;
>    }
> 
>    /**
>     * @{inheritDoc}
>     */
>    @Override
>    public Class<?> getObjectType()
>    {
>       return KnowledgeBase.class;
>    }
> 
>    /**
>     * @{inheritDoc}
>     */
>    @Override
>    public boolean isSingleton()
>    {
>       return true;
>    }
> 
>    /**
>     * @{inheritDoc}
>     */
>    @Override
>    public void afterPropertiesSet() throws Exception
>    {
>       knowledgeBase = builder.build();
> 
>    }
> 
> }
> 
> _______________________________________________
> rules-dev mailing list
> rules-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-dev
> 
> 

-- 
View this message in context: http://www.nabble.com/Drools-%2B-Spring-tp22859117p22859609.html
Sent from the drools - dev mailing list archive at Nabble.com.




More information about the rules-dev mailing list