www.espertech.comDocumentation

Chapter 18. Inlined Classes

18.1. Overview
18.2. Declaring a Local Class
18.3. Declaring a Global Class
18.4. Performance Notes
18.5. Known Limitations

Your application can add Java class code (or C# class code for NEsper) directly into the EPL. The EPL compiler compiles the classes your application provides and makes the classes available for use within EPL. Classes can be local i.e. visible to the current module only, or global i.e. visible to other modules according to EPL-object visibility rules. The typical use of classes is to perform imperative programming or provide utility, integration or extension functionality.

For declaring classes that are visible across multiple statements i.e. globally visible classes please consult Section 18.3, “Declaring a Global Class” that explains the create inlined_class clause. Inlined-classes are EPL-objects following visibility rules according to Section 15.2.2, “EPL-objects”.

Inlined classes can provide single-row functions as described in Section 22.2.1, “Using an Inlined Class to Provide a Single-Row Function”.

Inlined classes can provide aggregation single-functions as described in Section 22.5.1.4, “Using an Inlined Class to Provide an Aggregation Single-Function”.

Inlined classes can provide aggregation multi-functions as described in Section 22.5.2.8, “Using an Inlined Class to Provide an Aggregation Multi-Function”.

Inlined classes can serve as a variable type. The create variable clause allows the class name of an inlined class as a variable type.

A local inlined class is a class that is only visible to the same EPL statement that it is part of. The class is not visible to other EPL statements in the same module and is not visible to other modules.

The syntax for inlined classes is:

inlined_class """ class-text """

Use the inlined_class keyword to declare a class. Place the class-text class code within three double-quotes (""").

The class-text class code is a Java class, i.e. public class class-name{...} or public enum class-name{...}.

The class text may specify a package name and imports. The class text is subject to limitations as listed below.

The next example shows a statement that calls a static method of an inlined-class by name MyUtility which computes the Fibonacci total for a given number:

inlined_class """
  public class MyUtility {
    public static double fib(int n) {
      if (n <= 1)
        return n;
      return fib(n-1) + fib(n-2);
    }
  }
"""
select MyUtility.fib(intValue) from SomeEvent

Use the create inlined_class syntax to declare an inlined class. The compiler remembers the class and allows the class to be referenced by other statements of the same module and by other modules.

The below EPL declares a globally visible class by name MyUtility that provides a method midPrice that computes a mid-price and that a buy and sell parameter:

create inlined_class """ 
  public class MyUtility {
    public static double midPrice(double buy, double sell) {
      return (buy + sell) / 2;
    }
  }
"""

The next EPL returns mid-price for each event:

select MyUtility.midPrice(buy, sell) from MarketDataEvent

The class name must be unique for global inlined classes. It is not possible to declare the same global inlined-class twice with the same class name and the same module name.

The runtime, at the time your application deploys a module that references global inlined classes, provides a classloader that adds the imported inlined classes that are provided by the deployment that contains the inlined classes. Thus the compiler and runtime never copy or otherwise duplicate the inlined class or class byte code.

The EPL compiler compiles the inlined classes to byte code and the runtime loads the byte code using a classloader that is aware of the inlined classes.

It is recommended that inlined class code executes non-blocking.

It is recommended that inlined class code is stateless. Please use plug-in aggregation functions or variables instead.

It is recommended that inlined class code is thread safe. EPL execution generally allows certain EPL statements and partitions to execute lock-free.

The compiler for inlined classes is Janino. Janino does not support all of Java 8 and higher. Please see the Janino documentation for more information.

The following limitations apply: