如何从Drools中的列表中获取最大最小项 [英] How to get max min item from a list in Drools

查看:122
本文介绍了如何从Drools中的列表中获取最大最小项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个班级

class Person {
    public Date dateOfBirth;
    public List<Person> children;
}

我想创建一条Drools规则,该规则可以让我长大。例如:

I would like to create a Drools rule that gets me the oldest child. For example:

rule "Oldest Child"
    when
        $person: Person()
        $oldestChild: Person() from $person.children
    then
        insert($oldestChild)
end

按照书面规定,$ oldestChild是一个列表,但我真的很想成为一个实际的最大的孩子(单个对象而不是列表)。我玩了一点儿积累,但无法正常工作。有想法吗?

As written, $oldestChild is a list but I'd really like to be an actual oldest child (single object instead of a list). I played around with accumulate a bit but couldn't get it to work. Any ideas?

推荐答案

使用内联自定义代码进行累积会产生最老的孩子:

An accumulate with inline custom code produces the oldest child:

rule "oldest child"
when
  Person($pn: name, $pd: dateOfBirth, $children: children)
  Person($ocn: name) from accumulate(
    $child: Person( $cd: dateOfBirth) from $children,        
    init( Person minp = null; Date mind = new Date(); ),
    action( if( $cd.compareTo( mind ) < 0 ){
                minp = $child;
                mind = $cd;
            } ),
    result( minp ) )
then
  System.out.println( $pn + "'s oldest child is " + $ocn );
end

如果需要,可以实现自己的累加函数(在Java中)认真的工作-这是更多的工作,但却是更清洁的解决方案。请参阅文档。

You may implement your own accumulate function (in Java) if you need this for serious work - it is more work but the "cleaner" solution. See the docs.

这篇关于如何从Drools中的列表中获取最大最小项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