English 中文(简体)
Drools:nested
原标题:Drools: nested exists
  • 时间:2024-05-10 22:13:20
  •  标签:
  • drools

我想在我的规则中有一个空洞之处,但在我执政时,我会遇到错配的投入错误。 我正试图发现,对于有2个清单的物体,在清单1中存在一个物体,在清单2中存在一个类似物体。

相关目标如下:

public class A {
  List<B> bList;
  List<C> cList;
}

public class B {
  String name;
}

public class C {
  String id;
}

(B=====) C. 如果B. 名称=C.id

是否有只有德勒斯人的解决办法,还是为我想要做些什么而工作?

这里是我的规则(造成错误的输入错误)。

rule "find B-C match"

when
$a: A(bList != null && cList != null)
  exists $b: B(
    exists $c: C($b.name == $c.id)
    from $a.cList)
  from $a.bList
then
System.out.println("A has a match");
// do other stuff
end
问题回答

如果你只想核实在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奶制品,都不会引起对该规则的进一步处决。





相关问题
Drools Rules: How can I use a method on "when" section?

I need to execute a method on "when" section of a DSLR file and I´m not sure if it´s possible. Example: rule "WNPRules_10" when $reminder:Reminder(source == "HMI") $user:User(isInAgeRange("...

Extract DRL from DSLR

How can I access the DRL generated within a DSLR file in Drools?

Drools, spring and mule

Has anyone combined these technologies? Could you share lessons learnt?

drools persistence with mysql, can t get process instance

I m stumped. I m trying to implement persistence with Drools-flow, and I d like to grab the value of a property on a workitem / processinstance, but everytime I try to get the workitem or process ...

Using Maven to download drools sources

I m new to maven and drools, so this could be obvious... I m trying to work off of the drools snapshot repository using maven/eclipse. I d like to be able to link the sources/javadoc when updating ...

Spring-Drools integration?

There used to be spring-modules project that provides this integration; however, that project is deprecated now. Does anyone now if there is any continued support for this integration? Thanks.

热门标签