Chapter 4. Improving Our Rule Syntax

In the previous chapters, we've seen how to configure our environment in order to run our first Drools-based rules, written in the DRL language. We've played with the basic structure of rules and how to configure an environment for them to run, however, we've barely seen the surface of the Drools rule language (DRL) capacity. Like an iceberg, there's much more to it than what you see at first.

In this chapter, we'll discuss the DRL language, its syntax and possibilities, and examples from our eShop case in detail. We will cover the following topics:

  • Discussing the different ways in which the rules can read and modify the data, along with their understanding of the world
  • Configuring the rules using attributes, global variables, and many other features required to use the rules to its full potential
  • Discussing how the execution of Drools rules can be controlled by many different mechanisms

Adding external interactions with global variables

Interactions between our code and the business rules are mainly done by the rules that we define and the data that we feed in our running rule engine. In order to interact with data that is not in the Rule Engine context, Drools allows quite a variety of communication mechanisms to the other parts of our code and even to other systems. One of the most used tools for this are called global variables.

Global variables are defined in the DRL code in a similar way that we would define a variable in regular Java code. The syntax to follow is the global keyword, followed by the type of data, and then by the variable name:

global EShopConfigService configService;

Global variables can be a lot of things such as external services, lists of cached data, parameter values for our rule configurations, and anything that we might define in a Java code and we wish to have as a configurable component from our runtime.

In our DRL code examples (which we can find in the chapter-04-kjar project in the code bundle), we can find an example of how to define a service to configure our eShop example, called EShopConfigService. We will use it for demonstrative purposes only; however, if we replaced it with real interactions with external systems, such as a Data Access Object or a Web Service stub, the interconnectivity possibilities for business rules are endless. More about globals and the way they can be used in Drools can be found in the next chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.174.253