secbro
secbro

Reputation: 23

How to import Java static method into Drools file?

The java class and static method code is:

public class DroolsStringUtils {
    public static boolean isEmpty(String param) {
        if (param == null || "".equals(param)) {
            return true;
        }
        return false;
    }
}

The drl code is:

package com.rules

import com.secbro.drools.utils.DroolsStringUtils.isEmpty;

rule CheckIsEmpty
  when
    isEmpty("");
  then
    System.out.println("the param is not empty");
  end

But the IDEA hints:

"cannot relove" on the method 'isEmpty("")'.

I just want to import a static method from java class to drl file.

Upvotes: 2

Views: 6930

Answers (2)

cellepo
cellepo

Reputation: 4529

[this might have details only relevant for Drools 6.0 & earlier (thanks Roddy of the Frozen Peas)]

Looks like you could import the static method, as a function into Drools...

import function com.secbro.drools.utils.DroolsStringUtils.isEmpty

...and then be able to use your rule as you wrote it originally in your Question.

FWIW: To reference static fields - import the class with the static fields as normal import (not static or function), and then refer to static field with static reference:

import your.package.with.static.field.YourClass

...

YourClass.STATIC_FIELD

(That also works for static methods too, if you don't want to import them as function).

Upvotes: 2

laune
laune

Reputation: 31300

Use import static to import a static method.

import  static  com.secbro.drools.utils.DroolsStringUtils.isEmpty;
//      ^^^^^^

(edited:) and of course you cannot call a static method where a pattern is required:

rule CheckIsEmpty
when
    eval( isEmpty("") )
then
    System.out.println("the param is not empty");
end

(It helps considerably to read the Drools documentation.)

Upvotes: 4

Related Questions