This post explores how to integrate the Jess rule engine with your Spring Boot microservice. We’ll cover adding the necessary dependency, configuring Jess, and creating a service to utilize it.

1. Project Setup and Dependency

Begin by creating a Spring Boot project. Then, add the Jess dependency to your pom.xml (Maven) or build.gradle (Gradle) file:

Maven:

<dependency>
    <groupId>org.jess</groupId>
    <artifactId>jess</artifactId>
    <version>7.1p2</version> 
</dependency>

Gradle:

implementation 'org.jess:jess:7.1p2'

2. Jess Configuration

While Spring Boot doesn’t have auto-configuration for Jess, we can easily create a configuration class to initialize and manage the Jess engine.

import jess.Rete;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class JessConfig {

    @Bean
    public Rete rete() {
        return new Rete();
    }
}

This configuration:

  • Creates a Rete object, which is the core of the Jess engine. This object will be used to load and execute rules.

3. Creating and Loading Jess Rule Files

Create a rules directory in your src/main/resources folder. Add your Jess rule files (.clp files) here. For example, discount.clp:

(defrule apply-discount
    (order (totalPrice ?tp) (discount ?d))
    (test (> ?tp 100))
=>
    (modify ?order (discount (* ?d 1.1)))
)

In your service, load these rules into the Jess engine.

4. Using the Jess Engine in a Service

Inject the Rete bean into your service class and use it to execute rules against your domain objects.

import jess.Rete;
import jess.JessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderService {

    @Autowired
    private Rete rete;

    public Order applyRules(Order order) throws JessException {
        rete.reset(); // Reset the engine
        rete.eval("(load-facts \"classpath:rules/discount.clp\")"); // Load rules
        rete.add(order); // Assert the order object
        rete.run(); // Execute the rules
        return order; 
    }
}

This service:

  • Injects the Rete object.
  • Loads the discount.clp rule file.
  • Asserts the order object into the Jess working memory.
  • Executes the rules using rete.run().
  • Returns the modified order object.

5. YML Configuration (Optional)

Jess doesn’t typically require configuration in your application.yml or application.properties file. However, you might consider externalizing file paths or other Jess-specific settings if needed.

This guide provides a starting point for integrating Jess with your Spring Boot microservice. You can build upon this foundation by exploring more advanced Jess features like functions, templates, and defqueries to create complex rule-based systems. Remember to tailor the rules and domain models to your specific business needs.


Discover more from GhostProgrammer - Jeff Miller

Subscribe to get the latest posts sent to your email.

By Jeffery Miller

I am known for being able to quickly decipher difficult problems to assist development teams in producing a solution. I have been called upon to be the Team Lead for multiple large-scale projects. I have a keen interest in learning new technologies, always ready for a new challenge.