ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Give Your Business Logic a Framework with Drools
Pages: 1, 2, 3, 4, 5, 6, 7

This class now has some important methods:

  • loadRules(), which loads the rules from the BusinessRules.drl file.
  • An updated evaluateStockPurchase(), which evaluates these business rules. Some points to note about this method are:
    • We can reuse the same RuleSet over and over (as business rules in memory are stateless).
    • We use a new WorkingMemory for every evaluation, as this is our knowledge of what we know to be true at this time. We use assertObject() to place known facts (as Java Objects) into this memory.
    • Drools has an event listener model, to allow us to "see" what is going on within the event model. Here we use it to print debug information.
  • The fireAllRules() method on the working memory class causes the rules to be evaluated and updated (in this case, stock offer).

Before we can run the example again, we need to create our BusinessRules.drl file, as follows:

<?xml version="1.0"?>
<rule-set name="BusinessRulesSample"
    ="http://drools.org/rules rules.xsd
  http://drools.org/semantics/java java.xsd">
  <!-- Import the Java Objects that we refer 
                          to in our rules -->        
  <!-- A Java (Utility) function we reference 
    in our rules-->  
    public void printStock(
      net.firstpartners.rp.StockOffer stock)
          +" Price: "+stock.getStockPrice()     
          +" BUY:"
  <!-- Ensure stock price is not too high-->      
  <rule name="Stock Price Low Enough">
    <!-- Params to pass to business rule -->
    <parameter identifier="stockOffer">
    <!-- Conditions or 'Left Hand Side' 
        (LHS) that must be met for 
         business rule to fire -->
    <!-- note markup -->
      stockOffer.getRecommendPurchase() == null
      stockOffer.getStockPrice() < 100
    <!-- What happens when the business 
                      rule is activated -->

This rules file has several interesting parts:

  • Just after the XML-Schema definitions come the Java objects we reference in our rules. These objects can come from any Java library as required.
  • Next comes our functions, which can incorporate standard Java code. In this case, we incorporate a logging function to help us see what is going on.
  • After that comes our rule set, consisting of one or more rules.
  • Each rule can take parameters (the StockOffer class), one or more conditions that need to be fulfilled, and a consequence that is carried out if and when the conditions are met.

Having modified and compiled our code, we run the JUnit test simulations again. This time, the business rules are called, our logic evaluates correctly, and our tests pass, as seen in Figure 3. Congratulations--you've just built your first rule-based application!

Successful JUnit Test
Figure 3. Successful JUnit test

Pages: 1, 2, 3, 4, 5, 6, 7

Next Pagearrow