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 usedUse case for this scenario :
- 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 yourUtility
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.
No comments:
Post a Comment