Rule syntax

Rule structure is composed of the conditions and consequence, as follows:

rule "name"
    (Conditions) - also called Left Hand Side of the Rule (LHS)
    (Actions/Consequence) - also called Right Hand Side of the Rule (RHS)

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"
        $i: Item(cost < 200)

The line in the Left Hand Side of the rule can be separated in the following three sections:

  • The 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.
  • The cost < 200 filter will take a look in the item objects and make sure that the field cost contains a value under 200.
  • The $i represents 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.

Next article >>