Custom Java Simple Function Sample

Introduction

This sample demonstrates how to configure and call custom Java simple functions in an EventFlow module.

This sample's file's are:

custom-functions.sbapp

The sample EventFlow module file, placed in In src/main/eventflow/com.tibco.ep.sample.

sbengine.conf

The important sbengine type configuration file that defines aliases for the Java methods we want to call, placed in src/main/configurations. This file illustrates the use of the methodName property to allow more than one alias to the same method, and illustrates the syntax for argumentTypes as well as autoArguments.

Hypoteneuse.java, VarArgs.java

Java files containing our example custom classes, placed in src/main/java/com.tibco.ep.sample. Notice that the package name for these Java files is the same here as for the EventFlow module, but this is not a requirement.

TestCase.java

An EventFlow JUnit test file, configured and ready to run, placed in src/test/java/com.tibco.ep.sample. Run this test by selecting this file, right-clicking, and from the context menu, selecting Run As>EventFlow Fragment Unit Test.

pom.xml

The Maven object model file for this project, placed at the root of the project folder.

Description

The custom-functions.sbapp EventFlow module contains three independent streams.

Input stream TriangleSidesIn

The top stream illustrates four ways to calculate the hypotenuse of a right triangle, given two doubles representing the lengths of the right angle sides, plus a report on which of the two inputs is the maximum.

The module generates the output using the following expressions in a Map operator:

  • hypotenuse(x, y)

  • calchyp(x, y)

  • calljava('java.lang.Math', 'hypot', x, y)

  • hypot(x, y)

  • mx(x, y)

The first two expressions illustrate the use of two different aliases to the same method, as defined in this sample's configuration file. These are aliases for the calculate method in the Hypotenuse class defined in the Hypotenuse.java file in src/main/java. The third expression uses the calljava expression language function to call the java.lang.Math.hypot method directly. The fourth expression uses an alias that calls the same java.lang.Math.hypot method. The final expression shows an alias mx for java.lang.Math.max.

The two lower streams illustrate how custom Java functions can take variable-length arguments. The functions have aliases in the project's configuration file that enables them to be called directly (as opposed to being invoked with the calljava() expression language function). Note that a function alias can have the same name as the function it refers to, or can have a different name.

Input stream VarArgsIn

The middle stream uses the custom function IsIn, defined in class com.tibco.ep.sample.VarArgs, to determine whether input string needle exists in any of the following arguments strings haystack1 ... haystackn, and returns a boolean result.

Input stream VarArgsSums

The bottom stream uses the custom function SumAll in the VarArgs class to calculate the sum of any number of doubles and returns a double result.

To keep the example simple, the custom functions do not validate their inputs at runtime.

Importing This Sample into StreamBase Studio

In StreamBase Studio, import this sample with the following steps:

  • From the top-level menu, click File>Import Samples and Community Content.

  • Enter custom simple to narrow the list of options.

  • Select the Custom Java simple function sample from the Extending StreamBase category.

  • Click Import Now.

StreamBase Studio creates a project for the sample.

Running This Sample in StreamBase Studio

  1. In the Project Explorer view, open this sample's folder.

    Keep an eye on the bottom right status bar of the Studio window. Make sure any Updating, Downloading, Building, or Rebuild project messages finish before you proceed.

  2. Open the src/main/eventflow/packageName folder.

  3. Double-click to open the custom-functions.sbapp application. Make sure the application is the currently active tab in the EventFlow Editor.

  4. Click the Run button. This opens the SB Test/Debug perspective and starts the module.

  5. Wait for the Waiting for fragment to initialize message to clear.

  6. Enter data for the TriangleSidesIn stream.

    1. In the Manual Input view, select the TriangleSidesIn input stream.

    2. Enter the following values in the x and y fields, then press Send Data:

      x: 3
      y: 4

    3. In the Output Streams view, observe the emitted tuple. Look for four calculated hypotenuse values that are all 5.0, plus the field aliasedMax showing 4.0.

    4. Enter other values into the x and y fields. The calculated hypotenuse number is identical, no matter which function is called, which which calling option.

  7. Enter data for the VarArgsIsIn stream.

    1. In the Manual Input view, select the VarArgsIsIn input stream. Notice the three input fields: needle, the string to search for, and haystack1 and haystack2, the strings to search within. If either of the haystacks contains needle, the output field IsNeedleInHaystack will be true, else false.

    2. Enter the following unquoted strings in the respective fields:

      needle:    banana
      haystack1: time flies like an arrow
      haystack2: null

    3. In the Output Streams view, observe the emitted tuple. The value for IsNeedleInHaystack is false because neither haystack1 nor haystack2 contains the string banana.

    4. Now in the haystack2 field, enter:

      fruit flies like a banana

    5. Press Send Data again.

    6. This time in the Output Streams view, the emitted tuple's value for IsNeedleInHaystack is true.

    7. Experiment with adding more haystackn fields to the VarArgsIn input stream's schema. To get them included in the evaluation without changing the IsIn Java method, just append the same field names to the expression in the IsIn Map operator.

  8. Enter data for the VarArgsSums stream.

    1. In the Manual Input view, select the VarArgsSum input stream. It has three double fields; value1, value2, and value3.

    2. Enter the following numbers in the respective fields:

      value1: 10.
      value2: 24.6.
      value3: 365.2

    3. In the Output Streams view, in the emitted tuple, look for the value of the field sum to be 399.8.

    4. Stop the application by pressing F9 or click the Terminate EventFlow Fragment button.

    5. With custom-functions.sbapp selected in the EventFlow Editor, select the input stream VarArgsSums. Add one more field to the input schema: value4, type double.

    6. Select the SumAll Map operator. In the Output Settings tab, change the expression to sumAll(value1,value2,value3,value4), save the application, and run it again.

    7. In the Manual Input view, enter the following numbers in the respective fields:

      value1: 10.
      value2: 24.6
      value3: 365.2
      value4: 0.2

    8. Press Send Data again.

    9. In the Output Streams view, notice that emitted tuple's field sum is now 410.0.

      Because the Java function sumAll() takes a variable list of arguments, you can add any number of double fields to the input schema and reference them in the sum output expression without having to edit the Java source file.

  9. When done, press F9 or click the Terminate EventFlow Fragment button.

Sample Location

When you load the sample into StreamBase® Studio, Studio copies the sample project's files to your Studio workspace, which is normally part of your home directory, with full access rights.

Important

Load this sample in StreamBase® Studio, and thereafter use the Studio workspace copy of the sample to run and test it, even when running from the command prompt.

Using the workspace copy of the sample avoids permission problems. The default workspace location for this sample is:

studio-workspace/sample_custom-java-function

See Default Installation Directories for the default location of studio-workspace on your system.