Avoid clashes in ModelBuilder by reusing existing model elements#272
Avoid clashes in ModelBuilder by reusing existing model elements#272dyrpsf wants to merge 3 commits intosbmlteam:masterfrom
Conversation
There was a problem hiding this comment.
It would be great to add to the JavaDoc at least one line for each buildXYZ method stating that it either creates or returns the corresponding model element with the given identifier (ID). If such an element already exists, it returns that element as is without applying the other given arguments. If no such element with that ID can be found, it will be created, and the given arguments will be set as its attributes.
Please note that in the current imnplementation it is still possible that clashes can happen: In the case that another element with the given ID exists, e.g., a species with that ID when buildCompartment is called, the method won't find anything, but it also can't create it. Maybe it is safer to call findNamedSBase and check whether the returned object is an instance of the requested type.
Please note that UnitDefinition objects have their separate namespace.
Thanks for the helpful suggestions! I’ve updated the build methods as follows:
|
|
I’ve also added a small regression test class
|
Summary
This PR updates
ModelBuilderso that it checks whether a model element with a givenidalready exists before creating a new one. This avoids clashes and duplicate elements when
using
ModelBuilderto extend or modify existing models.The following methods now reuse existing elements if present:
buildModel(String id, String name)→ reuses the existing model in theSBMLDocumentif set.buildCompartment(...)→ usesmodel.getCompartment(id)if available.buildParameter(...)→ usesmodel.getParameter(id)if available.buildReaction(...)→ usesmodel.getReaction(id)if available.buildSpecies(...)→ usesmodel.getSpecies(id)if available.buildUnitDefinition(...)→ usesmodel.getUnitDefinition(id)if available.In each case, a new element is only created if no element with the given
idalready exists.Rationale
Previously,
ModelBuilderalways calledcreateXXX(id)on the model, assuming that elementswith the given IDs did not exist yet. When extending an existing model, this could lead to
clashes or exceptions if an element with that
idwas already present.With these changes,
ModelBuilderbecomes safer to use for both new and existing models.Related issue