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 propertiesnext
returns next value generated from sequence. If no more suitable value is available in sequence at the moment, it will benull
.last
returns last generated value. If there is no access tonext
property yet, it throws error.
and two methods
nextRandom
nextInLine
(if end of list is reached andmaxCount
value described below is > 1, it will start from the beginning of list again afterminDuration
)
both with following optional parameters
minDuration
- minimum duration before repeating same item again (default is 1.day)maxCount
- maximum count (default valueInt.MAX_VALUE
means unlimited)resetDuration
- ifcount
has reachedmaxCount
andtime < now - resetDuration
thencount
will be reset to0
and therefore value can be returned again (default valuenull
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
// sequence based on String list, returning items in line, each only once in a minute
val colorSequence by sessionSequenceAttribute(listOf("blue", "green", "yellow")) { nextInLine(1.minute) }
// sequence based on entity list, returning items randomly, each only once in an hour, and maximum 3 times in a day
val movieSequence by userSequenceAttribute(movies) { nextRandom(1.hour, 3, 1.day) }
// sequence based on entity list, returning items randomly
data class Example(override val name: String, val result: Int) : NamedEntity
val examples = mutableListOf<Example>().apply {
(1..10).forEach { a ->
(1..10).forEach { b ->
add(Example("$a plus $b", a + b))
if (b > a)
add(Example("$a minus $b", a - b))
if (a * b < 40)
add(Example("$a times $b", a * b))
}
}
}
val exampleSequence by sessionSequenceAttribute(examples) // no nextBlock = nextRandom() will be used by default
// next example response
if (exampleSequence.next == null) {
toNoMoreExamples
} else {
addResponseItem("What is ${exampleSequence.last.name}?")
toExampleInput
}
// comparing result to user input
if (exampleSequence.last.result == input.transcript.text.toInt())
toExampleSuccess
else
toExampleFailure
Last modified 2yr ago