The Server does not need a delay of 1 for this to work - it should work with a delay of 0. The reason for the extra Server is to hold the entity temporarily while we perform a computation that affects the entity's path (output switch). So a time delay is not required.
If you are not seeing the expected behavior then it could be an issue of event priorities. Here are the sequence of events that will happen above:
- Entity arrives at Server, which will schedule a departure event for this entity for timeNow+0. This event will have an event priority as specified in the Server block.
- The signal computations will then be executed. This includes the MATLAB Function block and hence a new value will be produced at the "p" port of the Output Switch.
- The Output Switch will sense this change and schedule an update event for tNow to switch its selected port. This update event will be at the priority specified in the Output Switch block, if specified.
- The system will then schedule the above two events (both for tNow) in the order of ascending event priorities.
So if you are seeing the entity leave from the wrong port, then it is likely that the Departure event has a higher priority than the PortSelection event. Try making the event priority parameter on the Output Switch to be a lower number (higher priority) than the event priority value on the Server. Or alternatively, turn off the checkbox "Resolve simultaneous signal updates using event priority".