简体   繁体   English

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

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

I've been trying to execute a set of rules on a request object on the basis of some set of configuration value.我一直在尝试根据一组配置值对请求对象执行一组规则。 Below is the example on what I am trying to do:以下是我正在尝试做的示例:

Configuration: config.json配置: 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],
    }
  }
}

Request Class Object: policyDataToBeVerified请求类对象: policyDataToBeVerified

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

I converted the config.json into a JsonNode object:configJson and passed both policyDataToBeVerified and configJson to drools working memory.我将 config.json 转换为 JsonNode 对象:configJson 并将 policyDataToBeVerified 和 configJson 都传递给 drools 工作内存。 Below are the approaches I tried to take to frame the rule but failed anyways:以下是我尝试采用的方法来制定规则,但无论如何都失败了:

APPROACH 1: drools.drl方法 1: drools.drl

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

Issue: I am getting null pointer exception at $configJson.get(company).get(site).get("workerWeeklyWorkingHours")问题:我在 $configJson.get(company).get(site).get("workerWeeklyWorkingHours") 处收到空指针异常

APPROACH 2: I even tried to use configJSON as a global variable but, then drools didn't allow me to use get methods of JsonNode to get the nested fields from JSON方法 2:我什至尝试使用 configJSON 作为全局变量,但是,drools 不允许我使用 JsonNode 的 get 方法从 JSON 中获取嵌套字段


I've been trying to find a solution to my problem for past few days, but nothing worked.过去几天我一直在尝试解决我的问题,但没有任何效果。 I'd be happy to learn even if we can view this problem from a different perspective and solve it in a different way, or if we can debug this, whatever works.我很乐意学习,即使我们可以从不同的角度看待这个问题并以不同的方式解决它,或者如果我们可以调试这个,无论什么都行。

Thank you谢谢

NOTE: IntellijIDEA has inaccurate linting for .drl files, so request you to not rely on it.注意: IntellijIDEA 对 .drl 文件的 linting 不准确,因此请您不要依赖它。

I created a validateSlotsResponse as following for better handling of response:为了更好地处理响应,我创建了一个 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;
}

I'll pass this class's object into the working memory of DROOLS, and will update it inside the drools program, which will then be used as the respose for validation.我会将这个类的对象传递到 DROOLS 的工作内存中,并在 drools 程序中对其进行更新,然后将其用作验证的响应。

Below are the rules inside my drools file, please make sure you have made the right imports:以下是我的 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM