Hello all!
I’m manually implementing my state charts FROM UML TO obj-c (a CHARACTER behaviour) AND my question IS:
When does a transition change object state, before OR after execution transition code?
Here IS my sample code that executes transition code before changing object state (it’s aMoleHid) but I’m NOT sure this IS correct:
-(void) changeState:(BurrowState)state {
[self onExitState:m_state];
if (m_state==BURROW_STARTING && state==BURROW_EMPTY) {
} ELSE if (m_state==BURROW_EMPTY && state==BURROW_MOLE_APPEARING) {
} ELSE if (m_state==BURROW_MOLE_APPEARING && state==BURROW_EMPTY) {
[m_naughtyMolesScene aMoleHid];
} ELSE if (m_state==BURROW_MOLE_APPEARING && state==BURROW_MOLE_APPEARING) {
} ELSE if (m_state==BURROW_MOLE_APPEARING && state==BURROW_FILLING_IN_WITH_ROCKS) {
} ELSE if (m_state==BURROW_FILLING_IN_WITH_ROCKS && state==BURROW_FILLED_IN_WITH_ROCKS) {
} ELSE {
NSLog(@"Unknown transition.");
exit(1);
}
m_state=state;
[self onEnterState:state];
}
Thanks a lot;
It obviously depends ON the semantics that you wish your statemachines TO have.
I would say that the object should reach the target state AFTER executing the service that IS mapped TO the transition (what you called the transition code I would encapsulate it IN a service/PROCEDURE/method).
The OO-Method, an object-oriented conceptual modelling method, defines the state transition diagrams IN that way. A suite OF tools named OLIVANOVA supports BOTH modelling AND automatic code generation, so I ensure you this strategy works (probably a different strategy will also WORK, though).
This way, transitions act AS preconditions FOR service execution, but ARE effective after the service IS executed.
I hope it helps you.
Regards,
Sergio
More info about the method:
NO. Pastor AND J.C. Molina, Model-Driven Architecture IN practice: a software production environment based ON conceptual modeling. New York: Springer, 2007.
Ó. Pastor, J. Gómez, E. Insfrán, AND V. Pelechano, “The OO-method approach for information systems modeling: from object-oriented conceptual modeling to automated programming,” Inform Syst, vol. 26, issue 7, 2001, pp. 507-534.
More info about the tool:
CARE Technologies. OLIVANOVA The Programming Machine http://www.care-t.com
Thanks Sergio. I already know olivanova, actually I was last year in your class 🙂
You say that depends about semantics you want, but does UML say about this? I’m going TO look FOR it RIGHT now.
I didn’t see transitions as preconditions before reading your post, thanks for that tip.
Hi Ricardo,
Nice TO meet you here 🙂
When I informally say that a transition acts AS a precondition, it means the following (I will try TO use UML terms).
Imagina a state machine IN which there ARE two transitions that refer TO the operation Op1: one goes FROM state St1 TO St2 AND the other goes FROM St4 TO St8.
If an object (i.e. a class instance) IS IN state Sa1, THEN operation Op1 can indeed be executed. THEN the object reaches St2. But now, since there IS NO transition defined FOR the operation Op1 that departs FROM St2, Op1 cannot be executed.
In this sense, the transitions act AS preconditions TO operation execution.
I am assuming, however, that you wish TO model SOME behaviour that involves non-sequential operations; that IS, FOR SOME OR ALL operations, an EXTERNAL stimulus IS required (e.g. a USER OR a sensor triggers the operations). If you intend TO model sequential behaviour (e.g. an algorithm) THEN you can can still use State Machines, but I would opt FOR one OF the following.
*) non-UML: Flowchart, Structure Chart (FROM NOT-so-old Structured Analysis AND Design)
*) UML: Sequence Diagrams, Collaboration Diagrams
(Note. WITH the UML notations you can also take INTO account EXTERNAL triggers OR USER INPUT by including a class that represents, e.g., the USER).
Regards,
Sergio
Thanks for reply.
What I want to model is the behaviour of a character in a iPhone game I’m writing. So, I guess it IS a sequential execution. I think I will model transitions AS preconditions, so state will be changed AFTER transition IS executed successfully.
Now I have another question, obviously several objects interact BETWEEN them (a bullet impacts IN main CHARACTER if NOT dead). WHEN an object’s state charts send a signal (call a method) in another object (moved by another state chart), are that calls called triggers?
In order to model interaction among/between different state charts (or objects), Sequence Diagrams and Collaboration Diagrams are used. Is there any way to simulate or validate that kind of interaction?
Thanks a lot for your time!
I meant to say ‘consecutive’ instead of ‘sequential’.
You can try to combine of STDs and Sequence or Collaboration Diagrams.
To simulate and verify the models (forget validation in this case, unless you have some requirements prior to the models) you will need tool support.
Which tool to use? I cannot help in this. Ask the experts (for which I recommend a new thread).
Regards,
sergio