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

advertisement

AddThis Social Bookmark Button

Using Drools in Your Enterprise Java Application
Pages: 1, 2, 3, 4

Now that we have our simple DAO implementation to serve as our database layer, how do we integrate it with the Drools business layer? The updated business rules file, BusinessLayer.xml, shows us how.




<?xml version="1.0"?>
<rule-set name="BusinessRulesSample"
  xmlns="http://drools.org/rules"
  xmlns:java="http://drools.org/semantics/java"
  xmlns:xs="
        http://www.w3.org/2001/XMLSchema-instance"
  xs:schemaLocation="
        http://drools.org/rules rules.xsd
  http://drools.org/semantics/java java.xsd">
  <!-- Import the Java Objects that 
                        we refer to in our rules --> 
  <java:import>
    java.lang.Object
  </java:import>
  <java:import>
    java.lang.String
  </java:import>
  <java:import>
    net.firstpartners.rp.StockOffer
  </java:import>
  <java:import>
    net.firstpartners.rp.DaoFactory
  </java:import>
  <java:import>
    net.firstpartners.rp.StockNameDao
  </java:import>
  <!-- Application Data not associated -->
  <!-- with any particular rule -->
  <!-- In this case it's our factory -->
  <!-- object which gives us back -->
  <!-- a handle to whatever Dao (Data -->
  <!-- access object) that we need -->
  <application-data 
    identifier="daoFactory">DaoFactory
  </application-data>
  <!-- A Java (Utility) function -->
  <! we reference in our rules -->        
  <java:functions>
    public void printStock(
      net.firstpartners.rp.StockOffer stock)
      {
          System.out.println(
          "Name:"+stock.getStockName()
          +" Price: "+stock.getStockPrice()     
          +" BUY:"+stock.getRecommendPurchase());
      } 
  </java:functions>
  <!-- Check for XYZ Corp-->      
  <rule name="XYZCorp" salience="-1">
    <!-- Parameters we can pass into--> 
    <!-- the business rule -->
    <parameter identifier="stockOffer">
      <class>StockOffer</class>
    </parameter">
    <!-- Conditions that must be met for -->
    <!-- business rule to fire -->        
    <java:condition>
      stockOffer.getStockName().equals("XYZ")
    </java:condition> 
    <java:condition>
      stockOffer.getRecommendPurchase() == null
    </java:condition>
    <java:condition>
      stockOffer.getStockPrice() > 10
    </java:condition>
        
    <!-- What happens when the business -->
    <!-- rule is activated -->
    <java:consequence>
        stockOffer.setRecommendPurchase(
        StockOffer.NO); 
        printStock(stockOffer);
    </java:consequence>
  </rule>
  <!-- Ensure that negative prices -->
  <!-- are not accepted -->       
  <rule name="Stock Price Not Negative">
    <!-- Parameters we can pass into the -->
    <!-- business rule -->
    <parameter identifier="stockOffer">
      <class>StockOffer</class>
    </parameter>
    <!-- Conditions for rule to fire -->
    <java:condition>
      stockOffer.getStockPrice() < 0
    </java:condition>
  
    <!--When rule is activated then ... -->
    <java:consequence>
      stockOffer.setRecommendPurchase
        (StockOffer.NO);        
      printStock(stockOffer);
    </java:consequence>
  </rule>
  <!-- Check for Negative Prices-->       
  <rule name="Stock Price Low Enough">
  <!-- Parameters for the rule -->
    <parameter identifier="stockOffer">
      <class>StockOffer</class>
    </parameter>
    
    <!-- Now uses Dao to get stock list -->
    <java:condition>
      daoFactory.getStockDao().isOnStockList(
        stockOffer.getStockName())
    </java:condition>
        
    <java:condition>
      stockOffer.getRecommendPurchase() == null
    </java:condition>
    <java:condition>
      stockOffer.getStockPrice() < 100
    </java:condition>
    <!-- When rule is activated do this -->
    <java:consequence>
        stockOffer.setRecommendPurchase(
          StockOffer.YES);      
          printStock(stockOffer);
    </java:consequence>
  </rule>
</rule-set>

There are several changes to this file to integrate the data access layer with our business rules:

  • At the top of the file, we have several new <java:import> statements to reference the StockNameDao, DaoImplementation, and DaoFactory classes that we added to the system.
  • We have a new tag, <application-data>, which assigns an instance of the DaoFactory class to a variable. <application-data> tags are similar to parameters, except they apply to all business rules, and not just one.
  • The Stock Price Low Enough rule has a new condition, which uses the DaoFactory and StockNameDao to check if the stock is on the list of those that we deal with.

We run our BusinessRulesTest (simulator) again. The simulator/unit tests run OK, since even though we have changed the structure of the program, we haven't (yet) changed what it does. From looking at the output logs, we can see that our business rules are using StockNameDao as part of their evaluations, and that DaoImplementation.isOnStockList() is being called.

While this example shows the reading of information from a data source, the principles are the same for writing information, if that is what a rule has decided should be done. The differences would be that our DAO would have a setSomeInformation() method, and that the method would be called in the <java:consequence> part of the business rule, once the specific conditions had been met.

Summary

In this article, we showed that most Java server applications have three tiers: presentation, business logic, and data persistence. While the use of frameworks is widely accepted in the presentation and persistence layers, until now no framework has been available to encapsulate low-level business logic. As we've seen in the examples, Drools and JSR-94 are ideal candidates for reducing the complexity and speeding the development of Java applications. I hope that these examples inspire you to take a closer look at rule engines, and that they save many hours of development and maintenance time in your applications.

Resources

Paul Browne , based in Dublin, Ireland, has been consulting in enterprise Java with FirstPartners.net for almost seven years.


Return to ONJava.com.