Rule writing or Syntax basics

If you see the default rule which is written in the FirstDroolsProgram project(Sample.drl), there are a lot of keywords used. Let us explain them one by one now :

Different Terms used in Drools


  1. Package : Every Rule starts with a package name. The package acts as a namespace for Rules. Package name has to be defined while creating the Rule resource. Rule names within a package must be unique. Packages in Rules are similar to packages in Java. When you create a new Rule by New -> Rule Resource -> then in the below window you need to mention the Rule name as well as package name.
  2. Different Terms used in Drools


  3. Import statement : Import statements are also similar to Java. All the FACTS classes and any helper classes needs to be imported for the rules to work on. For ex/- com.sample.DroolsTest.Message; in the above example.

  4. Rule Definition : The Rule definition consists of some drools specific keywords. Post you have added the plugins you can see the drools specific keywords in red color. In the above example, rule-when-then-end are the different keywords.
  5. rule keyword must be followed by a logical rule name.

    when keyword must be followed by some condition.

    then keyword must have the consequence part if the when condition is met.

    There are lot of other keywords which will be covered in further sections.
  6. Global variables : With global you define global variables. They are used to make application objects available to the rules. Typically, they are used to provide data or services that the rules use, especially application services used in rule consequences, and to return data from the rules, like logs or values added in rule consequences, or for the rules to interact with the application.

Now let us walk you through the terms used in the Java file used to load the drools and execute the rules.