Apama Fundamentals Course Module: Developing Apama Scenarios in Event Modeler Exercises: Scenarios List of Exercises for this Module Exercise 1: Exercise 2: Exercise 3: Exercise 4: Exercise 5: Exercise 6: Exercise 7: Exercise 8: Create a New Scenario .............................................................................................................3 Define States ............................................................................................................................5 Define Scenario Variables........................................................................................................6 Add Block Instances to the Scenario .......................................................................................8 Link Scenario Variables and Block Parameters .....................................................................10 Compose Rules ......................................................................................................................13 Run the Project .......................................................................................................................21 Link Blocks Together .............................................................................................................22 Objective In this exercise, you will learn how to use the Event Modeler tool to create a simple Apama Scenario for the Off Market Limit Order. Prerequisites You must have installed the current release of Apama and completed the previous lab exercise: Import Apama Projects. Start Apama Studio, if it is not already started. Overview of All Steps 1. Start with the provided Limit_Lab_2 project. 2. In the Event Modeler, create a new scenario and call it OffMarketLimitOrder. 3. Define three new states: Buy, Sell, and Wait until finished. 4. Define the following scenario variables: Input – Symbol (text, immutable), Side (choice, immutable) Price (number), Qty (integer number) Output – Final Qty, Final Price, Bid, Ask, MD Counter, Status Private – OrderId 5. Add instances of two blocks: OMS block – An Order Management system block that allows you to submit orders to buy and sell stock Market Data block – A block that simulates an event stream from a stock market data feed. It contains two operations that let you subscribe and unsubscribe from the simulated event stream. continued on next page Page 1 of 22 Copyright © 2009 Progress Software Corporation Apama Fundamentals Course Basic Steps (continued) Module: Developing Apama Scenarios in Event Modeler 6. Link scenario variables to block output feeds: Final Qty and Final Price to the OMS qtyExecuted and priceExecuted Bid and Ask to the corresponding Market Data feeds Link block parameters to the corresponding scenario variables: Market Data: Symbol OMS: OrderId, symbol, side, price, qty 7. Compose the rules for each state as follows: Start (a processing state) – Subscribe to the Market Data block and check the price of a particular stock. Then determine what type of order to process – an order to buy the stock or an order to sell it. Buy (a processing state) – When the ask price is less than or equal to the input Price (from the UI), submit a BUY order to the Order Management System (OMS), and wait for the order to fill. Sell (a processing state) – When the ask price is greater than or equal to the input Price (from the UI), submit a SELL order to the OMS, and wait for the order to fill. Wait unit finished (a wait state) – When the OMS has finished processing the order, the order is filled, so then unsubscribe from market data and enter the end state. Compose a global rule that ensures the bid and ask price parameters are flowing through from the Market_Data_Block. Use the MD Counter scenario variable to count the number of Market Data updates coming from the Market Data block. 8. Run the project and create an instance of the scenario in the Scenario Browser view. Page 2 of 22 Copyright © 2009 Progress Software Corporation Apama Fundamentals Course Module: Developing Apama Scenarios in Event Modeler Detailed Steps Exercise 1: Create a New Scenario Prepare the Project 1. In Apama Studio, select File > Import to import the Limit_Lab_2 project (provided by your instructor). This project is the same as the Limit_Lab_1_Complete project, except: Does not include the monitors/OffMarketLimitOrder.mon file Includes block catalogs and a function catalog 2. Add the scenario bundle to the project: a) Select Project > Properties from the Apama Studio menu. b) In the Properties dialog, select MonitorScript Build Path and select the Bundles tab. c) Click Add. d) In the Add Bundle Instance dialog, select the Scenario Service and click OK twice. Apama Studio adds a Bundles folder and a bundle_instance_files folder to your project. The Scenario Service bundle includes two monitor files: Page 3 of 22 ScenarioService.mon - Contains the shared event definitions that are generic across all scenarios. ScenarioDeleterSupport.mon - Provides event definitions for deleting scenario instances. Copyright © 2009 Progress Software Corporation Apama Fundamentals Course Create a new scenario Module: Developing Apama Scenarios in Event Modeler To create a new scenario: 1. In the Limit_Lab_2 project, select the Scenarios folder, then click New Scenario: 2. In the New Scenario dialog, type OffMarketLimitOrder for the filename. 3. Click Finish. The Event Modeler opens with the two default states already created: start and end. Through these exercises, you will create rules that cause the start state to subscribe to the Market Data block and check the price of a particular stock. It will then determine what type of order to process – an order to buy the stock or an order to sell it. The start state will use blocks that you need to add to the scenario before you can define the start state rules. The end state is a default state that indicates a scenario instance has finished processing. 4. Drag the end state so it is further down in the States panel. Page 4 of 22 Copyright © 2009 Progress Software Corporation Apama Fundamentals Course Module: Developing Apama Scenarios in Event Modeler Exercise 2: Define States Define three You will add the following additional states to the Off MarketLimitOrder scenario template: new states Buy (an action state) – When the ask price is less than or equal to the input Price (from the UI), submit a BUY order to the Order Management System (OMS), and wait for the order to fill. Sell (an action state) – When the ask price is greater than or equal to the input Price (from the UI), submit a SELL order to the OMS, and wait for the order to fill. Wait unit finished (a wait state) – When the OMS has finished processing the order, the order is filled, so then unsubscribe to market data and enter the end state To define these three new states: 1. At the top of the States panel, click the plus sign to create a new state. 2. Double-click the New State 1 label and rename it Buy. 3. Create another new state and rename it Sell. 4. Create another state and rename it Wait until finished. 5. Drag the new states so they are between the start and end states. 6. Select File > Save Scenario. Page 5 of 22 Copyright © 2009 Progress Software Corporation Apama Fundamentals Course Module: Developing Apama Scenarios in Event Modeler Exercise 3: Define Scenario Variables Define scenario variables To define scenario variables: Scenario variables can be used by any of the states or rules in the scenario. 1. In the Scenario Variables panel, click on the green box. 2. Type Symbol in the name column and check the input box. 3. Double-click on the green box to open the Properties dialog. 4. Specify the Type as Text and Immutable, then click OK.. 5. In the Scenario Variables panel, click in the value column for Symbol, then type PRGS. 6. To define the next variable, click on the next green box, and type Side in the name column. 7. Check the input box. 8. Double-click on the green box to open the Properties dialog, then specify the Type as Choice and Immutable. 9. Click the Constraints tab, add BUY and SELL as Choices, then click OK to exit the Properties dialog. 10. Double-click in the Value area and select SELL for the initial value. continued on next page Page 6 of 22 Copyright © 2009 Progress Software Corporation Apama Fundamentals Course Module: Developing Apama Scenarios in Event Modeler 11. Create the following additional variables: Define scenario Variable Input/Output variables Name (continued) Type Constraint Price Input Number, Mutable Qty Input Number, Mutable Whole Number Final Qty Output Number, Mutable Whole Number Final Price Output Number, Mutable Bid Output Number, Mutable Ask Output Number, Mutable Status Output Text, Mutable MD counter Output Number, Mutable OrderId Neither(private) Number, Mutable Mixed case Whole number 12. Select File > Save Scenario. Your scenario should now include the following variables: Page 7 of 22 Copyright © 2009 Progress Software Corporation Apama Fundamentals Course Module: Developing Apama Scenarios in Event Modeler Exercise 4: Add Block Instances to the Scenario Objectives In this exercise, you will add two pre-existing blocks of MonitorScript code and a predefined function to the simple Apama Scenario application you created in the previous exercise. Description of Blocks The two blocks that you will add are: Basic steps OMS block – An Order Management system block that allows you to submit orders to buy and sell stock Market Data block – A block that simulates an event stream from a stock market data feed. It contains two operations that let you subscribe and unsubscribe from the simulated event stream. When you add a block to a scenario, you are specifying that each instance of that scenario should create an instance of that block running within the scenario. To use a block in a scenario, you must select it in the Event Modeler Catalogs panel and add an instance to your scenario. You can add more than one instance of the same block to a scenario. Add the Market Data and Order Management System blocks To add instances of the Market Data block (code that simulates an event data stream) and Order Management System block (process orders) : 1. In the title bar of the Event Modeler Catalogs panel (at the bottom of Event Modeler), click the down arrow and select the Limit_Lab_2 blocks catalog. 2. In the Catalogs panel, select the Market Data block. continued on next page Page 8 of 22 Copyright © 2009 Progress Software Corporation Apama Fundamentals Course Add blocks (continued) Module: Developing Apama Scenarios in Event Modeler 3. Click the icon farthest to the right in the Catalogs title bar. An instance of the Market Data block is added to your scenario. The Event Modeler displays the block instance in the Blocks panel and calls it Market_Data_Block 1. (If you were to add another instance, it would be named Market_Data_Block 2.) 4. Repeat Steps 2 and 3 for the Order Management System block. When you are done, the Blocks panel should look like this: Page 9 of 22 Copyright © 2009 Progress Software Corporation Apama Fundamentals Course Module: Developing Apama Scenarios in Event Modeler Exercise 5: Link Scenario Variables and Block Parameters Objective In this exercise, you link the scenario’s variables to the output parameters of the two code blocks. Attaching a scenario variable to a block parameter provides greater flexibility because it allows the scenario itself to be parameterized.Whenever the block outputs an event on the given feed, the variable is automatically updated Link scenario variables to block output feeds To link scenario variables to output feeds: 1. In the Event Modeler’s Scenario Variables panel, right-click in the value column of the Bid variable. 2. From the pop-up menu, select Market_Data_Block_1 > best bid and ask > bid : 3. Right-click in the value column of the Ask variable, then select Market_Data_Block_1 > best bid and ask > ask. 4. Set the values of the following variables for interaction with the Order Management System block: Name Final Qty Final Price Page 10 of 22 Value qtyExecuted output feed from the OMS block priceExecuted output feed from the OMS block Copyright © 2009 Progress Software Corporation Apama Fundamentals Course Link block parameters to scenario variables Module: Developing Apama Scenarios in Event Modeler Whenever the variable’s value is modified, the block’s parameter is automatically updated. To link the value of a block parameter to the value of a scenario variable: 1. In the Blocks panel, right-click in the value field of the Market_Data_Block 1 symbol parameter and select Symbol from the pop-up menu. This automatically passes the value of the scenario's Symbol variable into the block, which causes the block to listen for events about Progress Software stock: 2. In the Blocks panel, right-click in the value field of the following OMS _Block_1 block parameters to select the corresponding scenario variable from the pop-up menu: OMS Block Parameter orderId symbol side price qty Value = Scenario Variable OrderID Symbol Side Price Qty Note that the Type block parameter does not need to be set. It is of type choice. Page 11 of 22 Copyright © 2009 Progress Software Corporation Apama Fundamentals Course Link block parameters and scenario variables Page 12 of 22 Module: Developing Apama Scenarios in Event Modeler When you have completed linking blocks and scenario variables, the Scenario and Blocks Panels should look like this: Copyright © 2009 Progress Software Corporation Apama Fundamentals Course Module: Developing Apama Scenarios in Event Modeler Exercise 6: Compose Rules Objective In this exercise, you will learn how to compose local rules for each of the four states (start, Buy, Sell, Wait until finished). You will also learn to define a global rule that applies to all states Overview of You will compose the following three rules in the start state: Subscribe to the Market_Data_Block, and continue to evaluate the next rule in start state the state rules Compose rules for the start state If the Side is set to BUY in the running scenario instance, move to the Buy state If the Side is set to SELL, move to the Sell state To subscribe to market data: 1. In the Event Modeler States panel, select the start state. 2. In the Local Rules panel, click the plus sign to add a new rule, then click the Up arrow button until the new rule is the first one at the top of the Local Rules panel. 3. Double-click on the name New Rule, then change it to Subscribe to external services. 4. In the When row, right-click condition and select true from the pop-up menu. 5. In the Then row, click the grey circle next to continue to add an action statement. Note: If you create more than one by mistake, you can delete the extra one by right-clicking on the grey circle. 6. Right-click action-statement and select Market Data Block 1 > subscribe. When you are done, the new rule should look like this: 7. Select File > Save Scenario. continued on next page Page 13 of 22 Copyright © 2009 Progress Software Corporation Apama Fundamentals Course Compose rules for the start state (continued) Module: Developing Apama Scenarios in Event Modeler To create a rule for moving to the Buy state: 1. In the Local Rules panel, click the plus sign to add a new rule. Use the Up or Down arrow buttons, if necessary, so that the new rule comes after the Subscribe to external services rule. 2. Change the name from New Rule to Order to BUY. 3. In the When row, right-click condition and select choice variable compared with choice expression. You want to compare the value of the Side scenario variable with a text expression you specify (“BUY”). When the result is true, Apama executes the action defined in this rule. When the result is false, Apama moves on to evaluate the When statement in the next local rule. 4. Right-click on choice variable and select Side. 5. Right-click on compared with and select is equal to. 6. Right-click choice expression and select BUY. 7. In the Then row, add an action statement by clicking the grey button next to the continue statement: 8. Right-click action-statement and select text variable = text expression. 9. Right-click text variable and select Status. 10. Right-click text expression and select Enter textual value and type: Executing Buy order. 11. Right-click continue and select Move to state [Buy]. A transition arrow immediately appears in the States pane going from the start state to the Buy state. 12. Select File >Save. 13. To create a rule for moving to the Sell state, repeat the Steps 1-11 above, replacing “buy” with “sell”. 14. Select File >Save. continued on next page Page 14 of 22 Copyright © 2009 Progress Software Corporation Apama Fundamentals Course Module: Developing Apama Scenarios in Event Modeler Compose rules for the start state (continued) Here are the three local rules in the start state: Compose a rule for the Buy state In the Buy state, when the ask price is less than or equal to the input Price (from the UI), submit a BUY order to the OMS system, and wait for the order to fill. To compose a rule in the buy state: 1. Click the Buy state. 2. In the Local Rules panel, click the plus sign to create a new rule. 3. Change the name of the rule to Wait for price. 4. To define the condition part of the rule: a) Right-click ‘condition’ and select ‘number expression’ ‘compared with’ ‘number expression’. You want to compare the asking price from the market feed with the value of the Price variable in the scenario. You want to wait to place an order to buy until the asking price is less than or equal to the scenario price. b) Right-click the first ‘number expression’ and select ‘number block field’, then ‘best bid and ask’, and then ‘ask’. c) Right-click ‘compared with’ and select is less than or equal to. d) Right-click the last ‘number expression’ and select ‘number scenario variable’, and then Price. continued on next page Page 15 of 22 Copyright © 2009 Progress Software Corporation Apama Fundamentals Course Compose a rule for the Buy state (continued) 5. Module: Developing Apama Scenarios in Event Modeler To define the action part of the rule: a) Click the gray circle next to continue three times to add three action statements. b) Right-click the first action statement and select ‘number variable’ = ‘number expression’. c) Right-click ‘number variable’ and select OrderId. d) Right-click ‘number expression’ and select Limit_Lab_2functions > GET_UNIQUE_ID(). e) Right-click the second action statement and select OMS_Block_1 > submitOrder. f) Right-click the third action statement and select ‘text variable’ = ‘text expression’. g) Right-click ‘text variable’ and select Status. h) Right-click text expression, select Enter textual value, and type Submitted BUY order to OMS block. i) Right-click continue and select Wait until finished. 6. Select File >Save. Here is the local rule in the Buy state: Page 16 of 22 Copyright © 2009 Progress Software Corporation Apama Fundamentals Course Compose a rule for the Sell state Module: Developing Apama Scenarios in Event Modeler In the Sell State, when the ask price is greater than or equal to the input Price (from the UI), submit a SELL order to the OMS system, and wait for the order to fill. To compose a rule in the sell state: In the Sell State, when the bid price ≥ input Price (from the UI), submit a SELL order to the OMS system, and wait for order to fill. To compose a rule in the sell state: 1. Copy the Wait for Price rule from the Buy state and paste it into the Sell state. 2. In the When row, right-click “ask” and select “Market_Date_Block_1, best bid and ask, and then bid. 3. Right-click is less than or equal to and select is greater than or equal to. 4. In the Then row, in the third action statement, right-click text expression, select Enter textual data, and change BUY to SELL. 5. Select File > Save Scenario. You should now have one rule in the sell state: Page 17 of 22 Copyright © 2009 Progress Software Corporation Apama Fundamentals Course Compose a rule for the Wait until finished state Module: Developing Apama Scenarios in Event Modeler In the Wait until finished state, when the OMS Block has finished processing the order, the order is filled so unsubscribe to market data and enter the end state. To compose the local rule for the Wait until finished state: 1. Select the Wait until finished state 2. In the Local Rules panel, click the plus sign to add a new rule. 3. Type Wait until… as the rule name. 4. Right-click condition and select condition block field > OrderUpdate >finished. 5. Create two action statements. 6. Right-click the first action statement and select ‘text variable’ = ‘text expression’. 7. Right-click ‘text variable’ and select Status. 8. Right-click ‘text expression’, select Enter textual value, and type Order executed. 9. Right-click the second action statement and select Market_Data_Block_1 and unsubscribe. 10. Right-click continue and select end. Here is the local rule in the Wait until finished state: Page 18 of 22 Copyright © 2009 Progress Software Corporation Apama Fundamentals Course Compose a global rule Module: Developing Apama Scenarios in Event Modeler A global rule is evaluated before any of the local (state-specific) rules. The purpose of this rule is to ensure that the bid and ask price parameters are flowing through from the Market_Data_Block. The MD Counter is the scenario variable that counts the number of Market Data updates coming from the Market Data block. To define the global rule: 1. In the Global Rules panel, click the plus sign. 2. For the rule name, enter Market Data counter to track # of updates. 3. Right-click ‘condition’, then select ‘variable changes’. 4. Right-click ‘variable’, select Market_Data_Block_1 > best bid and ask. 5. Create one action statement. 6. Right-click action statement then select ‘number variable’ = ‘number expression”. 7. Right-click ‘number variable’ then select MD Counter. 8. Right-click ‘number expression’ then select […] ‘operator’ ‘number expression’. 9. Right-click the first ‘number expression’ and select number scenario variable and MD Counter. 10. Right-click ‘operator’ and select +. 11. Right-click the last ‘number expression’, select Enter numeric value and type 1. Here is the global rule: Page 19 of 22 Copyright © 2009 Progress Software Corporation Apama Fundamentals Course Module: Developing Apama Scenarios in Event Modeler When you are done, your completed scenario should look like this: Page 20 of 22 Copyright © 2009 Progress Software Corporation Apama Fundamentals Course Module: Developing Apama Scenarios in Event Modeler Exercise 7: Run the Project Run the project To run the project and create instances of the scenario: 1. In Apama Studio’s Launch Control Panel, click the Run button to start the project. Apama Studio loads the project and opens the Apama Studio Scenario Browser view. 2. Expand the localhost node in the left panel in the Scenario Browser and select the OffMarketLimitOrder scenario. 3. At the top of the Scenario Browser view, click the plus button (+) to create an instance of the OffMarketLimitOrder scenario. 4. In the Add Scenario Instance dialog, next to Side, double-click in the Value field and select BUY. 5. Tab to Price, type 10. Click OK. 6. In the Scenario Browser, expand the OffMarketLimitOrder node, select the new scenario, and observe the values of the scenario variables. 7. In the Workbench Project View, right-click the events/marketevents.evt file and select Apama Runtime > Send Events. Observe the updates to the output in the Scenario Browser. You can experiment by creating multiple scenario instances and changing the values of the variables to see what happens. 8. To stop the project, click the Stop button in the Launch Control Panel. Page 21 of 22 Copyright © 2009 Progress Software Corporation Apama Fundamentals Course Module: Developing Apama Scenarios in Event Modeler Exercise 8: Link Blocks Together Objective In this exercise, you will link two blocks together: the Market Data block and a standard Data Distribution Calculator. The Data Distribution block calculates running data distribution statistics from the input data stream. In this example, you’ll use it to calculate the mean of the bid data coming from the Market Data block. To practice linking two blocks together: Steps for Linking Blocks 1. Add the Data Distribution Calculator block to your scenario. Hint: It is a standard, General Analytics block. Together 2. Open the block wiring panel: 3. Move the Market Data and the Data Distribution Calculator blocks so that they are next to each other, with the Market Data block first (on the left). 4. Click and hold the black semi-circle on the Market Data block. This represents the output feed of the Market Data block. 5. Drag a line from there to the input feed semi-circle(on the left) of the calculator block. The Configure Block Wiring dialog appears. In this dialog box, you need to pick the proper input/output feeds from the two provided drop-down lists. 6. Map the Market Data block’s bid field to the calculator’s value field. 7. Back in the Event Modeler’s main window, edit the appropriate scenario rule in the Start state to start the Data Distribution Calculator block. 8. Edit the appropriate scenario rule in the Wait Until Finished state to stop the Data Distribution Calculator block. 9. Add a new scenario output variable called Bid Mean and sets its value to the mean from the statistics output feed of the Data Distribution Calculator block. 10. Save your updates to the scenario. 11. Run the project and view the output in the Scenario Browser view of Apama Studio. Page 22 of 22 Copyright © 2009 Progress Software Corporation
© Copyright 2026 Paperzz