0

我想检查公司班级的emp id中是否存在学生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;
}
4

1 回答 1

0

至于功能,请使用

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
于 2018-01-04T05:48:10.243 回答