-
Notifications
You must be signed in to change notification settings - Fork 529
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
same state machine instance for multiple object instances #128
Comments
This is an interesting idea. In theory there's no principled reason why you shouldn't be able to use the same This should be easy to fix without breaking the API. Assuming we move all state out of the Anyway, thanks for opening this issue. I'll think about it some more, and we should be able to do at least something to accommodate this use case without having to change much. |
Thank you for your quick reply. I think having just the state name in the model is not a problem; it is always possible to access the actual state from the model and from a database persistence point of view having just strings for the state is much easier to handle. After reading some more of the transitions code i think it would be possible to maintain (just extend) the API, and optionally move all methods (callback, triggers) to the state machine itself, by introducing an optional base class for the models. E.g. for triggers something like this:
|
Unfortunately, I don't think it's that simple; the |
Oh, and just to clarify: having only the state name in the model is fine; like you say, the corresponding |
Hi, interesting thoughts. So the machine would manage the 'ruleset' of valid transitions but the actual state is part of the model. I see how this increases the reuse value. I do have some (initial) questions about the desired behaviour and relationship between model and machine though.
I would suggest to extend a I give it a try if one of you can provide a test case. Just to be sure I try to achieve the right thing :). |
@aleneum, agreed, that's exactly the implementation I would go with as well. I've played with this a bit already and have a version of core.py that passes all tests in test_core with machine.current_state completely removed and all references occurring via the model. I'll push the branch shortly for comment. If it looks good, we'd just need to amend the extensions accordingly. |
@aleneum: I don't know the code enough to understand the
|
… breaking API. extensions remain untouched. all tests in test_core.py pass.
I just pushed a multiple-models branch that modifies the core Right now all changes are completely compatible with the existing API. The one potential sticking point here is that we might want to rename self.model to self.models. Right now, to prevent API breakage, I internally store models in self.models, but define Machine.model as a property that returns a single model if the list has length 1, and the full list otherwise. I'm not thrilled about this, but it seems like the best way to preserve the API, and given that right now probably nobody in the wild is expecting multiple models, it doesn't seem wise to tamper with this. I also had to remove a test that in my view shouldn't be a supported use case anyway (passing optional arguments with send_event=False when the callback function is not explicitly defined to handle such an argument), as it was interfering with passing the model as the first argument to trigger. @aleneum, see what you think. If the changes look good, we'll need to similarly update all the extensions etc. A bit tedious, but should be straightforward, I think. @gemerden, assuming you're not using any of the extensions, do you want to test these changes out and see if they work for your use case? |
The tests should pass now but there is still some work to do for graphing. Right now, only the last transition executed will be shown in the graph. My plan would be to move the graph instance also to the model so that every model has a separate graph. What do you think? |
+1 on moving the graph to the model. |
And thanks for fixing all the other modules so quickly! |
Graph issues have been fixed and nesting also seems to work. But we might want to extend testing of this feature a bit before calling it stable enough for the master branch. |
I guess we can consider this solved with #135. Feel free to reopen this issue if you encounter bugs/limitations or open a new issue. |
In our use case we have many objects that could be handled with the same state machine (e.g. from database queries). Currently a state machine is instantiated for each object which creates (perhaps unnecessary) overhead.
Would it be possible to have a version of the state machine that points self.machine in multiple objects (of the same class) to a single state machine instance?
The text was updated successfully, but these errors were encountered: