I have a class :
public class FootballPlayer() {
private int scoredGoals;
//... some other attributes and some getters and setters
}
I need in my drools file to have a rule to decide who is a better player between 2 comapred players.
The difficulty that I cross is how to compare between the two objects of the same class?
Thank you a lot!
There is no difficulty, only a minor cliff that has to be circumnavigated.
rule "A better scorer"
when
$f1: FootballPlayer( $score1; scoredGoals )
$f2: FootballPlayer( $score2; scoredGoals > $score1 )
then
System.out.println( $f2.getName() + " is better than " + $f1.getName() );
end
Of course, it's possible that two players tie:
rule "Two scorers with equal cpability"
when
$f1: FootballPlayer( $score1; scoredGoals )
$f2: FootballPlayer( this != $f1, $score2; scoredGoals == $score1 )
then
System.out.println( $f2.getName() + " and " + $f1.getName() + " are in the same class" );
end
Note the constraint ensuring that the second one isn't idenfical to the first one! (This is the "cliff".)
You might also be interested in a rule to determine the best:
rule "The best scorer"
when
$f1: FootballPlayer( $score1; scoredGoals )
not FootballPlayer( scoredGoals > $score1 )
then
System.out.println( $$f1.getName() + " is the best" );
end