English 中文(简体)
Drools - Rules Syntax
  • 时间:2024-09-17

Drools - Rule Syntax


Previous Page Next Page  

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-pne comments.

For multi-pne comments, use the following format:

/*
   Another pne
   .........
   .........
*/

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 utipty/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, inpne evals, predicates, sapence 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 apppcations. It supports field and method/getter access. It is based on Java syntax.

Sapence

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

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

sapence ($account.balance * 5)

The default sapence value is 0. We should keep this in mind when assigning sapence 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