[rules-users] Getting one type from a collection of multiple types

lhorton LHorton at ABCLegal.com
Fri Aug 26 16:04:21 EDT 2011


The following code works, but I think it's ugly.  

the classes are:
DescribedAddress is a base class for addresses
VendorAddress extends DescribedAddress and implements method abcScore()
Step getLocations() is defined as a list of DescribedAddress objects but it
can also contain VendorAddress objects since they are a kind of
DescribedAddress

the LHS of the rule below extracts the VendorAddress with the highest
abcScore from step.locations 

if I omit the eval(), i get ClassCastException on any DescribedAddress in
locations, since DescribedAddress cannot be cast to VendorAddress.

is there a more elegant / clean / efficient way to get only the
VendorAddress objects from locations?

rule "Find highest scored address"
dialect "mvel"
when
        $step : Step()
	$location : VendorAddress (eval($location instanceof VendorAddress ),
abcScore>9) from $step.getLocations()

        not $addr : VendorAddress (eval($addr instanceof VendorAddress
),abcScore > $location.abcScore) from $step.getLocations()

then
	System.out.println("high score is: " + $location.getAbcScore());
end

--
View this message in context: http://drools.46999.n3.nabble.com/Getting-one-type-from-a-collection-of-multiple-types-tp3287877p3287877.html
Sent from the Drools: User forum mailing list archive at Nabble.com.



More information about the rules-users mailing list