Contents
The Interfaces sample application demonstrates how to use StreamBase interfaces to enforce a set of stream and schema definitions, and to use different implementations of the same interface for different purposes.
The top-level application uses two Extension Point operators that each implement a different StreamBase interface. One Extension Point references two modules, while the other references one module. See This Sample's Files for a description of each interface and module.
This sample illustrates several aspects of Extension Point operators and StreamBase interfaces. There are two related samples that complete the picture on these subjects:
Sample | Description |
---|---|
Extension Point Operator Sample | This sample uses an Extension Point operator to dispatch tuples based on a user-specified action to one of two modules, which double or triple an integer field within the tuple. If no action is specified, the tuple is sent to both modules. This sample illustrates StreamBase interfaces used with the Extension Point operator, and illustrates Dispatch Styles based on a predicate condition. |
Extension Point Deployment Sample | This sample is a variation of the Extension Point operator sample to illustrate the use of StreamBase deployment files. The
StreamBase interface in this sample specifies externally defined modules. Two example .sbdeploy files are provided, which you use to run this sample.
|
The Interfaces sample includes the following files:
File | Purpose |
---|---|
TopLevel.sbapp |
The top-level application for this sample. The application populates the HistoricalData table on startup from a CSV file.
|
quote-history.csv |
A small CSV file used to load data into the HistoricalData table in TopLevel.sbapp .
|
QuotesToOrders.sbint |
A StreamBase interface that defines an input and an output stream, and defines named schemas used by those streams. |
QuotesToOrdersWithHistoricalData.sbint |
A StreamBase interface that extends the QuotesToOrders.sbint interface, adding a table schema and placeholder table to the streams and schema definitions imported from QuotesToOrders.sbint .
|
BuyOrSellHalfQ2O.sbapp |
A module that implements the streams and schemas defined in the QuotesToOrders.sbint interface. This module determines whether to sell or buy based on the odd or evenness of the incoming price field. It contains
a deliberate error in the BuyOrSellHalf Map operator, which will send an order to the UnexpectedOrders stream. Tuples processed by this module have the prefix Half- in their order IDs.
|
RandomBuyOrSellQ2O.sbapp |
Another module that implements the streams and schemas defined in the QuotesToOrders.sbint interface. This module determines at random whether to buy or sell. Tuples processed by this module have the prefix Rndm- in their order IDs.
|
OrderIfBetterThanYesterday.sbapp |
A module that implements the streams, named schemas, table schema, and placeholder table defined in the QuotesToOrdersWithHistoricalData.sbint interface. This module attempts to match the incoming symbol field against the same field in the HistoricalData table. If
a match is found, the module sends a buy order if the incoming quote price is lower than yesterday's low price from the table,
or sends a sell order if the incoming price is higher than yesterday's high price. Tuples processed by this module have the
prefix Yester- in their order IDs.
|
-
In the Package Explorer, open the
sample_interfaces
project folder. -
Double-click to open the
TopLevel.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 application.
-
In the Manual Input view, select the QuotesIn input stream.
-
Enter
ibm
,119.00
, and2000
in thesymbol
,price
, andvolume
fields, respectively. (You can enter the stocksymbol
field in lowercase, because the first Map operator converts that field to uppercase.) -
Click
, and observe data like the following in the Application Output view. The first line might be a Buy order in your case, because it is processed by the module that randomizes buy and sell orders.Output Stream Fields SellOrders Time Output Stream Fields 13:56:38 SellOrders order_id=Rndm-1, symbol=IBM, order_type=sell, price=119.0, volume=2000 UnexpectedOrder Time Output Stream Fields 13:56:38 UnexpectedOrders order_id=Half-1, symbol=IBM, order_type=sellTYPOHERE, price=119.0, volume=1000 -
Notice that there is an order on the
UnexpectedOrder
stream. This is because the sample as shipped contains a deliberate error to force at least one case of unexpected orders. The Filter operator that splits the output into three streams looks for a match in theorder_type
field of eitherbuy
orsell
. However, one of the application's modules,BuyOrSellHalfQ2O.sbapp
, outputs eitherbuy
orsellTYPOHERE
, instead ofsell
. -
[Optional] You can choose to stop the sample now to correct the sample's error, then restart it. If so, follow the sub-steps below; if not, continue on with step 9.
-
Stop the application by pressing F9 or click the
Stop Running Application button.
-
In the Package Explorer, in the
sample_interfaces
project folder, double-clickBuyOrSellHalfQ2O.sbapp
to open it. -
Double-click the
BuyOrSellHalf
Map operator to open its Properties view. -
In the Properties view, locate the Expression column corresponding to the
order_type
output field. -
Remove
TYPOHERE
from the expression. -
Save
BuyOrSellHalfQ2O.sbapp
. -
Switch back to the
TopLevel.sbapp
application, save it, and click theRun button to restart it.
-
Re-enter the same data as before:
ibm
,119.00
, and2000
. -
Click
, and observe that this time, there are two orders on theSellOrders
stream:Output Stream Fields SellOrders Time Output Stream Fields 14:30:59 SellOrders order_id=Rndm-1, symbol=IBM, order_type=sell, price=119.0, volume=2000 SellOrders Time Output Stream Fields 14:30:59 SellOrders order_id=Half-1, symbol=IBM, order_type=sell, price=119.0, volume=1000 The prefix on the order_id field (
Half-
orRndm-
) tells you which sub-module processed the order. The first order was processed byRandomBuyOrSellQ2O.sbapp
, while the second order was processed byBuyOrSellHalf.sbapp
.
-
-
Enter similar data, but this time use an even number for the price field:
ibm
,120.00
, and2000
. -
Click
, and observe data like the following in the Application Output view. Remember that your results may vary because of processing by the module that randomizes buy and sell orders.Output Stream Fields SellOrders Time Output Stream Fields 14:33:24 SellOrders order_id=Rndm-2, symbol=IBM, order_type=sell, price=120.0, volume=3000 BuyOrders Time Output Stream Fields 14:33:24 BuyOrders order_id=Half-2, symbol=IBM, order_type=buy, price=120.0, volume=1500 BuyOrders Time Output Stream Fields 14:33:24 BuyOrders order_id=Yester-1, symbol=IBM, order_type=buy, price=120.0, volume=3000 -
Continue entering data in the Manual Input view to see the results. Keep in mind the following points:
-
The
BuyOrSellHalf.sbapp
module is set up to process quotes with even prices as buy orders, and odd prices as odd prices as sell orders. -
The
OrderIfBetterThanYesterday.sbapp
module only gets involved if thesymbol
you input is found in theHistoricalData
table, and the price meets certain criteria. TheHistoricalData
table is loaded from thequote-history.csv
file. Open that CSV file to determine the symbols and prices listed. To see this module in action, enter a price above the number in the first column of the CSV file, or below the number in the third column.
-
-
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 your workspace copy of the sample, as described above.
-
In window 1, type:
sbd TopLevel.sbapp
The window shows
notice[StreamBaseServer] listening on port 10000
. -
In window 2: type:
sbc dequeue
No output is displayed at this point, but the dequeuer is prepared to receive output. This window will eventually show the output of all three streams of the top-level application.
-
In window 3, type:
sbc enqueue QuotesIn
The sbc command is now awaiting keyboard input.
-
In windows 3, type the following. (You can enter the stock
symbol
field in lowercase, because the first Map operator inTopLevel.sbapp
converts that field to uppercase.)ibm, 119.00, 2000
Observe lines like the following in window 2. The prefix for the second field (
Half-
orRndm-
) tells you which sub-module processed the order. The first line might be a Buy order in your case, because it is processed by the module that randomizes buy and sell orders.SellOrders,Rndm-1,IBM,sell,119,2000 UnexpectedOrders,Half-1,IBM,sellTYPOHERE,119,1000
-
[Optional] Stop the application in order to fix the sample's error, then restart the application.
-
In window 3, type Ctrl+C to end the sbc session, then type:
sbadmin shutdown
-
In Studio, open the
BuyOrSellHalfQ2O.sbapp
application to correct its deliberate error, as described in step 8 of the previous section. -
Repeat steps 2, 3, and 4 of this procedure. In window 3, type the same data as step 5:
ibm, 119.00, 2000
This time, observe a different set of output lines in window 2.
-
-
In window 3, type another entry:
ibm, 120.00, 3000
Observe lines like the following in window 2. Your results may vary because of processing by the module that randomizes buy and sell orders.
BuyOrders,Half-2,IBM,buy,120,1500 BuyOrders,Yester-1,IBM,buy,120,3000 SellOrders,Rndm-2,IBM,sell,120,3000
-
Continue entering data in window 3. Keep in mind the following points:
-
The
BuyOrSellHalf.sbapp
module is set up to process quotes with even prices as buy orders, and odd prices as odd prices as sell orders. -
The
OrderIfBetterThanYesterday.sbapp
module only gets involved if thesymbol
you input is found in theHistoricalData
table, and the price meets certain criteria. TheHistoricalData
table is loaded from thequote-history.csv
file. Open that CSV file to determine the symbols and prices listed. To see this module in action, enter a price above the number in the first column of the CSV file, or below the number in the third column.
-
-
In window 3, type: Ctrl+C to exit the sbc session.
-
In window 3, type the following command to terminate the server and dequeuer:
sbadmin shutdown
In StreamBase Studio, import this sample with the following steps:
-
From the top menu, select
→ . -
Select
interfaces
from the Applications category. -
Click OK.
StreamBase Studio creates a single project for all the operator samples.
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 the permission problems that can occur when trying to work with the initially installed location of the sample. The default workspace location for this sample is:
studio-workspace
/sample_interfaces
See Default Installation Directories for the location of studio-workspace
on your system.
In the default TIBCO StreamBase installation, this sample's files are initially installed in:
streambase-install-dir
/sample/interfaces
See Default Installation Directories for the default location of studio-workspace
on your system.