Java Rule Engine-drools

Source: Internet
Author: User

Infoq published an introduction to the rule engine real-world rule engines, the original http://www.infoq.com/articles/Rule-Engines

There are also two articles on onjava:

Give your business logic a framework with drools http://www.onjava.com/lpt/a/6093

Using drools in your Enterprise Java application http://www.onjava.com/lpt/a/6160

Below is a summary of the essence:

Most Web and Enterprise Java applications can be divided into three parts: a front-end that interacts with users, a backend system, such as the database interaction service layer, and the business logic between them. Now it has become a common consensus to use the framework to build front-end and back-end systems (such as struts, cocoon, spring, hibernate, JDO, and entity beans ), however, there is no standard method to build business logic. Some frameworks, such as EJB and spring, implement business logic only at one level, but they do not help us to organize logic code. So why is there no framework to replace the complexity, if... then statements, this framework should be the same as other front-end or back-end frameworks, which are easy to configure, readable, and reusable. Next we will introduce the drools rule engine, which is the framework to solve our problem.

The following is a typical Java business logic code.

If (user. ismemberof (administratorgroup)
& User. ismemberof (teleworkergroup ))
| User. issuperuser (){

// More checks for specific cases
If (expenserequest. Code (). Equals ("b203 ")
| (Expenserequest. Code (). Equals ("a903 ")
& (Totalexpenses <200)
& (Bosssignoff> totalexpenses ))
& (Deptbudget. notexceeded )){
// Issue payments
} Else if {
// Check lots of other conditions
}
} Else {
// Even more business logic
}

This is the way most programmers write business logic, but there are the following problems:

  • If the user has another option, such as ("c987"), you need to modify the above Code. When the code is very long, it is difficult to modify the code.
  • How can we ensure the correctness of the code? This code can only be seen by programmers, real users, and business personnel cannot see this code
  • Many applications have the same business logic. If a business changes, how can we ensure the consistency of other business logic?
  • Can business logic be bound in different Java languages?
  • Whether the business logic can be used in other scripting languages.

The rule engine standard in Java is jsr94, which implements Jess Jena drools.

In drools, a typical business logic configuration is as follows:

<? 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>
<! -- 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>
<Rule-set>
<! -- Ensure stock price is not too high -->
<Rule name = "stock price low enough">
<! -- Params to pass to business rule -->
<Parameter identifier = "stockoffer">
<Class> stockoffer </class>
</Parameter>
<! -- Conditions or 'left hand side'
(LHS) that must be met
Business Rule to fire -->
<! -- Note markup -->
<Java: Condition>
Stockoffer. getrecommendpurchase () = NULL
</Java: Condition>
<Java: Condition>
Stockoffer. getstockprice () <100
</Java: Condition>
<! -- What happens when the business
Rule is activated -->
<Java: Consequence>
Stockoffer. setrecommendpurchase (
Stockoffer. Yes );
Printstock (stockoffer );
</Java: Consequence>
</Rule>
</Rule-set>

A rule is the rule in the rule-set. If there are many rules, you need to write a lot of rule.

The rule above indicates to determine whether the stock price is less than 100. If the 100 standard is changed, you only need to modify the rule file instead of modifying the source code.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.