/
StoryDefinition.kt
109 lines (91 loc) · 2.98 KB
/
StoryDefinition.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* Copyright (C) 2017/2021 e-voyageurs technologies
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ai.tock.bot.definition
import ai.tock.translator.UserInterfaceType
/**
* The definition of a "Story".
* A story holds a list of actions of the same domain.
* The story provides a set of starter intents.
* When theses intents are detected, The story is started.
*
* Story definitions should usually not directly extend this class,
* but instead extend [SimpleStoryHandlerBase] or [StoryDefinitionBase].
*/
interface StoryDefinition : IntentAware {
/**
* An unique identifier for a given bot.
*/
val id: String
/**
* One or more intents that start the story.
* Usually, you don't have the same starter intent in two different story definition.
*/
val starterIntents: Set<Intent>
/**
* The complete list of intents supported by the story.
*/
val intents: Set<Intent>
/**
* The story definition tags that specify different story types or roles.
*/
val tags: Set<StoryTag> get() = emptySet()
/**
* Does this story is tagged with specified [tag]?
*/
fun hasTag(tag: StoryTag): Boolean = tags.contains(tag)
/**
* The story handler of the story.
*/
val storyHandler: StoryHandler
/**
* The root steps of the story.
*/
val steps: Set<StoryStep<*>>
/**
* True if the story handle metrics and is not a main tracked story
*/
val metricStory: Boolean
get() = false
/**
* When this story does not support all [UserInterfaceType]s.
*/
val unsupportedUserInterfaces: Set<UserInterfaceType>
/**
* Is the specified intent is a starter intent?
*/
fun isStarterIntent(intent: Intent) = starterIntents.contains(intent)
/**
* Is the specified intent is supported by this story?
*/
fun supportIntent(intent: Intent) = intents.contains(intent)
/**
* The "referent" intent for this story.
*/
fun mainIntent(): Intent = starterIntents.first()
/**
* Implementation for [IntentAware].
*/
override fun wrappedIntent(): Intent = mainIntent()
/**
* Returns all steps of the story.
*/
fun allSteps(): Set<StoryStep<*>> =
mutableSetOf<StoryStep<*>>().apply { steps.forEach { allStep(this, it) } }
private fun allStep(result: MutableSet<StoryStep<*>>, step: StoryStep<*>) {
result.add(step)
step.children.forEach { allStep(result, it) }
}
}