- Index

< Previous |  | Next >

# Interfaces Sample

## About This Sample

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 below for a description of each interface and module.

This sample illustrates several aspects of Extension Point operators and StreamBase interfaces. For further exploration of the Extension Point operator, see the Extension Point Operator Sample.

## Importing This Sample into StreamBase Studio

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

- From the top-level menu, select File → Load StreamBase Sample.

- Enter `interf` to narrow the list of options.

- Select the interfaces sample from the Applications category.

- Click OK.

StreamBase Studio creates a project for the sample.

## Running This Sample in StreamBase Studio

1. In the Project Explorer view, open the sample you just loaded.

   If you see red marks on a project folder, wait a moment for the project to load its features.

   If the red marks do not resolve themselves after a minute, select the project, right-click, and select Maven → Update Project from the context menu.

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

3. Open the package folder.

4. Open the `TopLevel.sbapp` file and click the Run button. This opens the SB Test/Debug perspective and starts the module.

5. In the Manual Input view, select the QuotesIn input stream.

6. Enter `ibm`, `119.00`, and `2000` in the `symbol`, `price`, and `volume` fields, respectively. (You can enter the stock `symbol` field in lowercase, because the first Map operator converts that field to uppercase.)

7. Click Send Data, and observe data like the following in the Output Streams 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

8. 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 the `order_type` field of either `buy` or `sell`. However, one of the application's modules, `BuyOrSellHalfQ2O.sbapp`, outputs either `buy` or `sellTYPOHERE`, instead of `sell`.

9. [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.

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

   2. In the Project Explorer view, open the sample you just loaded.

   3. Open the `src/main/eventflow/`packageName`` folder.

   4. Open the `com.tibco.sb.sample.interfaces.modules` package folder.

   5. Double-click `BuyOrSellHalfQ2O.sbapp` to open it.

   6. Double-click the `BuyOrSellHalf` Map operator to open its Properties view.

   7. In the Properties view, locate the Expression column corresponding to the `order_type` output field.

   8. Remove `TYPOHERE` from the expression.

   9. Save `BuyOrSellHalfQ2O.sbapp`.

   10. Switch back to the `TopLevel.sbapp` application, save it, and click the Run button to restart it.

   11. Re-enter the same data as before: `ibm`, `119.00`, and `2000`.

   12. Click Send Data, and observe that this time, there are two orders on the `SellOrders` stream:

       Output Stream | Fields
       --- | ---
       SellOrders | Time Output Stream Fields 14:30:59 SellOrders order_id=Random-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-` or `Random-`) tells you which sub-module processed the order. The first order was processed by `RandomBuyOrSellQ2O.sbapp`, while the second order was processed by `BuyOrSellHalf.sbapp`.

10. Enter similar data, but this time use an even number for the price field: `ibm`, `120.00`, and `2000`.

11. Click Send Data, and observe data like the following in the Output Streams 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=Random-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

12. 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 the `symbol` you input is found in the `HistoricalData` table, and the price meets certain criteria. The `HistoricalData` table is loaded from the `quote-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.

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

## This Sample's Files

The Interfaces sample includes the following files:

In src/main/eventflow/com.tibco.sb.sample.interfaces:
  TopLevel.sbapp

    The top-level application for this sample. The application populates the `HistoricalData` table on startup from a CSV file.

  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`.

In src/main/eventflow/com.tibco.sb.sample.interfaces.modules:
  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.

  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.

  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.

In src/main/resources:
  quote-history.csv

    A small CSV file used to load data into the `HistoricalData` table in `TopLevel.sbapp`.

## 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_interfaces

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