Drools- in my classmate did not tell me, I have not heard of this thing, do not know what it is, so late night supplement dry food….
Drools is introduced
Drools are simply rules, and they live for rules. In some complex and changeable business scenarios, it is not enough to just rely on our daily judgment. Rules change rapidly. With uncontrollable factors (weather, date, activities, etc.), rules also change, and it is impossible for projects to be changed and put online accordingly.
Drools is one of the rules engines that matches the current business scenario changes. Once the data meets a conditional match, the corresponding logical processing is performed:
What conditions does when satisfy then the final resultCopy the code
Why do rules need to intervene
In some simple and uncomplicated systems, only conditional statements are needed to determine, there is no need to introduce rules engine. But for a large business scenario and a project where different rules may be involved at every point of the day, this is not the case. It’s just a headache for developers and product people.
Business rules tend to follow the following principles:
- They’re independent
- They are easy to update
- Each rule controls the minimum amount of information required
- They allow people from different backgrounds to collaborate
Drl execution process
The Drools rules engine converts business rules into execution trees:
Demo Introduction
My local simple implementation of a small Demo, which is a simple introduction to the use of the current rules engine.
Current project directory level
├ ─ ─ pom. XML ├ ─ ─ the SRC │ ├ ─ ─ the main │ │ ├ ─ ─ Java │ │ │ └ ─ ─ com │ │ │ └ ─ ─ montos │ │ │ ├ ─ ─ DroolsStartApplication. Java │ │ │ ├ ─ ─ controller │ │ │ │ └ ─ ─ TestController. Java │ │ │ ├ ─ ─ the entity │ │ │ │ └ ─ ─ Message. Java │ │ │ ├ ─ ─ impl │ │ │ │ └ ─ ─ DroolsServiceImpl. Java │ │ │ └ ─ ─ service │ │ │ └ ─ ─ DroolsService. Java │ │ └ ─ ─ resources │ │ ├ ─ ─ meta-inf │ │ │ └ ─ ─ Kmodule. XML │ ├ ─ sci-imp ─ new class │ ├ ─ sci-imptest│ └ ─ ─ Java └ ─ ─ target ├ ─ ─ classes │ ├ ─ ─ meta-inf │ │ └ ─ ─ kmodule. XML │ ├ ─ ─ com │ │ └ ─ ─ montos │ │ ├ ─ ─ DroolsStartApplication. Class │ │ ├ ─ ─ controller │ │ │ └ ─ ─ TestController. Class │ │ ├ ─ ─ the entity │ │ │ └ ─ ─ the Message. The class │ │ ├─ ├─ ├─ ├─ ├─ ├─ ├─ ├─ ├─ ├─ ├─ ├─ ├─ Garbage └── generating -sources garbage ─ annotationsCopy the code
Pom. XML dependency
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-core</artifactId>
<version>7.0.0. The Final</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
<version>7.0.0. The Final</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-decisiontables</artifactId>
<version>7.0.0. The Final</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-templates</artifactId>
<version>7.0.0. The Final</version>
</dependency>
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-api</artifactId>
<version>7.0.0. The Final</version>
</dependency>
<! -- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
Copy the code
ServiceImpl class
public class DroolsServiceImpl implements DroolsService {
@Override
public String fireRule(a) {
KieServices ks = KieServices.Factory.get();
KieContainer kContainer = ks.getKieClasspathContainer();
KieSession kSession = kContainer.newKieSession("ksession-rules");
Message message = new Message();
message.setMessage("Good Bye");
message.setStatus(Message.GOODBYE);
kSession.insert(message);/ / insert
kSession.fireAllRules();// Execute the rule
kSession.dispose();
returnmessage.getMessage(); }}Copy the code
kmodule.xml
<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
<kbase name="rules" packages="rules">
<ksession name="ksession-rules"/>
</kbase>
</kmodule>
Copy the code
Helloworld.drl
Package com.montos.drools.test1 Need not necessarily consistent with the physical existence of the package name import com. Montos. Entity. The Message the dialect "mvel" default # dialect is the Java rule # "Hello World" rules of the dialect "mvel" When m: Message(status.equals(message.hello), Message: Message)# then System.out.println(Message); # action modify (m) {message = "Goodbye cruel world",status = message.goodbye}; // Update statement, which causes the engine to recheck all rules for matching conditions, End rule "Good Bye" dialect "Java" when Message(status == message.goodbye, Message: message ) then System.out.println( message ); endCopy the code
The DRL file is used to execute the DRL policy. The DRL file is used to execute the DRL policy.
The above is a brief introduction to the current Drools. Most of the materials on the web are for simple Api introductions and similar to the current ones. There are no demos for complex business scenarios. If you have any information, you can share it with me. Thank you very much.
Article part data reproduced from: blog.csdn.net/chinrui/art…