Advanced usage of the Any type

In our previous Introduction to the Any Type blog, we learned about the new EPL any type introduced in 10.1. In 10.2, we expanded the set of reflection capabilities on the any type. These new additions increase our ability to write even more generic and dynamic Apama applications, so let’s explore them now!

Reflection Capabilities Of the Any Type:

Reflection allows EPL to act on values of an any type object in a generic way, including altering the behavior to adapt to what fields or actions a type has. These values can be passed as an any parameter value to some common code  or created via the any.newInstance method.

Fields or entries of 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 built-in methods) and constants can be obtained with the following methods on 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 on 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 to the generic type to execute the action.

Examples using reflection capabilities of Any type

In this example, we have written a generic map action which takes a mapper action of type any  . The values of the sequence are mapped as per the mapper action provided. We have used the reflection capability “getGenericAction()” to allow callbacks to the mapper action, regardless of the signature of that action.

In the below example, we have written a generic factory method which creates an instance of a type based on the type name, and initializes it by calling its init method with the default value for each argument.

We get the argument types using reflection on the any value which contains the event instance. Using this we can know about the type names at runtime and create their instances using the “newInstance” method on the any type.

As always, please post on our forums or leave a comment below with any questions or statements you may have; can you come up with any use that we haven’t? Thanks, and happy developing!

– Ashish