We get asked a lot about rule engine inference, such as “When rules execution happens in order or based on priority, then how does inference fit into this picture?” In this post, we are going to show you how rule engine inference works and how you can ask an engine to use inference capability over priority or order of the rules.
Let’s pick the sample rules below that diagnose based on some symptoms: nasal congestion, viremia, running nose, temperature, headache, fever, achiness and cough.
Order and Priority
In this example, the goal is to identify the diagnosis. When we run the rules sequentially from rule 1 to 5 (ruleset 1):
We will not get the result. See the ‘diagnosis’ value below is ‘null’:
The reason is in ‘ruleset 1’, which forces the execution to start with rule 1 which requires values for nasal congestion and viremia. Those values depend on the execution of rule 2 and rule 5. You can always change the order by setting the priority or just swapping the order of execution.
Using the rule engine inference capability allows the engine to figure out the dependencies between different rules at runtime. Rather than you specifying the order of rules in the ‘ruleset 1’, you let the engine decide what order is to be chosen based on the dependencies of available values in the condition part of the rules and changing values in the consequence part of the rules.
You can simply use the run on command on a rule set in order to use the rule engine inference capability:
Now even though in ‘ruleset 2’ we’ve written the rules in a set in order from rule 1 to 5, on the execution it will be run in the correct order and you can see the new results below:
Inference is not only letting the engine decide the order of rules execution, but also allows you to manage the dependencies at a high level when required.
Sequential execution of rules based on order and/or priority may not always be the answer to your challenge, especially when the complexity and dependencies of rules within one set grows. Using the inference capability of the rule engine allows dependencies of rules to be resolved at runtime and the engine to determine the correct order of rules to derive a goal (i.e., diagnosis in this example). This enables you to write your rules in any order and let the rule engine inference figure out the order for you.
In the future posts we will discuss in more detail how to define a goal and how to target specific goals using the inference capability.