Class ModelManagement<M extends IModel>
java.lang.Object
net.ssehub.easy.basics.modelManagement.ModelManagement<M>
- Type Parameters:
M- the specific model type
A reusable class performing model imports, model linking etc. The central class holding all model
instances and being
responsible for import resolution. Model loading is done by plugins of type
IModelLoader. Public access shall be synchronized (in any way) as multiple
source parts and tools may access the information concurrently. Please note that specific
information and settings are available via availableModels(),
events(), loaders(), locale() and locations().
The model needs a loader and a location to operate. After providing this
information, available models can be accessed via availableModels().- Author:
- Holger Eichelberger
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate AvailableModels<M> private ModelEvents<M> private booleanprivate ModelLoaders<M> private ModelLocaleprivate ModelLocations<M> private static final EASyLoggerFactory.EASyLoggerprivate ModelRepository<M> private Pool<ImportResolver<M>> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddProjectFolder(File folder) Adds a project folder, which acts as common base folder for IVML, VIL and VTL models.Returns access to the available models.voidclear()Clears all loaded models, available models (availableModels()), and model locations (locations()).(package private) voidclearLocation(File file, ProgressObserver observer) Removes a file location, i.e.voidclearModel(M model) Clearsmodelfrom the caches.voidclearModel(ModelInfo<M> info) Clears @link #clinfo(and the related model) from the caches.voidCallsclear()and clears all known models.static voidClears all project folders.protected abstract ModelRepository<M> Creates a model repository instance.protected abstract ImportResolver<M> Creates a resolver instance.events()Provides access to the event mechanisms.getAvailable(String name) Returns models available for a given modelname.getMatchingModelNames(String name) Returns the matching model names for a potential wildcard name.getModel(int index) Returns the specified model.intReturns the number of (known) models.Returns the current top-level resolver.static booleanisInProjectFolder(URI projectUri, URI query) Returns whetherqueryis in a project folder determined byprojectUri.booleanReturns whether loading a model fromlocationis currently happening.booleanReturns whether loading the model forinfois currently happening.booleanisOutdated(M model) Returns whether a model is outdated (will force a reload uponload(ModelInfo)).booleanisOutdated(ModelInfo<M> info) Returns whether a model information is outdated (will force a reload uponload(ModelInfo)).Loads the specified model and resolves its imports.private MLoads the specified model and resolves its imports by callingresolveImports(M, java.net.URI, java.util.List<net.ssehub.easy.basics.modelManagement.ModelInfo<M>>).(package private) MLoads the model related toinfowith a new default import resolver.(package private) MLoads the model related toinfo.loaders()Provides access to the model loaders.locale()Provides access to the internationalization mechanisms.Provides access to the model location.private voidnotifyLoading(ModelInfo<M> info, boolean started) Handles the notification about model loading.voidnotifyLoadingCompleted(M model, URI uri) Notifies that loading the given model is completed.voidUnresolves all models / forces reload.protected voidpostLoadModel(ModelInfo<M> info) Additional code to be executed after a model was loaded.voidreleaseResolver(ImportResolver<M> resolver) Releases a given resolver instance.private voidLoads a set of models.Update the given model by trying to reload it.Update the given model by trying to reload it.static voidremoveProjectFolder(File folder) Removes a project folder, which acts as common base folder for IVML, VIL and VTL models.protected ModelRepository<M> Returns the repository instance.resolve(String modelName, IVersionRestriction restriction, URI baseURI, IRestrictionEvaluationContext evaluationContext) Resolves the denoted model considering the givenrestrictions.Resolves the imports of the givenmodelwith transitive loading and returns messages on failures.Resolves the imports of the givenmodeland returns messages on failures.resolveImports(M model, URI uri, List<ModelInfo<M>> inProgress, ImportResolver<M> resolver) Resolves the imports of the givenmodelwith transitive loading and returns messages on failures.resolveImports(M model, URI uri, List<ModelInfo<M>> inProgress, ImportResolver<M> resolver, boolean transitiveLoading) Resolves the imports of the givenmodeland returns messages on failures.voidsetOutdated(M model) Declears a model as outdated (will force a reload uponload(ModelInfo)).voidsetOutdated(ModelInfo<M> info) Returns whether a model information is outdated.private MsetResolved(ModelInfo<M> info, M model) Defines the resolution forinfoand calls the listeners.intunload(M model, ProgressObserver observer) Unloadsmodelas well as unloadable imported models, but not the related model information.private voidunloadImpl(M model) Unloads a certain model.private StringupdateAvailableModels(ModelInfoHolder<M> holder) Updates the list of available models.(package private) voidupdateForLoader(IModelLoader<M> loader, ProgressObserver observer) Registers a model loader and updates the model information provided by the loader.private voidupdateForLoader(ModelLocations.Location location, IModelLoader<M> loader, ModelInfoHolder<M> holder, ObservableTask task, Set<ModelLocations.Location> done) Updateslocationvialoader.voidupdateModel(M model, URI uri) Add a model to this management instance.voidupdateModel(M model, URI uri, IModelLoader<M> loader) Add a model to this management instance.voidupdateModel(M model, URI uri, IModelLoader<M> loader, boolean deepReload) Add a model to this management instance.private voidupdateModelInformation(File file, ModelInfoHolder<M> holder, ProgressObserver observer, ProgressObserver.ITask task) Updates the model information infileconsideringknown.voidupdateModelInformation(File file, ProgressObserver observer) Updates the model information infile.private voidupdateModelInformation(ModelLocations.Location location, ModelInfoHolder<M> holder, ProgressObserver observer, ProgressObserver.ITask task, Set<ModelLocations.Location> done) Updates the model information in the givenlocationand the dependent locations.voidupdateModelInformation(ProgressObserver observer) Updates the model information in all known locations.
-
Field Details
-
LOGGER
-
projectFolders
-
locale
-
models
-
outdated
-
availableModels
-
repository
-
events
-
locations
-
loaders
-
loading
-
inUpdate
private transient boolean inUpdate -
resolverPool
-
-
Constructor Details
-
ModelManagement
protected ModelManagement()Singleton.
-
-
Method Details
-
createRepository
Creates a model repository instance.- Returns:
- the instance
-
createResolver
Creates a resolver instance.- Returns:
- the resolver instance
-
locale
Provides access to the internationalization mechanisms. For future compatibility, please store the returned instance just for temporary purposes within a method and not for long-term use in an attribute etc.- Returns:
- the locale object
-
events
Provides access to the event mechanisms. For future compatibility, please store the returned instance just for temporary purposes within a method and not for long-term use in an attribute etc.- Returns:
- the events object
-
loaders
Provides access to the model loaders. For future compatibility, please store the returned instance just for temporary purposes within a method and not for long-term use in an attribute etc.- Returns:
- the events object
-
locations
Provides access to the model location. For future compatibility, please store the returned instance just for temporary purposes within a method and not for long-term use in an attribute etc.- Returns:
- the events object
-
availableModels
Returns access to the available models. For future compatibility, please store the returned instance just for temporary purposes within a method and not for long-term use in an attribute etc.- Returns:
- the model paths object
-
repository
Returns the repository instance.- Returns:
- the repository instance
-
getResolverFromPool
Returns the current top-level resolver.- Returns:
- the top-level resolver
- See Also:
-
releaseResolver
Releases a given resolver instance.- Parameters:
resolver- the resolver to be released- See Also:
-
updateModel
Add a model to this management instance. Existing models are overwritten in case of same name and version.- Parameters:
model- the model to be addeduri- the URI of the model (used for unique identification)loader- The parser able to load model
-
notifyLoadingCompleted
Notifies that loading the given model is completed. CallspostLoadModel(ModelInfo).- Parameters:
model- the model to be addeduri- the URI of the model (used for unique identification)
-
updateModel
Add a model to this management instance. Existing models are overwritten in case of same name and version.- Parameters:
model- the model to be addeduri- the URI of the model (used for unique identification)loader- The parser able to load modeldeepReload- perform a deep reload of the dependent models
-
reload
Loads a set of models. Errors are logged.- Parameters:
models- the models to be (re)loaded (entries may be null)
-
reload
Update the given model by trying to reload it. Errors are logged.- Parameters:
model- the model to be reloaded- Returns:
- the loaded model (may be
modelif no new model was loaded)
-
reload
Update the given model by trying to reload it. Errors are logged.- Parameters:
model- the model to be reloadeddeepReload- perform a deep reload of the dependent models- Returns:
- the loaded model (may be
modelif no new model was loaded)
-
unload
Unloadsmodelas well as unloadable imported models, but not the related model information. No events will be sent but event listeners will be cleared. Please note that references withinmodelwill not be modified and not managed by this class after calling this method! Please release all references tomodelafter calling this method!- Parameters:
model- the model to be unloadedobserver- an optional progress observer (useProgressObserver.NO_OBSERVERbut not null in case that no observation is intended)- Returns:
- the number of unloaded models
- Throws:
ModelManagementException- in case that unloading fails
-
unloadImpl
Unloads a certain model.- Parameters:
model- the model to unload
-
updateModel
Add a model to this management instance. Existing models are overwritten in case of same name and version.- Parameters:
model- the model to be addeduri- the URI of the model (used for unique identification)
-
setResolved
Defines the resolution forinfoand calls the listeners. CallsIModel.dispose()if needed.- Parameters:
info- the information object to be updatedmodel- the resolving model- Returns:
- the old model, before replaced by
model(may be null if inconsistent or not yet loaded)
-
getModelCount
public int getModelCount()Returns the number of (known) models.- Returns:
- the number of models
-
getModel
Returns the specified model.- Parameters:
index- the 0-based index of the model- Returns:
- the model
- Throws:
IndexOutOfBoundsException- ifindex<=0 || index>=getModelCount()
-
resolveImports
Resolves the imports of the givenmodelwith transitive loading and returns messages on failures. Exceptions might be appropriate here but the caller shall be able to decide how to handle the level of detail, i.e. whether the first message shall be emitted or all. May modifymodelas a side effect. This method uses a new default import resolver.- Parameters:
model- the model to be resolveduri- the URI of the model to resolve (in order to find the closest model, may be null)inProgress- the model information objects of the models currently being processed at once (may be null)- Returns:
- messages which occur during resolution,
nullor empty if none
-
resolveImports
public List<IMessage> resolveImports(M model, URI uri, List<ModelInfo<M>> inProgress, boolean transitiveLoading) Resolves the imports of the givenmodeland returns messages on failures. Exceptions might be appropriate here but the caller shall be able to decide how to handle the level of detail, i.e. whether the first message shall be emitted or all. May modifymodelas a side effect. This method uses a new default import resolver.- Parameters:
model- the model to be resolveduri- the URI of the model to resolve (in order to find the closest model, may be null)inProgress- the model information objects of the models currently being processed at once (may be null)transitiveLoading- with or without transitive loading (seeImportResolver)- Returns:
- messages which occur during resolution,
nullor empty if none
-
resolveImports
public List<IMessage> resolveImports(M model, URI uri, List<ModelInfo<M>> inProgress, ImportResolver<M> resolver) Resolves the imports of the givenmodelwith transitive loading and returns messages on failures. Exceptions might be appropriate here but the caller shall be able to decide how to handle the level of detail, i.e. whether the first message shall be emitted or all. May modifymodelas a side effect.- Parameters:
model- the model to be resolveduri- the URI of the model to resolve (in order to find the closest model, may be null)inProgress- the model information objects of the models currently being processed at once (may be null)resolver- the import resolver (may be null to use a new default import resolver)- Returns:
- messages which occur during resolution,
nullor empty if none
-
resolveImports
public List<IMessage> resolveImports(M model, URI uri, List<ModelInfo<M>> inProgress, ImportResolver<M> resolver, boolean transitiveLoading) Resolves the imports of the givenmodeland returns messages on failures. Exceptions might be appropriate here but the caller shall be able to decide how to handle the level of detail, i.e. whether the first message shall be emitted or all. May modifymodelas a side effect.- Parameters:
model- the model to be resolveduri- the URI of the model to resolve (in order to find the closest model, may be null)inProgress- the model information objects of the models currently being processed at once (may be null)resolver- the import resolver (may be null to use a new default import resolver)transitiveLoading- with or without transitive loading (seeImportResolver)- Returns:
- messages which occur during resolution,
nullor empty if none
-
resolve
public M resolve(String modelName, IVersionRestriction restriction, URI baseURI, IRestrictionEvaluationContext evaluationContext) throws ModelManagementException Resolves the denoted model considering the givenrestrictions. This method uses a new default import resolver.- Parameters:
modelName- the name of the modelrestriction- the restriction (may be null if there is none)baseURI- the URI where to start the resolution at (may be an existing model)evaluationContext- the context for evaluating import restrictions (variable definitions... interpreted locally)- Returns:
- the resolved model
- Throws:
ModelManagementException- in case of resolution failures
-
load
Loads the model related toinfowith a new default import resolver.- Parameters:
info- the model info to load the model formessages- the messages collected so far (modified as a side effect)- Returns:
- the loaded model or null
-
load
Loads the model related toinfo.- Parameters:
info- the model info to load the model forresolver- the resolver to use (null for a new default import resolver)messages- the messages collected so far (modified as a side effect)- Returns:
- the loaded model or null
-
notifyLoading
Handles the notification about model loading.- Parameters:
info- the information object to notify aboutstarted-trueif loading started,falseelse
-
isLoading
Returns whether loading the model forinfois currently happening.- Parameters:
info- the information object to look for- Returns:
trueif loading is currently in progress,falseelse- See Also:
-
isLoading
Returns whether loading a model fromlocationis currently happening.- Parameters:
location- the location to look for- Returns:
trueif loading is currently in progress,falseelse- See Also:
-
postLoadModel
Additional code to be executed after a model was loaded.- Parameters:
info- the information object describing the model- Throws:
IOException- in case that loading fails - however, model loading will not fail
-
load
Loads the specified model and resolves its imports. Be careful when calling this method from inside this class as it automatically resolves the imports. Loading an already loaded model does not cause physical reloading but returns the cached model contents.- Parameters:
info- the information object of the model to load- Returns:
- the loaded model
- Throws:
ModelManagementException- in case of any error during loadinginfo
-
updateModelInformation
public void updateModelInformation(File file, ProgressObserver observer) throws ModelManagementException Updates the model information infile.- Parameters:
file- the location folder to searchobserver- an optional progress observer (useProgressObserver.NO_OBSERVERbut not null in case that no observation is intended)- Throws:
ModelManagementException- in case that the available information may be come inconsistent due to this update
-
updateModelInformation
Updates the model information in all known locations.- Parameters:
observer- an optional progress observer (useProgressObserver.NO_OBSERVERbut not null in case that no observation is intended)- Throws:
ModelManagementException- in case that the available information may be come inconsistent due to this update
-
updateModelInformation
private void updateModelInformation(ModelLocations.Location location, ModelInfoHolder<M> holder, ProgressObserver observer, ProgressObserver.ITask task, Set<ModelLocations.Location> done) throws ModelManagementException Updates the model information in the givenlocationand the dependent locations.- Parameters:
location- the location to update the information forholder- the combined information and result collection instanceobserver- an optional progress observer (useProgressObserver.NO_OBSERVERbut not null in case that no observation is intended)task- the task to run in (may be null in order to start a new task)done- already processed locations (in order to avoid cycles)- Throws:
ModelManagementException- in case that the available information may be come inconsistent due to this update
-
updateForLoader
void updateForLoader(IModelLoader<M> loader, ProgressObserver observer) throws ModelManagementException Registers a model loader and updates the model information provided by the loader.- Parameters:
loader- the model loader (must not be null)observer- an optional progress observer (useProgressObserver.NO_OBSERVERbut not null in case that no observation is intended)- Throws:
ModelManagementException- in case that theloaderaims at loading inconsistent information
-
updateForLoader
private void updateForLoader(ModelLocations.Location location, IModelLoader<M> loader, ModelInfoHolder<M> holder, ObservableTask task, Set<ModelLocations.Location> done) Updateslocationvialoader.- Parameters:
location- the location to be updatedloader- the specific loader to consider (may be null)holder- the combined information and result collection instancetask- the task to be informeddone- already processed locations in order to prevent loops
-
clearLocation
Removes a file location, i.e. a location from where models can be loaded.- Parameters:
file- the file location (null is ignored)observer- an optional progress observer (useProgressObserver.NO_OBSERVERbut not null in case that no observation is intended)
-
getAvailable
Returns models available for a given modelname.- Parameters:
name- the name to search for- Returns:
- the available modelss or null
-
updateAvailableModels
Updates the list of available models.- Parameters:
holder- the instance containing the results- Returns:
- an empty string if the update was available, information about inconsistencies else
-
updateModelInformation
private void updateModelInformation(File file, ModelInfoHolder<M> holder, ProgressObserver observer, ProgressObserver.ITask task) throws ModelManagementException Updates the model information infileconsideringknown.- Parameters:
file- the location to searchholder- the combined information and result collection instanceobserver- an optional progress observer (useProgressObserver.NO_OBSERVERbut not null in case that no observation is intended)task- the task to run in (may be null in order to start a new task)- Throws:
ModelManagementException- in case of model information inconsistencies
-
load
Loads the specified model and resolves its imports by callingresolveImports(M, java.net.URI, java.util.List<net.ssehub.easy.basics.modelManagement.ModelInfo<M>>). Be careful when calling this method from inside this class as it automatically resolves the imports. Loading an already loaded model does not cause physical reloading but returns the cached model contents.- Parameters:
info- the information object of the model to loadforce- load anyway- Returns:
- the loaded model
- Throws:
ModelManagementException- in case of any error during loadinginfo
-
clear
public void clear()Clears all loaded models, available models (availableModels()), and model locations (locations()). This method will not remove anyIModelLoaders orImportResolvers.
Warning: Be careful with this method, as this may affect other parts of the tooling as the whole singleton will be cleaned up. This method is mainly designed for testing purpose. -
clearModels
public void clearModels()Callsclear()and clears all known models. -
clearModel
Clearsmodelfrom the caches. Please handle with care.- Parameters:
model- the model to be cleared
-
clearModel
Clears @link #clinfo(and the related model) from the caches. Please handle with care.- Parameters:
info- the info to be cleared
-
outdateAll
public void outdateAll()Unresolves all models / forces reload. -
isOutdated
Returns whether a model is outdated (will force a reload uponload(ModelInfo)).- Parameters:
model- the model to be tested (may be null)- Returns:
trueif it is outdated,falseelse
-
isOutdated
Returns whether a model information is outdated (will force a reload uponload(ModelInfo)).- Parameters:
info- the object to be tested (may be null)- Returns:
trueif it is outdated,false(also forinfo==null) else
-
setOutdated
Declears a model as outdated (will force a reload uponload(ModelInfo)).- Parameters:
model- the model to be tested (may be null)
-
setOutdated
Returns whether a model information is outdated.- Parameters:
info- the object to be tested (may be null)
-
getMatchingModelNames
Returns the matching model names for a potential wildcard name.- Parameters:
name- the name to return the matches for- Returns:
- if name is a wildcard name, all known matching names; else,
name
-
addProjectFolder
Adds a project folder, which acts as common base folder for IVML, VIL and VTL models. Project folders support identifying/filtering related models. Relevant only if models from different projects are maintained together, e.g., in Eclipse.- Parameters:
folder- the folder, ignored if null
-
clearProjectFolders
public static void clearProjectFolders()Clears all project folders. -
isInProjectFolder
Returns whetherqueryis in a project folder determined byprojectUri.- Parameters:
projectUri- the URI determining the projectquery- the URI to query for- Returns:
trueifqueryis in a project folder or there is no project folder,falseif there are project folders butqueryis in none
-
removeProjectFolder
Removes a project folder, which acts as common base folder for IVML, VIL and VTL models. Project folders support identifying/filtering related models. Relevant only if models from different projects are maintained together, e.g., in Eclipse.- Parameters:
folder- the folder, ignored if null
-