Drools - Rule Syntax



As you saw the .drl (rule file) has its own syntax, let us cover some part of the Rule syntax in this chapter.

Conditions in Rules

A rule can contain many conditions and patterns such as −

  • Account (balance == 200)
  • Customer (name == “Vivek”)

The above conditions check if the Account balance is 200 or the Customer name is “Vivek”.

Variables in Rules

A variable name in Drools starts with a Dollar($) symbol.

  • $account − Account( )
  • $account is the variable for Account() class

Drools can work with all the native Java types and even Enum.

Comments in Rules

The special characters, # or //, can be used to mark single-line comments.

For multi-line comments, use the following format:

/*
   Another line
   .........
   .........
*/

Global Variables

Global variables are variables assigned to a session. They can be used for various reasons as follows −

  • For input parameters (for example, constant values that can be customized from session to session).

  • For output parameters (for example, reporting—a rule could write some message to a global report variable).

  • Entry points for services such as logging, which can be used within rules.

Functions in Rules

Functions are a convenience feature. They can be used in conditions and consequences. Functions represent an alternative to the utility/helper classes. For example,

function double calculateSquare (double value) {
   return value * value;
}

Dialect

A dialect specifies the syntax used in any code expression that is in a condition or in a consequence. It includes return values, evals, inline evals, predicates, salience expressions, consequences, and so on. The default value is Java. Drools currently supports one more dialect called MVEL. The default dialect can be specified at the package level as follows −

package org.mycompany.somePackage
dialect "mvel"

MVEL Dialect

MVEL is an expression language for Java-based applications. It supports field and method/getter access. It is based on Java syntax.

Salience

Salience is a very important feature of Rule Syntax. Salience is used by the conflict resolution strategy to decide which rule to fire first. By default, it is the main criterion.

We can use salience to define the order of firing rules. Salience has one attribute, which takes any expression that returns a number of type int (positive as well as negative numbers are valid). The higher the value, the more likely a rule will be picked up by the conflict resolution strategy to fire.

salience ($account.balance * 5)

The default salience value is 0. We should keep this in mind when assigning salience values to some rules only.

There are a lot of other features/parameters in the Rule Syntax, but we have covered only the important ones here.

Rule Consequence Keywords

Rule Consequence Keywords are the keywords used in the “then” part of the rule.

  • Modify − The attributes of the fact can be modified in the then part of the Rule.

  • Insert − Based on some condition, if true, one can insert a new fact into the current session of the Rule Engine.

  • Retract − If a particular condition is true in a Rule and you don’t want to act anything else on that fact, you can retract the particular fact from the Rule Engine.

Note − It is considered a very bad practice to have a conditional logic (if statements) within a rule consequence. Most of the times, a new rule should be created.

Advertisements