0

I want to check whether the student id is present in emp id of the company class. If a studentid is present in the empidlist, then i should throw the error.

I have tried it in two different ways:

1st

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.

When running it, I got the following error: cannot use .contains() from accumulate.

2nd

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.

Here it is not reading the List. I have tried giving the type as only List<> in my function, but that still doesn't work.

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

Class Employee {
private String empid;
}

Class Student  {
private String stdid;
}
laune
  • 31,114
  • 3
  • 29
  • 42
Anvesh
  • 1
  • 3

1 Answers1

0

As for the function, use

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

To use logic, it is advisable to have Student and Employee objects inserted as facts.

rule check
when
    Company( $el: emplist, $sl: stdlist )
    Student( $sid: stdid, this memberOf $sl )
    Employee( empid == $sid, this memberOf $el )
then
    // error
end
laune
  • 31,114
  • 3
  • 29
  • 42