-
Language:
English
-
Language:
English
24.3. Pet Store Example: Firing Rules from CheckoutCallBack.checkout()
public String checkout(JFrame frame, List<Product> items) { Order order = new Order(); // Iterate through list and add to cart for ( Product p: items ) { order.addItem( new Purchase( order, p ) ); } // Add the JFrame to the ApplicationData to allow for user interaction StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession(); ksession.setGlobal( "frame", frame ); ksession.setGlobal( "textArea", this.output ); ksession.insert( new Product( "Gold Fish", 5 ) ); ksession.insert( new Product( "Fish Tank", 25 ) ); ksession.insert( new Product( "Fish Food", 2 ) ); ksession.insert( new Product( "Fish Food Sample", 0 ) ); ksession.insert( order ); ksession.fireAllRules(); // Return the state of the cart return order.toString(); }
- 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
JFrame
Swing component surrounding the output text frame, at the bottom of the GUI. The second is a list of order items. This comes from theTableModel
storing 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
Order
JavaBean, 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
TableModel
into 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()
.