首页 > 解决方案 > 异常“无法解析 ObjectType 'getAmount'”

问题描述

在 Drools 中,我创建了一个包含以下内容的 drl 文件:

import com.myorg.model.UserAccount;
import function com.myorg.utils.UserAccountHelper.getAmount;

rule "Classification userC"
when
    $user : UserAccount(_age < 50);
    $amount: getAmount($user, "single");
then
    $user.set_userClassification("userC");    
end

在 Java 中我有一个静态方法 UserAccountHelper.getAmount

public static double getAmount(UserAccount account, String status)
{
   double amount = 0d;
   switch(status)
   {
      case "single":
         if (account.canBeFullyRefunded)
            amount = 1000;
         else
            amount = 100;
      default:
         amount = 0d;
   }
   return amount;
}

验证 drl 文件时出现异常“无法解析 ObjectType 'getAmount'”。有人可以帮忙吗?我正在使用 Drools 7.37。

标签: drools

解决方案


这不是在 DRL 中调用类的静态方法的方式。我建议您查看文档以更好地理解语法。

如果要在 Pattern 中调用静态(或实例)方法,可以这样做:

rule "Classification userC"
when
    $user : UserAccount(
        _age < 50, 
        $amount: getAmount(this, "single")
    )
then
    $user.set_userClassification("userC");    
end

推荐阅读