Popular Posts

Spring boot Drools Integration



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.

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.

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.

    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)
                resource -> {
                    try {
                        LOGGER.info("Reading file {}", 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.

    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();
        //then: validate 
        Assert.assertEquals(Student.Grade.C, student.getGrade()); (3)

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

No comments:

Post a Comment