如何在流口水的时候编写嵌套条件

时间:2018-09-10 16:06:03

标签: drools

这是我的JSON文档

    {
   "location": {
      "details": [
         {
            "country": "India",
            "state": "haryana"
         },
         {
            "country": "America",
            "state": "LA"
         },
         {
            "country": "India",
            "state": "Maharashtra"
         }
      ]
   },
   "organisation": {
      "details": [
         {
            "name": "AON",
            "country": "india"
         },
         {
            "name": "AON",
            "country": "America"
         }
      ]
   }
}

我必须以以下格式应用规则

 If(

        (location.details.country=='India' OR 
        location.details.state=='haryana') 
AND 

        organisation.details.name=='AON'
    )

到目前为止,我已经进行了研究,并且知道同一类的字段上的规则可以像下面这样的嵌套格式应用。在此链接中:How to write nested conditions in when in drools

// Use this instead
Person( ( age > 50 && weight > 80 ) || height > 2 )

但是我想在以下相同的嵌套条件下应用不同pojo类的规则

    If(

                (location.details.country=='India' OR 
                location.details.state=='haryana') 
        AND 

                organisation.details.name=='AON'
      AND 
               (location.details.country=='India' AND
                organisation.details.country=='India')
            )
//any level of nested between different pojo classes can be present

这可以流口水吗?

我写了以下规则

rule "rule1"
salience 1
when


        $rootDoc:RootDoc($locationList:location && $organisationList:organisation) 

     and
        (
            $orgList:Organisation($orgdetailsList:details) from $organisationList
            NamesList1:Details(name=='AON') from $orgdetailsList
            or
            $locList:Location($locdetailsList:details) from $locationList
            NamesList2:Details_(state=='haryana') from $locdetailsList

        )

then
    System.out.println("Pojo Welocome-------");



end

但是它向我显示了此错误:第18行:3在规则“ rule1”中的输入“ NamesList1”不匹配

基本上,当我在同一个括号中写入两个条件时,它显示出错误。

下面是pojo类

-----------------------------------pojo_Classes2.Detail.java-----------------------------------

package pojo_Classes2;

import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"name",
"country"
})
public class Detail {

@JsonProperty("name")
private String name;
@JsonProperty("country")
private String country;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("name")
public String getName() {
return name;
}

@JsonProperty("name")
public void setName(String name) {
this.name = name;
}

@JsonProperty("country")
public String getCountry() {
return country;
}

@JsonProperty("country")
public void setCountry(String country) {
this.country = country;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
-----------------------------------pojo_Classes2.Detail_.java-----------------------------------

package pojo_Classes2;

import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"country",
"state"
})
public class Detail_ {

@JsonProperty("country")
private String country;
@JsonProperty("state")
private String state;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("country")
public String getCountry() {
return country;
}

@JsonProperty("country")
public void setCountry(String country) {
this.country = country;
}

@JsonProperty("state")
public String getState() {
return state;
}

@JsonProperty("state")
public void setState(String state) {
this.state = state;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
-----------------------------------pojo_Classes2.Location.java-----------------------------------

package pojo_Classes2;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"details"
})
public class Location {

@JsonProperty("details")
private List<Detail_> details = null;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("details")
public List<Detail_> getDetails() {
return details;
}

@JsonProperty("details")
public void setDetails(List<Detail_> details) {
this.details = details;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
-----------------------------------pojo_Classes2.Organisation.java-----------------------------------

package pojo_Classes2;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"details"
})
public class Organisation {

@JsonProperty("details")
private List<Detail> details = null;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("details")
public List<Detail> getDetails() {
return details;
}

@JsonProperty("details")
public void setDetails(List<Detail> details) {
this.details = details;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
-----------------------------------pojo_Classes2.RootDoc.java-----------------------------------

package pojo_Classes2;

import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"organisation",
"location"
})
public class RootDoc {

@JsonProperty("organisation")
private Organisation organisation;
@JsonProperty("location")
private Location location;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("organisation")
public Organisation getOrganisation() {
return organisation;
}

@JsonProperty("organisation")
public void setOrganisation(Organisation organisation) {
this.organisation = organisation;
}

@JsonProperty("location")
public Location getLocation() {
return location;
}

@JsonProperty("location")
public void setLocation(Location location) {
this.location = location;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}

我想我已经找到问题了。

对吗?

之所以会发生这种情况,是因为如果条件之间为OR,则无法绑定变量并在RHS中使用它。???

1 个答案:

答案 0 :(得分:0)

是的,这完全有可能。

为简单起见,我假设您放入工作内存中的对象的结构如下:

class Data {
  Location location;
  Organization organization;
}

子类定义如下:

class Location {
  List<Detail> details;
}
class Organization {
  List<Detail> details;
}
class Detail {
  String name;
  String country;
  String state;
}

这应该准确地为您的问题中的JSON建模。为简便起见,省略了这些类中的获取器和设置器。

现在,我们的目标是编写一条规则,以在Detail中存在同时具有country =“ India”和state =“ Haryana”的Location时触发;在Detail中还存在一个名为{AON”的Organization

我们可以按以下步骤完成此操作:

rule "Trigger when Haryana, India is a Location and AON is an Organization name"
when
  // First we need to extract the location and organization so we can do work on them
  Data( $location: location != null,
        $organization: organization != null )

  // Confirm that there exists an organization detail with name = "AON"
  Organization( $orgDetails: details != null ) from $organization
  exists( Detail( name == "AON" ) from $orgDetails )

  // Confirm that there exists a location with country = India and state = Haryana
  Location( $locDetails: details != null ) from $location
  exists( Detail( country == "India", state == "Haryana" ) from $locDetails )
then
  System.out.println("Rule has been executed")
end

我使用exists谓词是因为我只是在检查是否存在符合条件的对象。如果我们想将已标识的对象绑定到变​​量,则可以简单地进行赋值-例如:

// This snippet matches the previous rule where we identify an Organization detail with name of "AON"
Organization( $ordDetails: details != null ) from $organization
$aon: Detail( name == "AON" ) from $orgDetails

甚至自己分配属性:

// This snippet matches the previous rule where we identify the Location detail for Haryana, India
Location( $locDetails: details != null ) from $location
Detail( country == "India",
        state == "Haryana",
        $name: name ) from $locDetails

我对Jackson的绑定库不是很熟悉,但是从我对您发布的POJO的了解中,对于您直接使用的对象,语法应该非常相似,即使不完全相同。 (据我所知,您的“ RootDoc”和我的“数据”实际上是同一件事。)

无论如何,这是可行的,因为Drools评估了所有左侧条件以决定是否应触发该规则。因此,如果我们看一下示例规则,它将检查组织内部是否存在符合我们条件的详细信息(名称为“ AON”)。如果不满足此条件,则规则不会触发,因为此时左侧已被评估为“假”。如果确实存在,那么我们将检查“位置”内是否有满足我们条件的详细信息(“国家/地区” =印度和“州” =哈里亚纳邦)。只有满足左侧的 all 条件时,才会触发此规则;这是一组“自然”的条件。

我认为值得指出的是,必须在同一谓词中同时检查“国家=印度”和“州=哈里亚纳邦”。否则,它最终可能会出现这样的情况:您有两个Details,每个Details占条件的一半,但没有一个Details兼具两个条件。例如,您可能有一个国家(地区)为空但州为哈里亚纳邦的详细信息,以及一个国家(地区)为印度而州为马哈拉施特拉邦的详细信息;如果您在同一谓词中没有两个检查,即使您没有同时满足这两个条件的 one 详细信息,此情况也可能触发规则。