Contents
The math.sbapp sample application demonstrates a custom
Java simple function. The application takes two doubles as input representing the
lenghts of the right angle sides of a triangle and outputs the hypotenuse of these
inputs using several different methods.
The application generates the output using these three expressions in a Map operator:
-
hypotenuse(x, y) -
hypot(x, y) -
calljava('java.lang.Math', 'hypot', x, y)
The first expression uses a function alias defined in the sbd.sbconf file that will use the Hypotenuse class defined in the
Hypotenuse.java sample file. The second expression uses a
function alias that will use the java.lang.math method. The
final expression uses calljava function to call the
java.lang.math method directly.
This topic describes how to load and run the math.sbapp
sample. For more information about custom Java functions, see the topic, Using the StreamBase Java Function
Wizard, in the API Guide.
By default, the sample files are installed in:
-
On Windows:
C:\Program Files\StreamBase Systems\StreamBase.n.m\sample\custom-java-function -
On UNIX:
/opt/streambase/sample/custom-java-function
When you load the sample into StreamBase Studio, Studio copies the
sample project's files to your Studio workspace. StreamBase Systems
recommends that you use the workspace copy of the sample, especially on UNIX, where
you may not have write access to /opt/streambase. In
the default installation, the path to this sample in your Studio workspace is:
UNIX: ~/streambase-studio-n.m-workspace/sample_custom-java-function Windows XP: C:\Documents and Settings\username\My Documents\StreamBase Studion.mWorkspace\ sample_custom-java-function Windows Vista: C:\Users\username\Documents\StreamBase Studion.mWorkspace\ sample_custom-java-function
The sample has of the following files:
-
The source code for the function,
Hypotenuse.java -
A sample configuration file,
sbd.sbconf, which tells StreamBase Server to load the custom function and defines function aliases. -
A sample application,
math.sbapp, which shows different ways to invoke the function. -
An ant
build.xmlfile that can be used to create the function's JAR file.
In StreamBase Studio, import this sample with the following steps:
-
From the top menu, click → .
-
Select the custom-java-function sample from the Extending StreamBase list.
-
Click OK.
StreamBase Studio creates a project for each sample.
-
In the Package Explorer, double-click to open the
math.sbappapplication. 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 application.
-
When the server starts, StreamBase Studio switches to the SB Test/Debug perspective.
-
On the Manual Input view, enter the following values in the
xandyfields, then press :x: 2
y: 2 -
In the Application Output view, observe the tuple emitted when you press . You should see the three calculated hypotenuse values are all identical and are approximately 2.83.
-
When done, press F9 or click the
Stop Running Application button.
This section describes how to run the sample in UNIX terminal windows or Windows command prompt windows. On Windows, be sure to use the StreamBase Command Prompt from the Start menu as described in the Test/Debug Guide, not the default command prompt.
-
Open three terminal windows on UNIX, or three StreamBase Command Prompts on Windows. In each window, navigate to the directory where the sample is installed, or to your workspace copy of the sample, as described above.
-
In window 1, launch the StreamBase Server for the sample application:
sbd -f sbd.sbconf math.sbapp -
In window 2, run a dequeuer so that you can see the output that will be produced:
sbc dequeue -
In window 3:
-
Run an enqueuer:
sbc enqueue InputStream1 -
Type x and y values, separated by commas, into the enqueuer.
-
-
In window 2, look for output from the application.
-
In window 2, type: Ctrl-Z (Windows) or Ctrl-D (UNIX) to close the sbc command.
-
In window 3, type:
sbadmin shutdown. This terminates the server and the dequeuer.
-
Created the custom Java application:
-
Edited and saved
Hypotenuse.javain a text editor. -
Built
Hyponetuse.javausing our favorite Java development tools, to generateHypotenuse.jar:Note
The installation includes a build file,
build.xml, which you can use to buildHypotenuse.javaif ant is installed on your system. Additionally, on windows a solution file is provided.
-
-
Launched StreamBase Studio
-
Created the custom-java-function project. Selected the option to Create template server configuration file.
-
From the top menu, in the SB Authoring perspective, selected → → . Selected the
custom-java-functionproject, and enteredmath.sbappfor the diagram name. -
Created an input stream:
-
Dragged an input stream from the palette to the canvas.
-
Clicked it on the canvas, which invoked the Input Stream Properties view.
-
On the Edit Schema tab, added:
Field Name:
x, Type:double, Size:8Field Name:
y, Type:double, Size:8
-
-
Set up the Map operator:
-
Dragged a Map operator from the palette to the canvas and opened its Properties view.
-
Dragged a connector from the Input Stream to the input of the Map1 operator.
-
On the Output Settings tab, clicked on the button to add a new row. Edited the row fields as follows:
-
Action:
Add -
Field Name :
customHypot -
Expression:
hypotenuse(x, y)
-
-
Clicked the button to add another row:
-
Action:
Add -
Field Name :
directMathHypot -
Expression:
calljava('java.lang.Math', 'hypot', x, y)
-
-
Clicked the button to add another row:
-
Action:
Add -
Field Name :
aliasedMathHypot -
Expression: hypot(x, y)
-
-
-
Created an output stream:
-
Dragged an output stream from the palette to the canvas.
-
Connected the Map1 operator to the output stream.
-
-
Double-clicked
sbd.sbconfin the Resources folder to open it in the StreamBase editor. In the file, removed unnecessary template code and added ajava-vmelement referring to theHypotenuse.jarcustom Java program. Also add the twocustom-functionalias definitions.
