Choose Values for Event Priorities

Overview of Example

This example (open modelmodel) shows how you can vary the processing sequence for simultaneous events by varying their event priorities. The example creates race conditions at a switch and illustrates multiple ways to resolve the race conditions.

At T=1, 2, 3,... the Repeating Sequence Stair block changes its value from 1 to 2 or from 2 to 1. The change causes the following events to occur, not necessarily in this sequence:

  • The top entity generator generates an entity.

  • The bottom entity generator generates an entity.

  • The Input Switch block selects a different entity input port.

Both entity generators are configured so that if a generated entity cannot depart immediately, the generator holds the entity and temporarily suspends the generation of additional entities.

In the model, the two Set Attribute blocks assign a Source attribute to each entity. The attribute value is 1 or 2 depending on which entity generator generated the entity. The Attribute Scope block plots the Source attribute values to indicate the source of each entity that departs from the switch.

Arbitrary Resolution of Signal Updates

If the two entity generators and the switch all have the Resolve simultaneous signal updates according to event priority option turned off, then you cannot necessarily predict the sequence in which the blocks schedule their reactions to changes in the output signal from the Repeating Sequence Stair block.

The rest of this example assumes that the two entity generators and the switch all use the Resolve simultaneous signal updates according to event priority option, for greater control over the sequencing of simultaneous events.

Selecting a Port First

Suppose the two entity generators and the switch have the explicit event priorities shown.

Event TypeEvent Priority
Generation event at top entity generator300
Generation event at bottom entity generator310
Port selection event at switch200

The following describes what happens at T=1, 2, 3 using messages from the simulation log of the SimEvents® debugger. To learn more about the debugger, see Start the SimEvents Debugger.

Behavior at T=1

  • The output signal from the Repeating Sequence Stair block changes from 1 to 2 and blocks that connect to it detect the relevant update.

    %==============================================================================%
    Detected Sample Time Hit                              Time = 1.000000000000000
    : Block = Input Switch
  • The blocks that react to the update then schedule events on the event calendar.

    %------------------------------------------------------------------------------%
    Discrete-Event System ID: 0  Highlight
        ID      EventTime           EventType          Priority Entity    Block
     => ev6     1.000000000000000   PortSelection      200      <none>    Input Switch
        ev4     1.000000000000000   EntityGeneration   300      <none>    Event-Based 
    Entity Generator1
        ev5     1.000000000000000   EntityGeneration   310      <none>    Event-Based 
    Entity Generator2
  • The switch selects its IN2 entity input port.

    %==============================================================================%
    Executing PortSelection Event (ev6)                   Time = 1.000000000000000
    : Entity = <none>                                     Priority = 200
    : Block  = Input Switch
  • The top entity generator generates an entity, which cannot depart because the switch's IN1 entity input port is unavailable.

    %==============================================================================%
    Executing EntityGeneration Event (ev4)                Time = 1.000000000000000
    : Entity = <none>                                     Priority = 300
    : Block  = Event-Based Entity Generator1
        %..........................................................................%
        Generating Entity (en1)
        : Block = Event-Based Entity Generator1
  • The bottom entity generator generates an entity. This entity advances from block to block until it reaches the Attribute Scope block, which destroys it.

    %==============================================================================%
    Executing EntityGeneration Event (ev5)                Time = 1.000000000000000
    : Entity = <none>                                     Priority = 310
    : Block  = Event-Based Entity Generator2
        %..........................................................................%
        Generating Entity (en2)
        : Block = Event-Based Entity Generator2
        %..........................................................................%
        Entity Advancing (en2)
        : From = Event-Based Entity Generator2
        : To   = Set Attribute2
        %..........................................................................%
    [...other messages...]
         %..........................................................................%
        Entity Advancing (en2)
        : From = Get Attribute
        : To   = Attribute Scope
        %..........................................................................%
        Destroying Entity (en2)
        : Block = Attribute Scope
    

Behavior at T=2

  • Blocks detect the next relevant update in the output signal from the Repeating Sequence Stair block, and react by scheduling events.

    %------------------------------------------------------------------------------%
    Discrete-Event System ID: 0  Highlight
        ID      EventTime           EventType          Priority Entity    Block
     => ev10    2.000000000000000   PortSelection      200      <none>    Input Switch
        ev8     2.000000000000000   EntityGeneration   300      <none>    Event-Based 
    Entity Generator1
        ev9     2.000000000000000   EntityGeneration   310      <none>    Event-Based 
    Entity Generator2
  • The switch selects its IN1 entity input port. This causes the top entity generator to output the entity it generated 1 second ago. This entity advances from block to block until it reaches the Attribute Scope block.

    %==============================================================================%
    Executing PortSelection Event (ev10)                  Time = 2.000000000000000
    : Entity = <none>                                     Priority = 200
    : Block  = Input Switch
        %..........................................................................%
        Entity Advancing (en1)
        : From = Event-Based Entity Generator1
        : To   = Set Attribute1
        %..........................................................................%
    [...other messages...]
        %..........................................................................%
        Entity Advancing (en1)
        : From = Get Attribute
        : To   = Attribute Scope
        %..........................................................................%
        Destroying Entity (en1)
        : Block = Attribute Scope
  • The top entity generator generates an entity, which advances from block to block until it reaches the Attribute Scope block. A total of two entities from the top entity generator reach the scope at this time instant.

    %==============================================================================%
    Executing EntityGeneration Event (ev8)                Time = 2.000000000000000
    : Entity = <none>                                     Priority = 300
    : Block  = Event-Based Entity Generator1
        %..........................................................................%
        Generating Entity (en3)
        : Block = Event-Based Entity Generator1
        %..........................................................................%
        Entity Advancing (en3)
        : From = Event-Based Entity Generator1
        : To   = Set Attribute1
        %..........................................................................%
    [...other messages...]
        %..........................................................................%
        Entity Advancing (en3)
        : From = Get Attribute
        : To   = Attribute Scope
        %..........................................................................%
        Destroying Entity (en3)
        : Block = Attribute Scopee
  • The bottom entity generator generates an entity, which cannot depart because the switch's IN2 entity input port is unavailable.

    %==============================================================================%
    Executing EntityGeneration Event (ev9)                Time = 2.000000000000000
    : Entity = <none>                                     Priority = 310
    : Block  = Event-Based Entity Generator2
        %..........................................................................%
        Generating Entity (en4)
        : Block = Event-Based Entity Generator2

Behavior at T=3

  • Blocks detect the next relevant update in the output signal from the Repeating Sequence Stair block, and react by scheduling events.

    %------------------------------------------------------------------------------%
    Discrete-Event System ID: 0  Highlight
        ID      EventTime           EventType          Priority Entity    Block
     => ev15    3.000000000000000   PortSelection      200      <none>    Input Switch
        ev13    3.000000000000000   EntityGeneration   300      <none>    Event-Based 
    Entity Generator1
        ev14    3.000000000000000   EntityGeneration   310      <none>    Event-Base
  • The switch selects its IN2 entity input port. This causes the bottom entity generator to output the entity it generated 1 second ago. This entity advances from block to block until it reaches the Attribute Scope block.

    %==============================================================================%
    Executing PortSelection Event (ev15)                  Time = 3.000000000000000
    : Entity = <none>                                     Priority = 200
    : Block  = Input Switch
        %..........................................................................%
        Entity Advancing (en4)
        : From = Event-Based Entity Generator2
        : To   = Set Attribute2
        %..........................................................................%
    [...other messages...]
        %..........................................................................%
        Entity Advancing (en4)
        : From = Get Attribute
        : To   = Attribute Scope
        %..........................................................................%
        Destroying Entity (en4)
        : Block = Attribute Scope
    
  • The top entity generator generates an entity, which cannot depart because the switch's IN1 entity input port is unavailable.

    %==============================================================================%
    Executing EntityGeneration Event (ev13)               Time = 3.000000000000000
    : Entity = <none>                                     Priority = 300
    : Block  = Event-Based Entity Generator1
        %..........................................................................%
        Generating Entity (en5)
        : Block = Event-Based Entity Generator1
  • The bottom entity generator generates an entity, which advances from block to block until it reaches the Attribute Scope block. A total of two entities from the bottom entity generator reach the scope at this time instant.

    %==============================================================================%
    Executing EntityGeneration Event (ev14)               Time = 3.000000000000000
    : Entity = <none>                                     Priority = 310
    : Block  = Event-Based Entity Generator2
        %..........................................................................%
        Generating Entity (en6)
        : Block = Event-Based Entity Generator2
        %..........................................................................%
        Entity Advancing (en6)
        : From = Event-Based Entity Generator2
        : To   = Set Attribute2
        %..........................................................................%
    [...other messages...]
        %..........................................................................%
        Entity Advancing (en6)
        : From = Get Attribute
        : To   = Attribute Scope
        %..........................................................................%
        Destroying Entity (en6)
        : Block = Attribute Scope
    

