Entity Collections

Entity collections in attributes

DialogueScript supports working with data entity collections (lists and sets) as attributes, storing automatically their names as references into attribute values (due to technical reasons, data entities cannot be stored directly). Data class must implement NamedEntity interface to provide name property used by entity collection attribute delegate as instance reference.
Scopes turn, session and user are supported (community not yet). Following delegate functions are provided, requiring data entity collection as first and optional second namespace parameters
  • turnEntityListAttribute, sessionEntityListAttribute and userEntityListAttribute
  • turnEntitySetAttribute, sessionEntitySetAttribute and userEntitySetAttribute

Example

1
// init code
2
data class Movie(override val name: String, val director: String, val year: Int) : NamedEntity
3
4
val movies by loader<List<Movie>>("./resources/movies")
5
val likedMovies by userEntityListAttribute(movies)
6
7
// functional code
8
val movie = movies.similarTo(input, { name }, 0.5)
9
if (movie != null)
10
likedMovies.add()
11
12
// response
13
You like #{likedMovies.list { name }, subj = "movie"}.
Copied!
Name should be considered as primary key thus unique and not changing in time. If your data class define different data suitable to be used as unique name, you can provide it via getter, see following example.1
1
data class Person(val givenName: String, val surname: String, val birthPlace: String) : NamedEntity {
2
override val name get() = "$givenName $surname born in $birthPlace"
3
}
Copied!

Maps in attributes

You can use attributes to store maps mapping String keys to values of Any type. Following delegate functions are available to do this
  • turnMapAttribute, sessionMapAttribute and userMapAttribute

Example

1
// init code
2
val test by sessionMapAttribute(mapOf("a" to "first", "b" to "second", "c" to "third"))
3
4
// functional code
5
test.put("a")
6
test.put("c")
7
8
logger.log(test.toString()) // this will return mapOf("a" to "first", "c" to "third")
Copied!