开发者

Compare objects of the same class in JBoss Drools

I have a quick question - is it possible to compare tw开发者_开发百科o objects of the same class in Drools? And if yes, how do I distinguish between the two objects?


Yes.

first : Object()
second : Object( this != first )

first and second are of the same class, but different instances.


To answer your follow-up. Yes, both objects would need to be in working memory for the rule to be activated. In Toni's example "first" is bound to the first object and "second" the second (ie you'd use "first" to access the first object). To access methods on the object you can use mvel/java syntax like first.userId or first.getUserId(). Thus, to see if there are two different Objects in WM with the same userid:

 when
   $first : Object()
   $second : Object( this != $first, userid==$first.userId )
 then
 ...

Note, by convention you'll often see the pattern binding variable prefixed with "$" as above.

hth

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