Contents
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 insrc/main/configurations
. This file illustrates the use of themethodName
property to allow more than one alias to the same method, and illustrates the syntax forargumentTypes
as well asautoArguments
. - 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 > . - pom.xml
-
The Maven object model file for this project, placed at the root of the project folder.
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 theHypotenuse.java
file insrc/main/java
. The third expression uses thecalljava
expression language function to call thejava.lang.Math.hypot
method directly. The fourth expression uses an alias that calls the samejava.lang.Math.hypot
method. The final expression shows an aliasmx
forjava.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 classcom.tibco.ep.sample.VarArgs
, to determine whether input stringneedle
exists in any of the following arguments stringshaystack1
...haystack
, and returns a boolean result.n
- Input stream VarArgsSums
-
The bottom stream uses the custom function
SumAll
in theVarArgs
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.
In StreamBase Studio, import this sample with the following steps:
-
From the top-level menu, click
> . -
Enter
custom simple
to narrow the list of options. -
Select the Custom Java simple function sample from the Extending StreamBase category.
-
Click
.
StreamBase Studio creates a project for the sample.
-
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
, orRebuild project
messages finish before you proceed. -
Open the
src/main/eventflow/
folder.packageName
-
Double-click to open the
custom-functions.sbapp
application. Make sure the application is the currently active tab in the EventFlow Editor. -
Click the Run button. This opens the SB Test/Debug perspective and starts the module.
-
Wait for the Waiting for fragment to initialize message to clear.
-
Enter data for the
TriangleSidesIn
stream.-
In the Manual Input view, select the
TriangleSidesIn
input stream. -
Enter the following values in the
x
andy
fields, then press :x: 3
y: 4 -
In the Output Streams view, observe the emitted tuple. Look for four calculated hypotenuse values that are all
5.0
, plus the fieldaliasedMax
showing4.0
. -
Enter other values into the
x
andy
fields. The calculated hypotenuse number is identical, no matter which function is called, which which calling option.
-
-
Enter data for the
VarArgsIsIn
stream.-
In the Manual Input view, select the
VarArgsIsIn
input stream. Notice the three input fields:needle
, the string to search for, andhaystack1
andhaystack2
, the strings to search within. If either of the haystacks containsneedle
, the output field IsNeedleInHaystack will be true, else false. -
Enter the following unquoted strings in the respective fields:
needle: banana
haystack1: time flies like an arrow
haystack2: null -
In the Output Streams view, observe the emitted tuple. The value for
IsNeedleInHaystack
isfalse
because neitherhaystack1
norhaystack2
contains the stringbanana
. -
Now in the
haystack2
field, enter:fruit flies like a banana
-
Press
again. -
This time in the Output Streams view, the emitted tuple's value for
IsNeedleInHaystack
istrue
. -
Experiment with adding more
haystack
fields to the VarArgsIn input stream's schema. To get them included in the evaluation without changing then
IsIn
Java method, just append the same field names to the expression in theIsIn
Map operator.
-
-
Enter data for the
VarArgsSums
stream.-
In the Manual Input view, select the
VarArgsSum
input stream. It has three double fields;value1
,value2
, andvalue3
. -
Enter the following numbers in the respective fields:
value1: 10.
value2: 24.6.
value3: 365.2 -
In the Output Streams view, in the emitted tuple, look for the value of the field
sum
to be399.8
. -
Stop the application by pressing F9 or click the Terminate EventFlow Fragment button.
-
With
custom-functions.sbapp
selected in the EventFlow Editor, select the input streamVarArgsSums
. Add one more field to the input schema:value4
, typedouble
. -
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. -
In the Manual Input view, enter the following numbers in the respective fields:
value1: 10.
value2: 24.6
value3: 365.2
value4: 0.2 -
Press
again. -
In the Output Streams view, notice that emitted tuple's field
sum
is now410.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 thesum
output expression without having to edit the Java source file.
-
-
When done, press F9 or click the Terminate EventFlow Fragment button.
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.