gpt4 book ai didi

drools - 如何在流口水时编写嵌套条件

转载 作者:行者123 更新时间:2023-12-04 12:56:48 25 4
gpt4 key购买 nike

这是我的 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

但它向我显示了这个错误: 规则“rule1”中的第 18:3 行不匹配输入“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,您无法绑定(bind)变量并在 RHS 中使用它。???

最佳答案

是的,这是完全可能的。

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

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 建模。为简洁起见,省略了这些类的 getter 和 setter。

现在,我们的目标是编写一个在存在 Detail 时触发的规则。在 Location 内有两个国家=“印度”和国家=“哈里亚纳邦”;还有一个 DetailOrganization 内名称为“AON”。

我们可以按如下方式完成:
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谓词,因为我只是在检查这些满足条件的对象是否存在。如果我们想将识别的对象绑定(bind)到变​​量,你可以简单地做一个赋值——例如:
// 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 的绑定(bind)库不是很熟悉,但是根据我对您发布的 POJO 的理解,对于您直接使用的对象,语法应该非常相似,如果不相同的话。 (据我所知,您的“RootDoc”和我的“数据”实际上是一回事。)

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

我认为值得指出的是,必须在同一个谓词中同时检查 country = India 和 state = Haryana。否则,它可能最终会出现这样一种情况,即您有两个细节,每个细节都有一半的条件,但没有一个细节同时具有这两个条件。例如,您可能有一个国家为空但州为哈里亚纳邦的细节,以及一个国家为印度而国家为马哈拉施特拉邦的细节;如果您没有在同一个谓词中进行两项检查,则即使您没有一个 Detail 满足这两个条件,这种情况也可能会触发规则。

关于drools - 如何在流口水时编写嵌套条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52261659/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com