【问题标题】:How to use the function in drools with two parameters两个参数如何在drools中使用函数
【发布时间】:2018-06-13 12:39:23
【问题描述】:

我想检查student id 是否存在于公司类的emp id 中。 如果 empidlist 中存在 studentid,那么我应该抛出错误。

我尝试过两种不同的方式:

第一个

rule :"check the student id is present in empid"
when
$company : Company()
accumulate(Employee(empid !=null, empid : empid) from $company.emplist;
empidlist: collectList(empid))
accumulate(Student(stdid !=null, empidlist.contains(stdid),stdid : stdid) from $company.stdlist; stdidlist: collectList(stdid);stdidlist.size()>0)
then
   //throw error.

运行时出现如下错误:cannot use .contains() from accumulate.

第二次

function boolean isStudentidExists(List<String> stdidlist, List<String> empidlist){
    for (String s : stdidlist) {
            for (String res : empidlist) {
                if (res.equals(s))
                    return true;
            }
        }
        return false;
}

rule :"check the student id is present in empid"
when
$company : Company()
accumulate(Employee(empid !=null, empid : empid) from $company.emplist; empidlist: collectList(empid))
accumulate(Student(stdid !=null,stdid : stdid) from $company.stdlist; stdidlist: collectList(stdid))
eval(isStudentidExists(stdidlist,empidlist))
then
   //throw error.

这里不是读取列表。我尝试在我的函数中仅将类型指定为 List,但这仍然不起作用。

Class Company {
private List<Employee> emplist;
private List<Student> stdlist;
}

Class Employee {
private String empid;
}

Class Student  {
private String stdid;
}

【问题讨论】:

    标签: drools drools-guvnor drools-planner drools-fusion drools-flow


    【解决方案1】:

    至于功能,使用

    function boolean isStudentidExists(List stds, List emps ){
      for( Object s : stds) {
        if( emps.contains( s ) ) return true;
      }
      return false;
    }
    

    要使用逻辑,建议将 Student 和 Employee 对象作为事实插入。

    rule check
    when
        Company( $el: emplist, $sl: stdlist )
        Student( $sid: stdid, this memberOf $sl )
        Employee( empid == $sid, this memberOf $el )
    then
        // error
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-30
      • 2019-08-28
      • 2023-01-20
      • 1970-01-01
      • 2018-02-11
      相关资源
      最近更新 更多