1. Origin of the Rule Engine
One day the operation wants to develop a credit strategy to calculate the extra credit amount according to the following rules:
The original price of the order is less than 100 without additional points.
100-500 plus 100 points;
500-1000 plus 500 points;
1000 plus 1000 points;
Traditional java business implements the following:
public class JavaScoreExample { public static void main(String[] args) throws Exception { List<Order> orderList = getInitData(); for (int i=0; i<orderList.size(); i++){ Order order = orderList.get(i); if (order.getAmout() <= 100){ order.setScore(0); addScore(order); }else if(order.getAmout() > 100 && order.getAmout() <= 500){ order.setScore(100); addScore(order); }else if(order.getAmout() > 500 && order.getAmout() <= 1000){ order.setScore(500); addScore(order); }else{ order.setScore(1000); addScore(order); } } } private static void addScore(Order o){ System.out.println("user" + o.getUser().getName() + "Enjoy extra points: " + o.getScore()); } private static List<Order> getInitData() throws Exception { List<Order> orderList = new ArrayList<Order>(); DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); { Order order = new Order(); order.setAmout(80); order.setBookingDate(df.parse("2015-07-01")); User user = new User(); user.setLevel(1); user.setName("Name1"); order.setUser(user); orderList.add(order); } { Order order = new Order(); order.setAmout(200); order.setBookingDate(df.parse("2015-07-02")); User user = new User(); user.setLevel(2); user.setName("Name2"); order.setUser(user); orderList.add(order); } return orderList; } }
At this time, due to the changes in market demand and the adjustment of rules, it is very difficult to modify and deploy code at the business level.It would be very convenient for developers if we could separate the decision rules from the application!Thus, the rule engine was born!
Rule Engine Advantages:
Users of the system
Delegate the right to create, modify, and maintain business strategies (rules) to the business manager
Increase business flexibility
Enhance transparency in business processes so business rules can be managed
Reduce Dependency on IT Personnel
Avoid risk of future upgrades
For IT developers
Simplify system architecture, optimize application
Improving system maintainability and maintenance costs
Convenient system integration
Reduce costs and risks of writing "hard code" business rules
There are currently several popular rule engines on the market:
Ilog JRules Is the most famous commercial BRMS;
Drools Is the most active open source rule engine;
Jess Is the java implementation of Clips, just like JRuby over Ruby, which is the representative of AI system;
Visual Rules (Flag Regular Rule Engine) Brand of domestic business rule engine.
This article will focus on Drools.
2. What is Drools
Drools is a CRTEE algorithm based on Charles Forgy's. It is easy to access enterprise strategy, easy to adjust and easy to manage open source business rule engine. It meets industry standards, is fast and efficient.
Business analysts or auditors can use it to easily view business rules to verify that coded rules execute the required business rules.
Drools is an open source rule engine written in Java that evaluates the rules written using the Rete algorithm.Drools allow business logic to be expressed declaratively.Rules can be written in non-XML native languages for easy learning and understanding.Also, you can embed Java code directly into the rule file, which makes learning Drools more interesting.
3. Drools Actual War
Here we'll use drools to strip some of the rules for calculating extra credit from the code:
1. Introducing pom files
<dependency> <groupId>org.drools</groupId> <artifactId>drools-core</artifactId> <version>7.0.0.Final</version> </dependency> <dependency> <groupId>org.drools</groupId> <artifactId>drools-compiler</artifactId> <version>7.0.0.Final</version> </dependency> <dependency> <groupId>org.drools</groupId> <artifactId>drools-decisiontables</artifactId> <version>7.0.0.Final</version> </dependency> <dependency> <groupId>org.drools</groupId> <artifactId>drools-templates</artifactId> <version>7.0.0.Final</version> </dependency> <dependency> <groupId>org.kie</groupId> <artifactId>kie-api</artifactId> <version>7.0.0.Final</version> </dependency>
2. Create a new Point-rules.drl rule file at src/main/resources/ruls:
package rules import com.neo.drools.entity.Order rule "zero" no-loop true lock-on-active true salience 1 when $s : Order(amout <= 100) then $s.setScore(0); update($s); end rule "add100" no-loop true lock-on-active true salience 1 when $s : Order(amout > 100 && amout <= 500) then $s.setScore(100); update($s); end rule "add500" no-loop true lock-on-active true salience 1 when $s : Order(amout > 500 && amout <= 1000) then $s.setScore(500); update($s); end rule "add1000" no-loop true lock-on-active true salience 1 when $s : Order(amout > 1000) then $s.setScore(1000); update($s); end
3. src/main/resources/META-INF New Profile kmodule.xml:
<?xml version="1.0" encoding="UTF-8"?> <kmodule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.drools.org/xsd/kmodule"> <kbase name="point-rulesKB" packages="rules"> <ksession name="point-rulesKS"/> </kbase> <kbase name="HelloWorldKB" packages="rules"> <ksession name="HelloWorldKS"/> </kbase> </kmodule>
4. Finally, the rule is invoked in the program to achieve:
/** * The rules for calculating extra credit amount are as follows: original order price amount * 100 Below, no bonus * 100-500 Add 100 points * 500-1000 Add 500 points * 1000 1000 points above * * @param args * @throws Exception */ public static final void main(final String[] args) throws Exception{ // KieServices is the factory for all KIE services KieServices ks = KieServices.Factory.get(); // From the kie services, a container is created from the classpath KieContainer kc = ks.getKieClasspathContainer(); execute( kc ); } public static void execute( KieContainer kc ) throws Exception{ // From the container, a session is created based on // its definition and configuration in the META-INF/kmodule.xml file KieSession ksession = kc.newKieSession("point-rulesKS"); List<Order> orderList = getInitData(); for (int i = 0; i < orderList.size(); i++) { Order o = orderList.get(i); ksession.insert(o); ksession.fireAllRules(); // After executing the rule, execute the relevant logic addScore(o); } ksession.dispose(); } private static void addScore(Order o){ System.out.println("user" + o.getUser().getName() + "Enjoy extra points: " + o.getScore()); } private static List<Order> getInitData() throws Exception { List<Order> orderList = new ArrayList<Order>(); DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); { Order order = new Order(); order.setAmout(80); order.setBookingDate(df.parse("2015-07-01")); User user = new User(); user.setLevel(1); user.setName("Name1"); order.setUser(user); order.setScore(111); orderList.add(order); } { Order order = new Order(); order.setAmout(200); order.setBookingDate(df.parse("2015-07-02")); User user = new User(); user.setLevel(2); user.setName("Name2"); order.setUser(user); orderList.add(order); } return orderList; }
Output of results:
Summary:
Application Scenarios
The management process must be automated to improve efficiency, despite the complexity of modern business rules.
The market requires frequent changes in business rules, and systems must be updated quickly and at a low cost based on changes in business rules.
For fast, low-cost updates, business people should be able to directly manage the rules in the system without the involvement of program developers.
Functions and advantages:
Separate and decouple business rules from business systems;
Implement natural language description rule logic, which is easy for business people to understand;
Visual rule customization, approval, inquiry and management;
Improving the maintainability of code that implements complex logic effectively;
Deal with special situations where the customer did not initially mention taking business logic into account;
Compliant with the organization's use of the agile or iterative development process;
As shown below:
The following will continue to explain if an enterprise uses springboot to integrate drools development and implement hot refresh rules!