Rule structure is composed of the conditions and consequence, as follows:
rule "name" when (Conditions) - also called Left Hand Side of the Rule (LHS) then (Actions/Consequence) - also called Right Hand Side of the Rule (RHS) end
The LHS of the rule is composed by conditional elements, which serve as the filters to define the conditions that need to be met for the rule to evaluate true. This conditional element filter facts, which in our case are the object instances.
If you take a look at our first rule LHS, the condition expressed is quite simple, as shown in the following:
rule "Classify Item - Low Range" when $i: Item(cost < 200) then $i.setCategory(Category.LOW_RANGE); end
The line in the Left Hand Side of the rule can be separated in the following three sections:
Item(...)filter for the item object type. This filter will pick up all the item objects that we insert into our session and filter them for processing.
cost < 200filter will take a look in the item objects and make sure that the field cost contains a value under 200.
$irepresents a variable binding, which is used to later reference the matched object. Notice that we are using the
$symbol to name the variables so that we can easily identify them in contrast with the object fields. Consider this as good practice.
To summarize, we are filtering based on Objects and their properties. It is important to understand that we will be filtering the Object instances that matches with these conditions. For each item instance that evaluates to true to all the conditions, the rule engine will create a match and execute the consequences.