Drools brief

Drools- my classmates did not tell me that, I have not heard of this thing, do not know what it is doing, so late at night supplementary rations ....

Drools Introduction

Drools rule is simply that he was born to rule. Some complex business scenarios, only by our daily determination process is not enough, the rules are changing rapidly, with the uncontrollable factors (weather, date, event, etc.), the rules also will change, and the project is You can not subsequently be changed on-line.

The Drools is in line with one of the current business scene change rules engine. Once the data satisfies the condition matching is performed, a logic corresponding to:

when 满足什么样的条件
then 最终的结果
复制代码

Why do we need rules of intervention

In some simple and not complicated traffic system, only we need conditional statements to determine what you like, no need to introduce rules engine. But for the business scene in a huge project every day as well as possible each time point will have different rules involved, this is not satisfied with the current situation. And product developers will only make people headaches.

Business rules often follow the following principles:

  • They are independent
  • They are easy to update
  • Each rule the minimum amount of information required for control
  • They allow people of different backgrounds to cooperate

Drl execution process

Drools rules engine to convert business rules to perform tree:

Each rule condition is divided into small pieces, connected in a tree structure, and reuse. Each time data is added to the rule engine, it will be carried out in the tree like this is evaluated, and reaches an action node, at the node, they will be marked as ready to perform data specific rules.

Simple Demo Description

I am here simply to achieve a local small Demo, which is a simple to use the current rules engine briefing.

The current project directory hierarchy

├── pom.xml
├── src
│   ├── main
│   │   ├── java
│   │   │   └── com
│   │   │       └── montos
│   │   │           ├── DroolsStartApplication.java
│   │   │           ├── controller
│   │   │           │   └── TestController.java
│   │   │           ├── entity
│   │   │           │   └── Message.java
│   │   │           ├── impl
│   │   │           │   └── DroolsServiceImpl.java
│   │   │           └── service
│   │   │               └── DroolsService.java
│   │   └── resources
│   │       ├── META-INF
│   │       │   └── kmodule.xml
│   │       └── rules
│   │           └── Helloworld.drl
│   └── test
│       └── java
└── target
    ├── classes
    │   ├── META-INF
    │   │   └── kmodule.xml
    │   ├── com
    │   │   └── montos
    │   │       ├── DroolsStartApplication.class
    │   │       ├── controller
    │   │       │   └── TestController.class
    │   │       ├── entity
    │   │       │   └── Message.class
    │   │       ├── impl
    │   │       │   └── DroolsServiceImpl.class
    │   │       └── service
    │   │           └── DroolsService.class
    │   └── rules
    │       └── Helloworld.drl
    └── generated-sources
        └── annotations

复制代码

pom.xml dependence

<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.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>
        <!-- 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>
复制代码

serviceImpl class

public class DroolsServiceImpl implements DroolsService {

    @Override
    public String fireRule() {
        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);//插入
        kSession.fireAllRules();//执行规则
        kSession.dispose();
        return message.getMessage();

    }
}
复制代码

kmodule.xml

<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
    <kbase name="rules" packages="rules">
        <ksession name="ksession-rules"/>
    </kbase>
</kmodule>
复制代码

Helloworld.drl

package com.montos.drools.test1 #虚拟包名,不一定需要与物理存在的包名一致
import com.montos.entity.Message
dialect  "mvel" # 方言  默认是java

rule "Hello World" # 规则名
    dialect "mvel"
    when
        m : Message(status.equals(Message.HELLO), message : message )#条件
    then
        System.out.println( message);#动作
    modify ( m ) { message = "Goodbye cruel world",status = Message.GOODBYE }; //更新语句,会导致引擎重新检查所有规则是否匹配条件,而不管其之前是否执行过
end

rule "Good Bye"
    dialect "java"
    when
       Message( status == Message.GOODBYE, message : message )
    then
        System.out.println( message );
end
复制代码

Above a brief introduction of some important code, which if implemented, mainly to see servicethe inside of the logic implementation classes, access policy configuration rules, and according to drlcarry on running in the outcome document of the rules in the policy.

For the current Drools above brief introduction on this information online mostly for simple and similar to the current presentation Api introduction Demo for complex business scenarios it is not. Data can leave a small partner to share the next I would be grateful.

Some of the information in the article reprinted from: blog.csdn.net/chinrui/art...

Guess you like

Origin juejin.im/post/5d8dbb4c6fb9a04dee180bc4
Recommended