Sparx Systems Forum
Enterprise Architect => General Board => Topic started by: idlehanzgray on October 25, 2022, 03:29:23 am
-
Greetings all - I've been learning about state machines and attempting to follow along on the various tutorial videos and RTFM. However, I've come across a problem with the use of the choice element in a simple state machine that I created (I wanted to create my own simple example to demo how it works).
This is my state machine
Initial
QueueingForIceCream
- Initialize variables sim.i = 0, weight = 150;
Deciding
- on entry - I run this snippet to give me a 0 or 1 (vanilla/chocolate)
- this.iceCreamFlavor = Math.floor(Math.random() * 2);
Choice
Two triggers from Choice are:
- this.iceCreamFlavor = 0;
- this.iceCreamFlavor = 1;
Depending on the 0/1 the following states are:
Vanilla - increment weight by 1
Chocolate - increment weight by 1
there is a junction which returns to the Deciding state
My challenge - the state machine works - but - when it gets to Choice - it just sits there. Even though I can check the value of this.iceCreamFlavor and see whether it is a 0 or 1 one - but the states sit in the waiting triggers queue and I have to double-click one of them to get it to fire. I want it to just run automagically.
What am I missing?
(https://imgur.com/a/TY2q95Y)
Link to image of state machine here: https://imgur.com/a/TY2q95Y (https://imgur.com/a/TY2q95Y)
-
It look like those aren't in the guard for the transitions. (They aren't between '[' and ']')
I suspect you'll also have a problem because it looks like you've put an assignment instead of equality check in that condition. (Javascript syntax)
Finally, I think none of the guards should have a semi-colon.
-
Thanks for the response. I've been playing with different options - one of which is using the javascript in the guard - but it had the same result. I'll redo with the guard and report the image.
-
Here's the link to the new image. The triggers are named Vanilla and Chocolate, respectively - and now the guards are once again populated with the JavaScript. I've expanded the capture so that the values of the variables can be seen - and the event window showing the two waiting triggers.
https://imgur.com/FQ5bNpn (https://imgur.com/FQ5bNpn)
-
Two of the problems I highlighted are still there, but you've added another one.
- '=' is assignment in javascript. It needs to be '=='.
- Trailing semi-colon in the guards is likely to be a syntax error. Think of it like you're putting it in an if condition. ie. if(sim.iceCreamFlavor = 1;)
- Transitions going out of a Pseudostate aren't allowed to have a trigger. Basically because a simulation shouldn't be able to rest on a pseudostate at all.
-
Thank you! That assignment operator was indeed tripping me up. If I had a nickel for every time I used = when I needed to use == in my life well... I'd have a lot of nickels.
The state machine now works as intended. I've posted an updated image in the link below for anyone else following along that may have similar challenges.
https://imgur.com/SMYgX4e (https://imgur.com/SMYgX4e)