Is there a better way to define the follow rule?
Following rule checks to see if a person is Overweight (by calling the greaterThan method defined in the Weight class).
Rule:
rule "OverWeightTest"
dialect "mvel"
when
$person : Person(weight.greaterThan(new Weight(200, Weight.Unit.LBS)) )
then
System.out.println($person + " is overweight!");
end
Java Classes:
public class Person
{
private final String name;
private final Weight weight;
}
public class Weight
{
private final int value;
private final Unit unit;
public boolean greaterThan(final Weight otherWeight) {
...
}
}
Thanks!!
Even if @Laune's answer is the most clean and follows all the best practices, I'll leave this alternative way you have to instantiate objects in the LHS of your rules (maybe because some of the necessary parameters in the constructor comes from a variable bound to a fact).
rule "OverWeightTest"
dialect "mvel"
when
$w: Weight() from new Weight(200, Weight.Unit.LBS)
$person : Person( weight.greaterThan($w) )
then
System.out.println($person + " is overweight!");
end
Again, I do agree that the rule above will be considered an unholy monstrosity by production rule systems purists!
Hope it helps,