<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <div class="moz-cite-prefix">I think you missed Wolfgang's points..
      this form is <b>**WRONG**</b> <br>
      <br>
      1) It assumes that the DiscountResult is in the working memory, <br>
      which may not be the case unless you insert it explicitly<br>
      2) it will fire for ANY DiscountResult/TripRequest combination<br>
      since there is no explicit join condition between the two.<br>
      3) Bound variables are not meant to be reassigned in the RHS.<br>
      4) Even the cast in the RHS will not save you from
      ClassCastExceptions,<br>
      unless it's implied that destinations "Egypt" can only have <br>
      DiscountResult as results.<br>
      <br>
      You need something like this:<br>
      <tt><br>
      </tt><tt>when</tt><tt><br>
      </tt><tt>$T : TripRequest( destination == "Egypt", </tt><tt><br>
      </tt><tt>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </tt><tt>$dest : result, </tt><tt><br>
      </tt><tt>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; result#DiscountResult // same
        as "instanceof"</tt><tt> here<br>
      </tt><tt>) </tt><tt><br>
      </tt><tt>then</tt><tt><br>
      </tt><tt>&nbsp;&nbsp;&nbsp; ((DiscountResult) $dest).setDiscount( 5 );&nbsp; </tt><tt><br>
      </tt><tt>end&nbsp; </tt><tt><br>
      </tt><br>
      <br>
      One thing that could be done - but does not work right now, <br>
      is to expand on the "#" operator, so that whenever<br>
      you write<br>
      <br>
      <font face="monospace">TripRequest( $dest : result#DiscountResult
        )</font><br>
      <br>
      the variable $dest is created with DiscountResult as an<br>
      inferred type, rather than its declared type IResult<br>
      As mentioned, this does not work right now... it may<br>
      be a bug (cc-ing Mario for discussion)<br>
      <br>
      Best<br>
      Davide<br>
      <br>
      <br>
      <br>
      <br>
      <br>
      <br>
      On 04/23/2014 04:17 AM, Leonard93 wrote:<br>
    </div>
    <blockquote cite="mid:1398251871123-4029333.post@n3.nabble.com"
      type="cite">
      <pre wrap="">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: <a class="moz-txt-link-freetext" href="http://drools.46999.n3.nabble.com/Is-there-a-better-way-to-write-this-rule-tp4029327p4029333.html">http://drools.46999.n3.nabble.com/Is-there-a-better-way-to-write-this-rule-tp4029327p4029333.html</a>
Sent from the Drools: User forum mailing list archive at Nabble.com.
_______________________________________________
rules-users mailing list
<a class="moz-txt-link-abbreviated" href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a>
<a class="moz-txt-link-freetext" href="https://lists.jboss.org/mailman/listinfo/rules-users">https://lists.jboss.org/mailman/listinfo/rules-users</a>

</pre>
    </blockquote>
    <br>
  </body>
</html>