# Stateflow-Based Energy Management Strategy for Hybrid Energy System to Mitigate Load Shedding
## 2 The HES Architecture and Modeling in Stateflow
### The Stateflow (SF)  
The SF is an add-on program for MATLAB and is explicitly linked to Simulink [22]. It is best used to mimic and simulate event-driven systems. SF is well suited for a reactive system, where multiple subsystems respond to the events happening in their surroundings while remaining aware of each other operations. It provides a graphical editor on which the objects are dragged from the design palette to create an extended finite state machine (FSM) function. Further, SF simplifies the system modeling and accommodates more complex logic by adding hierarchy, parallelism, events, actions, history, and truth tables, etc. These important extensions and flexible syntax increase the usefulness of SF to model the behavior of a complex system. Moreover, the excellent GUI guarantees strong evidence about the consistency, correctness, and completeness of system behavior [23]. To illustrate the concepts of SF, a simple example of a common compact disc (CD) player is shown in Figure 3.  
The root for execution of the SF model is called the SF chart. It holds a mixture of data, state, and transition diagrams that characterize an operation. The chart interacts with MATLAB/Simulink through input and output ports. The basic object in a chart is called a state. The state can be active (on) or inactive (off); an active state implies a state activity due to event execution or fulfillment of certain conditions. For the CD player example, the Switch_on is active when the condition for transition (CFT), i.e., Power = 1, is met. During execution, an active state is denoted by a blue-lined rectangular. Moreover, several states can be activated simultaneously, which means multiple events can take place at one time. This is illustrated by Switch_on, Play_mode, and Track2, which are all running in parallel. Since SF is organized hierarchically, each state is allowed to have a number of sub-states. The top level of the hierarchy is called a super-state, while the inner states are termed as parent- or child-state. The execution order of a parent-child state is based on two rules: (1) the child state is executed only after the parent is activated, and (2) the parent completes its execution only after all its child states have been evaluated. All states can have two types of decomposition, i.e., exclusive (OR) or parallel (AND). The OR decomposition  
Figure 3: Illustration of common compact disk player’s operation using SF. means that only one child state can be active at a time. In contrast, for AND, all child states are active at the same time. The states with AND and OR compositions are indicated by dashed and solid borders, respectively. In the given example, CD_Player is the super-state. It has two child states, namely Switch_on and Switch_off. Further, Switch_on is the parent of two child states, i.e., Stop_mode and Play_mode. Going down the hierarchy, Play_mode has three child states: Track1, Track2, and Track3. They are in a mutually OR relationship. The symbol H is the history function; it maintains the previous setting/record of Switch_on state in case of sudden interruption. The input/output data are variables that record the values used by the chart. In the given example, Power and Play are the input data read from Simulink through its input ports. Similarly, Track is the output of the SF execution and is passed to Simulink once the operations are completed. The state labels denote the state identifier. It has three optional types of actions. First, the entry actions are executed upon entering the state. In our example, the state Track1, for instance, has an entry action that consists of assigning the value 1 to the variable Track. Second, \"during\" actions take place when the state is active and there is no valid outgoing transition. The \"during\" action can be seen in the state Play_mode, where the counter is used to increment the value of variable i. Third, the exit actions are performed when the state is exited. The transition represents a passage between two nodes that can be states or junctions.