14.5. Advanced State BehaviorYou've seen the most common ways to model states. This section shows how to model additional details of a state, including entry behavior, exit behavior, and reactions to events while in a state. Figure 14-14 shows the detailed notation for a state: a large rounded rectangle with separate compartments for internal behavior and internal transitions. 14.5.1. Internal BehaviorInternal behavior is any behavior that happens while the object is in a state. You've already seen do behavior, which is behavior that is ongoing while the state is active. Internal behavior is a more general concept that also includes entry and exit behavior. Figure 14-14. Internal behavior and transitions of the Attack stateInternal behavior is written as label / behavior. The label indicates when the behavior executesin other words, events or circumstances causing the behavior. There are three special labels: entry, exit, and do. Entry behavior happens as soon as the state becomes active and is written as entry/behavior. Exit behavior happens immediately before the state becomes inactive and is written as exit/behavior. In Figure 14-15, unsheath sword is entry behavior and sheath sword is exit behavior. Unlike do behavior, entry and exit behaviors can't be interrupted. Figure 14-15. The middle compartment shows internal behavior14.5.2. Internal TransitionsAn internal transition is a transition that causes a reaction within a state, but doesn't cause the object to change states. An internal transition is different from a self transition (see Figure 14-11) because self transitions cause entry and exit behavior to occur whereas internal transitions don't. Internal transitions are written as trigger [guard] / behavior, and they are listed inside a state. In Figure 14-16, the Attack has an internal transition: when an opponent swings his weapon and is less than three feet away, the troll dodges. Figure 14-16. The bottom compartment shows internal transitionsUse internal transitions to model reactions to events that don't cause state changes. For example, you could use internal transitions to show that a pause-and-serve coffee-maker suspends dispensing the coffee when you remove the coffee pot but doesn't leave the Brewing state, as shown in Figure 14-17. Figure 14-17. An internal transition models a reaction while staying in the same state |