Benefits of using globals

In Chapter 5, Understanding KIE Sessions, global variables were introduced as a way of interacting with external services. Even if the chapter never explicitly stated this, its tests showed another important benefit of using global: different implementations of the same global may be provided, depending on the context. Right now, the particular context that we are interested in is the testing context.

When testing, mock versions of the global variables requiring interaction with external services, can be provided. These mock variables will immensely reduce the complexity and boiler-plate code required by our tests.

Let's take the following example from the code bundle of Chapter 5, Human Readable Rules:

global AuditService auditService;
rule "Send Suspicious Operation to Audit Service"
when
    $so: SuspiciousOperation()
then
    auditService.notifySuspiciousOperation($so);
end

The preceding rule uses a global to interact with an audit service and notify it about suspicious operations found in the session.

If, in the previous example, we had just instantiated a concrete AuditService instance on the right-hand side of the rule instead of using a global, it would have made the whole testing process more complicated.

Having this kind of Contexts and Dependency Injection (CDI) global variables makes our knowledge bases more test-friendly, we can always provide mock versions of them that facilitate the particular testing process that we are dealing with.

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

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