Reactors¶
Reactor Blocks can be used within Steps and UI Steps Languages. They are similar to Models but their function bodies cannot contain calls to most Built-ins to ensure thread safety. Reactor functions can be referenced as well and therefore are able to be passed as parameters to other functions, as shown at the bottom of this section.
Note:
The Reactor
states can only be simple data types, such as number, string, boolean or regex.
Syntax
1 2 3 4 5 6 | reactor <reactorName Identifier> func <reactorFunction Identifier>(<parameter1 ImmutableType>, ... , parameterN ImmutableType>) THREAD SAFE BLOCK end ... end |
Parameters
-
reactorName - A unique identifier assigned to the reactor which should not overlap with model names
-
reactorFunction - The reactor function's identifier
-
parameterX - The parameters passed to the
THREAD SAFE BLOCK
- The
reactor
states can only be immutable types, such as number, string, boolean or regex
- The
Example
1 2 3 4 5 6 7 8 9 10 | stepdef "gauss sum formula result of the value: {}" / gaussNumber / GaussReactorFunction := GaussSumReactor::gaussianFormula(gaussNumber) println(GaussSumReactor.result) end reactor GaussSumReactor func gaussianFormula(gaussN) GaussSumReactor.result := (1 / 2) * (gaussN) * (gaussN + 1) end end |
Note:
The THREAD SAFE BLOCK
can only access plain functions and JMS Built-in language expressions.
Reactor Function Reference¶
Syntax
1 | <functionReference FunctionRef(...)> := <reactorName Identifier>::<reactorFunction Identifier> |
Returns
The function reference to the Reactor function.
Example
1 | calculateGaussFunc := GaussReactor::gaussianFormula |
Invoking Reactor Functions¶
The referenced Reactor function is invoked by using the following syntax:
Syntax
1 | <returnValue Any> := <functionReference FunctionRef(...)>(<parameter1 ImmutableType>, ... , parameterN ImmutableType>) |
Returns
Any value that the Reactor function reference returns by invoking it.
Parameters
-
functionReference - The function reference to the reactor function
-
parameterX - The parameters passed to the
THREAD SAFE BLOCK
- The
reactor
states can only be immutable types, such as number, string, boolean or regex
- The
Example
1 2 | calculateGaussFunc := GaussReactor::gaussianFormula calculateGaussFunc(GaussN) |
Using Reactors with Stepdefs¶
In the following example, a Stepdef calls the Reactor MainReactorAdd
with its add
function:
Steps Example
1 2 3 4 5 6 7 8 9 10 11 | stepdef "Add number a: {} to number b: {}" / a,b / calculateAdd := AddReactor::add calculateAdd(a, b) println(AddReactor.result) end reactor AddReactor func add(a, b) AddReactor.result := a + b end end |
Feature File Example
1 2 3 | Feature: SimpleReactorFunction Scenario: AddReactor And Add number a: 3 to number b: 5 |
Reactor Functions Passed As Parameters ¶
In the following example, a reference to the Reactor's calculateFibonacci
function is used as a parameter for Steps function inside a Stepdef.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | stepdef "pass the fibonacci function as parameter to steps model with number {}" / number / calculateFibonacciFunc := MainReactor::calculateFibonacci // passing calculateFibonacciFunc as a parameter to a model function reactorTest.reactorPassedAsParam(calculateFibonacciFunc, number) println(MainReactor.result) end model reactorTest func reactorPassedAsParam(fibonacci, number) fibonacci(number) end end reactor MainReactor func calculateFibonacci(fibN) // declaration of i, previousResult and result, which then can be used as a // variable throughout the whole reactor thread safety block MainReactor.i := 0 MainReactor.previousResult := 0 MainReactor.result := 1 MainReactor.calculateFibonacciAux(fibN) end func calculateFibonacciAux(fibN) if i < fibN then temp := result result := temp + previousResult previousResult := temp i := i + 1 // calculateFibonacciAux() is called recursively MainReactor.calculateFibonacciAux(fibN) end end end |