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'.
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 on the start of the 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]]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…