Evidence from Plots and Signals

The plot of entities' Source attribute values shows an alternating pattern of dots, as does the plot of the port selection signal p. The list of times and values of the entities' Source attribute, as recorded in the Source_attr variable in the MATLAB® workspace, shows that two entities from the same entity generator reach the scope at T=2, 3, 4, etc.

Port Selection Signal

Switch Departures When Port Selection Is Processed First

[Source_attr.time, Source_attr.signals.values]

ans =

     0     0
     1     2
     2     1
     2     1
     3     2
     3     2
     4     1
     4     1
     5     2
     5     2
     6     1
     6     1
     7     2
     7     2
     8     1
     8     1
     9     2
     9     2
    10     1
    10     1

Generating Entities First

Suppose the two entity generators and the switch have the explicit event priorities shown below.

Event TypeEvent Priority
Generation event at top entity generator300
Generation event at bottom entity generator310
Port selection event at switch4000

At the beginning of the simulation, the port selection signal, p, is 1.

Behavior at T=1

  • The output signal from the Repeating Sequence Stair block changes from 1 to 2 and blocks that connect to it detect the relevant update.

    %==============================================================================%
    Detected Sample Time Hit                              Time = 1.000000000000000
    : Block = Input Switch
  • The blocks that react to the update then schedule events on the event calendar.

    %------------------------------------------------------------------------------%
    Discrete-Event System ID: 0  Highlight
        ID      EventTime           EventType          Priority Entity    Block
     => ev4     1.000000000000000   EntityGeneration   300      <none>    Event-Based 
    Entity Generator1
        ev5     1.000000000000000   EntityGeneration   310      <none>    Event-Based 
    Entity Generator2
        ev6     1.000000000000000   PortSelection      4000     <none>    Input Switch
  • The top entity generator generates an entity. This entity advances from block to block until it reaches the Attribute Scope block, which destroys it.

    %==============================================================================%
    Executing EntityGeneration Event (ev4)                Time = 1.000000000000000
    : Entity = <none>                                     Priority = 300
    : Block  = Event-Based Entity Generator1
        %..........................................................................%
        Generating Entity (en1)
        : Block = Event-Based Entity Generator1
        %..........................................................................%
        Entity Advancing (en1)
        : From = Event-Based Entity Generator1
        : To   = Set Attribute1
        %..........................................................................%
    [...other messages...]
        %..........................................................................%
        Entity Advancing (en1)
        : From = Get Attribute
        : To   = Attribute Scope
    
    
  • The bottom entity generator generates an entity, which cannot depart because the switch's IN2 entity input port is unavailable.

    %==============================================================================%
    Executing EntityGeneration Event (ev5)                Time = 1.000000000000000
    : Entity = <none>                                     Priority = 310
    : Block  = Event-Based Entity Generator2
        %..........................................................................%
        Generating Entity (en2)
        : Block = Event-Based Entity Generator2
  • The switch selects its IN2 entity input port. This causes the bottom entity generator to output the entity it just generated. This entity advances from block to block until it reaches the Attribute Scope block.

    %==============================================================================%
    Executing PortSelection Event (ev3)                   Time = 0.000000000000000
    : Entity = <none>                                     Priority = 4000
    : Block  = Input Switch2
        %..........................................................................%
    [...other messages...]
        %..........................................................................%
        Entity Advancing (en1)
        : From = Get Attribute
        : To   = Attribute Scope

