24.10. Pet Store Example: Checkout Rules from PetStore.drl

rule "Gross Total"
    agenda-group "checkout"
    dialect "mvel"
when
   $order : Order( grossTotal == -1)
   Number( total : doubleValue )
       from accumulate( Purchase( $price : product.price ), sum( $price ) )
then
    modify( $order ) { grossTotal = total };
    textArea.append( "\ngross total=" + total + "\n" );
end

rule "Apply 5% Discount"
    agenda-group "checkout"
dialect "mvel"
when
   $order : Order( grossTotal >= 10 && < 20 )
then
   $order.discountedTotal = $order.grossTotal * 0.95;
   textArea.append( "discountedTotal total=" + $order.discountedTotal + "\n" );
end


rule "Apply 10% Discount"
    agenda-group "checkout"
    dialect "mvel"
when
   $order : Order( grossTotal >= 20 )
then
   $order.discountedTotal = $order.grossTotal * 0.90;
   textArea.append( "discountedTotal total=" + $order.discountedTotal + "\n" );
end
There are three rules in the checkout agenda-group:
  • Gross Total accumulates the product prices into a total, puts it into Working Memory, and displays it via the Swing JTextArea using the textArea global variable.
  • If the gross total is between 10 and 20, Apply 5% Discount calculates the discounted total and adds it to the Working Memory and displays it in the text area.
  • If the gross total is not less than 20, Apply 10% Discount calculates the discounted total and adds it to the Working Memory and displays it in the text area.