-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds the new stateless group tick Any, the stateful group tick wrapper All, and the stateful tick wrapper Background. The theme of this release is backgroundable long running tasks, and an example has been added which demonstrates a background job mechanism, utilising Sync and Async.
- Loading branch information
1 parent
3b9e827
commit 5d99fb7
Showing
8 changed files
with
1,104 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
Copyright 2019 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 | ||
|
||
// All implements a tick which will tick all children sequentially until the first running status or error is | ||
// encountered (propagated), and will return success only if all children were ticked and returned success (returns | ||
// success if there were no children, like sequence). | ||
func All(children []Node) (Status, error) { | ||
success := true | ||
for _, child := range children { | ||
status, err := child.Tick() | ||
if err != nil { | ||
return Failure, err | ||
} | ||
if status == Running { | ||
return Running, nil | ||
} | ||
if status != Success { | ||
success = false | ||
} | ||
} | ||
if !success { | ||
return Failure, nil | ||
} | ||
return Success, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
Copyright 2019 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 | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
) | ||
|
||
func TestAll_error(t *testing.T) { | ||
e := errors.New(`some_error`) | ||
status, err := New(All, New(func(children []Node) (Status, error) { | ||
return Success, e | ||
})).Tick() | ||
if status != Failure { | ||
t.Error(status) | ||
} | ||
if err != e { | ||
t.Error(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
Copyright 2019 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 | ||
|
||
import "sync" | ||
|
||
// Any wraps a tick such that non-error non-running statuses will be overridden with a success if at least one child | ||
// succeeded - which is achieved by encapsulation of children, before passing them into the wrapped tick. Nil will be | ||
// returned if tick is nil, and nil children will be passed through as such. | ||
func Any(tick Tick) Tick { | ||
if tick == nil { | ||
return nil | ||
} | ||
var ( | ||
mutex sync.Mutex | ||
success bool | ||
) | ||
return func(children []Node) (Status, error) { | ||
children = func(src []Node) (dst []Node) { | ||
if src == nil { | ||
return | ||
} | ||
dst = make([]Node, len(src)) | ||
copy(dst, src) | ||
return | ||
}(children) | ||
for i := range children { | ||
child := children[i] | ||
if child == nil { | ||
continue | ||
} | ||
children[i] = func() (Tick, []Node) { | ||
tick, nodes := child() | ||
if tick == nil { | ||
return nil, nodes | ||
} | ||
return func(children []Node) (Status, error) { | ||
status, err := tick(children) | ||
if err == nil && status == Success { | ||
mutex.Lock() | ||
success = true | ||
mutex.Unlock() | ||
} | ||
return status, err | ||
}, nodes | ||
} | ||
} | ||
status, err := tick(children) | ||
if err != nil { | ||
return Failure, err | ||
} | ||
if status == Running { | ||
return Running, nil | ||
} | ||
mutex.Lock() | ||
defer mutex.Unlock() | ||
if !success { | ||
return Failure, nil | ||
} | ||
success = false | ||
return Success, nil | ||
} | ||
} |
Oops, something went wrong.