1

I have been trying to figure this out on and off for a really long time. I can imagine a lot of very verbose and non-droolsy ways to go about accomplishing this. However, I would like to know the best practice for dealing with a situation like this. I would like to know how to write the constraint I describe below in the drools dialect.

I wish to write a constraint that deals with a collection. Let's say we have CustomType which has a field Collection. The constraint should express we want to find facts of CustomType who have any object in the collection other than the specified object(P). Critically, it does not matter if the specified object is present. It only matters if there is at least one other object in the collection.

The collection in question is not a set. It is possible there are multiple instances of P in the same collection.

In pseudo Java I might write a method like:

public Boolean isThereANotP(CustomType cs){
for(str : cs.Collection){
    if(str != P){
        return true}
}
return false
}

How would this be expressed in the when clause of a drool? The closest I could come is specifying there is not a P, but that isn't what I want. I want to know that there is a not P.

4

2 回答 2

1

也许这个?

$c: CustomType()
$notP: Object(this != objectP) from $c.collection

实际上,这将为集合中不是 objectP 的每个对象激活。这可能会更好:

$c: CustomType()
exists Object(this != objectP) from $c.collection
于 2013-11-22T09:04:21.577 回答
0

小心,如果您的集合的第一个元素不是 P,您发布的伪代码将返回 true。或者,更通用地说,如果您在 P 之前没有任何 P 或根本没有 P,它会返回 true。假设您正在寻找的是:

查找集合中除指定对象(P)之外的任何对象的 CustomType 事实

你可以写一个类似这样的规则:

global P objectP; //I'm using a global, but you could use a fact, or a binding in one of your fact's fields. 

rule "Find CustomType with no P"
when
    $c: CustomType(collection not contains objectP)
then
    //use $c
end

我不知道如何解释您的其余要求:

至关重要的是,指定对象是否存在并不重要。仅当集合中至少有一个其他对象时才重要。

如果您的要求是集合不应该为空,那么您可以将模式编写为:

CustomType(collection.empty == false , collection not contains objectP)

希望能帮助到你,

于 2013-11-22T08:49:34.877 回答