Skip to content
Alexander Koller edited this page Nov 17, 2018 · 3 revisions

(this page in German)

Groovy is an object-oriented language that is very similar to Java; most valid Java code is also valid Groovy code. With Groovy, complex coding can be achieved within a DialogOS graph.

More information about how to code in Groovy can be found here.

Groovy Nodes

When DialogOS executes a Groovy node, it creates a new GroovyShell. It also creates a Binding for the shell and adds all of the graph’s global variables - both those specifically for Groovy (Graph -> Groovy Variables) and those that can be used both by Groovy and the in-house Script code (Graph -> Variables). If you have declared Groovy functions (Graph -> Groovy Functions), these are appended to the beginning of the Groovy node’s script.

DialogOS then runs the script in the Groovy node in the Groovy shell. This returns an Object. This return value is used to determine which outgoing edge the graph should traverse next. If the return value, when converted to a string, matches the labels of any of the outgoing edges, that edge will be traversed. Otherwise, the default edge will be traversed.

The global variables are then retrieved from the binding of the GroovyShell. The code iterates through the list of the (old) global variables. If the value of a global variable in the new list is different than in the old list, the variable is updated with the new value. If the type of a (non-Groovy-exclusive) variable has been changed, an exception will be thrown. The type of a Groovy-only variable, however, may be changed freely in the code.

Global functions and variables

Global Variables

DialogOS provides two options for global variables; those which can only be used and changed in Groovy code, and those which can be used and changed both in Groovy code and in ordinary DialogOS Script nodes. The reason for this difference is that the in-house script code is limited to only a few data types for its variables, while Groovy code can use any Object - the user could hypothetically even create their own Object class in a Groovy node and save it to the Groovy variables. The type of global variables which are not Groovy-specific cannot be changed in Groovy script; an exception will be thrown if this is attempted. It is, however, possible to change the type of a Groovy-exclusive global variable in the code. Currently, it is not possible to initialize the Groovy-only variables when first creating them; they must be declared in the Groovy variables window and initialized at some point in the graph.

Global Functions

Groovy global functions can be accessed in the “Graph” tab, under “Global Groovy Functions”. These functions can be used in any Groovy Node in the graph. The program makes these functions useable simply by adding the code from the global script to the top of the code within every Groovy node when the node is run. This code is therefore run at the beginning of every Groovy node. Thus, it is not advisable to change global variables within the global Groovy code (outside an uncalled function) unless these variables should be reset at the beginning of every Groovy node. A possible area for improvement in the implementation of Groovy code within DialogOS could be making these global functions accessible to all Groovy nodes without having to run this code before each Groovy node.

Multiple Edges

It is possible to have multiple outgoing edges from a Groovy node. The program determines which of these edges to traverse based on the return value of the Groovy script. The return value is given either through a return statement or is the value of the last statement run in the Groovy node. If the return value does not match the value of any of the edges, the default edge (which cannot be deleted) will be chosen. The value of an edge should never be empty or the same as another outgoing edge of the same node.

Groovy in speech synthesis nodes

When a speech synthesis node is executed by DialogOS, it first determines the string that should be spoken by the speech synthesizer. This can be done either by specifying the text literally ("Text" option) or by evaluating a DialogOS expression ("Expression" option). In addition, you can also choose the "Groovy Script" option to determine the string by evaluating Groovy code.

The evaluation works in the same way as in the Groovy node. Global Groovy functions and variables can be accessed as well as the non-Groovy global variables. The evaluation result of the script is passed to the speech synthesis and must be of type string. The evaluation result of a Groovy script is the result of the last line of the script or the return value passed after a return statement.