Demo Apache Drools with Scala and Templates

Introduction

Apache Drools is a software used to manage business rules of any kind. As it is a very extensive framework, we limit this article to directly apply a simple use case with Scala, thus leaving aside the theoretical explanations that can be found in the official documentation.

Business Rules with Drools

When defining rules in Drools, you have to do it from a specific language called Drools Rule Language or also known as DRL. This code has to be in a file with a .drl extension. It is beyond this article to explain how this language works.

Basically, there are three ways to generate the rules:

  • Program rule by rule using the DRL language.
  • Use spreadsheets with a very specific format that Drools internally converts to the DRL language.
  • Use templates that allow, by defining a .drl file, to generate a multitude of rules. The rules that are passed to the templates can come mainly from two sources:
    • Database.
    • Spreadsheets.
Figure 1. Example of a definition of rules where it is stated that the patient pays the fee according to his or her age.
Figure 2. Example of how rules affect each other.

Apache Drools Demo

In this article an application has been developed, whose code is public and can be found here, in which a template is used assuming a reading of a database where the rules are defined.

The repository has been made with the aim of getting familiar with Apache Drools, so it does not contain all the necessary implementations for a productive environment. However, it does have developed and functional tests that allow you to get familiar with the application. To run the tests, the SBT tool can be used by executing the sbt test command.

In the repository, the following steps are applied in the order mentioned for daily tennis court pricing:

  1. The rules, which contain the following information, are read:
    • Name: name of the rule.
    • DemandLowerBound: minimum demand.
    • DemandLowerBound: maximum demand.
    • DateLowerBound: minimum date.
    • DateUpperBound: maximum date.
    • PriceChange: price variation to be applied in case the rule applies.
  2. The rules are sent to the template located in src/main/resources/template.drl, to be converted by Apache Drools in the DRL language.
  3. Facts are read, corresponding to:
    • The date.
    • The demand level for that date.
  4. Facts are provided to the Drools session, which will work out which rules apply given the facts provided.
  5. The relevant price increase or decrease will be applied given the resolution of rules.
  6. Prices are updated.

In the demo, the most basic concepts of Apache Drools have been used in order to focus on the clarity of the message delivered. For this reason, the use of concepts such as salience and activation groups among many others, which drastically increase the ability to solve problems related to business rules, have been left out of the demo.

Conclusion

Apache Drools allows to centralize business logic, which is usually distributed throughout the company, making it possible to codify it through rules to be managed by business experts.

In this post we have made an introduction to the key aspects of Apache Drools. However, as it is a very extensive software, it offers a great number of possibilities and functionalities. 

If you found this article interesting, we encourage you to visit the Software category to see other posts similar to this one and to share it on social networks. See you soon!
Default image
Daniel Bestard
Articles: 14