如果你只想核实在CList中至少有一个项目通过<条码>b. 姓名=c.id标准对bList物项进行对比,那么你就使用<条码>exists(......)条码>。
rule "example 1"
when
A( $bList: bList, $cList )
B( $name: name) from $bList
exists( C( id == $name ) from $cList )
then
System.out.println("B == C exists!")
end
Alternatively, if you need references to the matching B
and C
instances for some operation, you can omit the exists
clause.
rule "example 2"
when
A( $bList: bList, $cList )
$b : B( $name: name) from $bList
$c: C( id == $name ) from $cList
then
System.out.println("B == C exists!")
// you can now reference $b and $c directly
end
两个例子的警示是,它们将发射each。
For example, let s say your object A looks something like this:
A {
bList: [
{ name: "1" }, // 0
{ name: "2" }, // 1
{ name: "3" } // 2
},
cList: [
{ id: "4" }, // 0
{ id: "3" }, // 1
{ id: "3" }, // 2
{ id: "1" } // 3
]
}
如果你对这一投入提出“例1”规则(exists
),则该规则造成2次火灾:
- once for the bList item at index 0, which matches to cList item at index 3
- once for the bList item at index 2, which matches to both the cList items at indices 1 and 2.
自您重新使用<代码>exists以来,只要在<代码>c上至少有一个匹配点。 www.un.org/chinese/ga/president
Now, if you fire the "Example 2" rule, it ll actually fire 3 times:
- once for
bList[0]
which matches cList[3]
- once for
bList[2]
and its pair at cList[1]
- once for
bList[2]
and its pair at cList[2]
由于not usedexists
,该规则将引发对等项目的每一pair。 当你需要为每个奶牛做手术时,这样做是有用的,但如果你至少要花了一次这样的鞋子,就更不用说了。
如果我们只想检查是否至少有一个配对奶制品? 在english,“至少是一件手工艺品,其名称等同于一件手工艺品”。 我们只想一劳永逸地击火,不要一枪匹马,或一枪 each。
如果我们想要按规定行事,我们就可以起草一条使用<代码>accumulate的规则——这项行动确实收集了与某种复杂比较相符的所有物品......这一案例我们有效地收集了所有对等的B-C楼。 然后,我们检查了由此产生的名单size >= 1
。
That sounds like a lot of work when we only want at least one match -- and it is. It s a lot simpler to just re-purpose the "example 1"
rule and have it only fire once.
rule "example 1 - only once"
when
not( String(this == "example1") )
A( $bList: bList, $cList: cList )
B( $name: name ) from $bList
exists( C( id == $name ) from $cList )
then
// do your operations here
insert( "example1" )
end
This is an old Drools trick; when you trigger your rule, insert something into working memory which makes it no longer valid to fire. In this case I went with a simple String but you can use any sort of object, really.
在评估该规则时,条件之一是,在工作记忆中,没有用“例1”。 如果该条件与其他条件(即B=C对等)一致得到满足,则该规则便可启动。 无论出于何种商业逻辑,我们随后插入“范例1”以体现为工作记忆。 现在该规则已不再有效,因此,无论在A物体中有多少B=C奶制品,都不会引起对该规则的进一步处决。