需要在列表中添加多个项目-决策表-Drools [英] Need to add multiple items in list - decision table - Drools

查看:152
本文介绍了需要在列表中添加多个项目-决策表-Drools的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在drools决策表中为Pojo类创建对象的新的多个实例.我使用学生事实和学科事实课这两个事实来实现.我需要触发决策表中的所有规则,并且需要将所有值添加到对象的数组列表中.但是我只得到决策表的最后一个规则值.似乎决策表的值已被覆盖.

I need to create a new multiple instance of objects for the Pojo class in drools decision table. I have implemented using two facts Student fact and subject fact class. I need to fire all the rules in the decision table and I need to add all the values into array-list of the objects. But I'm getting only last rule values of decision table. It seems like decision table values are getting overridden.

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

我的决策表如下

有人建议达到预期的反应吗?

Any one pls advise to achieve the expected response?

推荐答案

表中的每一行都成为一条规则,每个动作列均成为then块中的一行.
对于每个规则,您都需要一条语句来创建Subject,一条语句来填充它,以及一条语句来将它添加到匹配的学生中.
需要"CREATE"和"COLLECT"中的值,否则将跳过操作.
在没有目标对象"的单元格中,;是必需的,而在提供"$ subject"和"$ student"对象时则不需要.不要问我为什么.刚刚分析生成的drl.
您可能要隐藏两个技术行".

Each row in a table becomes a rule, each action column becomes a row in then block.
For each rule you need a statement to create Subject, statements to populate it and statement to add it to matching student.
Values in 'CREATE' and 'COLLECT' are needed, otherwise action will be skipped.
; is required in a cell without 'target object' and it is not required when you provide '$subject' and '$student' objects. Don't ask me why. Just analyzed generated drl.
You may want to hide two 'technical rows'.

这将生成如下两条规则

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'.

PS: I was struggling with " being automatically replaced by Calc editor to `` which was not valid symbol for drools parser, so I used single quotes, which appeared to be special symbol on the start of the cell in the editor and skipped. So actual cell value which finally worked for me was ''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]]

这篇关于需要在列表中添加多个项目-决策表-Drools的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