【问题标题】:I want to compare two lists using drools, but the rules are not firing我想使用流口水比较两个列表,但规则没有触发
【发布时间】:2021-10-18 10:21:50
【问题描述】:

在将 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);
}
}

【问题讨论】:

    标签: java drools rules


    【解决方案1】:

    正如您所说,您插入的是列表,而不是单个对象作为事实。你的规则是为个别事实而写的。因此,您可以在会话中的列表中插入每个元素,或者创建一个为您执行此操作的规则:

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

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

    【讨论】:

      猜你喜欢
      • 2019-06-14
      • 2011-12-05
      • 1970-01-01
      • 2011-12-06
      • 1970-01-01
      • 1970-01-01
      • 2015-06-20
      • 1970-01-01
      • 2019-04-08
      相关资源
      最近更新 更多