Spring - Drools
Overview
Spring boot integration with Drools.
Github: https://github.com/gitorko/project63
Drools
Drools is a Business Rule Management System (BRMS). Business & Non-Technical users can write the rules in a format that is easy to understand and plug it into drools engine. These rules/facts are processed to produce results. Cost of changing the rules is low.
Code
1package com.demo.project63;
2
3import java.util.Arrays;
4import java.util.Collections;
5import java.util.HashMap;
6import java.util.Map;
7
8import lombok.extern.slf4j.Slf4j;
9import org.kie.api.KieServices;
10import org.kie.api.builder.KieBuilder;
11import org.kie.api.builder.KieFileSystem;
12import org.kie.api.builder.KieModule;
13import org.kie.api.runtime.KieContainer;
14import org.kie.api.runtime.KieSession;
15import org.kie.internal.io.ResourceFactory;
16import org.springframework.boot.CommandLineRunner;
17import org.springframework.boot.SpringApplication;
18import org.springframework.boot.autoconfigure.SpringBootApplication;
19import org.springframework.context.annotation.Bean;
20
21@SpringBootApplication
22@Slf4j
23public class Main {
24
25 public static void main(String[] args) {
26 SpringApplication.run(Main.class, args);
27 }
28
29 @Bean
30 public CommandLineRunner sendData(KieContainer kContainer) {
31 return args -> {
32 //Simple
33 KieSession kieSession1 = kContainer.newKieSession();
34 Product p1 = new Product();
35 p1.setType("desktop");
36 p1.setRegions(Collections.emptyMap());
37 p1.setManufacturers(Collections.emptyList());
38 kieSession1.insert(p1);
39 kieSession1.fireAllRules();
40 kieSession1.dispose();
41 log.info("Discount on {} is {}", p1.getType(), p1.getDiscount());
42
43 //Iterates a Map.
44 KieSession kieSession2 = kContainer.newKieSession();
45 Product p2 = new Product();
46 p2.setType("laptop");
47 Map<String, String> r2 = new HashMap<>();
48 r2.put("region1", "A");
49 r2.put("region2", "B");
50 r2.put("region3", "C");
51 p2.setRegions(r2);
52 p2.setManufacturers(Collections.emptyList());
53 kieSession2.insert(p2);
54 kieSession2.fireAllRules();
55 kieSession2.dispose();
56 log.info("Discount on {} is {}", p2.getType(), p2.getDiscount());
57
58 //Iterates List
59 KieSession kieSession3 = kContainer.newKieSession();
60 Product p3 = new Product();
61 p3.setType("keyboard");
62 p3.setRegions(Collections.emptyMap());
63 p3.setManufacturers(Arrays.asList("Company1", "Company2"));
64 kieSession3.insert(p3);
65 kieSession3.fireAllRules();
66 kieSession3.dispose();
67 log.info("Discount on {} is {}", p3.getType(), p3.getDiscount());
68 };
69 }
70
71 @Bean
72 public KieContainer kieContainer() {
73 KieServices kieServices = KieServices.Factory.get();
74 KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
75 kieFileSystem.write(ResourceFactory.newClassPathResource("product-discount.drl"));
76 KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
77 kieBuilder.buildAll();
78 KieModule kieModule = kieBuilder.getKieModule();
79 return kieServices.newKieContainer(kieModule.getReleaseId());
80 }
81
82}
1import com.demo.project63.Product;
2import java.util.Map;
3
4rule "Discount Based on Product"
5 when
6 $product: Product(type == "desktop")
7 then
8 System.out.println("Discount provided for product");
9 $product.setDiscount(15);
10 end
11
12
13rule "Discount Based on Store A,B,C"
14 when
15 $product: Product($regions : regions)
16 $region: Map() from $regions
17 $entry: Map.Entry( $key: key, $val: value ) from $region.entrySet()
18 eval($val.equals("A") || $val.equals("B") || $val.equals("C"))
19 then
20 System.out.println("Discount provided for product in specific region");
21 $product.setDiscount(10);
22 end
23
24rule "Discount Based Manufacturer"
25 when
26 $product: Product($manufacturers : manufacturers)
27 $manufacturer: String() from $manufacturers
28 eval($manufacturer.equals("Company1") || $manufacturer.equals("Company2"))
29 then
30 System.out.println("Discount provided for manufacturer");
31 $product.setDiscount(5);
32 end
Setup
1# Project 63
2
3Spring Boot - Drools
4
5[https://gitorko.github.io/spring-boot-drools/](https://gitorko.github.io/spring-boot-drools/)
6
7### Version
8
9Check version
10
11```bash
12$java --version
13openjdk version "21.0.3" 2024-04-16 LTS
14```
15
16### Dev
17
18To run code.
19
20```bash
21./gradlew clean build
22./gradlew bootRun
23```
References
comments powered by Disqus