需要在列表中添加多个项目 - 决策 table - Drools

Need to add multiple items in list - decision table - Drools

我需要在 drools 决策 table 中为 Pojo class 创建一个新的多对象实例。我已经使用两个事实 Student fact 和 subject fact class 来实现。我需要触发决策 table 中的所有规则,并且我需要将所有值添加到对象的数组列表中。但我只得到决策 table 的最后规则值。似乎决策 table 值被覆盖了。

事实 1

Class StudentFact{

 private int id;
 private String name;
 private List<SubejctFact> subjectList;

 public void setId(int id){
    this.id = id;
 }

 public int getId(){
    return id;
 } 

 public void setName(String name){
    this.Name = name;
 }    

 public String getName(){
    return name;
 }

     public void setSubjectList(List<Subject> subjectList) {
        this.subjectList = subjectList;
    }



    public int getSubjectList() {
        return subjectList;
    }




}

事实 2

Class SubjectFact{
 private int subId;
 private String subjectName;

 public void setSubId(int subId){
     this.subId= subId;
 }

 public int getSubId(){
   return subId;
 }

 public void setSubjectName(String subjectName){
      this.subjectName = subjectName;
 }

 public int getSubejctName(){
      return subjectName;
 }

}

当前响应

{
  "id":123,
  "name": "xyz",
  "subjectList": [
     { 
        "id": 6,
        "name":"Hindi"
     },
     {
        "id": 6,
        "name":"Hindi"
     }
}

预期响应

 {
      "id":123,
      "name": "xyz",
      "subjectList": [
         { 
            "id": 5,
            "name":"English"
         },
         {
            "id": 6,
            "name":"Hindi"
         }
    }

我的决定Table看起来像

任何人请建议达到预期的反应?

table 中的每一行成为规则,每个操作列成为 then 块中的一行。
对于每个规则,您需要一个语句来创建主题、语句来填充它和语句来将它添加到匹配的学生。
'CREATE' 和 'COLLECT' 中的值是必需的,否则将跳过操作。
; 在没有 'target object' 的单元格中是必需的,当您提供“$subject”和“$student”对象时则不需要。不要问我为什么。刚刚分析生成的drl.
您可能想隐藏两个 'technical columns'.

这将生成如下两个规则

package draft;
//generated from Decision Table
import draft.Student;
import draft.Subject;
// rule values at A9, header at A4
rule "Rule 1"
    when
        $student:Student(id == "123")
    then
        Subject $subject = new Subject();
        $subject.setSubId(5);
        $subject.setSubjectName('English');
        $student.addSubject($subject);
end

// rule values at A10, header at A4
rule "Rule 2"
    when
        $student:Student(id == "123")
    then
        Subject $subject = new Subject();
        $subject.setSubId(6);
        $subject.setSubjectName('Hindi');
        $student.addSubject($subject);
end

PS:我一直在努力解决 " 被 Calc 编辑器自动替换为 `` 的问题,这对于 drools 解析器来说不是有效符号,所以我使用了单引号,这似乎是特殊符号在编辑器中单元格的开头并跳过。所以最终对我有用的实际单元格值是 ''English'.

这是我的模型

public class Student {
    private int id;
    private String name;
    private List<Subject> subjectList = new ArrayList<>();
    
    public Student(int id, String name) {
        this.id = id;
        this.name = name;
    }
    
    public void setId(int id) {
        this.id = id;
    }
    
    public int getId() {
        return id;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public String getName() {
        return name;
    }
    
    public void addSubject(Subject subject) {
        subjectList.add(subject);
    }
    
    public void setSubjectList(List<Subject> subjectList) {
        this.subjectList = subjectList;
    }
    
    public List<Subject> getSubjectList() {
        return subjectList;
    }
}

public class Subject {
    private int subId;
    private String subjectName;
    
    public void setSubId(int subId) {
        this.subId = subId;
    }
    
    public int getSubId() {
        return subId;
    }
    
    public void setSubjectName(String subjectName) {
        this.subjectName = subjectName;
    }
    
    public String getSubejctName() {
        return subjectName;
    }
}

测试

@DroolsSession(resources = "draft/ApplicableSubjects.xls",
        builderProperties = "drools.dump.dir = target/dump")
public class PlaygroundTest {
    
    @Rule
    public DroolsAssert drools = new DroolsAssert();
    
    @Test
    public void testIt() {
        drools.insertAndFire(new Student(123, "Student 123"));
        drools.printFacts();
    }
}

测试输出

00:00:00 --> inserted: Student[id=123,name=Student 123,subjectList=[]]
00:00:00 --> fireAllRules
00:00:00 <-- 'Rule 1' has been activated by the tuple [Student]
00:00:00 <-- 'Rule 2' has been activated by the tuple [Student]
00:00:00 Facts (1):
Student[id=123,name=Student 123,subjectList=[draft.Subject@1ded7b14, draft.Subject@29be7749]]