
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 theStockNameDao
,DaoImplementation
, andDaoFactory
classes that we added to the system. - We have a new tag,
<application-data>
, which assigns an instance of theDaoFactory
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 theDaoFactory
andStockNameDao
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
- Sample code for this article
- Drools Project home page
- Information on Drools rules
- "Introduction to Drools and Rule Engines," by the Drools project lead.
- Drools rules schema files
- JSR-94: Java Rule Engines, Overview
- Struts framework website
- Spring framework website
- Hibernate website
- JUnit test framework
- Jess Java rule engine
- Jena semantic and rule engine
- JSR-94 home page
- Jess in Action home page
- "Business Rule Thinking" (Jess-based)
- General introduction to rule systems
- "Jess implementation of the Rete algorithm"
Paul Browne , based in Dublin, Ireland, has been consulting in enterprise Java with FirstPartners.net for almost seven years.
Return to ONJava.com.

-
Deploying DROOLS on JBOSS
2007-12-26 21:53:58 visabox [View]
-
Problems in compiling jsp while using drools
2007-05-09 22:56:55 smithapnair [View]
- Trackback from http://jroller.com/page/bschneider?entry=drools_rules_engine
Drools rules engine
2005-09-19 04:46:28 [View]
- Trackback from http://radio.weblogs.com/0141460/2005/09/02.html#a88
Using Drools, If It Weren't For All That Bloody XML
2005-09-02 20:24:17 [View]
- Trackback from http://www.runningsystem.com/wordpress/2005/08/31/using-drools-in-your-enterprise-java-application/
Using Drools in Your Enterprise Java Application
2005-08-31 07:34:20 [View]
-
Drools and Spring (Email and reply)
2005-08-28 13:11:12 paul_browne [View]
-
Some questions regarding DRools
2005-08-27 00:00:09 Ronen [View]
-
Some questions regarding DRools
2005-08-27 12:15:29 paul_browne [View]
- Trackback from http://www.divisionbyzero.us/links/archives/2005/08/links_for_20050_127.html
links for 2005-08-26
2005-08-26 16:19:07 [View]
- Trackback from http://www.f1fe.com/blog/archives/000293.html
links for 2005-08-26
2005-08-26 16:18:47 [View]
