如何将 Java 静态方法导入 Drools 文件?

How to import Java static method into Drools file?

javaclass和静态方法代码为:

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

drl代码是:

package com.rules

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

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

但是IDEA提示:

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

我只想将静态方法从 java class 导入到 drl 文件。

使用 import static 导入静态方法。

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

(已编辑:)当然你不能调用需要模式的静态方法:

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

(阅读 Drools 文档有很大帮助。)

Looks like 您可以将静态方法作为 function 导入 Drools...

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

...然后能够使用您最初在问题中编写的规则。

FWIW:要引用静态字段 - 使用静态字段导入 class 作为正常导入(不是 staticfunction),然后引用 static具有静态引用的字段:

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

...

YourClass.STATIC_FIELD

(这也适用于静态方法,如果您不想将它们导入为 function)。