Lakshay Suthar
Lakshay Suthar

Reputation: 1

Use 2 objects of same class

I am new to Drools and have to compare value of 2 objects of same class. i.e. let's say the class is

public class Person() {
    public string Name;
}

So I want to compare person1's name and person2's name and throw an exception if they are equal. It would be great if someone can give an example of the same.

Upvotes: 0

Views: 60

Answers (1)

Esteban Aliverti
Esteban Aliverti

Reputation: 6322

That can easily be done with a rule similar to this:

rule 'Unique Names'
when
    $p1: Person()
    Person(this != $p1, name == $p1.name)
then
    //error
end

I would discourage you to simply throw an exception in the then part of the rule though. It is a better idea to collect your results somewhere and then validate those results outside Drools.

Upvotes: 1

Related Questions