How to deal with asynchronous events
The scenario engine is not meant to update at a high frequency, which can lead to the need to handle events that happened since the last scenario update. A typical situation is a sensor waiting for something to happen. To help handle this use case, you can use the DatedEventManager generic class.
The DatedEventManager contains methods to record new events that would typically be called in your main simulation code. Other methods are available to access the last events or events that happened during a time window. These would be mostly called in sensors or effectors.
How to use DatedEventManager
DatedEventManager is a generic class with static methods that you have to derive from in order to use it. The first parameter of the class is the data type of the event you wish to record. The second parameter is the time type you want to use (this type must implement IComparable).
public class ExampleDatedEventManager : DatedEventManager<MyDataType, DateTime> { }
You can then record new events in the manager from your simulation code:
// Some event happened
MyDataType event = new MyDataType(...);
TestDatedEventManager.InsertEvent(event, DateTime.Now);
In the case of a sensor waiting for an event to happen, you would need to use code like this:
public class MySensor : ASensor
{
/// <summary>
/// The last time the sensor checked if a valid event happened
/// </summary>
private DateTime lastSensorCheckDateTime;
/// <summary>
/// The result
/// </summary>
protected SimpleDictionary eventContext;
public MySensor(Event @event, Dictionary<string, Parameter> parameters, IContext externalContext, IContext scenarioContext, IContext sequenceContext)
: base(@event, parameters, externalContext, scenarioContext, sequenceContext)
{
lastSensorCheckDateTime = DateTime.Now;
}
/// <see cref="ASensor.SensorCheck"/>
public override Result SensorCheck()
{
DateTime currentDateTime = DateTime.Now;
var events = ExampleDatedEventManager.GetEventsBetween(lastSensorCheckDateTime, currentDateTime);
lastSensorCheckDateTime = currentDateTime;
bool expectedEventHappened = CheckIfExpectedEventHappened(events)
if (expectedEventHappened)
eventContext = new SimpleDictionary
{
/// fill event context data
};
return new Result(expectedEventHappened, eventContext );
}
}