Vaadin Drools

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Ā .

The structure of the project looks like that:

The maven project structure

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"
    	customer : Customer((phone == null || phone.trim.empty)
    	&& (email == null || email.trim.empty))
    	validationResult : ValidationResult()
        ValidationMessage message = ValidationMessage.createMessage(de.mekaso.logic.model.ValidationMessage.Severity.ERROR, 
        "Please enter a phone number or a email address.");

rule "Customer.validEmail"
    	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()
        ValidationMessage message = ValidationMessage.createMessage(de.mekaso.logic.model.ValidationMessage.Severity.ERROR, 
        "Please enter a valid email address.");

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:

  1. enter a phone number or a email address so that we can contact him later (Business rule Customer.contactData)
  2. 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:

private CustomerView view;

private CustomerModel model;

private KieSession kSession;

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) {

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.

the user should enter a phone number or a valid email


the user entered an invalid email

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.

By Meik Kaufmann

I am a certified oracle enterprise architect and like to share my knowledge and experience that I gain in the projects I am working on.

Leave a Reply

Your email address will not be published. Required fields are marked *