• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

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

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

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(sname)
I tried to add this line in the drl file but it gives an error "Unexpected token name"

Pls help.

[ April 09, 2008: Message edited by: Bharat Bonde ]
[ April 09, 2008: Message edited by: Bharat Bonde ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe a solution:

p: Object()
q: Object(field == p.field)

If you see another way....
 
Bharat Bonde
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object comparison in drools is somewhat different but pretty easy.
If you want to compare attributes of 2 objects here is what you have to do:

Object1{
int firstId;
}
Object2{
int secondId
}
Sample Drools file Syntax for comparing 2 objects :
package xxxxx

import Object1;
import Object2;

rule "Object Attributes Comparison"
when
object1: Object1($firstId : firstId)
object2: Object2(secondId == $firstId)
then
System.out.println("Id's are equal");

end

Once you have loaded objects in working memory,you will get the desired sysout(if you have public getters defined for the attributes!)
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!