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 - what is a pattern in drools - drools example programs

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 rules definition - drools example programs

learn drools tutorial - drools project - drools rules definition - drools example programs

learn drools tutorial - drools rules building - drools example programs

learn drools tutorial - drools project - drools rules building - drools example programs

learn drools tutorial - drools rules executing - 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:
/*
   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 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 pattern example - drools example programs

learn drools tutorial - drools project - drools pattern example - drools example programs

learn drools tutorial - drools pattern example 2 - drools example programs

learn drools tutorial - drools project - drools pattern example 2 - drools example programs

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 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:
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.
learn drools tutorial - drools sytax rules - drools example programs

learn drools tutorial - drools project - drools sytax rules - drools example programs

learn drools tutorial - drools condition logics system - drools example programs

learn drools tutorial - drools project - drools condition logics system - drools example programs

learn drools tutorial - 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.


Wikitechy drools tutorial provide you the details of all the following contents such as define drool , drools meaning , drools rule engine , java rule engine , drools example , jboss drools , java open source , rule engine java , open source bpm , jboss brms , open source rules engine , drools rules , drools java , drools engine , drools guvnor , drooled definition , drools documentation , java drools , drools fusion , drools eclipse plugin , drools decision table , drools net , drools rules examples , drools alternatives , drools rule engine tutorial , drools workbench tutorial , drools definition , drools jboss , drools spring , drools expert , apache drools , drools interview questions , drools wiki , rule engine drools , drools kie , drools ui , kie drools , jboss drools tutorial , drools syntax , drools java example , jbpm drools , drools jbpm , brms drools , drools rule language

Related Searches to Drools Rule Syntax