-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## [0.4.0] - 2022-02-16 - **[Added]** `RandomPickState` which randomly pick one of the children to be executed. All children has uniform probability being picked. - **[Added]** added `get_status` method in `State` which return the status of the State. - **[Changed]** name for `State` is now optional, the default is its class name. - **[Changed]** streamlined `ParallelState` to prevent potential interrupt race issues. Enable `AtLeastOneState` to overwrite two function for its functionality. - **[Changed]** `ParallelState` now ignores empty/None States if passed in as children. - **[Fixed]** condition in `AtLeastOneState` where other states aren't destroyed when exiting under success condition.
- Loading branch information
Showing
8 changed files
with
90 additions
and
12 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
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
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 @@ | ||
import threading | ||
from ..core import StateStatus, State, NestedState, Board | ||
import typing | ||
import random | ||
import threading | ||
|
||
class RandomPickState(NestedState): | ||
|
||
_picked_state: State | ||
_children: typing.Sequence[State] | ||
_lock: threading.RLock | ||
|
||
def __init__(self, children: typing.Sequence[State],name = ""): | ||
self._children = children | ||
self._picked_state = None | ||
self._lock = threading.RLock() | ||
super().__init__(name) | ||
|
||
def execute(self, board: Board) -> StateStatus: | ||
|
||
with self._lock: | ||
self._picked_state = random.choice(self._children) | ||
self._picked_state.start(board) | ||
|
||
self._picked_state.wait() | ||
|
||
# set the flow out and pass the status out. | ||
self.flow_out = self._picked_state.flow_out | ||
result_status = self._picked_state.get_status() | ||
with self._lock: | ||
self._picked_state = None | ||
return result_status | ||
|
||
def interrupt(self, timeout: float = None) -> bool: | ||
# we have a lock here just in case it suddenly become None when interrupting. | ||
self.signal_interrupt() | ||
with self._lock: | ||
if self._picked_state is not None: | ||
return self._picked_state.interrupt(timeout) | ||
return True |
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,29 @@ | ||
from behavior_machine.core import Machine, State, StateStatus, Board | ||
from behavior_machine.library import RandomPickState | ||
|
||
|
||
def test_random_pick(): | ||
|
||
c1 = 0 | ||
c2 = 0 | ||
class s1(State): | ||
def execute(self, board: Board) -> StateStatus: | ||
nonlocal c1 | ||
c1 += 1 | ||
return StateStatus.SUCCESS | ||
class s2(State): | ||
def execute(self, board: Board) -> StateStatus: | ||
nonlocal c2 | ||
c2 += 1 | ||
return StateStatus.SUCCESS | ||
|
||
ranPick = RandomPickState(children=[ | ||
s1(), | ||
s2() | ||
]) | ||
|
||
for i in range(0,1000): | ||
ranPick.start(None) | ||
ranPick.wait() | ||
assert 450 < c1 < 550 | ||
assert 450 < c2 < 550 |