java - Drools not matching nested object obtained from deserialization -
i'm trying create rule in drools trigger based on hypothetical student getting straight as.
import student import semester import java.util.* dialect "mvel" rule "straight as1" when $s : student( grades != null, $g : grades ) forall( semester(reading == "a", writing == "a", math == "a") $g ) system.out.println($s.getid() + ": s as1: " + $s); system.out.println($s.getid() + ": g as1: " + $g); end rule "straight as2" when $s : student( grades != null, $g : grades ) $a : list(size() == $g.size) collect (semester(reading == "a", writing == "a", math == "a") $g) system.out.println($s.getid() + ": s as2: " + $s); system.out.println($s.getid() + ": g as2: " + $g); end
the output of this:
001: s as1: student{id='001', grades=[{writing=a, reading=a, math=a}, {writing=a, reading=a, math=a}], name='albert'} 001: g as1: [{writing=a, reading=a, math=a}, {writing=a, reading=a, math=a}] ---------------- 002: s as1: student{id='002', grades=[{writing=b, reading=b, math=b}], name='bob'} 002: g as1: [{writing=b, reading=b, math=b}]
the problem here bob doesn't have as. not sure how either of straight as
rules fire albert , not bob - as1
attempt fires while as2
attempt doesn't fire anything.
i've been able write rules filter based on name. in debugging i've had breakpoint on getgrades()
trigger... none of breakpoints on getmath()
, getwriting()
or getreading()
have been hit.
a student object (tightened , tostring() removed) is:
public class student { private string id; private list<semester> grades; private string name; public student() { } public string getname() { return name; } public string getid() { return id; } public list<semester> getgrades() { return grades; } public void setname(string name) { this.name = name; } public void setid(string id) { this.id = id; } public void setgrades(list<semester> grades) { this.grades = grades; } }
the corresponding semester object (also tightened , tostring() removed):
public class semester { private string reading; private string writing; private string math; public semester() { } public string getreading() { return reading; } public string getwriting() { return writing; } public string getmath() { return math; } public void setreading(string reading) { this.reading = reading; } public void setwriting(string writing) { this.writing = writing; } public void setmath(string math) { this.math = math; } }
these objects instantiated code snippet:
yamlreader reader = new yamlreader(new filereader(file)); student student = reader.read(student.class); system.out.println(student.tostring()); rv.add(student);
and yaml object such as:
id: 001 grades: - reading: writing: math: - reading: writing: math: name: albert
use simplest possible approach:
rule "straight as1" when $s : student( grades != null, $g : grades ) not semester(reading != "a" || writing != "a" || math != "a") $g system.out.println($s.getid() + ": s as1: " + $s); system.out.println($s.getid() + ": g as1: " + $g); end
my main, using student , semester:
public class main { private kiesession kiesession; private kiescanner kiescanner; public void build() throws exception { kieservices kieservices = kieservices.factory.get(); kiefilesystem kfs = kieservices.newkiefilesystem(); fileinputstream fis = new fileinputstream( "alla/alla.drl" ); kfs.write( "src/main/resources/simple.drl", kieservices.getresources().newinputstreamresource( fis ) ); kiebuilder kiebuilder = kieservices.newkiebuilder( kfs ).buildall(); results results = kiebuilder.getresults(); if( results.hasmessages( message.level.error ) ){ system.out.println( results.getmessages() ); throw new illegalstateexception( "### errors ###" ); } kiecontainer kiecontainer = kieservices.newkiecontainer( kieservices.getrepository().getdefaultreleaseid() ); kiebase kiebase = kiecontainer.getkiebase(); kiesession = kiebase.newkiesession(); } public void exec(){ student albert = new student(); albert.setname( "albert" ); albert.setid( "001" ); albert.getgrades().add( new semester( "a", "a", "a" ) ); albert.getgrades().add( new semester( "a", "a", "a" ) ); kiesession.insert( albert ); student bob = new student(); bob.setname( "bob" ); bob.setid( "002" ); bob.getgrades().add( new semester( "b", "b", "b" ) ); kiesession.insert( bob ); kiesession.fireallrules(); } public static void main( string[] args ) throws exception { main m = new main(); m.build(); m.exec(); } }
Comments
Post a Comment