<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><a href="http://blog.athico.com/2012/09/conditional-named-consequences-in.html">http://blog.athico.com/2012/09/conditional-named-consequences-in.html</a><div><br></div><div><h3 class="post-title entry-title" style="margin: 0px; position: relative; font-weight: normal; font-size: 18px; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; color: rgb(51, 51, 51); background-color: rgb(255, 255, 255); z-index: auto; ">(Conditional) Named consequences in Drools 5.5</h3><div class="post-header" style="line-height: 1.6; margin: 0px 0px 1.5em; color: rgb(153, 153, 153); font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 14px; background-color: rgb(255, 255, 255); position: static; z-index: auto; "><div class="post-header-line-1"><span class="post-author vcard">Posted by&nbsp;<span class="fn">Mario Fusco</span></span></div></div><div class="post-body entry-content" style="width: 748px; line-height: 1.4; font-size: 15px; position: relative; color: rgb(51, 51, 51); font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; background-color: rgb(255, 255, 255); z-index: auto; ">Until now Drools rules have been always expressed in the form:<br><pre class="">rule "name"
when
    LHS (conditional element)
then
    RHS (consequence)
end</pre>Sometimes this could be somewhat limiting and leads to verbose and difficult to be maintained repetitions like in the following example:<br><pre class="">rule "Give 10% discount to customers older than 60"
when
    $customer : Customer( age &gt; 60 )
then
    modify($customer) { setDiscount( 0.1 ) };
end

rule "Give free parking to customers older than 60"
when
    $customer : Customer( age &gt; 60 )
    $car : Car ( owner == $customer )
then
    modify($car) { setFreeParking( true ) };
end
</pre>It is already possible to partially overcome this problem by making the second rule extending the first one like in:<br><pre class="">rule "Give 10% discount to customers older than 60"
when
    $customer : Customer( age &gt; 60 )
then
    modify($customer) { setDiscount( 0.1 ) };
end

rule "Give free parking to customers older than 60"
    extends "Give 10% discount to customers older than 60"
when
    $car : Car ( owner == $customer )
then
    modify($car) { setFreeParking( true ) };
end</pre>Anyway, starting from Drools 5.5, it is possible to define more labelled consequences other than the default one in a single rule, so, for example, the 2 former rules can be compacted in only one like it follows:<br><pre class="">rule "Give 10% discount and free parking to customers older than 60"
when
    $customer : Customer( age &gt; 60 )
    do[giveDiscount]
    $car : Car ( owner == $customer )
then
    modify($car) { setFreeParking( true ) };
then[giveDiscount]
    modify($customer) { setDiscount( 0.1 ) };
end</pre>This last rule has 2 consequences, the usual default one, plus another one named "giveDiscount" that is activated, using the keyword&nbsp;<b>do</b>, as soon as a customer older than 60 is found in the knowledge base, regardless of the fact that he owns a car or not. The activation of a named consequence can be also guarded by an additional condition like in this further example:<br><pre class="">rule "Give free parking to customers older than 60 and 10% discount to golden ones among them"
when
    $customer : Customer( age &gt; 60 )
    if ( type == "Golden" ) do[giveDiscount]
    $car : Car ( owner == $customer )
then
    modify($car) { setFreeParking( true ) };
then[giveDiscount]
    modify($customer) { setDiscount( 0.1 ) };
end</pre>The condition in the&nbsp;<b>if</b>&nbsp;statement is always evaluated on the pattern immediately preceding it. In the end this last, a bit more complicated, example shows how it is possible to switch over different conditions using a nested&nbsp;<b>if/else</b>statement:&nbsp;<br><pre class="">rule "Give free parking and 10% discount to over 60 Golden customer and 5% to Silver ones"
when
    $customer : Customer( age &gt; 60 )
    if ( type == "Golden" ) do[giveDiscount10]
    else if ( type == "Silver" ) break[giveDiscount5]
    $car : Car ( owner == $customer )
then
    modify($car) { setFreeParking( true ) };
then[giveDiscount10]
    modify($customer) { setDiscount( 0.1 ) };
then[giveDiscount5]
    modify($customer) { setDiscount( 0.05 ) };
end</pre>Here I wanted to give a 10% discount AND a free parking to Golden customers over 60, but only a 5% discount (without free parking) to the Silver ones. I achieved this result by activating the consequence named "giveDiscount5" using the keyword&nbsp;<b>break</b>&nbsp;instead of&nbsp;<b>do</b>. In fact&nbsp;<b>do</b>&nbsp;just schedules a consequence in the agenda, allowing the remaining part of the LHS to continue of being evaluated as per normal, while&nbsp;<b>break</b>&nbsp;also blocks any further pattern matching evaluation. Note, of course, that the activation of a named consequence not guarded by any condition with&nbsp;<b>break</b>&nbsp;doesn't make sense (and generates a compile time error) since otherwise the LHS part following it would be never reachable.</div></div></body></html>