Spring boot Drools Integration

Introduction

Introduction

Decision tables are a "precise yet compact" way of representing conditional logic, and are well suited to business level rules.

Refer Official documentation on drools decision tables and other supported formats.

Tip
Drools supports managing rules in a spreadsheet format. Supported formats are Excel (XLS), and CSV.

Core Components in Drools Java API

The KnowledgeBuilder is responsible for taking source files, such as a DRL file or an Excel file, and turning them into a Knowledge Package of rule and process definitions which a Knowledge Base can consume.

An object of the class ResourceType indicates the type of resource it is being asked to build.

The ResourceFactory provides capabilities to load resources from a number of sources, such as Reader, ClassPath, URL, File, or ByteArray.

Caution
Binaries, such as decision tables (Excel .xls files), should not use a Reader based resource handler, which is only suitable for text based resources.

The KieBuilder is created using the KnowledgeBuilderFactory.

Spring Integration

This tutorial demonstrates integration of drools with spring boot which helps to run as services.

KieContainer.java
  @Bean
    public KieContainer kieContainer() throws Exception {

        KieServices kieServices = KieServices.Factory.get(); (1)
        DecisionTableConfiguration configuration = KnowledgeBuilderFactory.newDecisionTableConfiguration(); (2)
        configuration.setInputType(DecisionTableInputType.XLSX); (3)
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        KieFileSystem fileSystem = kieServices.newKieFileSystem(); (4)
        Arrays.stream(resolver.getResources("classpath*:com/tvajjala/drools/*.xlsx")).forEach(
                resource -> {
                    try {
                        LOGGER.info("Reading file {}", resource.getFile());
                        fileSystem.write(ResourceFactory.newFileResource(resource.getFile()));
                    } catch (IOException e) {
                        LOGGER.error("Error loading file", e);
                    }
                }
        );
        KieBuilder kieBuilder = kieServices.newKieBuilder(fileSystem).buildAll(); (5)
        KieContainer kieContainer = kieServices.newKieContainer(kieBuilder.getKieModule().getReleaseId());(6)
        LOGGER.info("Created  kieContainer {} ", kieContainer);
        return kieContainer;

    }
  1. Create KieServices using KieServices.Factory.get()

  2. From KnowledgeBuilderFactory create DecisionTableConfiguration

  3. Define your input type configuration.setInputType(DecisionTableInputType.XLSX)

  4. Create KieFileSystem instance by passing spreadsheets location

  5. Create KieBuilder from KieServices instance

  6. Generate KieContainer from kieServices

Business Scenario

Student Grades Based on Marks and Attendance

Below picture depicts business rules for for student grades system.

Screenshot%2B2020 01 25%2B11.14.23

Consider scenario where student grade decision is made based on Marks and attendance percentage

below test case demonstrated the student grade based on his marks and attendance.

StudentGradeDecision.java
 @Test
    public void studentGradeTest() {
        //given: a student marks and attendance
        final Student student = new Student();
        student.setMarks(900); (1)
        student.setAttendance(70.5); (2)
        
        //when: evaluate grade
        final KieSession kieSession = kieContainer.newKieSession();
        kieSession.insert(student);
        kieSession.getAgenda().getAgendaGroup("GRADE").setFocus();
        kieSession.fireAllRules();
        
        //then: validate 
        Assert.assertEquals(Student.Grade.C, student.getGrade()); (3)
    }

Refer complete source code at https://github.com/tvajjala/decision-tables.git

Comments

Popular posts from this blog

IBM Datapower GatewayScript

Spring boot Kafka Integration

Spring boot SOAP Web Service Performance