Class DefaultImportResolver<M extends IModel>

java.lang.Object
net.ssehub.easy.basics.modelManagement.ImportResolver<M>
net.ssehub.easy.basics.modelManagement.DefaultImportResolver<M>
Type Parameters:
M - the specific model type

public class DefaultImportResolver<M extends IModel> extends ImportResolver<M>
A model imports resolver. Due to the instance data needed to perform cyclic model resolution, instances of this class are not reentrant during processing of one model and its imported models. Either instances are pooled or created individually per model translation.
Author:
Holger Eichelberger
  • Field Details

    • IMPORT_WITH_VERSION

      public static final boolean IMPORT_WITH_VERSION
      Allows to switch easily to the primitive (non-version checking) variant.
      See Also:
    • conflictMarker

      private final ModelInfo<M extends IModel> conflictMarker
    • localModelOverride

      private final Map<ModelInfo<M extends IModel>,M extends IModel> localModelOverride
    • allowCycles

      private boolean allowCycles
  • Constructor Details

    • DefaultImportResolver

      public DefaultImportResolver()
      Creates a resolver instance which, by default, does not allow cycles in the model imports.
    • DefaultImportResolver

      public DefaultImportResolver(boolean allowCycles)
      Creates a resolver instance which may allow cycles.
      Parameters:
      allowCycles - if true, allow and resolve cycles, if false emit an error
  • Method Details

    • clear

      public void clear()
      Description copied from class: ImportResolver
      Clears this instance for reuse.
      Overrides:
      clear in class ImportResolver<M extends IModel>
    • resolveImports

      public List<IMessage> resolveImports(M model, URI uri, List<ModelInfo<M>> inProgress, IModelRepository<M> repository, IRestrictionEvaluationContext evaluationContext)
      Description copied from class: ImportResolver
      Resolves the imports of the given model 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 modify model as a side effect.
      Specified by:
      resolveImports in class ImportResolver<M extends IModel>
      Parameters:
      model - the model to be resolved
      uri - 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)
      repository - the model repository
      evaluationContext - the context for evaluating import restrictions (variable definitions... interpreted locally)
      Returns:
      messages which occur during resolution
    • checkImportCycles

      private void checkImportCycles(M model, List<IMessage> messages, Set<M> done)
      Checks the import structure of model for cycles.
      Parameters:
      model - the (root) model to be checked
      messages - which occur during resolution, null or empty if none (modified as a side effect)
      done - for detecting cyclic imports (modified as a side effect)
    • resolveImports

      private List<ModelImport<M>> resolveImports(ResolutionContext<M> context, HashSet<M> done, List<IMessage> messages)
      Resolves the imports of the model in the given context 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.
      Parameters:
      context - the context of the models to be resolved
      done - for detecting cyclic imports (modified as a side effect)
      messages - which occur during resolution, null or empty if none (modified as a side effect)
      Returns:
      the conflicting model imports, null if there are none
    • setUnresolved

      private void setUnresolved(M model, List<ModelImport<M>> conflicts)
      Sets the given model and its import recursively to unresolved.
      Parameters:
      model - the model to be set to unresolved
      conflicts - the conflicts to unresolve exclusively, i.e., if not null other imports are not modified, otherwise all imports are set to unresolved
    • setUnresolved

      private void setUnresolved(M model, ModelImport<M> imp, List<ModelImport<M>> conflicts)
      Sets the given import in model and its imports recursively to unresolved.
      Parameters:
      model - the model containing imp
      imp - the import to be set to unresolved
      conflicts - the conflicts to unresolve exclusively, i.e., if not null other imports are not modified, otherwise all imports are set to unresolved
    • handleImports

      private void handleImports(ResolutionContext<M> context, M model, List<ModelImport<M>> todo)
      Handles all imports for the given model.
      Parameters:
      context - the resolution context
      model - the model to handle the imports
      todo - the imports to be handled in the calling step (may be null, then no todo list is built up)
    • handleImport

      private void handleImport(ResolutionContext<M> context, ModelImport<?> imp, List<ModelImport<M>> todo)
      Handles an import. If it denotes a conflict, it is added to the context, other to todo.
      Parameters:
      context - the context of the models to be resolved (may be modified as a side effect)
      imp - the import to be handled
      todo - the imports to be handled in the calling step (may be null, then no todo list is built up)
    • determineMatching

      private ModelInfo<M> determineMatching(ResolutionContext<M> context, List<VersionedModelInfos<M>> versions, IVersionRestriction restriction) throws ModelManagementException
      Determine the model matching the import restrictions. If multiple matching models exist, return the model with the highest version number.
      Parameters:
      context - the import context
      versions - the already known and matching model information objects
      restriction - the version restriction (may be null)
      Returns:
      the matching model (null if there no model was found or conflictMarker if no matching model was determined due to conflicts on all possible candidates)
      Throws:
      ModelManagementException - in case of restriction evaluation problems
    • addConflict

      private List<ModelImport<M>> addConflict(List<ModelImport<M>> conflicts, ModelImport<M> conflict)
      Adds the given conflict conflicts to conflicts. conflicts may be null and is created then lazily.
      Parameters:
      conflicts - the conflicts set to add the toAdd conflicts to
      conflict - the conflicts to be added
      Returns:
      conflicts if not null, a new list otherwise
    • resolve

      private List<ModelImport<M>> resolve(ResolutionContext<M> context, HashSet<M> done, List<IMessage> messages, List<VersionedModelInfos<M>> versions, ModelImport<M> imp)
      Resolves the import / conflict represented by imp.
      Parameters:
      context - the import context
      done - for detecting cyclic imports (modified as a side effect)
      messages - which occur during resolution, null or empty if none (modified as a side effect)
      versions - the already known and matching model information objects
      imp - the import to be resolved
      Returns:
      the conflicting model imports, null if there are none
    • cannotResolveImport

      protected void cannotResolveImport(ModelImport<M> imp, List<IMessage> messages, URI modelURI, IRestrictionEvaluationContext context)
      Is called if imp can finally not be resolved.
      Parameters:
      imp - the import to be resolved
      messages - which occur during resolution, null or empty if none (modified as a side effect)
      modelURI - the URI of the model
      context - the restriction evaluation context
    • load

      private List<ModelImport<M>> load(ResolutionContext<M> context, HashSet<M> done, ModelInfo<M> toLoad, List<IMessage> messages, ModelImport<M> imp, List<ModelImport<M>> conflicts)
      Loads the given model information, checks it for conflicts.
      Parameters:
      context - the import context
      done - for detecting cyclic imports (modified as a side effect)
      toLoad - the model to load
      messages - which occur during resolution, null or empty if none (modified as a side effect)
      imp - the import to be resolved
      conflicts - the actual import conflicts (may be null if none were detected so far)
      Returns:
      the actual import conflicts
    • checkImported

      protected boolean checkImported(ModelImport<M> imp, M model, List<IMessage> messages)
      Checks the imported interfaces for obvious import problems, such as importing models with interfaces as a whole.
      Parameters:
      imp - the import to be resolved
      model - the model which may resolve the import
      messages - which occur during resolution, null or empty if none (modified as a side effect)
      Returns:
      true if the import is fine, true if problems were detected
    • resolve

      public M resolve(String modelName, IVersionRestriction restrictions, URI baseUri, IModelRepository<M> repository, IRestrictionEvaluationContext evaluationContext) throws ModelManagementException
      Description copied from class: ImportResolver
      Resolves the denoted model considering the given restrictions.
      Specified by:
      resolve in class ImportResolver<M extends IModel>
      Parameters:
      modelName - the name of the model
      restrictions - the version restriction (may be null if there is none)
      baseUri - the URI to start resolving from (may be the URI of a model)
      repository - the model repository
      evaluationContext - the context for evaluating import restrictions (variable definitions... interpreted locally)
      Returns:
      the resolved model
      Throws:
      ModelManagementException - in case of resolution failures
    • resolveSingle

      private M resolveSingle(String modelName, IVersionRestriction restrictions, URI baseUri, IModelRepository<M> repository, IRestrictionEvaluationContext evaluationContext) throws ModelManagementException
      Resolves a single, non-wildcard model import.
      Parameters:
      modelName - the name of the model
      restrictions - optional version restrictions (may be null if there is none)
      baseUri - the URI to start resolving from (may be the URI of a model)
      repository - the model repository
      evaluationContext - the context for evaluating import restrictions (variable definitions... interpreted locally)
      Returns:
      the resolved model
      Throws:
      ModelManagementException - in case of resolution failures
    • resolve

      private List<ModelImport<M>> resolve(ResolutionContext<M> context, HashSet<M> done, List<IMessage> messages)
      Resolves the import represented by context.
      Parameters:
      context - the context to resolve (including the import to resolve)
      done - for detecting cyclic imports (modified as a side effect)
      messages - which occur during resolution, null or empty if none (modified as a side effect)
      Returns:
      the conflicting model imports, null if there are none
    • add

      private List<ModelImport<M>> add(List<ModelImport<M>> orig, List<ModelImport<M>> add)
      Adds add to orig.
      Parameters:
      orig - the original list, may be null
      add - the list to add, may be null
      Returns:
      orig with add or add if orig is null