- Index

< Previous |  | Next >

# Type Coercion Sample

## Introduction

This sample illustrates how certain numeric data types can coerce; that is, how same-named field values with certain compatible types can all appear to be carried by a new field with a type compatible with all original values. It consists of a Studio project containing a single EventFlow module file.

The module illustrates some of the basic types that are coercible:

- int and double coerce to double

- int and long coerce to long

Coercion also takes place when the fields are lists-of each type, or are individual fields within a field of type tuple.

Rearrange the arcs and try connecting the unconnected input streams to explore which types do not coerce: double will not coerce to long (loss of data from long type) no types coerce to string, timestamp or blobs

For details on type coercion, see Data Type Coercion and Conversion in the StreamBase Reference documentation.

## Importing This Sample into StreamBase Studio

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

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

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

- Select coercion from the Applications category.

- Click OK.

StreamBase Studio creates a project for each sample.

## Running This Sample in StreamBase Studio

In the EventFlow Editor, you can explore which data types can be coerced together and which may not. Follow these steps to explore this sample in the EventFlow editor:

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 `Coercions.sbapp` file in the `src/main/eventflow`packageName`` folder.

   Notice that there are two groups of input streams, union operators, and output streams, called Lists and Tuples . The only difference between them is their schemas.

3. In the Lists group, hover the mouse pointer over the arc exiting the intmeetslong Union operator (bottom right).

   Notice that its schema is list(long). This is because it is coercing its inputs, one of which is list(int) and the other of which is list(long), to data type long.

4. Click on the arc exiting the input stream ListOfDouble near its arrowhead.

   Notice that its schema is list(double).

5. Drag the arc away from the intmeetsdouble Union operator and instead connect it to the intmeetslong Union operator.

   A new input port on the intmeetslong Union operator appears to receive the arc.

6. Again, hover the mouse pointer over the arc exiting the intmeetslong Union operator.

   Notice that the schema of that arc has changed from list(long) to list(double) as the result of type coercion.

7. Try connecting the input streams ListOfTimestamp and ListOfString to either Union operator in its group. What happens? Hover over the Union operator to see.

   If you want to undo any change you have made to the diagram, click Edit → Undo or type Ctrl+Z (Windows) or command+Z (Mac).

8. Repeat any of the above steps on the components in the Tuples group. Observe how it behave similarly to the Lists group.

9. If the application has no typecheck errors, click the Run button to send lists and tuples to it. (You may be asked to save the application first). The SB Test/Debug perspective opens and the module starts.

10. In the Manual Input view, select ListOfInts.

11. Click the green + button to the right of mylist to add a list element.

12. Enter an integer in the mylist(0) field

13. Optionally repeat steps 9 and 10 to add more list elements and enter their values.

14. Click Send Data.

    In the Output Streams view, observe the values in the OutListOfLong and OutListOfDouble streams and notice where type coercion has occurred.

15. Select a different input stream and repeat steps 10–13 to observe when coercion takes place.

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

## This Sample's Files

The Type Coercion sample consists of the files shown in the following table.

File | Description
--- | ---
`Coercions.sbapp` | An EventFlow module illustrates which data types are compatible and in what ways.
`Coercions.sblayout` | The file describing how the application is laid out in StreamBase Studio.

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

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