springboot integrates drools rule engine

Drools is a Java-based open source rules engine that allows users to define and manage business rules to automate the decision-making process in applications. Below is a simple example showing how to integrate the Drools rules engine into a Spring Boot application.

  1. add dependencies

First, add the Drools dependency to the Spring Boot application. The following dependencies can be added to the pom.xml file:

<dependency>
    <groupId>org.drools</groupId>
    <artifactId>drools-core</artifactId>
    <version>7.62.0.Final</version>
</dependency>
  1. Create rule file

Next, you need to create a rules file that defines the business rules. You can create a file named rules.drl in the resources directory and fill in the following content:

package com.example.rules

import com.example.model.Person

rule "Age greater than 18"
    when
        person : Person(age > 18)
    then
        person.setEligibleForVoting(true);
end

In this example, we define a rule called Age greater than 18, which will set the eligibleForVoting property to true for Person objects whose age is greater than 18.

  1. Create model classes

Next, a model class needs to be created to represent the business data. You can create a class named Person in the com.example.model package and fill in the following:

package com.example.model;

public class Person {
    
    
    private int age;
    private boolean eligibleForVoting;

    // getters and setters
}

In this example, we define a class called Person that contains an age property and an eligibleForVoting property.

  1. Create a rule engine service

Next, a rule engine service needs to be created to load the rule file and execute the rules. You can create a class called DroolsService in the com.example.service package and fill in the following:

package com.example.service;

import com.example.model.Person;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.stereotype.Service;

@Service
public class DroolsService {
    
    

    public void executeRules(Person person) {
    
    
        KieServices kieServices = KieServices.Factory.get();
        KieContainer kieContainer = kieServices.getKieClasspathContainer();
        KieSession kieSession = kieContainer.newKieSession();
        kieSession.insert(person);
        kieSession.fireAllRules();
        kieSession.dispose();
    }
}

In this example, we define a class called DroolsService that contains an executeRules method that loads the rules file and executes the rules. In the executeRules method, we first use the KieServices factory class to obtain the KieServices instance, and then use the KieServices instance to obtain the KieContainer instance. Next, we use the KieContainer instance to get the KieSession instance, and insert the Person object into the KieSession. Finally, we execute the rules using the fireAllRules method of the KieSession instance and close the KieSession using the dispose method.

  1. Call the rule engine service

Now, we can call the rule engine service in the controller or other services. You can create a controller called PersonController in the com.example.controller package and fill in the following:

package com.example.controller;

import com.example.model.Person;
import com.example.service.DroolsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/person")
public class PersonController {
    
    

    @Autowired
    private DroolsService droolsService;

    @PostMapping
    public void checkEligibility(@RequestBody Person person) {
    
    
        droolsService.executeRules(person);
    }
}

In this example, we define a controller called PersonController that contains a method called checkEligibility to call the rules engine service. In the checkEligibility method, we pass the Person object as the request body and pass it to the executeRules method of DroolsService.

Now, we can use a POST request to send a Person object to the /person endpoint, and the rule engine service will judge whether the Person object meets the conditions according to the rules in the rule file, and set the eligibleForVoting property to true or false.

Note: The above example is just a simple example, and it needs to be modified and optimized according to the specific situation in actual application. For example, you can place the rule file in a separate file, and use FileSystemResource in DroolsService to load the rule file; you can also use Decision Tables provided by Drools to define rules and so on.

Guess you like

Origin blog.csdn.net/m0_68705273/article/details/131008613