首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用带有两个参数的drools函数

如何使用带有两个参数的drools函数
EN

Stack Overflow用户
提问于 2018-01-03 19:46:36
回答 1查看 653关注 0票数 0

我想检查一下学生身份是否存在于公司班级的emp id中。如果在空列表中存在学生,那么我应该抛出错误。

我试过两种不同的方法:

第一个

代码语言:javascript
复制
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.

在运行它时,我得到了以下错误:无法使用.contains()。

第二位

代码语言:javascript
复制
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<>,但这仍然不起作用。

代码语言:javascript
复制
Class Company {
private List<Employee> emplist;
private List<Student> stdlist;
}

Class Employee {
private String empid;
}

Class Student  {
private String stdid;
}
EN

回答 1

Stack Overflow用户

发布于 2018-01-04 05:48:10

至于功能,请使用

代码语言:javascript
复制
function boolean isStudentidExists(List stds, List emps ){
  for( Object s : stds) {
    if( emps.contains( s ) ) return true;
  }
  return false;
}

要使用逻辑,建议将“学生”和“雇员”对象作为事实插入。

代码语言:javascript
复制
rule check
when
    Company( $el: emplist, $sl: stdlist )
    Student( $sid: stdid, this memberOf $sl )
    Employee( empid == $sid, this memberOf $el )
then
    // error
end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48084148

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档