[rules-users] DRL Pattern match to subclass?

Kris Nuttycombe kris.nuttycombe at gmail.com
Fri Aug 7 13:57:50 EDT 2009


On Fri, Aug 7, 2009 at 10:37 AM, Kris
Nuttycombe<kris.nuttycombe at gmail.com> wrote:
> On Fri, Aug 7, 2009 at 10:33 AM, Kris
> Nuttycombe<kris.nuttycombe at gmail.com> wrote:
>> Hi, all,
>>
>> I'm trying to figure out a way to perform the following pattern match
>> in a "when" clause in a drl file. This is with Drools 4.x
>>
>> I have the following parameterized class (boilerplate omitted):
>>
>> abstract class Property<T> {
>>  public String getName() {
>>      //...
>>  }
>>
>>  public abstract T getValue();
>> }
>>
>> and subclasses such as
>>
>> class BigDecimalProperty extends Property<BigDecimal> {
>>  public BigDecimal getValue() {
>>    //...
>>  }
>> }
>>
>> I then have an interface as such:
>>
>> public interface Propertied {
>>  public Set<Property<?>> getProperties();
>> }
>>
>> and an implementing class:
>>
>> class Plan implements Propertied {
>>  //...
>> }
>>
>> In my rules file, I would like to be able to perform the following
>> pattern match:
>>
>> rule "Ensure that plan minimum commitment has been met."
>>    agenda-group "evaluate-balance"
>> when
>>    $plan: Plan()
>>    $minCommitProperty: BigDecimalProperty(name ==
>> "minimum_commitment", $minCommitment : value) from $plan.properties
>> then
>>    //...
>> end

So, I have concocted the following abomination:

rule "Ensure that plan minimum commitment has been met."
   agenda-group "evaluate-balance"
when
  $plan: Plan()
  $prop: Property(name == "minimum_commitment") from
$scheduledSegment.segment.properties
  BigDecimalProperty($minCommitment : value) from $prop.class.cast($prop)
then
  //...
end

This (unbelievably) at least compiles and passes my tests; of course,
at the moment I don't have any Property instances with a name of
"minimum_commitment" that are not instances of BigDecimalProperty. Is
there any way to at least encode an instanceof check into the Property
pattern match to eliminate the possibility of a class cast exception?
How do I reference BigDecimalProperty.class at all?

Actually, this brings me to anotherpressing question - how do I refer
to anything other than a method that obeys the bean property
convention in a from clause?
I've got a number of instances where I'd like to be able to do something like:


when
    $cont : Container()
    Value() from $cont.value()
then

where Container (a third-party class that I can't change) defines:

interface Container {
  public Value value();
}

(in this case, Container is really Option from functionaljava.org, and
value() is really some())

Is it possible to sidestep the bean convention? It seems like the
Drools compiler attempts to convert everything to getter method
without even checking whether an appropriate method exists on the
target class.

Kris




More information about the rules-users mailing list