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

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Sun Aug 26 14:11:25 EDT 2007


Author: mark.proctor at jboss.com
Date: 2007-08-26 14:11:24 -0400 (Sun, 26 Aug 2007)
New Revision: 14602

Modified:
   labs/jbossrules/trunk/drools-examples/drools-examples-drl/.classpath
   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:
-more updates to the petstore example.

Modified: labs/jbossrules/trunk/drools-examples/drools-examples-drl/.classpath
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/.classpath	2007-08-26 18:07:18 UTC (rev 14601)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/.classpath	2007-08-26 18:11:24 UTC (rev 14602)
@@ -6,7 +6,10 @@
 	<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
 	<classpathentry excluding="**/*.java" kind="src" output="target/test-classes" path="src/test/resources"/>
 	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
-	<classpathentry kind="con" path="DROOLS/JBoss Rules"/>	
+	<classpathentry combineaccessrules="false" kind="src" path="/mvel"/>
+	<classpathentry combineaccessrules="false" kind="src" path="/drools-compiler"/>
+	<classpathentry combineaccessrules="false" kind="src" path="/drools-core"/>
+	<classpathentry kind="con" path="DROOLS/JBoss Rules"/>
 	<classpathentry kind="lib" path="lib/foxtrot-2.0.jar"/>
 	<classpathentry kind="lib" path="lib/jgoodies-forms-1.0.4.jar"/>
 	<classpathentry kind="output" path="target/classes"/>

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-26 18:07:18 UTC (rev 14601)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/PetStore.java	2007-08-26 18:11:24 UTC (rev 14602)
@@ -41,27 +41,27 @@
 
     public static void main(String[] args) {
         try {
-//            RuleSetLoader ruleSetLoader = new RuleSetLoader();
-//            ruleSetLoader.addFromUrl( PetStore.class.getResource( args[0] ) );
-//
-//            RuleBaseLoader ruleBaseLoader = new RuleBaseLoader();
-//            ruleBaseLoader.addFromRuleSetLoader( ruleSetLoader );
-//            RuleBase ruleBase = ruleBaseLoader.buildRuleBase();
-            
-            PackageBuilder builder = new PackageBuilder( );
-            builder.addPackageFromDrl(  new InputStreamReader( PetStore.class.getResourceAsStream( "PetStore.drl" ) ) );
+            //            RuleSetLoader ruleSetLoader = new RuleSetLoader();
+            //            ruleSetLoader.addFromUrl( PetStore.class.getResource( args[0] ) );
+            //
+            //            RuleBaseLoader ruleBaseLoader = new RuleBaseLoader();
+            //            ruleBaseLoader.addFromRuleSetLoader( ruleSetLoader );
+            //            RuleBase ruleBase = ruleBaseLoader.buildRuleBase();
+
+            PackageBuilder builder = new PackageBuilder();
+            builder.addPackageFromDrl( new InputStreamReader( PetStore.class.getResourceAsStream( "PetStore.drl" ) ) );
             RuleBase ruleBase = RuleBaseFactory.newRuleBase();
             ruleBase.addPackage( builder.getPackage() );
-            
+
             //RuleB
 
             Vector stock = new Vector();
-            stock.add( new CartItem( "Gold Fish",
-                                     5 ) );
-            stock.add( new CartItem( "Fish Tank",
-                                     25 ) );
-            stock.add( new CartItem( "Fish Food",
-                                     2 ) );
+            stock.add( new Product( "Gold Fish",
+                                    5 ) );
+            stock.add( new Product( "Fish Tank",
+                                    25 ) );
+            stock.add( new Product( "Fish Food",
+                                    2 ) );
 
             //The callback is responsible for populating working memory and
             // fireing all rules
@@ -72,128 +72,7 @@
             e.printStackTrace();
         }
     }
