Este contenido no está disponible en el idioma seleccionado.
Chapter 27. Pet Store Example
27.1. Pet Store Example Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
All of the Java code for the Pet Store Example is contained in the file
PetStore.java. It defines the following principal classes (in addition to several classes to handle Swing Events):
Petstorecontains themain()method.PetStoreUIis responsible for creating and displaying the Swing based GUI. It contains several smaller classes, mainly for responding to various GUI events such as mouse button clicks.TableModelholds the table data. It is a JavaBean that extends the Swing classAbstractTableModel.CheckoutCallbackallows the GUI to interact with the Rules.Ordershowkeeps the items that the customer wishes to buy.Purchasestores details of the order and the products the customer is buying.Productis a JavaBean holding details of the product available for purchase and its price.
27.2. Pet Store Example: Creating the PetStore RuleBase in PetStore.main Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
- The code shown above loads the rules from a DRL file on the classpath. It does so via the second last line where a
PetStoreUIobject is created using a constructor. This accepts theVectorobjectstockthat collects the products. - The
CheckoutCallbackclass contains the Rule Base that has been loaded.
27.3. Pet Store Example: Firing Rules from CheckoutCallBack.checkout() Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
- The Java code that fires the rules is within the
CheckoutCallBack.checkout()method. This is triggered (eventually) when the Checkout button is pressed by the user. - Two items get passed into this method. One is the handle to the
JFrameSwing component surrounding the output text frame, at the bottom of the GUI. The second is a list of order items. This comes from theTableModelstoring the information from the "Table" area at the top right section of the GUI. - The for loop transforms the list of order items coming from the GUI into the
OrderJavaBean, also contained in the filePetStore.java. - All states in this example are stored in the Swing components. The rules are effectively stateless.
- Each time the "Checkout" button is pressed, the code copies the contents of the Swing
TableModelinto the Session's Working Memory. - There are nine calls to the Working Memory. The first creates a new Working Memory as a Stateful Knowledge Session from the Knowledge Base. The next two pass in two objects that will be held as global variables in the rules. The Swing text area and the Swing frame used for writing messages.
- More inserts put information on products into the Working Memory and the order list. The final call is the standard
fireAllRules().
27.4. Pet Store Example: Package, Imports, Globals and Dialect from PetStore.drl Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
- The first part of file
PetStore.drlcontains the standard package and import statements to make various Java classes available to the rules. - The two globals
frameandtextAreahold references to the Swing componentsJFrameandJTextAreacomponents that were previously passed on by the Java code calling thesetGlobal()method. These global variables retain their value for the lifetime of the Session.
27.5. Pet Store Example: Java Functions in the Rules Extracted from PetStore.drl Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
- Having these functions in the rules file makes the Pet Store example more compact.
- You can have the functions in a file of their own, within the same rules package, or as a static method on a standard Java class, and import them using
import function my.package.Foo.hello. doCheckout()displays a dialog asking users whether they wish to checkout. If they do, focus is set to thecheckOutagenda-group, allowing rules in that group to (potentially) fire.requireTank()displays a dialog asking users whether they wish to buy a tank. If so, a new fish tankProductis added to the order list in Working Memory.
27.6. Pet Store Example: Putting Items Into Working Memory from PetStore.drl Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
- The first extract fires first because it has the
auto-focusattribute set totrue. - This rule matches against all orders that do not yet have their
grossTotalcalculated . It loops for each purchase item in that order. Some parts of the "Explode Cart" rule should be familiar: the rule name, the salience (suggesting the order for the rules being fired) and the dialect set tojava. agenda-groupinitdefines the name of the agenda group. In this case, there is only one rule in the group. However, neither the Java code nor a rule consequence sets the focus to this group, and therefore it relies on the next attribute for its chance to fire.auto-focustrueensures that this rule, while being the only rule in the agenda group, can fire whenfireAllRules()is called from the Java code.kcontext....setFocus()sets the focus to theshow itemsandevaluateagenda groups in turn, permitting their rules to fire. In practice, you can loop through all items on the order, inserting them into memory, then firing the other rules after each insert.
27.7. Pet Store Example: Show Items in the GUI from PetStore.drl Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
- The
show itemsagenda-group has only one rule, called "Show Items" (note the difference in case). For each purchase on the order currently in the Working Memory (or Session), it logs details to the text area at the bottom of the GUI. ThetextAreavariable used for this is a global variables. - The
evaluateAgenda group also gains focus from theExplode Cartrule.
27.8. Pet Store Example: Evaluate Agenda Group from PetStore.drl Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
The rule
"Free Fish Food Sample" will only fire if:
- The store does not already have any fish food, and
- The store does not already have a free fish food sample, and
- The store does have a Gold Fish in its order.
The rule
"Suggest Tank" will only fire if
- The store does notalready have a Fish Tank in its order, and
- The store does have more than five Gold Fish Products in its order.
- If the rule does fire, it calls the
requireTank()function . This shows a Dialog to the user, and adding a Tank to the order and Working Memory if confirmed. - When calling the requireTank() function the rule passes the global frame variable so that the function has a handle to the Swing GUI.
- If the rule does fire, it creates a new product (Fish Food Sample), and adds it to the order in Working Memory.
27.9. Pet Store Example: Doing the Checkout Extract from PetStore.drl Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
- The rule
"do checkout"has no agenda group set and no auto-focus attribute. As such, it is deemed part of the default (MAIN) agenda group. This group gets focus by default when all the rules in agenda-groups that explicitly had focus set to them have run their course. - There is no LHS to the rule, so the RHS will always call the
doCheckout()function. - When calling the
doCheckout()function, the rule passes the globalframevariable to give the function a handle to the Swing GUI. - The
doCheckout()function shows a confirmation dialog to the user. If confirmed, the function sets the focus to the checkout agenda-group, allowing the next lot of rules to fire.
27.10. Pet Store Example: Checkout Rules from PetStore.drl Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
There are three rules in the checkout agenda-group:
Gross Totalaccumulates the product prices into a total, puts it into Working Memory, and displays it via the SwingJTextAreausing thetextAreaglobal variable.- If the gross total is between 10 and 20,
Apply 5% Discountcalculates 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% Discountcalculates the discounted total and adds it to the Working Memory and displays it in the text area.
27.11. Pet Store Example: Running PetStore.java Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
To use PetStore.java, the following conditions must be met:
- The
main()method has run and loaded the Rule Base but not yet fired the rules. So far, this is the only code in connection with rules that has been run. - A new
PetStoreUIobject has been created and given a handle to the Rule Base, for later use. - Swing components are deployed and the console waits for user input.
- The file
PetStore.javacontains amain()method, so that it can be run as a standard Java application, either from the command line or via the IDE. This assumes you have your classpath set correctly. - The first screen that appears is the Pet Store Demo. It has a list of available products, an empty list of selected products, checkout and reset buttons, and an empty system messages area.
Pressing the "Checkout" button fires the business rules:
- Method
CheckOutCallBack.checkout()is called by the Swing class waiting for the click on the "Checkout" button. This inserts the data from theTableModelobject and inserts it into the Session's Working Memory. It then fires the rules. - The first rule to fire will be the one with
auto-focusset to true. It loops through all the products in the cart, ensures that the products are in the Working Memory, and then gives theShow ItemsandEvaluationagenda groups a chance to fire. The rules in these groups add the contents of the cart to the text area (at the bottom of the window), decide whether or not to give the user free fish food, and to ask us whether they want to buy a fish tank.
27.12. Pet Store Example: The Do Checkout Rule Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
- The Do Checkout rule is part of the default (MAIN) agenda group. It always calls the doCheckout() functionwhich displays a 'Would you like to Checkout?' dialog box.
- The
doCheckout()function sets the focus to thecheckoutagenda-group, giving the rules in that group the option to fire. - The rules in the
checkoutagenda-group display the contents of the cart and apply the appropriate discount. - Swing then waits for user input to either checkout more products (and to cause the rules to fire again), or to close the GUI.