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



将inboundreregionalproduct和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

另一个选项是在现有规则中包含List提取部分。例如:

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中包含List的问题是,如果您开始具有不同含义的不同List,它可能会变得混乱。在这种情况下,您可以创建自己的包含List的特定类型的Object包装器,然后编写有关这些对象的规则。

我的建议:从你的Java代码中插入每个单独的事实到你的会话。

最新更新