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

vvppvyoh  于 2022-11-08  发布在  其他
关注(0)|答案(1)|浏览(148)

我需要为Drools决策表中的Pojo类创建一个新的多个对象示例。我已经使用两个事实实现了Student事实和Subject事实类。我需要触发决策表中的所有规则,并将所有值添加到对象的数组列表中。但我只获得了决策表的最后一个规则值。看起来决策表的值被覆盖了。

事实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;
    }

}

事实二

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"
         }
    }

我的决策表如下所示

有没有人建议达到预期的React?

waxmsbnn

waxmsbnn1#

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

这将生成两个规则,如下所示

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]]

相关问题