Comments (3)
Well, this is embarrassing - I was using statemachines version 1.0.3. Now that I've upgraded, the decorator works. Sorry!
Working example -
from statemachine import StateMachine, State, exceptions
from statemachine.exceptions import TransitionNotAllowed
class ExampleStateMachine(StateMachine):
idle = State("idle", initial = True)
moving = State("Moving!")
stopped = State("Stopped")
forbidden_state = State("Forbidden!")
start_moving = moving.from_(idle,stopped)
move_from_stop = stopped.to(moving)
stop_from_moving = moving.to(stopped)
cycle = move_from_stop | stop_from_moving
forbidden_transition = idle.to(forbidden_state, cond = "always_return_false")
@cycle.cond
def cycle_cond(self):
print("This should run")
return True
def on_transition(self):
print("This should print on a transition!")
def always_return_false(self):
return False
#cycle._add_callback(cycle_cond, "cond")
exm = ExampleStateMachine()
try:
exm.forbidden_transition()
except TransitionNotAllowed:
print("Transition not allowed")
exm.start_moving()
exm.cycle()
print(exm.current_state)
exm.move_from_stop()
output
Transition not allowed
This should print on a transition!
This should run
This should print on a transition!
State('Stopped', id='stopped', value='stopped', initial=False, final=False)
This should run
This should print on a transition!
from python-statemachine.
Update:
I made a mock machine that works, but it uses the private method _add_callback to add a callback to a transition list.
This has a disadvantage - it binds the condition to every transition in the transitionlist, which for some cases may be desirable, but I can imagine only wanting it bound to the transitionlist instead of the elements of the transitionlist.
I think this will resolve my issue in the short-term, but I think it would be worthwhile to fix the decorator bug and also perhaps correct the documentation.
from statemachine import StateMachine, State, exceptions
from statemachine.exceptions import TransitionNotAllowed
class ExampleStateMachine(StateMachine):
idle = State("idle", initial = True)
moving = State("Moving!")
stopped = State("Stopped")
forbidden_state = State("Forbidden!")
start_moving = moving.from_(idle,stopped)
move_from_stop = stopped.to(moving)
stop_from_moving = moving.to(stopped)
cycle = move_from_stop | stop_from_moving
#@cycle.cond
def cycle_cond(self):
print("This should run")
return True
def on_transition(self):
print("This should print on a transition!")
def always_return_false(self):
return False
forbidden_transition = idle.to(forbidden_state, cond = always_return_false)
cycle._add_callback(cycle_cond, "cond")
exm = ExampleStateMachine()
try:
exm.forbidden_transition()
except TransitionNotAllowed:
print("Transition not allowed")
exm.start_moving()
exm.cycle()
print(exm.current_state)
exm.move_from_stop()
output:
Transition not allowed
This should print on a transition!
This should run
This should print on a transition!
State('Stopped', id='stopped', value='stopped', initial=False, final=False)
This should run
This should print on a transition!
from python-statemachine.
Hi @lucaswhipple-sfn , nice it worked as expected!
from python-statemachine.
Related Issues (20)
- Type annotations missing for `initial_state` and `final_states`; leading to linting errors with Pylint/Pyright HOT 3
- Expensive instantiation of StateMachine HOT 3
- AttributeError: object has no attribute 'model' HOT 3
- Enable Multithreading HOT 2
- [Feature request] State machine based workflow for database storage on entity state tracking HOT 1
- Action callback gets called twice when mixing definition methods HOT 1
- Check that all state transitions can reach a final state. HOT 6
- Execute a state machine from another state machine HOT 1
- Guards and conditions passed as strings really necessary? HOT 6
- ROS2, state transition not possible HOT 2
- Send a state machine over MQTT / serializing-deserializing a SM HOT 6
- Question: How can I use cond functions to gatekeep, but not fail? HOT 5
- Failed to generate a graphic image HOT 2
- Models instantiated in Djangos setUpTestData seem to skip conditions HOT 5
- Persistence, timeouts, and branching conditionals? HOT 7
- feature request: return to previous state
- sm.current_state.name capitalizes the state name and replaces underscores with spaces HOT 2
- Introduction of asyncio causes RuntimeError exception in multi-threaded application HOT 3
- Using async wrappers on callbacks causes errors with sync-only callbacks HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from python-statemachine.