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
      Game(guessCount == @{guesscount} )
      if ( decision.getOutcome()==null )

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:

public class NumberGuessTemplateDataProvider implements TemplateDataProvider
   Collection<Map<String, Object>> templateData = new ArrayList<Map<String,Object>>();
   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
   // 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="" knowledge-base-config="">

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