How to get dynamic value from drool file

Drools is a business logic integration platform written in Java. Drool is used to define rules in the applications to integrate business logic in a standard way. It is used to separate and reason over logic and data found within business processes.

Prerequisites: Knowledge in java and drool

Audience Level: Beginner, Intermediate

Some of the common terms used in drools:

Rule: Rule is a statement expressed as “When some condition occurs then do some tasks”.

Facts: Facts are the data on which the rules will act upon. From Java perspective, Facts are the POJO.

Session: A Knowledge Session in Drools is the core component to fire the rules. It is the knowledge session that holds all the rules and other resources. A Knowledge Session is created from the KnowledgeBase.

Variables: Variables are used to represent the java objects/facts. It is prefixed with $ symbol. Global variables are variables assigned to a session.

For the rules engine to work, Facts are inserted into the session and when a condition is met, the subsequent rule gets fired.

Concept: This blog is to intend to explain how to get the dynamic value/data from the drool file.

Business Scenario:

The sample project below is developed for the business scenario “If a contact person’s citizenship is USA, then retrieve the contact Id (dynamic data) from the drool file”. Rule is “when citizenship=USA, then return the contact Id of the person”.

Steps to Implement:

Created an example drool file ‘citizenshiprule.drl’ for firing the above rule.

Covid 19
COVID-19: Digital Insights For Enterprise Action

Access Perficient’s latest insights into how you can leverage digital technologies to not only respond to the pandemic, but drive your operations forward and deliver experiences your customers need.

Get Informed

Have a ContactPerson model class to hold the contact Id, name, citizenship status. The file contains code to fire the rule and get the output.

The Project Structure is as follows. Include the required drool jar files under lib folder or install drool plugin in the IDE.



In the Citizenshiprule.drl file, Import the required java file, in this example ‘’. Declare a variable for the modal class as ct:ContactPerson.  Now we can use the get and set methods of the java bean to access the data as given in below code.

citizenshipdrl file to initialize the drool and fire the rule to get the output.


The remaining methods in file.


Run the above the application and the output console is as follows.



Thus we got the contact Id of the person when the citizenship is USA as per our business scenario rule. We got the dynamic data returned from the drool file.






About the Author

More from this Author

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to the Weekly Blog Digest:

Sign Up