Comments (2)
Hi @juan11iguel,
It would be nice if there was a convenience method to call triggers by its name (a string).
good news: you can do that via the decorated trigger
method. In the README Non-quickstart right before the next section:
Furthermore, there is a method called trigger now attached to your model (if it hasn't been there before). This method lets you execute transitions by name in case dynamic triggering is required.
Or in the section about Callback Execution Order:
In summary, there are currently three ways to trigger events. You can call a model's convenience functions like lump.melt(), execute triggers by name such as lump.trigger("melt") or dispatch events on multiple models with machine.dispatch("melt")
Long story short: That's how it's done:
from transitions import Machine
states = ['solid', 'liquid', 'gas', 'plasma']
transitions = [
{'trigger': 'melt', 'source': 'solid', 'dest': 'liquid'},
{'trigger': 'evaporate', 'source': 'liquid', 'dest': 'gas'},
{'trigger': 'sublimate', 'source': 'solid', 'dest': 'gas'},
{'trigger': 'ionize', 'source': 'gas', 'dest': 'plasma'}
]
machine = Machine(states=states, transitions=transitions, initial='liquid')
machine.trigger("evaporate")
assert machine.is_gas()
Note that I did not pass lump
as an argument. The first argument passed to Machine
acts as a model. So when I pass something there, all the convenience functions will be added to the object. If no model is provided machine itself acts as a model.
Let me know if this covers your use case and best regards.
from transitions.
Ha! It seems I cannot read, thanks for the detailed response, this solves it then.
Best regards
from transitions.
Related Issues (20)
- AsyncMachine transitions fail when a list of `State` objects are passed in to the FSM. HOT 2
- get_triggers does not work HOT 3
- Cannot use Pydantic `BaseModel` class for models HOT 2
- All possible transitions are initialized, instead of those provided as an argument. HOT 2
- Machine get into wrong state. HOT 1
- after_state_change not triggered by set_state HOT 2
- AsyncMachine with queued transitions breaks when transition is cancelled
- AsyncMachine transitions aren't atomic HOT 4
- The Event and Machine have cyclic dependency. HOT 3
- Update COPR packages
- get_triggers does not get child states in HierarchicalAsyncMachine HOT 3
- may_ function in parallel state does not report correct value HOT 1
- 3.12 Performance HOT 10
- New version release HOT 5
- Very confusing in inherited HierarchicalMachine. HOT 1
- Pylance attributeerror on generated members HOT 6
- 😭 How do you specify final state? The front page docs do not say HOT 1
- Is it normal to have a bunch of warnings of "event doesn't exist" for parallel machines (with disjoint event sets)? HOT 1
- Provide features to ease static type checking HOT 6
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 transitions.