IIRC, there has been some discussion among Drools developers to add this instanceof test automatically. But right now, the instanceof is required - not necessarily in the posted way.
(1) Whether a Step object contains L as a subset of its List<DescribedAddress> or in any other way should not be exposed to the clients of this class. So, add a List<VendorAddress> getVendorAddresses() to Step and hide the dirty details where they belong. (2) With or without (1), in this particular situation, an accumulate would be indicated which determines the VendorAddress with maximum abcScore in one pass over all objects. -W On 26 August 2011 22:04, lhorton <[email protected]> wrote: > 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. > _______________________________________________ > rules-users mailing list > [email protected] > https://lists.jboss.org/mailman/listinfo/rules-users >
_______________________________________________ rules-users mailing list [email protected] https://lists.jboss.org/mailman/listinfo/rules-users
