-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmemorize.go
71 lines (66 loc) · 2.19 KB
/
memorize.go
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
/*
Copyright 2021 Joseph Cumines
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 behaviortree
// Memorize encapsulates a tick, and will cache the first non-running status for each child, per "execution", defined
// as the period until the first non-running status, of the encapsulated tick, facilitating execution of asynchronous
// nodes in serial with their siblings, using stateless tick implementations, such as sequence and selector.
//
// Sync provides a similar but more flexible mechanism, at the expense of greater complexity, and more cumbersome
// usage. Sync supports modification of children mid-execution, and may be used to implement complex guarding behavior
// as children of a single Tick, equivalent to more complex structures using multiple memorized sequence nodes.
func Memorize(tick Tick) Tick {
if tick == nil {
return nil
}
var (
started bool
nodes []Node
)
return func(children []Node) (status Status, err error) {
if !started {
nodes = copyNodes(children)
for i := range nodes {
var (
child = nodes[i]
override Tick
)
if child == nil {
continue
}
nodes[i] = func() (Tick, []Node) {
tick, nodes := child()
if override != nil {
return override, nodes
}
if tick == nil {
return nil, nodes
}
return func(children []Node) (Status, error) {
status, err := tick(children)
if err != nil || status != Running {
override = func(children []Node) (Status, error) { return status, err }
}
return status, err
}, nodes
}
}
started = true
}
status, err = tick(nodes)
if err != nil || status != Running {
started = false
nodes = nil
}
return
}
}