Drools: How to compare attributes of two different objects in drools?

3.5k views Asked by At

I am using drools for business level validations. Is it possible to load two objects in working memory and compare their values? Lets say i have 2 objects:

Object1{
String name;
}

Object2{
String sname;
}

Can I compare name(Object1) with sname(Object2) in the drl file?

Object1(name)==Object(name)

I tried to add this line in the drl file but it gives an error "Unexpected token name"

Also Help me to insert these objects in working memory. I am getting the kie session using below steps

KieContainer container=KieServices.Factory.get().getKieClasspathContainer();

KieSession kieSession = container.newKieSession("SampleSession");

Now can Insert object1 and Object2 using insert method simultaneously ?

kieSession.insert(object);
1

There are 1 answers

2
laune On BEST ANSWER

How to insert objects:

Object1 o1 = ...;
Object2 o2 = ...; 
kieSession.insert( o1 );
kieSession.insert( o2 );

How to compare their attributes:

rule namecomp
when
    Object1( $n1: name )
    Object2( sname == $n1 )
then
    System.out.println( "Names are equal" );
end

These are elementary questions. Makes sure to read the Drools documentation.