Apama 10.7.2 | Developing Apama Applications | Developing Apama Applications in EPL | Defining What Happens When Matching Events Are Found | Handling the any type
 
Handling the any type
EPL supports an any type that can hold a value of a concrete EPL type (that is, a type other than the any type). See the API Reference for EPL (ApamaDoc) for full details of the any type.
The switch statement is the preferred way of handling any values unless the type is known or not important. See Handling any values of different types with the switch statement for details on the switch statement.
An any value may be empty and not contain a value, or it can contain a value which has a type associated with it. The type of the value can be obtained using the getTypeName() method on the any type.
A variable of a concrete type can be used where an any value is expected in:
*assignments and initialization, for example:
any anyVariable := "string value";
*return values, for example:
action a() returns any { return new sequence<integer>; }
*passing a parameter to an action, for example:
actionWithAnyParameter("string value");
*the index of a dictionary with an any key type.
In these cases, the concrete type is automatically converted to the any type. This is always safe and valid, and will not throw an exception.
Reflection on types
Reflection allows EPL to act on values of any type in a generic way, including altering the behavior to adapt to what fields or actions a type has. This can be values passed as an any parameter value to some common code, matching an any() listener (see Listening for events of all types), or created via the any.newInstance method.
Fields or entries from an any value can be accessed using the following methods:
*setEntry(any key, any value)
*getEntry(any key) returns any
*getEntries() returns sequence<any>
For event types, the key should be a string containing the field name. For sequences, key is the index and should have an integer value.
The actions (including methods) and constants can be obtained with the following methods of the any type:
*getActionNames() returns sequence<string>
*getAction(string name) returns any
*getConstant(string) returns any
*getConstantNames() returns sequence<string>
Actions may be cast to the correct action type and then invoked directly.
For actions, a list of the action's parameter names, a dictionary mapping from the parameter name to the parameter type, and the name of the return type can be obtained with the following methods of the any type:
*getActionParameterNames() returns sequence<string>
*getActionParameters() returns dictionary<string,string>
*getActionReturnTypeName() returns string
For actions, it is also possible to use a generic form to call the action via the following method of the any type, even if the signature type is not known at compile time:
*getGenericAction() returns action<sequence<any>> returns any
A sequence<any> of the parameter values of the correct count and types must be supplied.
For detailed information on the above methods, see the any type in the API Reference for EPL (ApamaDoc) .
Cast operations
EPL supports casting of the any type to a concrete target type and vice versa.
*Casting to the any type
Casting a concrete type is allowed to any only. The cast is redundant in this case. Example:
integer i := 10;
 
any a := <any> i; //redundant cast
any a2 := i; //valid
Example of a cast that is not redundant:
sequence<any> entries := (<any> evt).getEntries();
*Casting to a concrete type
targetType tgtValue := <targetType> anyValue;
Casting an any type with an empty value throws Exception with type set to CastException. See also the Exception type in the API Reference for EPL (ApamaDoc) .
If the anyValue does not contain an object of targetType, it throws Exception with type set to CastException, and with the message mentioning the actual type contained by anyValue and targetType.
Examples:
any a := 10;
 
integer i := <integer> a; // Valid
 
// Will inject but throws a CastException during runtime.
string s := <string> a ;
*Casting to the optional type
optional<targetType> opt := <optional<targetType>> anyValue ;
Casting to optional<targetType> will never throw. If the any value cannot be converted, then an empty optional<targetType> is returned instead.
If the anyValue is empty, the cast returns an empty optional<targetType>.
If anyValue contains object of optional<targetType> type, the cast returns that object of type optional<targetType>.
If the anyValue contains an object of targetType , the cast returns object of type optional<targetType> containing targetType.
If the anyValue does not contain an object of targetType or optional<targetType>, the cast returns an empty optional<targetType>.
Examples:
any a := 10;
 
// Returns optional <integer> containing the value 10.
optional<integer> opti := <optional<integer>> a;
 
// Returns an empty optional <string>.  
optional<string> opts := <optional<string>> a;