Spring boot Drools Integration
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.
@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;
}
-
Create
KieServices
usingKieServices.Factory.get()
-
From
KnowledgeBuilderFactory
createDecisionTableConfiguration
-
Define your input type configuration.setInputType(DecisionTableInputType.
XLSX
) -
Create
KieFileSystem
instance by passing spreadsheets location -
Create
KieBuilder
fromKieServices
instance -
Generate
KieContainer
from kieServices
Business Scenario
Student Grades Based on Marks and Attendance
Below picture depicts business rules for for student grades system.
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.
@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
Post a Comment