Drools Rule Syntax - rules engine - drools tutorial - business rules engine
What is Rule Syntax in Drools?
As you know the .drl (rule file) has its own syntax, let us see some part of the Rule syntax.
Conditions in Rules
- A rule can contain many conditions and patterns such as:
- Account (balance == 200)
- Customer (name == “Vivek”)
learn drools tutorial - drools project - what is a pattern in drools - drools example programs
- The above conditions check if the Account balance is 200 or the Customer name is “Vivek”.
learn drools tutorial - drools project - drools rules definition - drools example programs
learn drools tutorial - drools project - drools rules building - drools example programs
learn drools tutorial - drools project - drools rules executing - drools example programs
Variables in Rules
- A variable name in Drools starts with a Dollar($) symbol.
- $account : Account( )
- $account is the variable for Account() class
- Drools can access 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:
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 suitability feature. They can be used in conditions and consequences. Functions represent an alternative to the utility/helper classes. For example,
learn drools tutorial - drools project - drools pattern example - drools example programs
learn drools tutorial - drools project - drools pattern example 2 - drools example programs
Dialect
- A dialect specifies the syntax used in any code expression that is in a condition or in a value. It includes return values, evals, inline evals, predicates, salience expressions, significances, 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:
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.
- 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.
learn drools tutorial - drools project - drools sytax rules - drools example programs
learn drools tutorial - drools project - drools condition logics system - drools example programs
learn drools tutorial - drools project - drools condition logics system - drools example programs
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.