-    
-    public static class ShoppingCart
-    {
-        private List          items;
 
-        private double        discount;
-
-        private Map           states;
-
-        private static String newline = System.getProperty( "line.separator" );
-
-        public ShoppingCart()
-        {
-            states = new HashMap( );
-            this.items = new ArrayList( );
-            this.discount = 0;
-        }
-
-        public boolean getState(String state)
-        {
-            if ( states.containsKey( state ) )
-            {
-                return ( ( Boolean ) states.get( state ) ).booleanValue( );
-            }
-            else
-            {
-                return false;
-            }
-        }
-
-        public void setState(String state, boolean value)
-        {
-            states.put( state, new Boolean( value ) );
-        }
-
-        public void setDiscount(double discount)
-        {
-            this.discount = discount;
-        }
-
-        public double getDiscount()
-        {
-            return this.discount;
-        }
-
-        public void addItem(CartItem item)
-        {
-            this.items.add( item );
-        }
-
-        public List getItems()
-        {
-            return this.items;
-        }
-
-        public List getItems(String name)
-        {
-            ArrayList matching = new ArrayList( );
-
-            Iterator itemIter = getItems( ).iterator( );
-            CartItem eachItem = null;
-
-            while ( itemIter.hasNext( ) )
-            {
-                eachItem = ( CartItem ) itemIter.next( );
-
-                if ( eachItem.getName( ).equals( name ) )
-                {
-                    matching.add( eachItem );
-                }
-            }
-
-            return matching;
-        }
-
-        public double getGrossCost()
-        {
-            Iterator itemIter = getItems( ).iterator( );
-            CartItem eachItem = null;
-
-            double cost = 0.00;
-
-            while ( itemIter.hasNext( ) )
-            {
-                eachItem = ( CartItem ) itemIter.next( );
-
-                cost += eachItem.getCost( );
-            }
-
-            return cost;
-        }
-
-        public double getDiscountedCost()
-        {
-            double cost = getGrossCost( );
-            double discount = getDiscount( );
-
-            double discountedCost = cost * ( 1 - discount );
-
-            return discountedCost;
-        }
-
-        public String toString()
-        {
-            StringBuffer buf = new StringBuffer( );
-
-            buf.append( "ShoppingCart:" + newline );
-
-            Iterator itemIter = getItems( ).iterator( );
-
-            while ( itemIter.hasNext( ) )
-            {
-                buf.append( "\t" + itemIter.next( ) + newline );
-            }
-
-            buf.append( "gross total=" + getGrossCost( ) + newline );
-            buf.append( "discounted total=" + getDiscountedCost( ) + newline );
-
-            return buf.toString( );
-        }
-    }    
-
     /**
      * This swing UI is used to create a simple shopping cart to allow a user to add
      * and remove items from a shopping cart before doign a checkout upon doing a
@@ -319,7 +198,7 @@
                                                       ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED );
             bottomHalf.add( outputPane,
                             BorderLayout.CENTER );
-            
+
             this.callback.setOutput( this.output );
         }
 
@@ -346,7 +225,7 @@
         private class ListSelectionHandler extends MouseAdapter {
             public void mouseReleased(MouseEvent e) {
                 JList jlist = (JList) e.getSource();
-                tableModel.addItem( (CartItem) jlist.getSelectedValue() );
+                tableModel.addItem( (Product) jlist.getSelectedValue() );
             }
         }
 
@@ -369,10 +248,10 @@
             public void mouseReleased(MouseEvent e) {
                 JButton button = (JButton) e.getComponent();
                 try {
-//                    output.append( callback.checkout( (JFrame) button.getTopLevelAncestor(),
-//                                                      tableModel.getItems() ) );
+                    //                    output.append( callback.checkout( (JFrame) button.getTopLevelAncestor(),
+                    //                                                      tableModel.getItems() ) );
                     callback.checkout( (JFrame) button.getTopLevelAncestor(),
-                                        tableModel.getItems() );                    
+                                       tableModel.getItems() );
                 } catch ( org.drools.FactException fe ) {
                     fe.printStackTrace();
                 }
@@ -401,7 +280,7 @@
             }
 
             public void setValue(Object object) {
-                CartItem item = (CartItem) object;
+                Product item = (Product) object;
                 setText( item.getName() );
             }
         }
@@ -415,8 +294,8 @@
             }
 
             public void setValue(Object object) {
-                CartItem item = (CartItem) object;
-                setText( Double.toString( item.getCost() ) );
+                Product item = (Product) object;
+                setText( Double.toString( item.getPrice() ) );
             }
         }
     }
@@ -455,10 +334,10 @@
         }
 
         public Class getColumnClass(int c) {
-            return CartItem.class;
+            return Product.class;
         }
 
-        public void addItem(CartItem item) {
+        public void addItem(Product item) {
             items.add( item );
             fireTableRowsInserted( items.size(),
                                    items.size() );
@@ -493,13 +372,13 @@
      *  
      */
     public static class CheckoutCallback {
-        RuleBase ruleBase;
+        RuleBase  ruleBase;
         JTextArea output;
-        
+
         public CheckoutCallback(RuleBase ruleBase) {
             this.ruleBase = ruleBase;
         }
-        
+
         public void setOutput(JTextArea output) {
             this.output = output;
         }
@@ -514,48 +393,140 @@
          */
         public String checkout(JFrame frame,
                                List items) throws FactException {
-            ShoppingCart cart = new ShoppingCart();
+            Order order = new Order();
 
             //Iterate through list and add to cart
             for ( int i = 0; i < items.size(); i++ ) {
-                cart.addItem( (CartItem) items.get( i ) );
+                order.addItem( new Purchase( order, (Product) items.get( i ) ) );
             }
 
             //add the JFrame to the ApplicationData to allow for user interaction
             WorkingMemory workingMemory = ruleBase.newStatefulSession();
             workingMemory.setGlobal( "frame",
-                                      frame );
-            workingMemory.setGlobal( "textArea", this.output);
-            workingMemory.insert( cart );
+                                     frame );
+            workingMemory.setGlobal( "textArea",
+                                     this.output );
+            workingMemory.insert( order );
             workingMemory.fireAllRules();
 
             //returns the state of the cart
-            return cart.toString();
+            return order.toString();
         }
     }
 
