[jboss-svn-commits] JBL Code SVN: r14635 - in labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main: rules/org/drools/examples and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Aug 27 08:01:16 EDT 2007


Author: mark.proctor at jboss.com
Date: 2007-08-27 08:01:16 -0400 (Mon, 27 Aug 2007)
New Revision: 14635

Modified:
   labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/PetStore.java
   labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/PetStore.drl
Log:
-updated the example, now working.

Modified: labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/PetStore.java
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/PetStore.java	2007-08-27 11:11:53 UTC (rev 14634)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/PetStore.java	2007-08-27 12:01:16 UTC (rev 14635)
@@ -397,7 +397,8 @@
 
             //Iterate through list and add to cart
             for ( int i = 0; i < items.size(); i++ ) {
-                order.addItem( new Purchase( order, (Product) items.get( i ) ) );
+                order.addItem( new Purchase( order,
+                                             (Product) items.get( i ) ) );
             }
 
             //add the JFrame to the ApplicationData to allow for user interaction
@@ -406,6 +407,18 @@
                                      frame );
             workingMemory.setGlobal( "textArea",
                                      this.output );
+
+            workingMemory.insert( new Product( "Gold Fish",
+                                               5 ) );
+            workingMemory.insert( new Product( "Fish Tank",
+                                               25 ) );
+            workingMemory.insert( new Product( "Fish Food",
+                                               2 ) );
+            
+            workingMemory.insert( new Product( "Fish Food Sample",
+                                               0 ) );            
+           
+
             workingMemory.insert( order );
             workingMemory.fireAllRules();
 
@@ -417,23 +430,15 @@
     public static class Order {
         private List          items;
 
-        private double        discount;
+        private double        grossTotal      = -1;
+        private double        discountedTotal = -1;
 
-        private static String newline = System.getProperty( "line.separator" );
+        private static String newline         = System.getProperty( "line.separator" );
 
         public Order() {
             this.items = new ArrayList();
-            this.discount = 0;
         }
-        
-        public void setDiscount(double discount) {
-            this.discount = discount;
-        }
 
-        public double getDiscount() {
-            return this.discount;
-        }
-
         public void addItem(Purchase item) {
             this.items.add( item );
         }
@@ -442,31 +447,22 @@
             return this.items;
         }
 
-        /*
-        public double getGrossCost() {
-            Iterator itemIter = getItems().iterator();
-            Product eachItem = null;
+        public void setGrossTotal(double grossCost) {
+            this.grossTotal = grossCost;
+        }
 
-            double cost = 0.00;
+        public double getGrossTotal() {
+            return this.grossTotal;
+        }
 
-            while ( itemIter.hasNext() ) {
-                eachItem = (Product) itemIter.next();
+        public void setDiscountedTotal(double discountedCost) {
+            this.discountedTotal = discountedCost;
+        }
 
-                cost += eachItem.getPrice();
-            }
-
-            return cost;
+        public double getDiscountedTotal() {
+            return this.discountedTotal;
         }
 
-        public double getDiscountedCost() {
-            double cost = getGrossCost();
-            double discount = getDiscount();
-
-            double discountedCost = cost * (1 - discount);
-
-            return discountedCost;
-        }
-*/
         public String toString() {
             StringBuffer buf = new StringBuffer();
 
@@ -478,31 +474,57 @@
                 buf.append( "\t" + itemIter.next() + newline );
             }
 
-//            buf.append( "gross total=" + getGrossCost() + newline );
-//            buf.append( "discounted total=" + getDiscountedCost() + newline );
+            //            buf.append( "gross total=" + getGrossCost() + newline );
+            //            buf.append( "discounted total=" + getDiscountedCost() + newline );
 
             return buf.toString();
         }
     }
-    
+
     public static class Purchase {
-        private Order order;
+        private Order   order;
         private Product product;
+
         public Purchase(Order order,
                         Product product) {
             super();
             this.order = order;
             this.product = product;
         }
-        
+
         public Order getOrder() {
             return order;
         }
+
         public Product getProduct() {
             return product;
-        }                
-    }    
+        }
 
+        public int hashCode() {
+            final int PRIME = 31;
+            int result = 1;
+            result = PRIME * result + ((order == null) ? 0 : order.hashCode());
+            result = PRIME * result + ((product == null) ? 0 : product.hashCode());
+            return result;
+        }
+
+        public boolean equals(Object obj) {
+            if ( this == obj ) return true;
+            if ( obj == null ) return false;
+            if ( getClass() != obj.getClass() ) return false;
+            final Purchase other = (Purchase) obj;
+            if ( order == null ) {
+                if ( other.order != null ) return false;
+            } else if ( !order.equals( other.order ) ) return false;
+            if ( product == null ) {
+                if ( other.product != null ) return false;
+            } else if ( !product.equals( other.product ) ) return false;
+            return true;
+        }
+        
+        
+    }
+
     public static class Product {
         private String name;
 
@@ -525,8 +547,30 @@
         public String toString() {
             return name + " " + this.price;
         }
-    }
 
