Numberguess example using Drools5 Templates

Posted by    |      

Before I dig into the a Fusion example, I wanted to show current integration with Drools5 Templates. Rule templates are helpful when you have a number of rules which follow some sort of pattern(s). In addition you can use templates when you have reoccurring hard-coded values in your rule patterns or actions. The Seam numberguess example is not the best example for rule templates however is good enough for a simple example.

If we look at base numberguess.drl DRL, we can see some hard-coded values, for example the guessCount is set to 9, the guess increment value is 1 and we also see hard-coded values for win and loose, all of which we want to externalize from our rule resource. This is what our new rule template can look like. If we look at rule Lose we can see some relevant changes:

rule Lose
   when 
      Game(guessCount == @{guesscount} )
   then
      if ( decision.getOutcome()==null )
      {
         decision.setOutcome("@{losetext}");
      }
end

Please follow the Drools documentation on further explanation on the template syntax.

In order to expand our template, we must provide a data source. With Seam this expansion is driven by custom Seam components which implements the TemplateDataProvider interface that defines a single method:

   public Collection<Map<String,Object>> getTemplateData();

For this simple example we implement a template data provider with hard-coded values:

@Name("numberGuessTemplateDataProvider")
public class NumberGuessTemplateDataProvider implements TemplateDataProvider
{
   Collection<Map<String, Object>> templateData = new ArrayList<Map<String,Object>>();
   
   @Create
   public void init()
   {
      //  We use a Map which provides the values for substituting template parameters.
      // It should have a (String) key set matching all of the parameter names. 
      // Values can be from any class, as long as they provide a good toString() method
      Map<String, Object> m1 = new HashMap<String, Object>();
      m1.put("wintext", "win");  // substitutes the @{wintext} template parameter
      m1.put("losetext", "lose"); // substitutes the @{losetext} template parameter
      m1.put("guesscount", 9); // substitutes the @{guesscount} template parameter
      m1.put("increment", 1); // substitutes the @{increment} template parameter
      
      templateData.add(m1);
   }
   
   // getters and setters
}

Now all that is left is configuration in components.xml. First we define our KnowledgeBase:

<drools:knowledge-base name="kbase" knowledge-builder-config="kbuilderconfig.properties" knowledge-base-config="kbaseconfig.properties">
    	<drools:rule-resources>
    		<value>classpath;numberguesstemplate.drl;DRL;numberGuessTemplateDataProvider</value>
    	</drools:rule-resources>	
    	<drools:event-listeners>
    		<value>org.drools.event.knowledgebase.DefaultKnowledgeBaseEventListener</value>
    	</drools:event-listeners>
</drools:knowledge-base>

Note here that we have only added the name of our template data provider to the rule-resource string...that's it. Last thing to do is to define our StatefulKnowledgeSession as shown in the previous post.


Back to top