在自定义对象列表中的条件中删除规则

时间:2017-03-10 18:03:16

标签: drools

我有学生班,其中有相关的部门列表。

public class Student {

    private String name;
    private String desc;
    private List<Department> department = new ArrayList<Department>();
}

public class Department {

    private String name;
    private String desc;
}

我在Drools尝试写规则。规则就像 - 如果学生属于某些部门,那么就采取行动。我无法找到正确的方法。我在下面写的一个不起作用。据我所知,它的字符串列表是有效的。有没有办法在自定义对象上进行包含。

rule "Language"

when
    $student : Student(department contains "English" && department contains "French")

then
    System.out.println("Belongs to Language");

调用规则的代码

                Student student  = new Student();
                student.setName("John");
                Department a1 = new Department();
                a1.setName("English");
                student.addDepartment(a1);
                Department a2 = new Department();
                a2.setName("French");
                student.addDepartment(a2);
                System.out.println("Student :" + student);
                ksession.insert(student);  
                ksession.fireAllRules();  

我正在使用Drools 6.2版本。

1 个答案:

答案 0 :(得分:1)

List<Department>中寻找字符串并不起作用。您必须查找name字段是这两个值之一的成员。

rule "Language"
when
  $student: Student( $dep: department )
  Department( name == "French" || == "English",
              this memberOf $dep )
then
  System.out.println("Belongs to Language");
end

编辑:您还需要将DEpartment对象作为事实插入。

如果学生必须同时使用这两种语言,请使用

rule "Language"
when
  $student: Student( $dep: department )
  Department( name == "French", this memberOf $dep )
  Department( name == "English", this memberOf $dep )
then
  System.out.println("Belongs to Language");
end