2 Replies Latest reply on Sep 4, 2010 8:13 PM by Robert Morse

    Drools - Dynamic rule-files or url specification

    Robert Morse Apprentice

      I'm using Seam 2.2.0.GA and Drools as described in the docs without any issues by specifying the rule-files in components.xml like this:


      <drools:rule-base name="ingPOCRules" rule-files="/WEB-INF/ingPOCRules.drl"/>
      


      or using Guvnor, like this:


      <drools:rule-agent name="pocRules"
         url="http://localhost:8080/drools-guvnor-5.1.0.M1/org.drools.guvnor.Guvnor/package/plus/LATEST"
         local-cache-dir="/usr/local/jboss/server/default/data/cache/" poll="30"/> 
      



      What I'd like to do is specify either the rule-files (in the first case), or the url (in the second case) dynamically so my application can choose which rule files or url to use.  For example, in the second case, dynamically change from using the 'LATEST' rules package to a 'SNAPSHOT' rules package.
      Thanks!


        • 1. Re: Drools - Dynamic rule-files or url specification
          Neil Richardson Newbie

          There are a couple of things to solve here - there first is how to dynamically assign the filename/url in the built-in components, the second is to decide whether to use the file-based rulebase or Guvnor to resolve your rulebase.


          To solve the first issue create a new component:


          @Name("dynamicRuleFiles")
          @Scope(ScopeType.APPLICATION)
          public class DynamicRuleFiles {
          
          
              private String[] ruleFiles;
          
              /**
               * Call this method to clear the application-scoped rule base component
               * from the application context. The next time it is requested, Seam will
               * automatically reload it using the new value for the rule files.
               */
              public void reloadRuleBase() {
                  Contexts.getApplicationContext().remove("ingPOCRules");
              }
          
              public String[] getRuleFiles() {
                  return ruleFiles;
              }
          
              public void setRuleFiles(String[] ruleFiles) {
                  this.ruleFiles = ruleFiles;
              }
          }
          



          next configure the built-in RuleBase component to use the dynamic value in this component


          <drools:rule-base name="ingPOCRules" rule-files="#{dynamicRuleFiles.ruleFiles}"/>
          



          To use the component in your app to change the rule files dynamically can do something like:


          
              @In
              private DynamicRuleFiles dynamicRuleFiles;
          
              public void changeFiles() {
                  dynamicRuleFiles.setRuleFiles({"/WEB-INF/different-file.drl"});
                  dynamicRuleFiles.reloadRuleBase();
              }
          



          You can use a similar technique to dynamically set the url on the rule agent.


          Solving the second issue of how to decide which type of rule base to use kind of depends on how you are using the rulebase. How are you using it exactly?

          • 2. Re: Drools - Dynamic rule-files or url specification
            Robert Morse Apprentice

            Thank you for the reply.  It is very helpful.  I'd prefer using Guvnor.  However, there was an issue in 5.0 that didn't handle a rule refresh properly.  A server restart was required when a rule was changed.  I read somewhere this was fixed in the latest version, but have not tried it.  Thanks again for your reply!