Pooled State Machines
[Previous]  [Next] 
|
User Manual [Previous]  [Next] Pooled State MachinesQueued and regular state machines always process events in the order they arrive, and ignore events that cannot be handled in the current state. Sometimes, however, you want to 'save' events that arrive out of sequence, and process them as soon you enter a state that can handle them. This is accomplished by adding the word 'pooled' before a state machine definition. In a pooled state machine there is a queue in a separate thread, just like in a queued state machine, however, if the next event to be processed has no handler in the current state, then it remains at the front of the queue, and the queue processor looks further back in the queue for the first event that can be handled. A pooled state machine will therefore often process events in a different order from a regular or queued state machine. Example// Test of pooled state machines. // The word pooled results in a thread being // created to process events, and 'pooled' // semantics being employed. Events which cannot // be handled are kept waiting until entry into a // state where they can be handled. class TestSM { String ev=""; pooled sm{ s1 { e1 /{ev="e1";} ->s2; e5 /{ev="e5";} ->s2; } s2 { e2 /{ev="e2";} ->s3; } s3 { e3 /{ev="e3";} ->s4; } s4 { e4 /{ev="e4";} ->s1; } } after e* { if(wasEventProcessed) System.out.println(ev); } public static void main(String [] ags){ TestSM test=new TestSM(); test.e1(); // processed s2 // Will only be processed in the test.e5(); // pooled case (eventually) test.e2(); // processed s3 test.e3(); // processed s4 test.e4(); // processed s1 test.e1(); // processed s2 // queued: ignored // pooled: left in queue, until we test.e3(); // are next in s3 // pooled: left in queue, until test.e4(); // next in s4 // pooled: processed goes to s3 // pooled: process e3 from queue // goes to s4 // pooled: process s4 from queue test.e2(); // goes to s1 test.e1(); test.e3(); test.e2(); test.e4(); test.e1(); test.e2(); test.e3(); test.e4(); test.e1(); test.e2(); test.e4(); test.e3(); test.e4(); test.e1(); test.e3(); test.e2(); test.e3(); test.e1(); test.e4(); test.e2(); test.e1(); test.e2(); } } Load the above code into UmpleOnline Syntax//Issue 148 inlineStateMachine : [=queued]? [=pooled]? [~name] {  ( [[comment]]  | [[state]]  | [[trace]]  | [[mixsetDefinition]]  | [=||]  | [[standAloneTransition]]  )* } |