Please state the source of the article. Welcome to add Echo wechat (wechat id: T2421499075) for exchange and learning.


Drools is not difficult to implement; the key is to understand its syntax

An introduction to case

  • Create a simple SpringBoot project

  • When selecting a dependency, add a Web dependency

  • Add drools dependencies to pom.xml for the new project
<properties>
    <java.version>1.8</java.version>
    <drools.version>7.14.0. The Final</drools.version>
</properties>
<dependencies>
<! -- Drools dependencies -->
    <dependency>
        <groupId>org.drools</groupId>
        <artifactId>drools-core</artifactId>
        <version>${drools.version}</version>
    </dependency>
    <dependency>
        <groupId>org.drools</groupId>
        <artifactId>drools-compiler</artifactId>
        <version>${drools.version}</version>
    </dependency>
    <! -- Decision table -->
    <dependency>
        <groupId>org.drools</groupId>
        <artifactId>drools-decisiontables</artifactId>
        <version>${drools.version}</version>
    </dependency>
    <! -- -- -- > templates
    <dependency>
        <groupId>org.drools</groupId>
        <artifactId>drools-templates</artifactId>
        <version>${drools.version}</version>
    </dependency>
    
    <dependency>
        <groupId>org.kie</groupId>
        <artifactId>kie-api</artifactId>
        <version>${drools.version}</version>
    </dependency>
</dependencies>
Copy the code
  • Create Kie related beans to be managed by Spring
import org.kie.api.KieBase;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.KieModule;
import org.kie.api.builder.KieRepository;
import org.kie.api.builder.ReleaseId;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.internal.io.ResourceFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.io.Resource;

import java.io.IOException;

/ * * *@author tang.sl
 * @dateThe 2021-01-05 09:37 * /
@Configuration
public class KiaSessionConfig {

    private static final String RULES_PATH = "rules/";

    @Bean
    public KieFileSystem kieFileSystem(a) throws IOException {
        KieFileSystem kieFileSystem = getKieServices().newKieFileSystem();
        for (Resource file : getRuleFiles()) {
            kieFileSystem.write(ResourceFactory.newClassPathResource(RULES_PATH + file.getFilename(), "UTF-8"));
        }
        return kieFileSystem;
    }

    private Resource[] getRuleFiles() throws IOException {

        ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
        final Resource[] resources = resourcePatternResolver.getResources("classpath*:" + RULES_PATH + "* * / *. *");
        return resources;

    }

    @Bean
    public KieContainer kieContainer(a) throws IOException {

        final KieRepository kieRepository = getKieServices().getRepository();
        kieRepository.addKieModule(new KieModule() {
            public ReleaseId getReleaseId(a) {
                returnkieRepository.getDefaultReleaseId(); }}); KieBuilder kieBuilder = getKieServices().newKieBuilder(kieFileSystem()); kieBuilder.buildAll();return getKieServices().newKieContainer(kieRepository.getDefaultReleaseId());

    }

    private KieServices getKieServices(a) {
        return KieServices.Factory.get();
    }

    @Bean
    public KieBase kieBase(a) throws IOException {
        return kieContainer().getKieBase();
    }

    @Bean
    public KieSession kieSession(a) throws IOException {
        returnkieContainer().newKieSession(); }}Copy the code

At this point, our basic preparations are complete and we are ready to create an actual case to use Drools.

  • Define an entity class based on the previous requirements
@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private String name;
    private String age;
    private String address;
    private String phone;
    private String sex;
}
Copy the code
  • Create a DRL rule file

Based on what was described in the previous articles, let’s design a rules file. The files will be placed under the Resources directory, where a rules file will be created to hold all the following rule files

package com.echo.drools.dto import com.echo.drools.dto.User dialect "java" rule "address eq beijing" when $user: Println ($user.getName() + ""); End rule "address eq Shanghai "when $user: user (address ==" Shanghai ") then system.out.println ($user.getName() + "Shanghai "); endCopy the code
  • Then we can test using this rule
import com.echo.drools.dto.User;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class DroolsApplicationTests {

    @Autowired
    private KieSession session;

    @Test
    void contextLoads(a) {
        User user = new User();
        user.setAddress("Beijing");
        user.setAge("20");
        user.setName("echo");
        user.setPhone("136123456");
        user.setSex("1");
        // Insert the user
        session.insert(user);
        // Execute the rule
        session.fireAllRules();
    }

    /** * Remember to release resources after execution */
    @AfterEach
    public void runDispose(a) { session.dispose(); }}Copy the code
  • Many times test

The result is exactly what we expected, as in the example above, which eventually outputs “Echo is north drift” on the console.

Note: we have only written two rules here, and we have not written the corresponding business code, which will be covered later. This is just a primer case. Don’t worry too much about why there is no business code