Patterns - dialogos-project/dialogos GitHub Wiki
Patterns in DialogOS can be used in two ways: First, they serve as control structures for deciding along which edge the dialog will follow; Secondly, they can be used to store data in variables which can be accessed later on in the dialog.
There are different variants of patterns which can be used in input and test variable nodes. In general, the dialog will continue along the edge of a node which represents the first matching pattern. Patterns are checked in a top-down manner.
The following list gives an overview of pattern variants and their corresponding advantages and disadvantages:
-
Wildcard
_
: The wildcard pattern_
matches all input and is often used at the end of an input pattern. It does not store data, but it will create an outgoing edge from the node containing this pattern. -
Values: It is possible to use strings, numbers and boolean values as patterns. As a pattern, they match if and only if the input has the same value. This pattern cannot store data, but will create an outgoing edge from the node containing this pattern. Examples of values as patterns are:
1
,3.142
,"Highway"
,true
. -
Variables: If you use a variable as pattern, the input value will be assigned to it and the dialog will continue along the respective output edge. The type of the input must match the type of the variable.
-
Lists: Lists are analogous to values as patterns. Additionally, it is possible to represent list patterns with the :: operator to ensure a certain amount of elements. For example, the pattern
_::_::[]
matches all lists with exactly two elements, whereas_::_::_
matches all lists with at least two elements. -
Structs: Structures are also analogous to values when used as patterns. Note that a struct matches all input structs that have at least the same amount of information. For this reason, it is useful to add structure patterns with more information before those with less, as the dialog will exit at the first matched pattern. For example, the pattern
{ help = _ }
would match{ help = "control" }
, as well as{ help = "control" , route = "Highway" }
, and will exit based on the first one it hits. -
Variable as Pattern: It is possible to use a variable plus a value as a pattern. If you use this method, the input will be compared to the value first and if it matches, it will be assigned to the specified variable. An example of this would be the pattern
chosen as { help = _ }
, wherein input containing the label help will match, and the value of the input will be assigned to the variable chosen. The dialog will then continue along the respective output edge. -
Regular expressions (/regexp/ = (Variables)): Regular expressions can be used as patterns if and only if the value of the input is a string. Regular expressions make it possible to assign substrings to variables. For example, you can extract the name of an artist from the string "I want to hear a song from Robbie Williams", using the regular expression:
/I want to hear a song from (.*)/=(artist)
The content of ".*" will thus be assigned to the variable artist. This also works with multiple variables, such as:
/I want to hear (.*) from (.*)/=(song,artist)
The content of the n-th set of parantheses will be assigned to the n-th variable of the right-hand list.
Next page: Functions and Scripts