Home/Tutorials/Drools Business Rules Engine/How to call a external function from a DRL file
Drools Business Rules Engine2 min read

How to call a external function from a DRL file

This tutorial will let us know how to call static function from a Java file within your DRL file.

Drools: How to call a external function from a .drl file

Let us demonstrate how to call a static function from a java file within your DRL file. Creating a class Utility.java in the same package com.sample

public class Utility {
	
	public static void writeHello(String name) {
		System.out.println("HELLO" + name + "!!!!!!");
	}

}

We then add the import statement in our drl file to call the writeHello method from our drl file. Again we have added the syntax in the same drl file used


Use case for this scenario:

Following is a step by step explantion for the usecase:

  • There are lot of times where you need to do calculation with respect to the parameters of your Object which you are using in your DRL's or for that matter you need to check whether the particular variable is a part of some static data or a static configuration and based on that decision you want to execute your rule, so at such places it's advisable to call a external function either in your DRL file or in your Utility class so that processing can be done in those functions and the rules can act on the output of your function.
  • Primarily Drools is meant for the business layer i.e to evaluate what to do next if a particular condition is met and hence the processing part in case if needed for specific rules can be done in function and hence the provision given.
  • Another example is that Java is a Object oriented language but it still lets you write static/helper functions to do some calculations and use static method without initiating any object. It is against the object oriented programming concept but still we can directly call a method without creating a object, so similarly Drools also provides that feature.

INTERACTIVE SIMULATION CHALLENGE+250 XP
Workbench Verification

Ready to test "How to call a external function from a DRL file" in production?

See how Drools Business Rules Engine algorithms and server loops perform under 100k req/s traffic spikes.

Simulator: SysDesign Ops Simulator