Pattern Matcher Grammar
A pattern string has four main clauses: define pattern, using, with, and starts with clause. These clauses define the patterns that need to be identified with specified streams.
The following figure displays the process flow of the four different clauses:
Syntax Example
The following is a simple example to illustrate the four clauses of a pattern. This example checks for an incorrect order of events in an order fulfillment and shipping flow.
define pattern /OrderTracker using /Order as order and /Fulfillment as fulfillment and /Shipment as shipment with order.customerId and fulfillment.customerId and shipment.customerId starts with order then fulfillment then shipment
The example demonstrates how Pattern Matcher correlates events across three different event streams. The pattern listens to all three streams (Order, Fulfillment and Shipment).
A pattern listens only to events that are sent to the Pattern Matcher service. See Send Events to the Service.
Syntax Diagrams
The syntax diagrams show the structure of a pattern and of each clause in a pattern.
Read the syntax diagrams from left to right. Items above or below a main line are optional. Items that can repeat are shown by lines that loop back from the end to the beginning of the repeating section, along with the separator character or word if one is required.
Names
- Pattern names (URIs) and event name can be any character inside double quotes, except double quote itself. Pattern URI needs quotes only if there is a space in the URI.
- Field and property names must be valid Java identifiers.
- Each alias must be globally unique in the whole pattern.
- To escape a keyword, use the pound sign (# — also known as a hash sign) before the keyword, for example, #define.
- Alias name, field name, property name, subscription field and bind variables can be use any of the following:
Bind Variables
Variables begin with the dollar sign ($). The value is provided at deploy time. See Create the Pattern String.
You cannot use bind variables with the datetime() or date() functions. For example, it is not possible to use this type of call:
$datetime($year,$day,$month,....)
You can, however, use bind variables using the following function:
Pattern.Manager.setParameterDateTime()
For example:
Pattern.Manager.setParameterDateTime(patternSc14, "javaUtilDate", date);
Not (Negation) Scenarios
Although the language has no explicit operator for Negation or Not, negative scenarios can be implemented in other ways such as:
So, when the undesired event does occur due to the subscription, the pattern instance will fail. The following pattern example demonstrates a negation scenario:
define pattern OrderFullfilment using Order as order and Fulfillment as fulfillment and Cancellation as cancellation where order.customerId and fulfillment.customerId and cancellation.customerId starts with order then within 10 minutes fulfillment then after 5 minutes
This pattern subscribes to Cancellation events but does not use them in the pattern. After the Order and the Fulfillment events arrive within the times specified, the pattern waits for another five minutes, during which it does not expect any input. If during this or any other time the Cancellation event occurs, then the pattern fails.
- Define Pattern Clause
The Define Pattern clause specifies a unique URI for the pattern. - Using Clause
The using clause specifies one or more event types to subscribe to in the pattern, and an alias for each. One says that the pattern subscribes to these events. - With Clause
In the with clause of a pattern, you specify a property for each event in the using clause. You cannot use the event payload. Each event type can have one such property defined. The property or properties are used for correlations and subscriptions. - Correlation or Subscription Value
The subscriptionvar item specifies the correlation or subscription value. - With Clause and the Correlation Property
The first term in a with clause must be a correlation property. It is also sometimes referred to as the subscription property. - Correlation and Exact Match
You can use one or more exact matches in your pattern. To use an exact match, specify a property and an exact value for that property. - Starts With Clause
A pattern describes a sequence of events, beginning with the starts with event, followed by each then event, in the order specified. The starts with clause is where the actual event sequence or pattern is described. For the pattern to succeed, all the events must be received according to the specified order (and any additional time constraints). - Clauses for Explicit Temporal Constructs
The pattern grammar implicitly describes a sequence of events: there is an implicit time component in each pattern. In addition three constructs enforce explicit time-based restrictions on a sequence: within, during, and after. You can use these constructs to enforce stricter time-based constraints on a sequence: