8.3 Types
Rapide-EPL is strongly typed, much like most modern object-oriented languages. It may seem odd to impose strong typing on patterns, but it turns out to help users avoid all kinds of silly errors in patterns, such as typos. Such errors show up as type mismatches in the pattern. Types also play a powerful role in restricting the context in which a pattern can match. This makes pattern matching more efficient.
There are three kinds of types: data types, event types, and execution types.
When we write a pattern, we must first declare the types of data in the pattern and the types of events we expect to match the pattern against. A set of type declarations is called a type context.
Example 1: Defining Warning events
If we want to write patterns that will match Warning events, either about the loads on routes in a network or about the distance between aircraft, we first define the types of these Warning events:
// type declarations preceding a pattern typedef Network Path . . . ; data type typedef Aircraft . . . : data type action Warning (Network Path P1, Real P2); event type action Warning (Aircraft P1, Feet P2); event type
This type context declares two types of Warning events. It defines a global type context for patterns that follow it. Following these type declarations (that is, in the scope of the global type context), we can write a pattern such as this:
// pattern that matches network Warning events. (Network Path Route; Real Load) Warning(Route, Load);
This a basic event pattern that can match single events. It consists of declarations of the types of parameters in the pattern followed by an event template. The type declarations of the parameters are a local type context for the pattern that restricts the types of parameters only in this pattern.
As the example shows, a pattern consists of two parts: first a list in parentheses of the variables in the pattern together with their types, and then the pattern partthe part that describes the events to be matched. Events have names called action names followed by a list of parameters. The name of an event is in fact a parameter of the event that we give a special syntactic emphasis by putting it first. It is similar to the subject of a message. The name of an event must match the action name in the event pattern in order for a match to be possible. So, for example, a Warning(. . .) pattern cannot match a ConnectTime event.
Here, the pattern matches events with the action name, Warning, and parameters Route (which is of type Network Path) and Load (which is a Real number).
Patterns are checked for type consistency before they are compiled for matching. This is where typos and other errors are caught.
Matching must be consistent with the types of the parameters in the pattern, and this restricts the events that can match the pattern. Our Warning pattern will match events like Warning(LondonNewYork, 0.75), which is the kind of event we are looking for. LondonNewYork is a Route and 0.75 is a Real number giving a measure of urgency.
If we omitted the typed parameter list from the pattern and just wrote the pattern part, we might get matches of Warning events from a different type of Warning event, like Warning(UA51, 5000), a warning of an aircraft within 5,000 feet. Although this may be an interesting match, it isn't a network warning, which is the kind of event our pattern is intended to match.
A pattern has both a global type context, where the types of events it can match are declared, and a local type context, where its variables and their types are declared. Because different types of events can have the same action name, the local context is important in disambiguating action names and specifying the types of events a pattern is intended to match. The data types in the local context must be subtypes of data types in the global context.
8.3.1 Predefined Types
Rapide-EPL has a set of predefined types. These are very common data types that appear in the events generated by many systems.
The predefined types include Boolean, Character, String, Integer, Float, and so on. Each predefined type comes with a set of operations. For example, Integers have the usual operations "+", "-", "=", and so on. Strings have a rich set of predefined operationsfor example, comparison operations, such as S < T; selection operations, such as S [2 . . 4], which returns the substring consisting of the second, third, and fourth characters of S; and concatenation, &, which lets us construct a new string, S&T, from strings S and T.
8.3.2 Structured Types
Structured types are composed from other types. They let us define objects that have other objects as components.
Common kinds of structured types that are in many languages are pre-defined: record (record types), array (array types), enum (enumeration types), and some special types that we will describe later. Each of these structured types has predefined selection operations that let us select out the components of structured objects. To construct a structured object, we can assign objects as its components.
Structured types are defined by using type definitions. A type definition lets us define a type and give it a name. We can then use that name in declaring parameters of that type. The form of a type definition is typedef type-expression name ;
Example 1: The record type definition
An example of a record type definition is
typedef record {Node N1; Connection C; Node N2} Network Path;
Here we have defined Network Path to be a record type with three components: two nodes and a connection. We can select components of a record object using the parameters of the type definition and the "." selection operation. So, if Route is an object of type Network Path, "Route . N1" is the first Node of Route, and so on.
Example 2: The array type definition
An example of an array type definition is
typedef array [1, 2, 3] of String Triplet;
The index type in "[ , ]" must be an enumeration type, such as Integer. All the components of an array type must be of the same typein this example, String.
Selection of components is done by applying a value of the index type to an object of the array type using "( )" notation. For example, if ThreeSome is of type Triplet, then ThreeSome(1) is its first string component.
8.3.3 Event Types
Events are objects that are tuples of data. An event contains the values of predefined attributes (such as its action name and its timestamps) as well as additional data parameters.
In Rapide-EPL there is a predefined event type, Event, which is the type of all events. This is the type of all events with any name, any parameter list (which can be empty), and the predefined attributes (which may have undefined values).
It is usefulfor example, for efficient pattern matchingto be able to classify events into subtypes. Subtypes of events are declared by action declarations.
An action declaration specifies a subtype of events. An action declaration has the format
action identifier (list of parameter declarations ) ;
where the identifier is the action name, and the list of parameters in para en-theses declares the tuple of data in the events. The parameter list is a list of declarations that consist of the type of a parameter followed by the name of the parameter. The predefined attributes are always implied members of the list of parameters and are not explicitly declared. An action declaration specifies the set of those events that have the action's name as their action name attribute and contain a tuple of data parameters that conform to the types of the action's parameter declarations.
Example 1: A Warning event
A type of Warning event is
action Warning( Network Path Route; Real Load );
Examples of events in this action type are
Warning(London-NewYork, 0.75) Warning(Paris-London, 0.92)
However, the following events are not members of this action type:
ConnectTime(London-NewYork, 02.56) -because the action name is not "Warning" Warning(UA51, 5000) -because the data parameters are of the wrong types
Example 2: An Order event
A type of order event in a supply chain system is
action Order(Cust Id Customer, Parts Order Data, Accnt no Accnt, . . . );
Here we specify a subtype of events that contain the customer's Id, the order form in a required Parts Order format, the account number, and other data. Order events will also contain the attributes they inherit from the Rapide-EPL event type, such as timestamps.
Figure 8.1 shows three subtypes of events that can be defined by action declarations. Each event subtype defines events from a particular system, or problem domain. The DTP events are the kind of events created by a distributed transaction system, the FAA events are the type of events created in air traffic control, and SPARC V9 events are created by a simulation of a CPU architecture. In each subtype, the events inherit the attributes of the basic event type and contain additional data specific to a particular kind of system.
Figure 8.1: Subtypes of the RAPIDE event type
The subtype of XML eventsthat is, events having an XML formatis another example of an event subtype.
The idea behind actions is very simple. We think of an activity in the target system as leading to the creation of an event. We call such an activity an action. We give it a nameits action nameand a list of parameter declarations. The action declaration defines the subtype of the forms of events1 that signify the system activity.
8.3.4 Execution Types
An execution is a poset of events. Its type is called an execution type. An execution type is a set of action declarations specifying the types of events that can happen in an execution. We can define execution types using the keyword execution:
typedef execution {list of action declarations } Name;
As we shall see, execution types are useful in ensuring the correct use of event processing agents, particularly connecting them to work together.
Example 1: The NetMngmt execution type
If a network monitoring system generates load warnings, connect times, and messages on various topics, we can specify its execution type as consisting of the following event types:
typedef execution { action Warning(Network Path Route; Real Load); action Alert(Node Type Node; Real CPU Load, Memory Allocation; Int 1 .. 5 Severity; Time Type Time); action ConnectTime(Network Path Route; Time Type Time); action Send(Subject Type Subject; String Message, Id; Time Type Time); action Acknowledge(String Id; Time Type Time) } NetMngmt ;
Suppose NetMngmt is the type of execution a network monitor is expected to deal with. Its events are classified into five subtypes, with the action names Warning, Alert, ConnectTime, Send, and Acknowledge. These are the types of events it expects to deal with and should be programmed to handle.
Example 2: The ATM-Use execution type
A similar example is a simple automated bank teller system (ATM). Its execution type might be specified according to three actions that it allows customers to perform at the ATMs:
typedef execution { action Deposit(Dollars Amount; Account Type Accnt); action Withdraw(Dollars Amount; Account Type Accnt); action Transfer(Dollars Amount; Account Type From Accnt, To Accnt) } ATM-Use ;
If an ATM's execution type is ATM-Use, we know that its executions can consist only of events with the action names Deposit, Withdraw, and Transfer.
Example 3: The SupplyChainEvents execution type
An execution type for a supply chain
typedef execution { action RFQ(RFQId Id; ProdSpec Spec; Dollars Price; Quantity Num; Schedule . . . ); action Bid(Vendor VId; BidId Id; ProdSpec Spec; Dollars Price; Quantity Num; . . . ) action Order(OrderId Id; CustId Customer; PartsOrder Data; AccntNo Accnt; . . . ); action Confirm(OrderId Id; PartsOrder Data; AccntNo Accnt; Schedule Dates; . . . ); . . . } SupplyChainEvents;
The supply chain execution type defines the types of events that we used in Chapter 2 to illustrate the global event cloud. In real life, of course, it will have many more action types in it. Execution types will eventually be the subject of standardization for particular industries, analogously to the standards for sets of message types todayfor example, the ISO 15022 standard for for messaging to execute transactions in financial markets.
8.3.5 Subtyping of Executions
Execution types have a simple subtyping rule:
type T 1 is a subtype of T 2 if T 2 T 1 -- T 1 contains T 2.
This rule means that T1 is a subtype of T2 if the set of actions in T1 contains all the actions in T2.
This is very similar to the object-oriented subtyping rule: type colored point is a subtype of type point. A colored point contains the X and Y coordinates of a point and the additional color component. The importance in object-oriented programs is that any function that computes on points also computes on colored points because colored points contain all the data it needsbut not conversely. The O-O rule is that you can always evaluate a function on an object of a subtype of a function's input type.
Execution types turn out to be useful when we build networks of agents. Event processing agents (EPAs) are typed with their input and output execution types. This lets us analyze whether or not it is sensible to feed the output of one agent into the input of another. Here's how we do it. Composition of EPAs follows the same rule as function composition in typed languages. Namely, if EPA1 outputs to EPA2, the execution type of the output of EPA1 must be a subtype of the execution type of input expected by EPA2. So, it's just as if EPA1 is pumping out colored points and EPA2 is computing on points.
It is easy to see that we shouldn't try, for example, to hook up a network monitor agent and an ATM agent, because they process entirely different types of events. Such a hookup would be a complete waste of time.