我想使用 drools 比较两个列表,但规则没有触发

I want to compare two lists using drools, but the rules are not firing

将 InboundRegionalProduct 和 ExistingRegionalProduct 类型的两个数组列表插入 drools 会话后,规则没有被触发。不知道是什么问题。 这是drl文件。

package rules;
import com.ferguson.mw.k8.pricing.b2ccontroller.model.InboundRegionalProduct;
import com.ferguson.mw.k8.pricing.b2ccontroller.model.ExistingRegionalProduct;
dialect "java"

rule "Exists, no change in flag"
 when
 $in : InboundRegionalProduct();
 $existing : ExistingRegionalProduct(productId == $in.productId, regionallyPriced == $in.regionallyPriced);
then
 delete($in);
 delete($existing);
end
// Match based on prodcutId and regionallyPriced flags are different from one another.
query "delta"
 $in : InboundRegionalProduct();
 ExistingRegionalProduct(productId == $in.productId);
end
// Inbound but no existing product. The regionallyPriced attribute must be set to "true" 
query "add"
 $in : InboundRegionalProduct();
 not ExistingRegionalProduct(productId == $in.productId);
end
// Match based on having an existing product with a flag and no matching inbound product. The regionallyPriced attribute should be removed for these.
query "remove"
$existing : ExistingRegionalProduct()
not InboundRegionalProduct(productId == $existing.productId)
end

pojo 类 在下面;

@Data
@AllArgsConstructor
public abstract class RegionalProduct {
private final String productId;
private final Boolean regionallyPriced;
}



@Data
@EqualsAndHashCode(callSuper = true)
public class InboundRegionalProduct extends RegionalProduct {
public InboundRegionalProduct(final String productId) {
super(productId, Boolean.TRUE);
}
}



@Data
@EqualsAndHashCode(callSuper = true)
public class ExistingRegionalProduct extends RegionalProduct {
public ExistingRegionalProduct(final String productId, final Boolean regionallyPriced) {
super(productId, regionallyPriced);
}
}

如您所说,您插入的是列表,而不是作为事实的单个对象。您的规则是针对个别事实编写的。因此,您要么将每个元素插入到会话中的列表中,要么创建一个规则来为您执行此操作:

rule "Insert List elements"
when
    $l: List()
    $p: RegionalProduct() from $l
then
    insert($p);
end

另一种选择是在现有规则中包含列表提取部分。例如:

rule "Exists, no change in flag"
 when
 $l: List()
 $in : InboundRegionalProduct() from $l
 $existing : ExistingRegionalProduct(productId == $in.productId, regionallyPriced == $in.regionallyPriced) from $l;
then
 delete($in);
 delete($existing);
end

将列表包含在您的 DRL 中的问题是,如果您开始使用具有不同含义的不同列表,它可能会变得混乱。在这种情况下,您可以创建自己的包含列表的特定类型的对象包装器,然后编写有关这些对象的规则。

我的建议:只需在您的会话中插入您 Java 代码中的每个单独事实。