0

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 Answers1

-1

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.

laune
  • 31,114
  • 3
  • 29
  • 42
  • Thanks for help. I am going to add other rules in drl file. Is there any way to restrict firing of above rule only for the first two facts ? I am going to insert 4 facts into the session and the above rule should be fired for only first two facts – Rohith Kammula Dec 29 '16 at 14:07
  • "First" and "second" decided by insertion order? You'll have to maintain an attribute for these ordinals and write some constraints based on that. But that's a strange requirement. -- Perhaps you write another question, providing all of the background, why 4 at a time, why only #1 and #2 should match, etc. – laune Dec 30 '16 at 07:34