+        public int hashCode() {
+            final int PRIME = 31;
+            int result = 1;
+            result = PRIME * result + ((name == null) ? 0 : name.hashCode());
+            long temp;
+            temp = Double.doubleToLongBits( price );
+            result = PRIME * result + (int) (temp ^ (temp >>> 32));
+            return result;
+        }
 
+        public boolean equals(Object obj) {
+            if ( this == obj ) return true;
+            if ( obj == null ) return false;
+            if ( getClass() != obj.getClass() ) return false;
+            final Product other = (Product) obj;
+            if ( name == null ) {
+                if ( other.name != null ) return false;
+            } else if ( !name.equals( other.name ) ) return false;
+            if ( Double.doubleToLongBits( price ) != Double.doubleToLongBits( other.price ) ) return false;
+            return true;
+        }
+        
+        
+    }
 
 }

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-27 11:11:53 UTC (rev 14634)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/PetStore.drl	2007-08-27 12:01:16 UTC (rev 14635)
@@ -4,7 +4,8 @@
 import org.drools.examples.PetStore.Purchase
 import org.drools.examples.PetStore.Product
 import java.util.ArrayList
-
+import javax.swing.JOptionPane;
+        
 global javax.swing.JFrame frame 
 global javax.swing.JTextArea textArea
 
@@ -19,61 +20,103 @@
 		$item : Purchase() from $order.items
 	then
 		insert( $item );
-//		drools.getWorkingMemory().getRuleBase().removeRule("org.drools.examples", "Explode Cart" );
-		
 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"
-	dialect "java"
+    salience 8
+	dialect "mvel"
 	when
 	    $order : Order()
 		not ( $p : Product( name == "Fish Food") && Purchase( product == $p ) )
 		not ( $p : Product( name == "Fish Food Sample") && Purchase( product == $p ) )		
-		exists ( $p : Product( name == "Gold Fish") && Purchase( product == $p ) )				
+		exists ( $p : Product( name == "Gold Fish") && Purchase( product == $p ) )	
+		$fishFoodSample : Product( name == "Fish Food Sample" );
 	then
 		System.out.println( "Adding free Fish Food Sample to cart" );
-		Product product = new Product( "Fish Food Sample", 0.00 );
-		Purchase purchase = new Purchase($order, product);
+		purchase = new Purchase($order, $fishFoodSample);
 		insert( purchase );
 		$order.addItem( purchase );	
 end
-/*
+
+
 // Suggest a tank if we have bought more than 5 gold fish and dont already have one
 rule "Suggest Tank"
+       dialect "java"
 	when
-		not CartItem(name == "Fish Tank" )
-		ArrayList( size > 5 ) from collect( CartItem(name == "Gold Fish" ) )
+	    $order : Order()
+		not ( $p : Product( name == "Fish Tank") && Purchase( product == $p ) )	
+		ArrayList( $total : size > 5 ) from collect( Purchase( product.name == "Gold Fish" ) )
+		$fishTank : Product( name == "Fish Tank" )		
 	then
+
+ 	
+        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 );
+             insert( purchase );
+             $order.addItem( purchase );
+           System.out.println( "Yes" );
+       } else {
+            System.out.println( "No" );
+       }	
 end	
 
+rule "Gross Total"
+    salience -10
+    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 );    
+end
+
 rule "Apply 5% Discount"
-	dialect "java"
+	dialect "mvel"
 	when
-		$cart : ShoppingCart( grossCost >= 10 && < 20, discount < 0.05 )
+		$order : Order( grossTotal >= 10 && < 20 )
 	then
-//	    with( $cart ) { discount = 0.05 };
-		$cart.setDiscount( 0.05 );
+		$order.discountedTotal = $order.grossTotal * 0.95;
+	    textArea.append( "discountedTotal total=" + $order.discountedTotal + "\n" );		
 end
 
+
 rule "Apply 10% Discount"
-	dialect "java"
+	dialect "mvel"
 	when
-		$cart : ShoppingCart( grossCost >= 20, discount < 0.10 )
+		$order : Order( grossTotal >= 20 )
 	then
-//	    with( $cart ) { discount = 0.10 };
-		$cart.setDiscount( 0.10 );
-end
-*/
-rule "Gross Total"
-    salience -10
-    dialect "java"
-	when
-	    $order : Order()
-		Number( total : doubleValue ) from accumulate( Purchase( price : product.price ) from $order.items,
-   			        			   					   sum( price) )
-	then	
-	     textArea.append( "gross total=" + total + "\n" );
-end
-	
\ No newline at end of file
+		$order.discountedTotal = $order.grossTotal * 0.90;
+	    textArea.append( "discountedTotal total=" + $order.discountedTotal + "\n" );	
+end
\ No newline at end of file




More information about the jboss-svn-commits mailing list