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

查看:23
本文介绍了需要在列表-决策表-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?

推荐答案

表格中的每一行成为一条规则,每个操作列成为块中的一行.
对于每条规则,您都需要一个语句来创建主题、填充它的语句以及将其添加到匹配学生的语句.
'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天全站免登陆