I think you missed Wolfgang's points.. this form is ***WRONG***

1) It assumes that the DiscountResult is in the working memory,
which may not be the case unless you insert it explicitly
2) it will fire for ANY DiscountResult/TripRequest combination
since there is no explicit join condition between the two.
3) Bound variables are not meant to be reassigned in the RHS.
4) Even the cast in the RHS will not save you from ClassCastExceptions,
unless it's implied that destinations "Egypt" can only have
DiscountResult as results.

You need something like this:

when
$T : TripRequest( destination == "Egypt",
                            $dest : result,
                            result#DiscountResult // same as
"instanceof"here
)
then
    ((DiscountResult) $dest).setDiscount( 5 ); 
end 


One thing that could be done - but does not work right now,
is to expand on the "#" operator, so that whenever
you write

TripRequest( $dest : result#DiscountResult )

the variable $dest is created with DiscountResult as an
inferred type, rather than its declared type IResult
As mentioned, this does not work right now... it may
be a bug (cc-ing Mario for discussion)

Best
Davide






On 04/23/2014 04:17 AM, Leonard93 wrote:
> It only works if you do it like this:
> rule "Discount Egypt"
>         salience 0
>     when
>         $T : TripRequest ( destination == "Egypt" )
>         $dest: DiscountResult()
>     then
>         $dest= (DiscountResult) $T.getResult();
>        $dest.setDiscount(5);
> end
>
>
> Since getResult() returns an instance of the interface IResult, it needs to
> be cast to the proper type first. But this approach does not feel better
> than the previous notation.
>
> But thanks for the replies.
>
>
>
>
> --
> View this message in context: 
> http://drools.46999.n3.nabble.com/Is-there-a-better-way-to-write-this-rule-tp4029327p4029333.html
> Sent from the Drools: User forum mailing list archive at Nabble.com.
> _______________________________________________
> rules-users mailing list
> rules-users@lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>

_______________________________________________
rules-users mailing list
rules-users@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users

Reply via email to