Sequence Attributes
Sequences are special attributes allowing do provide sequence of items of type String or named data entities (objects of data class implementing NamedEntity interface). This is useful if you want give user any kind of questions or tasks and prevent to repeat them (or pass too often) by remembering them. Implementation is based on MemoryMutableSet<String> storing each value (data entity name) into value property together with count and time properties. Developer specifies how next item will be selected implementing lambda nextBlock function called by sequence in scope of SequenceAttribute object, providing properties
  • next returns next value generated from sequence. If no more suitable value is available in sequence at the moment, it will be null.
  • last returns last generated value. If there is no access to next property yet, it throws error.
and two methods
  • nextRandom
  • nextInLine (if end of list is reached and maxCount value described below is > 1, it will start from the beginning of list again after minDuration)
both with following optional parameters
  • minDuration - minimum duration before repeating same item again (default is 1.day)
  • maxCount - maximum count (default value Int.MAX_VALUE means unlimited)
  • resetDuration - if count has reached maxCount and time < now - resetDuration then count will be reset to 0 and therefore value can be returned again (default value null means no reset time). e.g. 1.week
Please remember that *Duration parameters work always only within attribute scope - therefore using hour, day or week units make sense to use only in user, not session context scope (which usually takes only minutes)
Sequence attributes should be always declared as val because their value is object of type SequenceAttribute

Examples

Init Code

1
// sequence based on String list, returning items in line, each only once in a minute
2
val colorSequence by sessionSequenceAttribute(listOf("blue", "green", "yellow")) { nextInLine(1.minute) }
3
4
// sequence based on entity list, returning items randomly, each only once in an hour, and maximum 3 times in a day
5
val movieSequence by userSequenceAttribute(movies) { nextRandom(1.hour, 3, 1.day) }
6
7
// sequence based on entity list, returning items randomly
8
data class Example(override val name: String, val result: Int) : NamedEntity
9
val examples = mutableListOf<Example>().apply {
10
(1..10).forEach { a ->
11
(1..10).forEach { b ->
12
add(Example("$a plus $b", a + b))
13
if (b > a)
14
add(Example("$a minus $b", a - b))
15
if (a * b < 40)
16
add(Example("$a times $b", a * b))
17
}
18
}
19
}
20
val exampleSequence by sessionSequenceAttribute(examples) // no nextBlock = nextRandom() will be used by default
Copied!

Functional code

1
// next example response
2
if (exampleSequence.next == null) {
3
toNoMoreExamples
4
} else {
5
addResponseItem("What is ${exampleSequence.last.name}?")
6
toExampleInput
7
}
8
9
// comparing result to user input
10
if (exampleSequence.last.result == input.transcript.text.toInt())
11
toExampleSuccess
12
else
13
toExampleFailure
14
Copied!