当前位置: 首页 > 知识库问答 >
问题:

Drools自定义对象列表中的条件规则

龙繁
2023-03-14

我有一个学生班,它有相关部门的列表。

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个答案

单修德
2023-03-14

在列表中查找字符串

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

编辑:您还需要插入部门对象和事实。

如果学生必须掌握两种语言,请使用

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
 类似资料: