This post explains how to integrate OpenL Tablets with your Spring Boot microservice. We’ll cover adding the necessary dependencies, configuring OpenL Tablets, and creating a service to use it.
1. Project Setup and Dependencies
Begin by creating a Spring Boot project. Then, add the following OpenL Tablets dependencies to your pom.xml
(Maven) or build.gradle
(Gradle) file:
Maven:
XML
<dependency>
<groupId>org.openl.rules</groupId>
<artifactId>org.openl.rules.ruleservice</artifactId>
<version>5.27.8</version>
</dependency>
<dependency>
<groupId>org.openl.rules</groupId>
<artifactId>org.openl.rules.ruleservice.spring</artifactId>
<version>5.27.8</version>
</dependency>
Gradle:
Gradle
implementation 'org.openl.rules:org.openl.rules.ruleservice:5.27.8'
implementation 'org.openl.rules:org.openl.rules.ruleservice.spring:5.27.8'
2. OpenL Tablets Configuration
Create a configuration class to set up OpenL Tablets. This class will define a bean for org.openl.rules.ruleservice.loader.RulesLoader
and configure the location of your Excel spreadsheet rules.
Java
import org.openl.rules.ruleservice.loader.JcrRulesLoader;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class OpenLTabletsConfig {
@Bean
public JcrRulesLoader rulesLoader() {
JcrRulesLoader rulesLoader = new JcrRulesLoader();
rulesLoader.setWorkspace("default"); // Your JCR workspace
rulesLoader.setRulesLocation("/rules"); // Location of your rules in the workspace
return rulesLoader;
}
}
This configuration:
- Creates a
JcrRulesLoader
which loads rules from a JCR repository. - Sets the
workspace
andrulesLocation
properties to specify where your rule files are located. You’ll likely need to adjust these based on your setup.
3. Create OpenL Tablets Rule Files
Create an Excel spreadsheet with your rules defined in OpenL Tablets format. For example, discount.xlsx
:
Condition | Discount |
---|---|
total price > 100 | 0.1 |
total price > 50 | 0.05 |
otherwise | 0 |
This simple table defines discount rules based on the total price of an order.
4. Deploying Rule Files
You need to deploy your Excel spreadsheet rule files to your JCR repository (e.g., Jackrabbit). The specific deployment process will depend on your JCR repository setup.
5. Using OpenL Tablets in a Service
Inject the RulesLoader
bean and use org.openl.rules.ruleservice.RuleService
to execute rules against your domain objects.
Java
import org.openl.rules.ruleservice.RuleService;
import org.openl.rules.ruleservice.loader.RulesLoader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class OrderService {
@Autowired
private RuleService ruleService;
@Autowired
private RulesLoader rulesLoader;
public Order applyRules(Order order) throws Exception {
// Make sure the rules are loaded
rulesLoader.reload();
// Execute the rule
Object result = ruleService.execute("discount", "getDiscount", new Object[] { order });
// Assuming the rule returns a double representing the discount
order.applyDiscount((Double) result);
return order;
}
}
This service:
- Injects the
RuleService
andRulesLoader
. - Calls
rulesLoader.reload()
to ensure the latest rules are loaded. - Executes the
getDiscount
method from thediscount
rule file (your Excel spreadsheet). - Applies the returned discount to the order.
6. YML Configuration (Optional)
You might need to configure your JCR repository connection details in your application.yml
or application.properties
file.
This guide provides a basic framework for integrating OpenL Tablets with your Spring Boot microservice. You can expand on this by exploring more advanced OpenL Tablets features and integrating them with your business logic. Remember to adapt the rules and domain models to your specific requirements.
Discover more from GhostProgrammer - Jeff Miller
Subscribe to get the latest posts sent to your email.