Calling code from a transition in a state chart
-
Hello,
I'm new here and I hope it is the good place to post my question.
What I would like to do is this. When the "validate" transition is called, I would like to call some code to some stuff and check user input, and allow the transition only if user input is correct, for example. (And I have quite a lot of those transitions to make). I've read the documentation, it is called a guarded transition. But I don't know how to do it with a "drawn" state chart using the state chart editor.
If there was a function such as stateChart.connectToTransition("State_1", "Validate", mySlot) it would be perfect.
And mySlot would be a function returning a boolean, true would allow the transition, and false would forbid the transition.Is it possible ?
edit : (when I say "code", I mean "C++ code")
-
@max22
I'm not an expert (never having used them!), but perhaps you start from https://forum.qt.io/topic/103405/calling-code-from-a-transition-in-a-state-chart, section Events, Transitions and Guards. The example showsbool QAbstractTransition::eventTest()
being used as a guarded transition, to return true/false according as the transition is to proceed or not.Seems odd to have to test that at the event level, maybe, but that's what I came across.
-
How did you draw your states? Curious...
-
@enjoysmath i drew them (very quickly!) with qtcreator... why do you find it curious ?
@jonB thanks for the advice, but I still don't know how to make the link with the state chart... unfortunately there is little documentation about state charts... maybe I should give up and use the code of your link instead of state charts, but I work with people who don't code, and it would be useful...
-
Hey,
So I spent some time with SCXML and State Machine and I can't recommend using SCXML. I am not sure on the progress you made and the time you spent on your SCXML app but trust me using the stardard state machine framework and writing the code will be a lot cleaner.
With that said I do not suggest checking during your transition but check before your transition occurs, OR transition to a state dedicated to checking. The checking is done in that state and you can either transition back on failure or forward on success.
If you are using the SCXML state machine you can tap into signals coming from that (I don't have code for you sadly). Which will say when you enter and exit states. You can use those signals connected to slots that will allow you to do run some code and invoke events back into the SCXML state machine from there.
If you want to use the regular State Machine Framework then you can either override the onEntry() methods for your classes that inherit QState or simple have a manager which will execute slots when states are entered and exited (QState signals for entered and exited). Also transitioning is much more conceptually straight forward with signals for transitions. Also once you get the syntax for State Machine Framework all the complexity of your code will really be in the setup up the state machine and transitions. If you set it up all right it just runs.
Again I can't really recommend using SCXML, sure you are getting the convenience of a visual tool to set up your state machine (which is somewhat subpar especially one you get a complex state machine), however the games you need to play in code to make it work in my opinion are not clean at all.
-
@MrShawn Thanks for your message, it helped me a lot ! sorry for my late reply. Your idea of using a state dedicated to checking was very useful. (I am translating the code of a java project in which i used a big switch statement as a state machine, so the state machine model is not exactly the same).