Rule Syntax

The Drools Rule Resource(DRL) has a different syntax and covering the syntax in the below section :

Conditions in Rules :

A rule can contain many conditions/patterns:

  • Student(id == 001)
  • Employee(name == "Vivek")

The above conditions checks if Student id is 1 and the Employee name is Vivek.


Variable in Rules :

A variable name in Drools starts with a $ dollar symbol.

$student : Student()

$student is the variable for Student() class.

This is similar to Student student = new Student() in java. Drools can work with all the native Java types and even Enum.


Comments in Rules:

In Drools 5.x, # or // can be used as a single line comments

For Multi Line comments:

/* 
	Another line 
*/

Functions in Rules:

Functions are a convenience feature. They can be used in conditions and consequences. If there is any modification to be done in the then part of the rule, a normal static java helper function can be called.

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

Dialect:

Dialect specifies the syntax used in any code expression that is in a condition or a consequence. 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. mvel supports field and method/getter access. It is based on Java syntax.


The no-loop attribute

This attribute informs the rule engine that a rule should be activated only once per matched facts. If there is a generic condition in the rule, then this attribute should be used to avoid the infinite loop scenario.


Salience:

Salience is a very important feature of Rule Syntax.

It is used by the conflict resolution strategy to decide which rule to fire first.If there are two rules and both have the conditions are met, then salience is used to determine the order of rule firing. It 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)
salience 100

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 the important ones.