Tutorial: Using event strategy in Pega 7 Platform real-time event processing (7.2.2)
Event strategy is a visual representation of processing logic that is applied to all events that come from a data source in real-time. You can use event strategies to detect and query the events across a data stream and react to emerging patterns.
To use an event strategy in your application, you must create a stream data set that is the source of customer records, an Event Strategy rule that constitutes the processing instructions that are applied to the sourced data, and a data flow to trigger data processing.
- Prerequisites
- Configuring an Event Strategy rule for trend detection
- Creating a data flow for event strategies
- Tips for event strategy testing
- Running data flows that contain an event strategy
Representation of an event strategy processing pattern on the rule canvas
Prerequisites
Perform the following actions before you create and configure an event strategy:
- Create and configure a data set that is the source of events for the Event Strategy rule. For more information, see About Data Set rules.
Although you can select data sets of various types, the most common data set type for sourcing events is Stream. This data set type processes a continuous stream of records in real time (for example, with call details from a telecommunications network, customer transactions, and so on) and can take in high volumes of low-latency data. You can use this type of data set to save data to the stream and to browse data from the stream.
- Ensure that the class in which you create contains the properties whose values you want to use in the event strategy, for example, call details, transaction value, and so on. In this tutorial, the following properties are used:
- ID – The event identification number
- CustomerID – The customer identification number
- Value – The transaction value
Configuring an Event Strategy rule for trend detection
Configure an event strategy on a canvas-based modeler and develop your business logic by using various shapes and connectors. The following event strategy configuration detects trends in the events that are streamed in real time by comparing the moving average over two windows of different size. The goal of the event strategy is to determine whether the average value of customer transactions in the last seven days is greater than the average value for the last 31 days. The event strategy emits events when it detects an upward trend in the customer transaction value in the period of the last seven days.
Creating an Event Strategy rule
Follow these steps to create an Event Strategy rule in your application.
- In Designer Studio, click + Create > Decision > Event Strategy.
- Provide the rule label and identifier.
- Provide the ruleset, Applies To class, and ruleset version of the rule.
- Click Create and open.
Configuring the Real-time data shape
Configure the Real-time data shape that is the source of properties that come
- On the Event Strategy form, click the Event Strategy tab to access the modeling canvas.
- Right-click the Real-time data shape and click Properties.
- In the Properties panel, expand the Event Key list and select the property that stores the identification numbers of customers, for example, CustomerID. The Event Key property is used for grouping incoming events.
- In the Event timestamp section, select the System time check box. This way, your system provides the value for the time property of incoming events.
- Click Submit.
Splitting the strategy into multiple processing paths
For the purpose of this tutorial, you must split the strategy into two independent processing paths: primary and secondary. On each of these paths, you will store and emit events in separate windows, and then compute the average values of customer transactions over the specified period of time.
- Click the connector that radiates from the Real-time data shape and then click Split and Join. For more information, see Split and Join logic in event strategies.
Splitting an event strategy into two processing paths
Configuring Window shapes for storing events
Create and configure Window shapes that store and emit events at different intervals.
- On each path, click the connector that radiates from the Split shape, and then click Window.
- Configure the Window shape on the primary path:
- Right-click the shape and click Properties.
- In the Name field, enter 7 days. This window tracks the customer transactions over seven days.
- Select the Sliding check box. Sliding windows process events by gradually moving the window over the data in single increments. As the new events come in, the oldest events are removed. By selecting this window type, you ensure that events are tracked continuously.
- In the Look for last field enter 7 and select Days from the drop-down list to complete the expression.
- Click Submit.
- Configure the Window shape on the secondary path:
- Right-click the shape and click Properties.
- In the Name field, enter 31 days. This window tracks customer transactions over 31 days.
- Select the Sliding check box. Sliding windows process events by gradually moving the window over the data in single increments. As the new events come in, the oldest events are removed. By selecting this window type, you ensure that the events are tracked continuously.
- In the Look for last field enter 31 and select Days from the drop-down list to complete the expression.
- Click Submit.
Aggregating average transaction values for a specific time period
Create and configure Aggregate shapes that compute the moving average of credit card usage over 7 and 31 days per customer.
- On each path, click the connector that radiates from the Window shape, and then click Aggregate.
- Configure the Aggregate shape on the primary path:
- Right-click the shape and click Properties.
- In the Name field, enter Moving Average 7. This shape computes the average transaction value per customer for a period of seven days.
- Click Add aggregator.
- From the drop-down list, select Average aggregator.
- In the Source field, select the Value property. You can select this property only if that property belongs to the event strategy class.
- In the Aggregate field, enter Mean7.
- Configure the Aggregate shape on the secondary path:
- Right-click the shape and click Properties.
- In the Name field, enter Moving Average 31. This shape computes the average transaction value per customer for a period of 31 days.
- Click Add aggregator.
- From the drop-down list select Average aggregator.
- In the Source field, select the Value property. You can select this property only if that property belongs to the event strategy class.
- In the Aggregate field, enter Mean7.
Combining the averages
Configure the Join shape that was created when the strategy was split into two separate processing paths. In this shape, you join the average values of customer transactions over 7-day and 31-day periods to determine whether the value of customer transactions increased in the last 7 days as compared with the last month.
- Right-click the Join shape to open the Properties panel.
- In the Join section, from the drop-down list for Moving Average 7 and Moving Average 31, select the ID property of the event strategy class.
- In the Output section, click Add Field.
- Complete the output expression:
- Expand the drop-down list and select Mean31.
- Type Mean31 to complete the expression phrase From Moving Average 31 as...
The join expressions for joining data from the primary and secondary paths
Filtering the results
At this point in the event strategy processing path, the events that are emitted from the Join shape contain the values for both 7-day and 31-day period customer transactions. To emit only the events in which the value of customer transactions in the last seven days is trending up as compared with the last 31 days, you must add a filter shape, create a formula for trend computation, and configure the filter condition.
You can specify your filter condition to emit only the events whose trend is greater than the specified value, for example, 0.15.
- Click the connector that radiates from the Join shape and select Filter.
- Double-click the Filter shape to open the Properties panel.
- In the Name field, enter Trending Up.
- I the Local variables section, click Add variable.
- In the Let field, enter mean7_minus_mean31.
- In the field to the left of the equals operator ("="), press the Down Arrow key and select Mean7.
- From the list of operators, select - (for subtraction).
- In the field that is located to the left of the operator list, press the Down Arrow key and select Mean31.
- Click Add variable.
- In the Let field, enter Trend.
- In the field to the right of the equal sign ("="), press the Down Arrow key and select mean7_minus_mean31.
- From the list of operators, select the division operator ("/").
- In the field that is located to the right of the operator list, press the Down Arrow key and select Mean31.
- In the Filter conditions section, click Add condition.
- From the list of operators, select (">").
- In the field to the left of the list of operators, press the Down Arrow key and select Trend.
- In the field to the right of the list of operators enter 0.15.
- Click Submit.
- Save the event strategy.
Detecting trends in event strategies
Emitting the events
In the Emit shape, you can specify when your event strategy should emit events.
- Double-click the Emit shape to open the Properties panel.
- Expand the Emit event drop-down list and select As it happens. When this option is selected, the events and all their properties are emitted as soon as they occur.
Creating a data flow for event strategies
To activate an event strategy, you must create and configure a data flow that references that event strategy and the stream data set. Data flows offer a flexible solution for combining all your data points into a processing pattern that has a source from which the input data is taken and a destination to which the results are saved. Between the stream data set, event strategy, and destination, you can apply various other processing instructions in the form of different patterns to combine different types of data and get the best results for determining the next best action for your customers. For more information, see About Data Flow rules.
Data flow for event processing
Tips for event strategy testing
You can use various external or Pega-provided tools to ensure that the configuration of the Event Strategy rule and all accompanying rules produces the expected results before you deploy the configuration in the production environment.
Sending test events
You can use external tools or data from external tools or any other source to send events to test event strategies. The following list contains a sample list of tools that you can use to create and send events to the event strategy:
- Python or R scripts – You can use the processing power of object-oriented languages such as Python or R to fully control the data that you send and to verify the results.
- Chrome Postman plug-in – You can use the plug-in that is provided by Chrome as a browser extension to manually send events.
Monitoring the output
Running data flows that contain an event strategy
Use the Data Flows landing page to trigger real-time runs of data flows that contain event strategies. For more information, see Creating a real-time run for data flows.
Previous topic Tutorial: Uploading JAR files that contain models to Decision Data rules Next topic Tutorial: Using managed data flow runs