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

User Manual    [Previous]   [Next]   

Nested State Machines

A state machine can be nested inside another. This often allows for simpler Umple (or UML) notation, since events that need to cause effects in every substate of the outer state do not have to be repeated in each of the substates.

The first example below illustrates nesting abstractly. The next example shows nesting in a concrete example. The third example is the same as the second, but with no nesting, to illustrate the difference.

Abstract example of nested state machines


// Illustration of nested states. Event e1 will
// take the system into state s2, and its first
// substrate s2a. Event e2 directly goes to s2b.
// Event e3 toggles between s2b and s2a.
// Event e1 when in s2, goes to s1

class A {
  sm {
     s1 {
        e1 -> s2;
        e2 -> s2b;
     }
     s2 {
        e1 -> s1;
        s2a {
          e3 -> s2b;
        }
        s2b {
          e3 -> s2a;
        }
     }
  }
}
      

Load the above code into UmpleOnline

 

Concrete example of nested state machines

// Example showing nested states.

class Course {
  code;
  description;
  1 -- * CourseSection;
}

class CourseSection
{
  // Example from Lethbridge and Laganiere: Object
  // Oriented Software Engineering: Practical
  // Software Development using UML and Java
  // McGraw Hill, 2005
  // https://www.site.uottawa.ca/school/research/lloseng/

  sectionId;
  Integer classSize = 0;
  Integer minimumClassSize = 10;
  Integer maximumClassSize = 100;
  
  // State machine controlling status
  status
  {
    Planned {
      openRegistration -> NotEnoughStudents;
    }
    Open {
      cancel -> Cancelled;
      NotEnoughStudents {
        closeRegistration -> Cancelled;
        register
          [getClassSize() > getMinimumClassSize()]
          -> EnoughStudents;
      }
      EnoughStudents {
        closeRegistration -> Closed;
        register
          [getClassSize() > getMaximumClassSize()]
          -> Closed;
      }
    }
    Cancelled {}
    Closed {}
  }

  boolean requestToRegister(Student aStudent)
  {
    register();
    return setClassSize(getClassSize()+1);
  }
}

class Student {
  //Remainder of this class defined elsewhere
}
      

Load the above code into UmpleOnline

 

Concrete example of state machines without nesting

// Example with an opportunity to nest states, to
// avoid repeating transitions

class Course {
  code;
  description;
  1 -- * CourseSection;
}

class CourseSection
{
  // Example from Lethbridge and Laganiere: Object
  // Oriented Software Engineering: Practical
  // Software Development using UML and Java
  // McGraw Hill, 2005
  // https://www.site.uottawa.ca/school/research/lloseng/

  sectionId;
  Integer classSize = 0;
  Integer minimumClassSize = 10;
  Integer maximumClassSize = 100;
  
  // State machine controlling status
  status
  {
    Planned {
      openRegistration -> OpenNotEnoughStudents;
    }
    OpenNotEnoughStudents {
      closeRegistration -> Cancelled;
      cancel -> Cancelled;
      register
        [getClassSize() > getMinimumClassSize()]
        -> OpenEnoughStudents;
    }
    OpenEnoughStudents {
      closeRegistration -> Closed;
      cancel -> Cancelled;
      register
        [getClassSize() > getMaximumClassSize()]
        -> Closed;
    }
    Cancelled {}
    Closed {}
  }

  boolean requestToRegister(Student aStudent)
  {
    register();
    return setClassSize(getClassSize()+1);
  }
}

class Student {
  // Remainder of class defined elsewhere
}
      

Load the above code into UmpleOnline