Search code examples
javadroolsstatic-methods

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.


Solution

  • 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.)