英文:
Need to add multiple items in list - decision table - Drools
问题
我需要在drools决策表中为Pojo类创建新的多个对象实例。我已经使用两个事实类Student fact和Subject fact实现了这一点。我需要触发决策表中的所有规则,并将所有值添加到对象的数组列表中。但是我只得到了决策表的最后一条规则值。似乎决策表的值被覆盖了。
事实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<SubjectFact> subjectList) {
this.subjectList = subjectList;
}
public List<SubjectFact> 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 String getSubjectName(){
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"
}
}
我的决策表如下所示:
请有人建议如何实现期望的响应?
英文:
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.
Fact 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;
}
}
Fact 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;
}
}
Current Response
{
"id":123,
"name": "xyz",
"subjectList": [
{
"id": 6,
"name":"Hindi"
},
{
"id": 6,
"name":"Hindi"
}
}
Expected Response
{
"id":123,
"name": "xyz",
"subjectList": [
{
"id": 5,
"name":"English"
},
{
"id": 6,
"name":"Hindi"
}
}
My Decision Table looks like
Any one pls advise to achieve the expected response?
答案1
得分: 4
每个表中的每一行都变成一个规则,每个动作列都变成“then”块中的一行。
对于每个规则,您需要一个语句来创建主体,用语句填充它,并用语句将其添加到匹配的学生中。
在“CREATE”和“COLLECT”中需要值,否则动作将被跳过。
在没有“目标对象”的单元格中需要“;”,而在提供“$subject”和“$student”对象时则不需要。不要问我为什么,只是分析生成的DRL代码。
您可能希望隐藏两个“技术列”。
这将生成类似以下的两个规则:
package draft;
// 从决策表生成
import draft.Student;
import draft.Subject;
// A9处的规则值,A4处的标题
rule "Rule 1"
when
$student: Student(id == "123")
then
Subject $subject = new Subject();
$subject.setSubId(5);
$subject.setSubjectName('English');
$student.addSubject($subject);
end
// A10处的规则值,A4处的标题
rule "Rule 2"
when
$student: Student(id == "123")
then
Subject $subject = new Subject();
$subject.setSubId(6);
$subject.setSubjectName('Hindi');
$student.addSubject($subject);
end
附注:我在使用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 getSubjectName() {
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'已被元组[Student]激活
00:00:00 <-- 'Rule 2'已被元组[Student]激活
00:00:00 Facts (1):
Student[id=123,name=Student 123,subjectList=[draft.Subject@1ded7b14, draft.Subject@29be7749]]
英文:
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 columns'.
This will generate two rules like below
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: 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 at the start of a cell in the editor and skipped. So actual cell value which finally worked for me was ''English'
.
Here are my models
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;
}
}
test
@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();
}
}
test output
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]]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论