Writing Your First EventFlow Application

Purpose

In this topic, you will edit your new MyFirstApp.sbapp EventFlow application to add some functionality. If you chose a different name for the file, that is fine; there is no need to rename it.

As described in Quick Facts: StreamBase Terms and Components, the EventFlow format is one of two ways of writing queries in StreamBase applications. If you prefer to develop only StreamSQL (ssql files) and do not plan to use StreamSQL queries, you can click Next to skip this topic.

Prerequisite

In the StreamBase Studio menu, click WindowPreferences. Open the StreamBase Studio page and verify that the default setting, Automatically typecheck components, is enabled. For this example, also enable the option, Show component types.

We will talk more about typechecking later in this tutorial.

To Build Your First EventFlow Application

To add functionality to your application:

  1. Double-click the MyFirstApp.sbapp file to open it in the EventFlow Editor. You can have multiple editors open in this area of your perspective, each with its own tab; your tab is labeled with the application file name, MyFirstApp.sbapp).

    Also notice the Palette view in the lower left of StreamBase Studio: when you first open an EventFlow application, the Palette is loaded with application components (it is empty when no EventFlow is open).

  2. In the Palette view, open the Streams category, then drag the Input Stream icon onto from the Palette into the EventFlow Editor (sometimes referred to as the canvas). The red arrow in the following example is added to indicate the drag and drop action:

    StreamBase Input Stream example

    The yellow background of the icon on the canvas indicates that this component does not typecheck yet, because there is no schema associated with it. We will explain typechecking soon, but for now let's go to the next step.

  3. On the canvas, click the input stream. When you click any component in the EventFlow Editor, StreamBase Studio displays its Properties view. Initially, the Properties view and the editor look like this:

    StreamBase Input Stream example
  4. In the Properties view, on the General tab, change the name of the input stream to ItemsInputStream.

  5. Then click the Edit Schema tab, and click the AddAdd button twice to add rows where you will define two fields for this schema. Initially, the fields are named UnnamedField1 and UnnamedField2.

    On the Edit Schema tab, click into the first row and enter these values for the first field:

    Name

    ITEM_NAME

    Type

    string (use the pull-down menu to select the data type)

    Size

    10

    Then click into the second row and enter these values for the second field:

    Name

    SKU

    Type

    int

    Size

    4 (default - cannot be set otherwise for an int)

    (In our simple example, SKU is a stock keeping unit.)

    At this point, the ItemsInputStream Properties view looks similar to the following:

    StreamBase Input Stream example

    Important

    There is no Save button on the Properties view or its tabs. The settings or changes that you enter are preserved automatically (but not yet written to disk) when you "deselect" the component on the canvas (the blue selection dots are no longer displayed). Or you can click the Apply and Typecheck button Apply and Typecheck button, near the top-right area of the Properties View:

    This allows you to apply changes you make on the Properties tabs, without having to save the entire application. You can, however, enter FileSave to write the application's .sbapp file to disk.

  6. Click the Outline view. Notice that, so far, the list of components in your StreamBase application contains the ItemsInputStream.

    StreamBase components example

    Back in the EventFlow Editor, notice how the color of your ItemsInputStream icon changed from yellow to white. That happened because the StreamBase Server running on behalf of StreamBase Studio (a client program) is now aware of the schema for this just-defined component, ItemsInputStream. (This assumes that automatic typechecking is enabled, as mentioned in the first step.)

  7. Open the Operators section of the Palette tab. Drag and drop a Filter operator from the palette onto the canvas. Its default name is Filter1. At this point, the canvas will look similar to this:

    StreamBase example canvas

    Its background color is yellow, again meaning its typecheck status is unknown, because it has not been connected yet to a component with a known and valid schema.

  8. Now click the Filter1 shape on the canvas. StreamBase Studio opens the Filter Properties view. In the General tab, change the name of the operator to skuGT5000Filter.

  9. Click the Predicate Settings tag. Click the green plus sign (+) to enter the following expression for predicate #1:

    SKU > 5000

    A predicate is an expression that always evaluates to true or false.

    Also select the Create output port for non-matching tuples option. (If we leave this option off, non-matching tuples will simply be omitted from the streaming data.) At this point the Filter Properties view should be similar to the following:

    StreamBase Filter predicate example

    At runtime when a tuple comes into this Filter operator, if its SKU is greater than 5000 (predicate #1 is TRUE), it will be sent out on port 1. Because of the way this particular filter operator is defined, all other tuples will be sent out on port #2. Thus for each tuple, the filter evaluates the expressions in order. When a predicate returns TRUE, the tuple is sent out on that port immediately, and subsequent predicates are not checked.

    On the canvas, StreamBase Studio shows the output ports as small black squares on the right side of the operator. The upper-most black square represents port 1; the next black square down represents port 2, and so on. In our example:

    StreamBase my first application example
  10. Now we need to connect the input stream to the filter operator. On the canvas, find the black square on the right side of the ItemsInputStream1 icon. Drag your cursor from that square to the black square on the left side of the filter operator. That square represents the operator's input port. StreamBase Studio draws an arc connecting the two components. Arcs represents internal streams within EventFlows. After the arc connects the two entities and you release your cursor, the input stream and the filter operator should turn white or transparent, indicating that both typechecked successfully. For example:

    StreamBase Filter operator's icon showing two output ports

    Conceptually, typechecking operates from the left side of the application's diagram — the starting points — and will proceed through the operators to the right. When the typecheck process finds an error condition, StreamBase changes the background color of the adjacent right-side operator to red. Also, operators to the right (or "downstream") of the offending operator are not typechecked yet. The typecheck can fail for a number of reasons, such as:

    • Incorrect operator parameters (for example, if the user edited the .sbapp file's XML directly, and entered an invalid parameter name or value).

    • Incorrect use of the expression language, such as an invalid character in a field name, again entered by a user who edited the sbapp XML directly.

    • An operator that does not have a schema declared yet.

    • An operator has a schema declared, but it is in conflict with the prior (upstream) connected operator.

    Note

    This is one of the advantages of designing in StreamBase Studio: its running sbd server performs typechecking automatically as you work.

  11. Now drag and drop an Output Stream from the Palette onto the canvas. Select the output stream on the canvas (click once).

  12. In the Output Stream Properties view, change its name to ItemsOutputStream.

  13. Click into a blank area of the canvas. Draw a connecting arrow:

    • From the upper output port (1) on the right side of the skuGT5000Filter icon.

    • To the input port on the left side of ItemsOutputStream icon.

    Your EventFlow application should look similar to this screen shot:

    StreamBase my first application example
  14. Drag and drop a second Output Stream onto the canvas. Select the output stream on the canvas (click once).

  15. In the Output Stream Properties view, change its name to AllTheRest.

  16. Draw a connecting arrow from the lower output port (2) to the AllTheRest output stream.

    To automatically arrange the components neatly on the canvas, you can click DiagramLayout Application.

    To improve the arrangement of arrows around components (for example, if an arc overlaps a component's name), try clicking DiagramReroute Connections.

    Now the layout of your EventFlow application should look similar to the following:

    StreamBase my first application example
  17. Before we save the application, let's click the Outline tab again. If you expand the lists, you should see these components:

    StreamBase my first application example
  18. Now select FileSave from the menu, and save your StreamBase Application (for example, MyFirstApp.sbapp).

Summary and Next Steps

In this topic you learned how to define tuple fields in a StreamBase schema, add components to your application, connect them together, and define functionality by editing the properties of components. You also learned something about working with StreamBase typechecking. Your application doesn't do much, but it's finished and ready to run.

Do not close StreamBase Studio. To continue the tutorial, please click Next to proceed to one of the following topics:

  • If you would like to create a StreamSQL application, complete the next topic, Creating Your First StreamSQL Application The result will be a complete application that does exactly the same work as the MyFirstApp.sbapp application, and in subsequent topics, you will be able to run either version of the application with identical results.

  • If you are not interested in using StreamSQL, skip the next topic: click Next one more time to go on to Running Your First StreamBase Application .

Back to Top ^