如何将嵌套的 JsonNode 字段值与 DROOLS 中的 Java 对象参数值进行比较?

How to compare nested JsonNode field values with Java object parameter values in DROOLS?

我一直在尝试根据一组配置值对请求对象执行一组规则。以下是我正在尝试做的示例:

配置: config.json

{
  "company1": {
    "site1": {
      "maxWeeklyWorkingHours": 40,
      "rule2": [1, 3, 4],
    },
    "site2": {
      "maxWeeklyWorkingHours": 40,
      "rule2": [1, 3, 4],
    }
  },
  "company2": {
    "site1": {
      "maxWeeklyWorkingHours": 40,
      "rule2": [1, 3, 4],
    },
    "site2": {
      "maxWeeklyWorkingHours": 40,
      "rule2": [1, 3, 4],
    }
  }
}

请求Class对象: policyDataToBeVerified

PolicyDataToBeVerified(company=company1, site=site1, workerWeeklyWorkingHours=39, isRequestValid=0)

我将 config.json 转换为 JsonNode object:configJson 并将 policyDataToBeVerified 和 configJson 传递给 drools 工作内存。以下是我尝试制定规则但最终失败的方法:

方法 1: drools.drl

rule "maxWorkingHours"
when
    $configJson: JsonNode()
    $policyData: PolicyDataToBeVerified(maximumWeeklyWorkingHours <= $configJson.get(company).get(site).get("workerWeeklyWorkingHours"))
then
    $policyData.setIsRequestValid(true)
 end

问题:我在 $configJson.get(company).get(site).get("workerWeeklyWorkingHours")

收到空指针异常

方法 2: 我什至尝试将 configJSON 用作全局变量,但是 drools 不允许我使用 JsonNode 的 get 方法从 JSON

获取嵌套字段

过去几天我一直在努力寻找解决问题的方法,但没有任何效果。即使我们可以从不同的角度看待这个问题并以不同的方式解决它,或者如果我们可以调试它,我也很乐意学习。

谢谢

注意: IntellijIDEA 对 .drl 文件的检查不准确,因此请您不要依赖它。

为了更好地处理响应,我创建了一个 validateSlotsResponse 如下:

package com.example.demo;
import lombok.Builder;
import lombok.Data;
import java.util.List;

@Data
@Builder
public class ValidateSlotsResponse {
    boolean isRequestValid;
    List<String> comments;
}

我会将这个class的对象传递到DROOLS的工作内存中,并在drools程序中更新它,然后将其用作验证的响应。

以下是我的 drools 文件中的规则,请确保您已正确导入:

rule "fetchMaximumAllowedWeeklyWorkingHours"
when
     $validateSlotsResponse: ValidateSlotsResponse()
     $policyDataToBeVerified: PolicyDataToBeVerified()
     $configJson: JsonNode()
     $workingHoursAllowed:Integer() from $configJson.get($policyDataToBeVerified.getCompany()).get($policyDataToBeVerified.getSite()).get("maxWeeklyWorkingHours").intValue()
then
 end


 rule "maxWorkingHoursAccept" extends "fetchMaximumAllowedWeeklyWorkingHours"
 when
     eval($policyDataToBeVerified.workerWeeklyWorkingHours() <= $workingHoursAllowed)
 then
     $policyDataToBeVerified.setIsRequestValid(true);
     $validateSlotsResponse.setRequestValid(true);
     $validateSlotsResponse.setComments(new ArrayList<>(Arrays.asList("Worker allowed to work for "+$policyDataToBeVerified.getMaximumWeeklyWorkingHours() + " hours")));
  end


  rule "maxWorkingHoursReject" extends "fetchMaximumAllowedWeeklyWorkingHours"
  when
       eval($policyDataToBeVerified.workerWeeklyWorkingHours() > Integer.parseInt($workingHoursAllowed.toString()))
  then
       $policyDataToBeVerified.setIsRequestValid(false);
       $validateSlotsResponse.setRequestValid(false);
       $validateSlotsResponse.setComments(new ArrayList<>(Arrays.asList("Worker not allowed to work more than "+ $workingHoursAllowed + " hours")));
  end