Class ModelManagement<M extends IModel>
- java.lang.Object
-
- net.ssehub.easy.basics.modelManagement.ModelManagement<M>
-
- Type Parameters:
M- the specific model type
- Direct Known Subclasses:
BuildModel,TemplateModel,VarModel
public abstract class ModelManagement<M extends IModel> extends java.lang.ObjectA 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 typeIModelLoader. 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 viaavailableModels(),events(),loaders(),locale()andlocations(). The model needs a loader and a location to operate. After providing this information, available models can be accessed viaavailableModels().- Author:
- Holger Eichelberger
-
-
Field Summary
Fields Modifier and Type Field Description private AvailableModels<M>availableModelsprivate ModelEvents<M>eventsprivate booleaninUpdateprivate ModelLoaders<M>loadersprivate java.util.Set<ModelInfo<M>>loadingprivate ModelLocalelocaleprivate ModelLocations<M>locationsprivate static EASyLoggerFactory.EASyLoggerLOGGERprivate java.util.List<M>modelsprivate java.util.Set<ModelInfo<M>>outdatedprivate ModelRepository<M>repositoryprivate Pool<ImportResolver<M>>resolverPool
-
Constructor Summary
Constructors Modifier Constructor Description protectedModelManagement()Singleton.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description AvailableModels<M>availableModels()Returns access to the available models.voidclear()Clears all loaded models, available models (availableModels()), and model locations (locations()).(package private) voidclearLocation(java.io.File file, ProgressObserver observer)Removes a file location, i.e.voidclearModel(M model)Clearsmodelfrom the caches.voidclearModel(ModelInfo<M> info)Clearsinfo(and the related model) from the caches.protected abstract ModelRepository<M>createRepository()Creates a model repository instance.protected abstract ImportResolver<M>createResolver()Creates a resolver instance.ModelEvents<M>events()Provides access to the event mechanisms.java.util.List<VersionedModelInfos<M>>getAvailable(java.lang.String name)Returns models available for a given modelname.MgetModel(int index)Returns the specified model.intgetModelCount()Returns the number of (known) models.ImportResolver<M>getResolverFromPool()Returns the current top-level resolver.booleanisLoading(java.net.URI location)Returns whether loading a model fromlocationis currently happening.booleanisLoading(ModelInfo<M> info)Returns 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)).Mload(ModelInfo<M> info)Loads the specified model and resolves its imports.private Mload(ModelInfo<M> info, boolean force)Loads 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) Mload(ModelInfo<M> info, java.util.List<IMessage> messages)Loads the model related toinfowith a new default import resolver.(package private) Mload(ModelInfo<M> info, ImportResolver<M> resolver, java.util.List<IMessage> messages)Loads the model related toinfo.ModelLoaders<M>loaders()Provides access to the model loaders.ModelLocalelocale()Provides access to the internationalization mechanisms.ModelLocations<M>locations()Provides access to the model location.private voidnotifyLoading(ModelInfo<M> info, boolean started)Handles the notification about model loading.voidnotifyLoadingCompleted(M model, java.net.URI uri)Notifies that loading the given model is completed.voidoutdateAll()Unresolves 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 voidreload(java.util.List<M> models)Loads a set of models.Mreload(M model)Update the given model by trying to reload it.Mreload(M model, boolean deepReload)Update the given model by trying to reload it.protected ModelRepository<M>repository()Returns the repository instance.Mresolve(java.lang.String modelName, IVersionRestriction restriction, java.net.URI baseURI, IRestrictionEvaluationContext evaluationContext)Resolves the denoted model considering the givenrestrictions.java.util.List<IMessage>resolveImports(M model, java.net.URI uri, java.util.List<ModelInfo<M>> inProgress)Resolves the imports of the givenmodelwith transitive loading and returns messages on failures.java.util.List<IMessage>resolveImports(M model, java.net.URI uri, java.util.List<ModelInfo<M>> inProgress, boolean transitiveLoading)Resolves the imports of the givenmodeland returns messages on failures.java.util.List<IMessage>resolveImports(M model, java.net.URI uri, java.util.List<ModelInfo<M>> inProgress, ImportResolver<M> resolver)Resolves the imports of the givenmodelwith transitive loading and returns messages on failures.java.util.List<IMessage>resolveImports(M model, java.net.URI uri, java.util.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 java.lang.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, java.util.Set<ModelLocations.Location> done)Updateslocationvialoader.voidupdateModel(M model, java.net.URI uri)Add a model to this management instance.voidupdateModel(M model, java.net.URI uri, IModelLoader<M> loader)Add a model to this management instance.voidupdateModel(M model, java.net.URI uri, IModelLoader<M> loader, boolean deepReload)Add a model to this management instance.private voidupdateModelInformation(java.io.File file, ModelInfoHolder<M> holder, ProgressObserver observer, ProgressObserver.ITask task)Updates the model information infileconsideringknown.voidupdateModelInformation(java.io.File file, ProgressObserver observer)Updates the model information infile.private voidupdateModelInformation(ModelLocations.Location location, ModelInfoHolder<M> holder, ProgressObserver observer, ProgressObserver.ITask task, java.util.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 Detail
-
LOGGER
private static final EASyLoggerFactory.EASyLogger LOGGER
-
locale
private ModelLocale locale
-
availableModels
private AvailableModels<M extends IModel> availableModels
-
repository
private ModelRepository<M extends IModel> repository
-
events
private ModelEvents<M extends IModel> events
-
locations
private ModelLocations<M extends IModel> locations
-
loaders
private ModelLoaders<M extends IModel> loaders
-
inUpdate
private transient boolean inUpdate
-
resolverPool
private Pool<ImportResolver<M extends IModel>> resolverPool
-
-
Method Detail
-
createRepository
protected abstract ModelRepository<M> createRepository()
Creates a model repository instance.- Returns:
- the instance
-
createResolver
protected abstract ImportResolver<M> createResolver()
Creates a resolver instance.- Returns:
- the resolver instance
-
locale
public ModelLocale 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
public ModelEvents<M> 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
public ModelLoaders<M> 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
public ModelLocations<M> 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
public AvailableModels<M> 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
protected ModelRepository<M> repository()
Returns the repository instance.- Returns:
- the repository instance
-
getResolverFromPool
public ImportResolver<M> getResolverFromPool()
Returns the current top-level resolver.- Returns:
- the top-level resolver
- See Also:
releaseResolver(ImportResolver)
-
releaseResolver
public void releaseResolver(ImportResolver<M> resolver)
Releases a given resolver instance.- Parameters:
resolver- the resolver to be released- See Also:
getResolverFromPool()
-
updateModel
public void updateModel(M model, java.net.URI uri, IModelLoader<M> loader)
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
public void notifyLoadingCompleted(M model, java.net.URI uri)
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
public void updateModel(M model, java.net.URI uri, IModelLoader<M> loader, boolean deepReload)
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
private void reload(java.util.List<M> models)
Loads a set of models. Errors are logged.- Parameters:
models- the models to be (re)loaded (entries may be null)
-
reload
public M reload(M model)
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
public M reload(M model, boolean deepReload)
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
public int unload(M model, ProgressObserver observer) throws ModelManagementException
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
private void unloadImpl(M model)
Unloads a certain model.- Parameters:
model- the model to unload
-
updateModel
public void updateModel(M model, java.net.URI uri)
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
private M setResolved(ModelInfo<M> info, M model)
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
public M getModel(int index)
Returns the specified model.- Parameters:
index- the 0-based index of the model- Returns:
- the model
- Throws:
java.lang.IndexOutOfBoundsException- ifindex<=0 || index>=getModelCount()
-
resolveImports
public java.util.List<IMessage> resolveImports(M model, java.net.URI uri, java.util.List<ModelInfo<M>> inProgress)
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 java.util.List<IMessage> resolveImports(M model, java.net.URI uri, java.util.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 java.util.List<IMessage> resolveImports(M model, java.net.URI uri, java.util.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 java.util.List<IMessage> resolveImports(M model, java.net.URI uri, java.util.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(java.lang.String modelName, IVersionRestriction restriction, java.net.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
M load(ModelInfo<M> info, java.util.List<IMessage> messages)
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
M load(ModelInfo<M> info, ImportResolver<M> resolver, java.util.List<IMessage> messages)
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
private void notifyLoading(ModelInfo<M> info, boolean started)
Handles the notification about model loading.- Parameters:
info- the information object to notify aboutstarted-trueif loading started,falseelse
-
isLoading
public boolean isLoading(ModelInfo<M> info)
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:
IModelProcessingListener
-
isLoading
public boolean isLoading(java.net.URI location)
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:
IModelProcessingListener
-
postLoadModel
protected void postLoadModel(ModelInfo<M> info) throws java.io.IOException
Additional code to be executed after a model was loaded.- Parameters:
info- the information object describing the model- Throws:
java.io.IOException- in case that loading fails - however, model loading will not fail
-
load
public M load(ModelInfo<M> info) throws ModelManagementException
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(java.io.File file, ProgressObserver observer) throws ModelManagementExceptionUpdates 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
public void updateModelInformation(ProgressObserver observer) throws ModelManagementException
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, java.util.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, java.util.Set<ModelLocations.Location> done)
Updateslocationvialoader.- Parameters:
location- the location to be updatedholder- the combined information and result collection instanceloader- the specific loader to consider (may be null)task- the task to be informeddone- already processed locations in order to prevent loops
-
clearLocation
void clearLocation(java.io.File file, ProgressObserver observer)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
public java.util.List<VersionedModelInfos<M>> getAvailable(java.lang.String name)
Returns models available for a given modelname.- Parameters:
name- the name to search for- Returns:
- the available modelss or null
-
updateAvailableModels
private java.lang.String updateAvailableModels(ModelInfoHolder<M> holder)
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(java.io.File file, ModelInfoHolder<M> holder, ProgressObserver observer, ProgressObserver.ITask task) throws ModelManagementExceptionUpdates 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
private M load(ModelInfo<M> info, boolean force) throws ModelManagementException
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.
-
clearModel
public void clearModel(M model)
Clearsmodelfrom the caches. Please handle with care.- Parameters:
model- the model to be cleared
-
clearModel
public void clearModel(ModelInfo<M> info)
Clearsinfo(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
public boolean isOutdated(M model)
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
public boolean isOutdated(ModelInfo<M> info)
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
public void setOutdated(M model)
Declears a model as outdated (will force a reload uponload(ModelInfo)).- Parameters:
model- the model to be tested (may be null)
-
-