-    public static class CartItem {
+    public static class Order {
+        private List          items;
+
+        private double        discount;
+
+        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 );
+        }
+
+        public List getItems() {
+            return this.items;
+        }
+
+        /*
+        public double getGrossCost() {
+            Iterator itemIter = getItems().iterator();
+            Product eachItem = null;
+
+            double cost = 0.00;
+
+            while ( itemIter.hasNext() ) {
+                eachItem = (Product) itemIter.next();
+
+                cost += eachItem.getPrice();
+            }
+
+            return cost;
+        }
+
+        public double getDiscountedCost() {
+            double cost = getGrossCost();
+            double discount = getDiscount();
+
+            double discountedCost = cost * (1 - discount);
+
+            return discountedCost;
+        }
+*/
+        public String toString() {
+            StringBuffer buf = new StringBuffer();
+
+            buf.append( "ShoppingCart:" + newline );
+
+            Iterator itemIter = getItems().iterator();
+
+            while ( itemIter.hasNext() ) {
+                buf.append( "\t" + itemIter.next() + newline );
+            }
+
+//            buf.append( "gross total=" + getGrossCost() + newline );
+//            buf.append( "discounted total=" + getDiscountedCost() + newline );
+
+            return buf.toString();
+        }
+    }
+    
+    public static class Purchase {
+        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 static class Product {
         private String name;
 
-        private double cost;
+        private double price;
 
-        public CartItem(String name,
-                        double cost) {
+        public Product(String name,
+                       double cost) {
             this.name = name;
-            this.cost = cost;
+            this.price = cost;
         }
 
         public String getName() {
             return this.name;
         }
 
-        public double getCost() {
-            return this.cost;
+        public double getPrice() {
+            return this.price;
         }
 
         public String toString() {
-            return name + " " + this.cost;
+            return name + " " + this.price;
         }
-
     }
+
+
+
 }

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-26 18:07:18 UTC (rev 14601)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/PetStore.drl	2007-08-26 18:11:24 UTC (rev 14602)
@@ -1,7 +1,8 @@
 package org.drools.examples
 
-import org.drools.examples.PetStore.ShoppingCart
-import org.drools.examples.PetStore.CartItem
+import org.drools.examples.PetStore.Order
+import org.drools.examples.PetStore.Purchase
+import org.drools.examples.PetStore.Product
 import java.util.ArrayList
 
 global javax.swing.JFrame frame 
@@ -14,11 +15,11 @@
     salience 10
 	dialect "java"
 	when
-	    $cart : ShoppingCart()
-		$item : CartItem() from $cart.getItems()
+	    $order : Order()
+		$item : Purchase() from $order.items
 	then
 		insert( $item );
-		drools.getWorkingMemory().getRuleBase().removeRule("org.drools.examples", "Explode Cart" );
+//		drools.getWorkingMemory().getRuleBase().removeRule("org.drools.examples", "Explode Cart" );
 		
 end
 
@@ -27,15 +28,18 @@
 rule "Free Fish Food Sample"
 	dialect "java"
 	when
-	    $cart : ShoppingCart()
-		not CartItem(name == "Fish Food")
- 	    not CartItem(name == "Fish Food Sample")
- 	    exists CartItem(name == "Gold Fish" )
+	    $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 ) )				
 	then
 		System.out.println( "Adding free Fish Food Sample to cart" );
-		$cart.addItem( new CartItem( "Fish Food Sample", 0.00 ) );	
+		Product product = new Product( "Fish Food Sample", 0.00 );
+		Purchase purchase = new Purchase($order, product);
+		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"
 	when
@@ -61,13 +65,14 @@
 //	    with( $cart ) { discount = 0.10 };
 		$cart.setDiscount( 0.10 );
 end
-
+*/
 rule "Gross Total"
     salience -10
     dialect "java"
 	when
-		Double( total : doubleValue ) from accumulate( CartItem( cost : cost)
-   			        			   					   sum( cost) )
+	    $order : Order()
+		Number( total : doubleValue ) from accumulate( Purchase( price : product.price ) from $order.items,
+   			        			   					   sum( price) )
 	then	
 	     textArea.append( "gross total=" + total + "\n" );
 end




More information about the jboss-svn-commits mailing list