When editing properties for a StreamBase operator in the Properties View of StreamBase Studio, you can define one or more dynamic variables for that operator, and then use the variables in the operator's expressions. A dynamic variable can be updated by any Input Stream or Output Stream in your application.
This topic discusses the use of dynamic variables in .sbapp EventFlows. For related information about using dynamic variables in .ssql StreamSQL applications, see the DECLARE Statement topic in the StreamSQL Guide.
Also, see Using Dynamic Variables, an article on the StreamBase Developer Zone website.
The following EventFlow example shows you how to define, initialize, and update a dynamic variable. The example takes prices from an input stream and sends them to two output streams: one for prices above a certain threshold; another for prices below that threshold.
To split the prices, we will use a Filter operator whose predicate compares each incoming price to the threshold. , we define the threshold make it a dynamic variable. In addition to the input stream containting two input streams: one to change the value of our threshold, we pass in a new value using another input stream. That way, at run time we can pass in different threshold values, changing the behavior of the application on the fly.
Follow these steps:
-
Create a new EventFlow application in StreamBase Studio. For example, create or open a StreamBase project, and click → → .
-
With your new application open in the EventFlow Editor, let us first add an input stream that can be used to update the dynamic variable:
-
Drag an input stream from the Palette to the canvas, and open the input stream's Properties view.
-
In the General tab, change the name to
UpdateVariable. -
In the Edit Schema tab, add a double field named
NewThreshold.
Note that updating streams do not need to be connected to any other component; as you will see, it can be used as is to update dynamic variables.
-
-
Now for the main part of the application: First, add the input stream that will receive price data: Drag an input stream from the Palette to the canvas, and rename it PricesIn. For the schema, add a double field named Price.
-
Drag a Filter operator from the Palette to the canvas, and connect the PricesIn stream to it.
-
Open the Filter1 operator's Properties view. In the Dynamic Variables tab, click and fill in the columns as follows:
Variable Name Updating Stream Updating Expression Initial Value Threshold UpdateVariableIn NewThreshold 100 This defines a local dynamic variable called Threshold. Its initial value (
100) will be used when the application starts. After that, the value of Threshold will be reset to the value of NewThreshold whenever a tuple arrives on the UpdateVariableIn stream. -
In the Predicate Settings tab:
-
Enable the Create output port for non-matching tuples option.
-
For Output Port 1, enter this Predicate expression:
Price <= Threshold
This adds a second output port to the Filter operator, and evaluates each arriving tuple: if the expression resolves to "true" (that is, Price is less than or equal to the Threshold), the output is emitted on Output Port 1; if the expression if false, it is sent to Port 2.
-
-
Drag two output streams to the canvas. Name them LowPricesOut and HighPricesOut. Connect these streams to the top and bottom output ports (respectively) of the Filter1 operator.
-
Your application should now typecheck. Save the application.
You can now run this application within Studio and manually enqueuing values on the Input Stream, to observe what values get output on which streams. Manually input a new threshold on the UpdateVariableIn stream and confirm that the Filter's behavior has changed.
Note that any input or output stream can update a dynamic variable. A more advanced
use of dynamic variables is to create an output stream at any point in your
application, resulting in a kind of feedback. As an example, you could make the
output stream of the Filter's second port be the updating stream, using an updating
expression of NewThreshold * 2. This would double the
threshold every time a value exceeded the threshold.
