Java Rules Engine - Drools

xiaoxiao2021-04-01  238

InfoQ released an introduction to the rule engine. Real-world rule engines, original http://www.infoq.com/articles/rule-ENGINES

There are also two articles on ONJAVA:

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

Using drools in your enterprise java application http://www.onjava.com/lpt/a/6160

Here, you will summarize the essence:

Most web and enterprise Java applications can be divided into three parts: a front desk, one and background systems, such as database interactions, and business logic in the intermediate business logic. Now use the framework to construct the front desk and the background system has become a general consensus (for example, Struts, Cocoon, Spring, Hibernate, JDO, and Entity Beans, but there is no standard way to build business logic. Some frameworks, such as EJB and Spring only implement business logic in a high-level, but there is no help to our organization logic code, so why don't there be a frame to replace over-feasible, wrongful if ... the statement, this framework should be Other front desk or background frames are easy to configure, readability and reuse. Below we will introduce the DROOLS rule engine, this to solve the framework of our problem.

Below is a typical Java business logic code

IF ((AdministratorGroup && user.Ismemberof) || user.issuperuser () {// More checks for specificic case if ((explaceserequest.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 a method of writing business logic, but there is a problem:

If the user has another option, for example ("c987"), then the above code needs to be modified, when the code is very long, the modification code is how we guarantee the correctness of the code, this code is only programmers to see Real users, business people can't see this code has the same business logic, if a business changes, how to ensure that the consistency business logic of other business logic does not match the Java language binding? Can business logic use other scripting languages.

The rules engine standard under Java is JSR94, and its implementation has Jess Jena Drools.

In Drools, a typical business logic configuration, as follows

java.lang.object java.lang.string Net.FirstPartners.rp.stockoffer Public Void PrintStock (Net. Firstpartners.rp.stockoffer stock) {system.out.println ("name:" stock.getstockName () "Price:" stock.getStockPrice () "Buy:" stock.getrecommendpurchase ());} / java: functions> stockoffer - Note Markup -> stockoffer.getrecommendpurchase () == NULL Stockoffer.getStockPrice () <

100 stockOffer.setRecommendPurchase (StockOffer.YES); printStock (stockOffer); <- - What happens when the business rule is activated!> A rule is Rule in Rule-Set, if there are many rules, there is a lot of Rule.

The above rule means to determine if the stock price is less than 100, if the standard changes, then only the rule file is modified, not to modify the source code.

转载请注明原文地址:https://www.9cbs.com/read-131169.html

New Post(0)