Introduction

Getting started

Walkthrough Guides

CafeX Apps

Workflows

Using CafeX Collaborate App

Reporting

Managing CafeX

Integrating CafeX

Security

App Studio

CafeX Apps

Creating a Ruleset

Modified on Tue, 5 Sep, 2023 at 7:01 AM

The Business Rules Engine (BRE) provides a tool to evaluate user defined rules against application data including an easy to use rule builder directly within the CafeX Platform. The rules can be used in the traditional sense, to drive decision making and workflows, or to define next best action and visibilities in user interfaces. The BRE allows non-technical users to design if/then/else style rules that can be used to abstract complex business decision making into maintainable units, called Rulesets.


TABLE OF CONTENTS


Understanding Rulesets

A Ruleset is composed of one or more Input Facts, one or more Result Facts and one or more Rules.

Input Facts

Input Facts are the data passed into the BRE from the App. The Input Facts need to provide enough information for the Rules to be able to produce the Result Facts. Each Input Fact is declared within the Ruleset as a name and data type, where the data type can be one of Text, Number or Boolean (True/False).


The BRE takes a copy of the Input Facts when it starts processing the Ruleset. If the Rules modify the Input Facts then that modification will only exist within the current execution of the Ruleset and won't affect any other part of the App.

Result Facts

Result Facts are the data emitted from the BRE as the result of the Ruleset execution and are available to be read by the part of the App that executed the Ruleset. Each Result Fact is declared within the Ruleset as a name, data type and a default value. The data type can be one of Text, Number or Boolean (True/False). The default value can be left blank , however if a default value is set then it must match the defined data type. The Result Fact will be set to this value at the start of the Ruleset execution before any Rules have executed.

Rules

Rules represent the business logic within the Ruleset. A rule is declared as a name and description, which are used for identification but have no impact on execution, plus one or more Conditions and one or more Actions that drive the execution.


A Condition determines if a rule will be executed. If a Condition for a Rule doesn't match the facts, then the Action for that Rule will not be executed. A Condition is a read-only construct and cannot change the state of any of the facts.


An Action determines how the facts should be manipulated to reflect the Rule being executed. The setting / modification of facts by an Action must match the data type of the fact being set. For example, if a fact has a 'number' data type then an Action cannot assign text to that fact.


Creating a Ruleset

The Ruleset editor can be accessed through the 'Rulesets' menu item within the App Studio. Selecting the menu item will list all of the Rulesets for the current App and allow you to create a new one using the Add new Ruleset button.


When creating a Ruleset you can give it a name and a description to help explain the function of the business rules. These settings don't affect the operation of the Ruleset but the name is used to reference the Ruleset later on, so it needs to be easily identifiable if you are going to have multiple Rulesets within your App.

Basic Example

Let's run through a basic example of a Ruleset that indicates if a motorist was breaking the speed limit.


We can see in the screenshot that we have defined an Input Fact of type Number for the speed limit and a second Input Fact of type Number for the motorist's speed. We've also defined a Result Fact of type True/False to indicate whether the motorist's speed exceeds the speed limit. The Result Fact defaults to False in this example.


We have created a simple Rule for our Rule Set that can encapsulate the required logic in one step. The Condition for this rule is simply if the Motorist Speed is greater than the Speed Limit and the Action is simply to set the Is Speeding value to true if the Rule evaluates.


You will notice from the example that the way to reference Facts for both reading existing values and assigning new values is by wrapping it in braces. To reference the Motorist Speed Fact, you need to enter {Motorist Speed} in the field.

Evaluating multiple Rules

A Ruleset can consist of many Rules. When there are multiple Rules in a Ruleset then they are executed in order from the top to the bottom as they appear in the Ruleset editor.


By default all rules will be executed, however this behaviour can be modified in the Options section in the Ruleset editor. The alternative options are either to stop evaluating the Rules as soon as one Rule matches, or to stop evaluating the Rules as soon as one Rule doesn't match. In both of these cases the Rules are still evaluated in the same order, up to the point at which evaluation is terminated.

For our example we will use the default value of processing all Rules. 


In the jurisdiction using this example Ruleset there is a law that means that the speed limit for Trucks is 10mph lower than the posted speed limit. In order to handle this condition we have added an Input Fact with name 'Is Truck' and data type True/False. We use this Input Fact as a Rule Condition to trigger an Action that reduces the speed limit Input Fact for this Ruleset execution.


We now have the Rules that we need to evaluate the additional consideration in the Ruleset, however the new Rule as been added after the previous one. As Rules are evaluated in order, this means that the latest rule will execute too late to impact the 'Speed exceeds limit' Rule.


Clicking on the up and down arrows icon at the top of the Rules list switches the list into reorder mode and allows the Rules to be dragged and dropped into the desired order. Clicking the tick icon returns the list of Rules from reorder mode to edit mode.

Complex Conditions

The Conditions that we have used for our examples have been simple so far, with a single part to them. Rules often have complex Conditions associated with them however that consist of multiple parts. A Rule can encapsulate this complex Condition with both AND Conditions, where multiple Conditions all have to match in order for the Rule to evaluate, or OR Conditions, where any Condition matching will result in the Rule evaluating.


We want to extend our example with a Result Fact 'Review Incident' that determines whether the incident for which the Rule is being executed needs to be reviewed or not. This Fact should return True if the Motorist Speed exceeds the Speed Limit more than 10mph. In this example we're going to create a Rule with two Conditions where both need to be true. We do this with the Add an additional condition button.


To add more complexity however, if the vehicle is an emergency vehicle with their emergency lights on, or is an undercover police vehicle, then the Ruleset should never flag the incident for review.


To achieve this we will add a Rule that will evaluate if both Is 'Emergency Vehicle' AND 'Emergency Lights Active' are true, OR if 'Undercover Police Vehicle' is true. We can achieve the OR condition by clicking on the Add an alternative group button underneath 


Testing a Ruleset

Once a Ruleset has been saved within the Ruleset editor, a Test Ruleset button becomes active at the top of the Ruleset editor.


Clicking Test Ruleset enters the Ruleset test mode. In test mode there are fields to enter any of the Input Facts. If you leave an Input Fact blank then its default value will apply, if provided. Clicking the Run button will execute the Ruleset, populate the Result Facts and outline which Rules evaluated and what affects they had on the Facts within the Ruleset.


Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article