Submodel
In ERS, a simulation model can be split up into multiple submodels. Each of these submodels is paired with its own simulator and can run in parallel with other submodels.
A submodel is used to store the data of the simulation, like entities, components and the submodel context. Each submodel only has access to its own data. Therefore, different entities may have the same entity ID because they are in different submodels. When entities are transferred between submodels, their ID is automatically updated, see sync-events.
When to use multiple submodels
For guidance on when it makes sense to split your model into multiple submodels, see the model splitting page.
Editing a submodel
ERS keeps track of which submodel is the current submodel.
To interact with a submodel, the current submodel first needs to be set, which we call "entering a submodel".
Afterwards the submodel can be "exited" again.
This can be done via the EnterSubModel and ExitSubModel respectively, which are available on a simulator.
A submodel is automatically entered and exited during the run of a simulation, see model execution.
Ers::Model::ModelContainer modelContainer = Ers::Model::ModelContainer::Create();
Ers::Simulator sim1 = modelContainer.AddSimulator("Simulator 1", Ers::SimulatorType::DiscreteEvent);
sim1.EnterSubModel();
// The SubModel of "Simulator 1" is entered and can be used now
Ers::SubModel& submodel = Ers::Get();
sim1.ExitSubModel();
// The SubModel of "Simulator 1" is exited and can no longer be used
ModelContainer model = ModelContainer.Create();
Simulator sim1 = model.AddSimulator("Simulator 1", SimulatorType.DiscreteEvent);
sim1.EnterSubModel();
// The SubModel of "Simulator 1" is entered and can be used now
SubModel subModel = SubModel.Get();
sim1.ExitSubModel();
// The SubModel of "Simulator 1" is exited and can no longer be used
After a submodel is made current, it is possible to modify the entities and components, or use a view. For example, entities can be edited or removed, and components can be registered, and added or removed to / from components. See the entities and components sections for more details.
Submodel context
A submodel context is a piece of data you can attach to a submodel. Each simulator can have multiple submodel contexts, as long as they are each of a different type. This is useful when you want easily accessible data related to a submodel, for example when performing sync-events.
A submodel context can be added via the AddSubModelContext<T> function, where T is the type of the submodel context.
To retrieve a submodel context, use GetSubModelContext<T>.
A submodel context can be defined like a struct or class, depending on the language.
struct SomeSubModelContext
{
int ImportantInt;
float ImportantFloat;
EntityID ImportantEntity;
};
// Somewhere else
Ers::SubModel& subModel = Ers::Get();
SomeSubModelContext& context = subModel.AddSubModelContext<SomeSubModelContext>();
context.ImportantInt = 123;
context.ImportantFloat = 4.56f;
context.ImportantEntity = someEntity;
class SomeSubModelContext
{
int ImportantInt;
float ImportantFloat;
Entity ImportantEntity;
}
// Somewhere else
SubModel subModel = SubModel.Get();
SomeSubModelContext context = subModel.AddSubModelContext<SomeSubModelContext>();
context.ImportantInt = 123;
context.ImportantFloat = 4.56f;
context.ImportantEntity = someEntity;