[jboss-svn-commits] JBL Code SVN: r14680 - labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Aug 27 23:19:22 EDT 2007


Author: mark.proctor at jboss.com
Date: 2007-08-27 23:19:22 -0400 (Mon, 27 Aug 2007)
New Revision: 14680

Modified:
   labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/PetStore.drl
Log:
-got petstore working

Modified: labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/PetStore.drl
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/PetStore.drl	2007-08-28 02:37:04 UTC (rev 14679)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/PetStore.drl	2007-08-28 03:19:22 UTC (rev 14680)
@@ -1,42 +1,39 @@
 package org.drools.examples
 
+import org.drools.WorkingMemory
 import org.drools.examples.PetStore.Order
 import org.drools.examples.PetStore.Purchase
 import org.drools.examples.PetStore.Product
 import java.util.ArrayList
 import javax.swing.JOptionPane;
+
+import javax.swing.JFrame 
         
-global javax.swing.JFrame frame 
+global JFrame frame 
 global javax.swing.JTextArea textArea
 
 dialect "mvel"
 
 // insert each item in the shopping cart into the Working Memory 
 rule "Explode Cart"
+    agenda-group "init"
+	auto-focus true    
     salience 10
 	dialect "java"
 	when
-	    $order : Order()
+	    $order : Order( grossTotal == -1 )
 		$item : Purchase() from $order.items
 	then
 		insert( $item );
+		drools.setFocus( "show items" );		
+		drools.setFocus( "evaluate" );		
 end
 
-// insert each item in the shopping cart into the Working Memory 
-rule "Remove Explode Cart"
-    salience 9
-	dialect "java"
-	when
-	then
-		drools.getWorkingMemory().getRuleBase().removeRule("org.drools.examples", "Explode Cart" );		
-		drools.getWorkingMemory().getRuleBase().removeRule("org.drools.examples", "Remove Explode Cart" );				
-end
 
-
 // Free Fish Food sample when we buy a Gold Fish if we haven't already  bought 
 // Fish Food and dont already have a Fish Food Sample
 rule "Free Fish Food Sample"
-    salience 8
+    agenda-group "evaluate"
 	dialect "mvel"
 	when
 	    $order : Order()
@@ -54,6 +51,7 @@
 
 // Suggest a tank if we have bought more than 5 gold fish and dont already have one
 rule "Suggest Tank"
+       agenda-group "evaluate"
        dialect "java"
 	when
 	    $order : Order()
@@ -61,48 +59,40 @@
 		ArrayList( $total : size > 5 ) from collect( Purchase( product.name == "Gold Fish" ) )
 		$fishTank : Product( name == "Fish Tank" )		
 	then
+		requireTank(frame, drools.getWorkingMemory(), $order, $fishTank, $total); 		
+end	
 
- 	
-        Object[] options = {"Yes",
-                            "No"};
-                            
-        int n = JOptionPane.showOptionDialog(frame,
-                                             "Would you like to buy a tank for your " + $total + " fish?",
-                                             "Purchase Suggestion",
-                                             JOptionPane.YES_NO_OPTION,
-                                             JOptionPane.QUESTION_MESSAGE,
-                                             null,
-                                             options,
-                                             options[0]);
-                                             
-       System.out.print( "SUGGESTION: Would you like to buy a tank for your "
-  	                     + $total + " fish? - " );
+rule "Show Items"
+    agenda-group "show items"
+    dialect "mvel"
+	when
+	    $order : Order( )
+	    $p : Purchase( order == $order )
+	then	
+		textArea.append( $p.product + "\n");
+end
 
-       if (n == 0) {
-             Purchase purchase = new Purchase( $order, $fishTank );
-             insert( purchase );
-             $order.addItem( purchase );
-           System.out.println( "Yes" );
-       } else {
-            System.out.println( "No" );
-       }	
-end	
+rule "do checkout"
+	    dialect "java"
+    when
+    then
+        doCheckout(frame, drools.getWorkingMemory());
+end    
 
 rule "Gross Total"
-    salience -10
+    agenda-group "checkout"
     dialect "mvel"
-    no-loop
 	when
 	    $order : Order( grossTotal == -1)
 		Number( total : doubleValue ) from accumulate( Purchase( $price : product.price ) from $order.items,
    			        			   				 	   sum( $price ) )
 	then	
-	    $order.grossTotal = total;
-	    textArea.append( "gross total=" + $order.grossTotal + "\n" );	    
-	    update( $order );    
+	    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 )
@@ -113,10 +103,56 @@
 
 
 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
\ No newline at end of file
+end
+
+function void doCheckout(JFrame frame, WorkingMemory workingMemory) {
+        Object[] options = {"Yes",
+                            "No"};
+                            
+        int n = JOptionPane.showOptionDialog(frame,
+                                             "Would you like to checkout?",
+                                             "",
+                                             JOptionPane.YES_NO_OPTION,
+                                             JOptionPane.QUESTION_MESSAGE,
+                                             null,
+                                             options,
+                                             options[0]);
+
+       if (n == 0) {
+ 	       workingMemory.setFocus( "checkout" );
+       }   
+}
+
+function boolean requireTank(JFrame frame, WorkingMemory workingMemory, Order order, Product fishTank, int total) {
+        Object[] options = {"Yes",
+                            "No"};
+                            
+        int n = JOptionPane.showOptionDialog(frame,
+                                             "Would you like to buy a tank for your " + total + " fish?",
+                                             "Purchase Suggestion",
+                                             JOptionPane.YES_NO_OPTION,
+                                             JOptionPane.QUESTION_MESSAGE,
+                                             null,
+                                             options,
+                                             options[0]);
+                                             
+       System.out.print( "SUGGESTION: Would you like to buy a tank for your "
+  	                     + total + " fish? - " );
+
+       if (n == 0) {
+             Purchase purchase = new Purchase( order, fishTank );
+             workingMemory.insert( purchase );
+             order.addItem( purchase );
+             System.out.println( "Yes" );
+       } else {
+            System.out.println( "No" );
+       }      
+       return true;
+}
\ No newline at end of file




More information about the jboss-svn-commits mailing list