Separating the business logic from application code has been a customer’s dream and one of the most important architecture goals for many IT development projects. However countless “if – else” statements in your java project tell another story. With a solid MVP architecture and the integration of a business rule engine, developers can separate the business logic from the code and concentrate on the user interface layer.
To create a library that contains all business rules (the business logic) we will integrate a (freeware) business rule management system. We can choose between
- Drools (part of the KIE platform)
- Camunda (BPM with business rule engine)
- Activity (BPM with business rule engine)
I created a small sample project with Vaadin 8 for a web UI and Drools for business rules, that can be downloaded fromĀ https://bitbucket.org/mekasocore/vaadin-drools .
The structure of the project looks like that:
The business rules are encapsulated in the business-rule project. This project contains some helper classes and the drools business rule file that contains 2 simple rules:
package de.mekaso.logic.model rule "Customer.contactData" when customer : Customer((phone == null || phone.trim.empty) && (email == null || email.trim.empty)) validationResult : ValidationResult() then ValidationMessage message = ValidationMessage.createMessage(de.mekaso.logic.model.ValidationMessage.Severity.ERROR, "Please enter a phone number or a email address."); validationResult.addValidationMessage(message); end rule "Customer.validEmail" when customer : Customer(email != null && !email.trim.empty && email not matches "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$") validationResult : ValidationResult() then ValidationMessage message = ValidationMessage.createMessage(de.mekaso.logic.model.ValidationMessage.Severity.ERROR, "Please enter a valid email address."); validationResult.addValidationMessage(message); end
The rules can be manually typed or created with the KIE Drools Workbench, that is a web application to create and modify business rules with an visual editor.
The rules are simple and should just show, that we are able to keep the business logic out of our java code to concentrate on the web application. We want to validate the data of a customer and want him to submit valid contact data:
- enter a phone number or a email address so that we can contact him later (Business rule Customer.contactData)
- check the email address if its valid (contains @ and . characters and so on…) (Business rule Customer.validEmail)
The integration of the business rule engine and the execution of the 2 business rules looks like this:
@Inject private CustomerView view; @Inject private CustomerModel model; @Inject @KSession("customer-rules") private KieSession kSession; @Override public void validate(@Observes Customer customer) { ValidationResult validationResult = ValidationResult.createValidationResult(); FactHandle customerHandle = this.kSession.insert(customer); FactHandle resultHandle = this.kSession.insert(validationResult); this.kSession.fireAllRules(RuleFilter.applyRules(Arrays.asList("Customer.contactData", "Customer.validEmail"))); if (validationResult.getValidationState() == ValidationState.FAILURE) { this.view.showMessage(validationResult.getMessages()); } this.kSession.delete(customerHandle); this.kSession.delete(resultHandle); }
As you can we need to have a KieSession
(injected in lines 7-9) to fire the rules and receive a validation result from the business rule engine. Drools will populate the message list of the validation result with a message in case one of the rules is executed. If we find a message in the message list, we show it as a notification for the user.
As you can see Drools and Vaadin can be integrated in an elegant way (because of the well done integration features of drools via dependency injection and the MVP architecure) to separate the business logic from your java development project. Try it out!
If you are interested to integrate business logic with Camunda or Activity, just leave a comment and I will try help you with a sample project.