[jboss-svn-commits] JBL Code SVN: r15274 - labs/jbossrules/trunk/documentation/manual/en/Chapter-Examples.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Sep 21 02:52:48 EDT 2007


Author: michael.neale at jboss.com
Date: 2007-09-21 02:52:48 -0400 (Fri, 21 Sep 2007)
New Revision: 15274

Modified:
   labs/jbossrules/trunk/documentation/manual/en/Chapter-Examples/Section-Examples.xml
Log:
added Shopping example

Modified: labs/jbossrules/trunk/documentation/manual/en/Chapter-Examples/Section-Examples.xml
===================================================================
--- labs/jbossrules/trunk/documentation/manual/en/Chapter-Examples/Section-Examples.xml	2007-09-21 05:19:54 UTC (rev 15273)
+++ labs/jbossrules/trunk/documentation/manual/en/Chapter-Examples/Section-Examples.xml	2007-09-21 06:52:48 UTC (rev 15274)
@@ -1209,7 +1209,145 @@
   <section>
     <title>Shopping Example</title>
 
-    <para></para>
+    <programlisting><emphasis role="bold">Name:</emphasis>Shopping Example
+<emphasis role="bold">Main class:</emphasis> org.drools.examples.ShoppingExample
+<emphasis role="bold">Type:</emphasis> java application
+<emphasis role="bold">Rules file:</emphasis> Shopping.drl
+<emphasis role="bold">Objective:</emphasis> demonstrate truth maintenance, accumulate
+</programlisting>
+
+    <para>The shopping example simulates a very simple shopping cart type
+    application, where the idea is to track a users purchases in a stateful
+    session, and apply discounts as appropriate.</para>
+
+    <section>
+      <title>Running the example</title>
+
+      <para>The following is a listing of the interesting parts that are used
+      to launch the example:</para>
+
+      <programlisting>Customer mark = new Customer( "mark",
+                                      0 );
+        session.insert( mark );
+        Product shoes = new Product( "shoes",
+                                     60 );
+        session.insert( shoes );
+        Product hat = new Product( "hat",
+                                   60 );
+        session.insert( hat );
+        session.insert( new Purchase( mark,
+                                      shoes ) );
+        FactHandle hatPurchaseHandle = session.insert( new Purchase( mark,
+                                                                     hat ) );
+        session.fireAllRules();
+        session.retract( hatPurchaseHandle );
+        System.out.println( "Customer mark has returned the hat" );
+        session.fireAllRules();
+      </programlisting>
+
+      <para>Refering the the above listing, we can see there is a Customer
+      ("mark"), and there are 2 Products ("shoes" and "hat") which are
+      available for Purchase. In this case, a Purchase combines a customer
+      with a product (and a product has a price attribute).</para>
+
+      <para>Note that after we fireAllRules(), we then retract the purchase of
+      a hat (but leave the purchase of shoes in). Running the example as a
+      java application should see the following output:</para>
+
+      <programlisting>Customer mark just purchased hat
+Customer mark just purchased shoes
+Customer mark now has a shopping total of 120.0
+Customer mark now has a discount of 10
+Customer mark has returned the hat
+Customer mark now has a discount of 0
+      </programlisting>
+    </section>
+
+    <section>
+      <title>Discounts and purchases</title>
+
+      <para>We want to give discounts to customers who purchase stuff of
+      enough value. This discount could also be removed should the customer
+      decide not to purchase enough to fall within the threshold.</para>
+
+      <programlisting>
+rule "Purchase notification"
+    salience 10
+
+ when
+  $c : Customer()
+  $p : Purchase( customer == $c)	    
+ then
+     System.out.println( "Customer " + $c.name + " just purchased " + $p.product.name );
+end 
+
+rule "Discount removed notification"
+ when
+     $c : Customer()
+  not Discount( customer == $c )
+ then
+  $c.discount = 0 ;
+  System.out.println( "Customer " + $c.name + " now has a discount of " + $c.discount );
+end
+
+rule "Discount awarded notification"
+ when
+     $c : Customer()
+     $d : Discount( customer == $c )
+ then
+  System.out.println( "Customer " + $c.name + " now has a discount of " + $d.amount );
+end
+      </programlisting>
+
+      <para>The "Purchase notification" rule simply makes note of the purchase
+      event for a given customer. The "Discount removed notification" rule
+      removes the customer discount (by checking for the non existence of a
+      discount for that customer). The "Discount awarded notification" simply
+      makes not of the fact that the discount was applied.</para>
+    </section>
+
+    <section>
+      <title>Calculating the discount</title>
+
+      <para>Calculating the discount is done with a single rule, using the
+      higher order logic of "accumulate".</para>
+
+      <programlisting>
+rule "Apply 10% discount if total purcahses is over 100"			
+ no-loop true
+ dialect "java"
+    when
+      $c : Customer()
+      $i : Double(doubleValue  &gt; 100) from accumulate ( Purchase( customer == $c, $price : product.price ), 
+                                                            sum( $price ) )
+    then
+      $c.setDiscount( 10 );
+      insertLogical( new Discount($c, 10) );	
+      System.out.println( "Customer " + $c.getName() + " now has a shopping total of " + $i );
+end
+      </programlisting>
+
+      <para>An interesting part of this rule is the "accumulate": this is
+      saying to accumulate a total (sum) of the $price of a product
+      (product.price) for all Purchase facts that belong to the customer ($c).
+      The result of this is a Double. The rule then checks to see if this
+      total is greater then 100. If it is, it applies the discount (of 10),
+      and then inserts a logical fact of the Discount object.</para>
+
+      <para>The purpose of the logical insertion of the Discount, is to
+      automatically retract the Discount object should the total of the
+      purchases not add up to &gt; 100 (when the LHS is no longer satisified,
+      restract the resulting logical assertions - this is what is meant by
+      "truth maintenance"). The act of inserting the Discount, causes the
+      "Discount awarded notification" rule to activate. However, should the
+      discount fact be retracted, the "Discount removed notification" will
+      activate, resulting in the customers discount being wiped out. In the
+      example you can see this happen, as after the first fireAllRules(), a
+      purchase is retracted, causing the total to fall below 100, which means
+      the conditions that satisfied the "Apply 10% discount..." rule no longer
+      apply, hence the logical fact of "Discount" is automatically
+      retracted.</para>
+    </section>
   </section>
 
   <section>




More information about the jboss-svn-commits mailing list