Behavior at T=2

  • Blocks detect the next relevant update in the output signal from the Repeating Sequence Stair block, and react by scheduling events.

    %------------------------------------------------------------------------------%
    Discrete-Event System ID: 0  Highlight
        ID      EventTime           EventType          Priority Entity    Block
     => ev9     2.000000000000000   EntityGeneration   300      <none>    Event-Based 
    Entity Generator1
        ev10    2.000000000000000   EntityGeneration   310      <none>    Event-Based 
    Entity Generator2
        ev11    2.000000000000000   PortSelection      4000     <none>    Input Switch
    
  • The top entity generator generates an entity, which cannot depart because the switch's IN1 entity input port is unavailable.

    %==============================================================================%
    Executing EntityGeneration Event (ev9)                Time = 2.000000000000000
    : Entity = <none>                                     Priority = 300
    : Block  = Event-Based Entity Generator1
        %..........................................................................%
        Generating Entity (en3)
        : Block = Event-Based Entity Generator1
    
  • The bottom entity generator generates an entity, which advances from block to block until it reaches the Attribute Scope block.

    %==============================================================================%
    Executing EntityGeneration Event (ev10)               Time = 2.000000000000000
    : Entity = <none>                                     Priority = 310
    : Block  = Event-Based Entity Generator2
        %..........................................................................%
        Generating Entity (en4)
        : Block = Event-Based Entity Generator2
        %..........................................................................%
        Entity Advancing (en4)
        : From = Event-Based Entity Generator2
        : To   = Set Attribute2
        %..........................................................................%
    [...other messages...]
        %..........................................................................%
        Entity Advancing (en4)
        : From = Get Attribute
        : To   = Attribute Scope
  • The switch selects its IN1 entity input port. This causes the top entity generator to output the entity it just generated. This entity advances from block to block until it reaches the Attribute Scope block.

    %==============================================================================%
    Executing PortSelection Event (ev11)                  Time = 2.000000000000000
    : Entity = <none>                                     Priority = 4000
    : Block  = Input Switch
        %..........................................................................%
    [...other messages...]
        %..........................................................................%
        Entity Advancing (en5)
        : From = Get Attribute
        : To   = Attribute Scope
    

Plots and Signals

The plot of entities' Source attribute values shows that two entities from different entity generators depart from the switch every second.

Switch Departures When Entity Generations Are Processed First

[Source_attr.time, Source_attr.signals.values]

ans =

     1     1
     1     2
     2     2
     2     1
     3     1
     3     2
     4     2
     4     1
     5     1
     5     2
     6     2
     6     1
     7     1
     7     2
     8     2
     8     1
     9     1
     9     2
    10     2
    10     1

Randomly Select a Sequence

Suppose the two entity generators and the switch have equal event priorities. By default, the application uses an arbitrary processing sequence for the entity-generation events and the port-selection events, which might or might not be appropriate in an application. To avoid bias by randomly determining the processing sequence for these events, set Execution order to Randomized in the model's Configuration Parameters dialog box.

Sample attribute values and the corresponding plot are below, but your results might vary depending on the specific random numbers.

Switch Departures When Processing Sequence is Random

[Source_attr.time, Source_attr.signals.values]

ans =

     1     2
     2     2
     2     1
     3     2
     4     1
     4     1
     5     1
     5     2
     5     2
     6     1
     7     1
     7     2
     8     2
     8     1
     9     2
    10     1
    10     1
Was this topic helpful?