list of dots Digital Research Alliance of Canada logo  NSERC logo  University of Ottawa logo / UniversitĂ© d'Ottawa

User Manual    [Previous]   [Next]   

State Machine Actions and Do Activities

Transition actions: When a transition is taken, an action can occur. This is indicated using a slash "/" followed by arbitrary code in braces. The arbitrary code may be placed after the event name or before the destination state name. In other words the arrow -> can be placed either directly after the event (before the action), or directly before the destination state (after the action)

Entry and exit actions: Similarly, when entering or exiting a state, an action can occur. This is indicated using the keywords entry or exit, followed by a slash, followed by code.

The actions described above should be programmed such that they take negligible time to execute; in other words they shouldn't contain significant loops..

Do activities: If a longer-running computation (activity) needs to be done while in a state, encode this using the keyword do, followed by a block of code in curly brackets. In languages such as Java that support it, a thread will be started to invoke the code. This allows the state machine to 'stay live' and be able to respond to other events, even while the do activity is running. A do activity thread can be interrupted and cancelled when a transition is taken out of the state. To enable this in Java, the code it needs to catch InterruptedException as shown in the first example.

Example


namespace example;

class LightFixture
{
  bulb
  {
    On {
      entry / { doEntry(); }
      exit / { doExit(); }
      push -> /{ doTransition(); } Off;
      do { doThisContinuouslyWhileOn(); }
    }
    Off {}
  }

  void doEntry() {System.out.println("Entry");}
  void doExit() {System.out.println("Exit");}
  void doTransition() {
    System.out.println("Transition");}
  void doThisContinuouslyWhileOn() {
    while (true) {
      System.out.println("Still on");
      try {
        Thread.sleep(1000);
      }
      catch (InterruptedException e) {}
    }
  }
}

      

Load the above code into UmpleOnline

 

Example with different target languages

// This demonstrates that actions can be different
// in different target languages
class DemoSM
{
  Integer id;
  sm {
    s1 {
      e1 / Java {id=5;} Php {$id=5;} -> s2;
    }
    s2 {
    }
  }
}
      

Load the above code into UmpleOnline

 

Syntax


// An action can be executed on a transition, or on entry or exit
action : / [[moreCode]]+

entryOrExitAction : [=type:entry|exit] [[guardCode]]? / [[moreCode]]+

// A do activity is long-lasting and can be interrupted
activity : do [[moreCode]]+