Class ModelQuery

java.lang.Object
net.ssehub.easy.varModel.model.ModelQuery
Direct Known Subclasses:
IvmlModelQuery

public class ModelQuery extends Object
Provides an interface to query models.
Author:
Holger Eichelberger
  • Field Details

  • Constructor Details

    • ModelQuery

      public ModelQuery()
  • Method Details

    • getDatatypeVisitorInstance

      private static final ModelQuery.MqDatatypeVisitor getDatatypeVisitorInstance(QualifiedNameMode mode)
      Returns a pooled datatype visitor instance.
      Parameters:
      mode - the name mode
      Returns:
      the visitor instance
      See Also:
    • releaseDatatypeVisitor

      private static final void releaseDatatypeVisitor(ModelQuery.MqDatatypeVisitor vis)
      Releases a datatype visitor instance.
      Parameters:
      vis - the instance to be released
    • getType

      private static final String getType(ContainableModelElement element, QualifiedNameMode mode)
      Returns the textual representation of the given element.
      Parameters:
      element - the element to be represented
      mode - the name mode
      Returns:
      the textual representation
    • getType

      private static final String getType(IDatatype type, QualifiedNameMode mode)
      Returns the textual representation of the given type.
      Parameters:
      type - the type to be represented
      mode - the name mode
      Returns:
      the textual representation
    • getReferenceTypeSearchName

      public static final String getReferenceTypeSearchName(IDatatype type)
      Returns the name to be used for searching for a reference with contained type type.
      Parameters:
      type - the contained type
      Returns:
      the search name
    • findRefining

      public static List<Compound> findRefining(IResolutionScope scope, Compound compound)
      Returns all (reachable) compounds (transitively) refining the given compound starting at scope also considering imported scopes.
      Parameters:
      scope - the scope to search the refining compounds within
      compound - the compound that must be part of the refinement hierarchy of all result compounds
      Returns:
      the refining compounds, may be empty
    • findRefining

      private static void findRefining(IResolutionScope scope, Compound compound, List<Compound> result, Set<IResolutionScope> done)
      Returns all (reachable) compounds (transitively) refining the given compound starting at scope also considering imported scopes. [initial implementation]
      Parameters:
      scope - the scope to search the refining compounds within
      compound - the compound that must be part of the refinement hierarchy of all result compounds
      result - the list of refining result compounds (modified as a side effect)
      done - already processed scopes to prevent import circles
    • findProject

      public static Project findProject(Project project, String name)
      Finds a project with given name.
      Parameters:
      project - the project to start the search at (including imports)
      name - the name to search for
      Returns:
      the found project or null if there is none
    • findProject

      private static Project findProject(Project project, String name, String path, Set<Project> done)
      Finds a project with given name.
      Parameters:
      project - the project to start the search at (including imports)
      name - the name to search for
      path - the current qualified search path
      done - already processed projects
      Returns:
      the found project or null if there is none
    • findType

      public static IDatatype findType(IResolutionScope elements, String name, Class<? extends IDatatype> type) throws ModelQueryException
      Finds the specified data type definition in the given project.
      Parameters:
      elements - the elements to search for
      name - the name of the type to search for (may be qualified)
      type - the specific type of datatype to be returned, IDatatype is used if null
      Returns:
      the corresponding type or null
      Throws:
      ModelQueryException - in case of violated project access restrictions
      See Also:
    • findType

      private static IDatatype findType(IResolutionScope elements, String name, boolean considerVariableDeclarations, Class<? extends IDatatype> type, Set<IResolutionScope> done) throws ModelQueryException
      Finds the specified data type definition in the given project.
      Parameters:
      elements - the elements to search for
      name - the name of the type to search for (may be qualified)
      considerVariableDeclarations - take the types of variable declarations into account
      type - the specific type of datatype to be returned, IDatatype is used if null
      done - already done resolution scopes
      Returns:
      the corresponding type or null
      Throws:
      ModelQueryException - in case of violated project access restrictions
    • findElementByTypeName

      public static IDatatype findElementByTypeName(IResolutionScope elements, String typeName, Class<? extends IDatatype> type) throws ModelQueryException
      Finds the specified data type definition in the given elements.
      Parameters:
      elements - the elements to search for
      typeName - the name of the type to search for (may be qualified)
      type - the specific type of datatype to be returned, IDatatype is used if null
      Returns:
      the corresponding type or null
      Throws:
      ModelQueryException - in case of violated project access restrictions
    • findElementByTypeName

      public static IDatatype findElementByTypeName(IResolutionScope elements, String typeName, Class<? extends IDatatype> type, Set<IResolutionScope> done) throws ModelQueryException
      Finds the specified data type definition in the given elements.
      Parameters:
      elements - the elements to search for
      typeName - the name of the type to search for (may be qualified)
      type - the specific type of datatype to be returned, IDatatype is used if null
      done - the already processed resolution scopes
      Returns:
      the corresponding type or null
      Throws:
      ModelQueryException - in case of violated project access restrictions
    • findElementByTypeName

      public static IDatatype findElementByTypeName(IDecisionVariableContainer container, String typeName) throws ModelQueryException
      Finds the specified data type definition in the given container.
      Parameters:
      container - the container to search for
      typeName - the name of the type to search for (may be qualified)
      Returns:
      the corresponding type or null
      Throws:
      ModelQueryException - in case of violated project access restrictions
    • getScope

      private static IResolutionScope getScope(IResolutionScope scope, String name) throws ModelQueryException
      Returns the resolution scope determined by the qualified name relative to the given scope.
      Parameters:
      scope - the scope to start with
      name - the (qualified) name of the relative scope to return
      Returns:
      the relative scope
      Throws:
      ModelQueryException - in case of violated project access restrictions
    • getImportedScope

      private static IResolutionScope getImportedScope(IResolutionScope scope, String namespace) throws ModelQueryException
      Returns the current scope due to imports.
      Parameters:
      scope - the scope to start with
      namespace - the namespace of the resulting scope
      Returns:
      the relative scope
      Throws:
      ModelQueryException - in case of violated project access restrictions
    • searchImportedProject

      private static Project searchImportedProject(String namespace, IResolutionScope scope, Set<Project> done)
      Searches for an indirectly imported project in depth-first search.
      Parameters:
      namespace - the namespace to search for
      scope - the scope to search within
      done - already visited projects (avoid cycles)
      Returns:
      the project matching the namespace or null
    • checkInterfaceImport

      private static IResolutionScope checkInterfaceImport(IResolutionScope scope, ModelQuery.IncrementalNamespace ispace, String namespace, IResolutionScope project) throws ModelQueryException
      Performs final checks on accessing an imported interface.
      Parameters:
      scope - the scope containing the interface being imported (may be null)
      ispace - the current state in resolving the namespace
      namespace - the initial namespace (for error messages)
      project - the containing project
      Returns:
      scope
      Throws:
      ModelQueryException - in case of interface access errors, i.e. scope does not contain the expected/specified interface in ispace
    • checkScopeForImport

      private static boolean checkScopeForImport(String name, ProjectImport imp, String namespace) throws ModelQueryException
      Check the validity of the scope being imported.
      Parameters:
      name - the name of the element being imported
      imp - the import model element
      namespace - the total namespace being accessed
      Returns:
      true if the scope matches the import, false else
      Throws:
      ModelQueryException - in case of illegal accesses
    • removeNamespace

      private static String removeNamespace(String name)
      Removes a namespace prefix from name.
      Parameters:
      name - the name to remove the namespace from
      Returns:
      the stripped name (or name if name does not contain a namespace prefix)
    • findVariable

      public static AbstractVariable findVariable(IResolutionScope elements, String name, Class<? extends AbstractVariable> type) throws ModelQueryException
      Searches for a specified variable.
      Parameters:
      elements - the elements to search for
      name - the name of the variable to search for (may be qualified)
      type - the specific variable of datatype to be returned, AbstractVariable is used if null
      Returns:
      the corresponding variable or null
      Throws:
      ModelQueryException - in case of ambiguities
    • findVariableUse

      public static IModelElement findVariableUse(IResolutionScope elements, String name, Class<?> type) throws ModelQueryException
      Searches for a specified variable or variable use.
      Parameters:
      elements - the elements to search for
      name - the name of the variable to search for (may be qualified)
      type - the specific variable of datatype to be returned, AbstractVariable is used if null
      Returns:
      the corresponding variable or CompoundAccessStatement, null else
      Throws:
      ModelQueryException - in case of ambiguities
    • findVariableUse

      private static IModelElement findVariableUse(IResolutionScope elements, String name, Class<?> type, Set<IResolutionScope> done) throws ModelQueryException
      Searches for a specified variable or variable use.
      Parameters:
      elements - the elements to search for
      name - the name of the variable to search for (may be qualified)
      type - the specific variable of datatype to be returned, AbstractVariable is used if null
      done - already processed scopes
      Returns:
      the corresponding variable or CompoundAccessStatement, null else
      Throws:
      ModelQueryException - in case of ambiguities
    • findCompoundOrAttributeAccess

      private static IModelElement findCompoundOrAttributeAccess(IResolutionScope elements, String name, String innerName, IModelElement result) throws ModelQueryException
      Finds a variable by compound/attribute access as part of findVariable(IResolutionScope, String, Class).
      Parameters:
      elements - the elements to search for
      name - the name of the variable to search for
      innerName - the name of the compound element/attribute
      result - the result determined so far
      Returns:
      the subsequent compound, compound access or attribute access
      Throws:
      ModelQueryException - in case of ambiguities
    • getAttribute

      private static AbstractVariable getAttribute(IAttributableElement element, String name)
      Finds an attribute with name on element.
      Parameters:
      element - the attributable element
      name - the name of the attribute to search for
      Returns:
      the attribute or null if none was found
    • findVariable

      private static AbstractVariable findVariable(Compound compound, String name)
      Searches for a variable of name in a given compound. This method takes refined compounds (super compounds) into account.
      Parameters:
      compound - the compound to search for
      name - the name of the variable to search for (may be qualified)
      Returns:
      the variable or null
    • findElementByName

      public static ContainableModelElement findElementByName(IResolutionScope elements, String name, Class<?> type) throws ModelQueryException
      Finds an element by its given name in elements.
      Parameters:
      elements - the elements to search for
      name - the name of the element to search for
      type - the specific type of element to be returned
      Returns:
      the found element or null
      Throws:
      ModelQueryException - in case of violated project access restrictions
    • findElementByName

      private static ContainableModelElement findElementByName(IResolutionScope elements, String name, Class<?> type, Set<IResolutionScope> done) throws ModelQueryException
      Finds an element by its given name in elements.
      Parameters:
      elements - the elements to search for
      name - the name of the element to search for
      type - the specific type of element to be returned
      done - already processed resolution scopes
      Returns:
      the found element or null
      Throws:
      ModelQueryException - in case of violated project access restrictions
    • checkElement

      private static ContainableModelElement checkElement(ContainableModelElement element, String name, Class<?> type)
      Checks a containable model element.
      Parameters:
      element - the element to check
      name - the name to check for
      type - the specific type of element to be returned
      Returns:
      element if found or null
    • findEnum

      public static Enum findEnum(IResolutionScope elements, String name) throws ModelQueryException
      Finds a referenced enum literal in project.
      Parameters:
      elements - the elements to search for
      name - the name (may be qualified)
      Returns:
      the found literal or null if not found
      Throws:
      ModelQueryException - in case of project access restriction violations
    • enumLiteralAsValue

      public static Value enumLiteralAsValue(IResolutionScope elements, String name) throws IvmlException, ModelQueryException
      Finds a referenced enum literal in project.
      Parameters:
      elements - the elements to search for
      name - the name (may be qualified)
      Returns:
      the found literal as value or null if not found
      Throws:
      IvmlException - in case of type compliance problems when creating the value
      ModelQueryException - in case of project access restriction violations
    • getElementsByNamePrefix

      public static List<SearchResult> getElementsByNamePrefix(ModelElement element, String namePrefix, DatatypeVisitor datatypeVisitor, SearchContext context, Class<?>... restrictions)
      Searches a given set of elements by a name prefix starting at element.
      Parameters:
      element - the element to start searching at
      namePrefix - the name prefix
      datatypeVisitor - a visitor for resolving type names
      context - additional information to include or exclude locally defined model elements
      restrictions - type restrictions on the elements to include into the search
      Returns:
      the search results
    • getAllAttributes

      public static List<Attribute> getAllAttributes(IResolutionScope scope)
      Returns all attributes in scope.
      Parameters:
      scope - the scope to be searched
      Returns:
      the attributes in scope, may be empty
    • getAllAttributes

      private static void getAllAttributes(IResolutionScope scope, List<Attribute> attributes, Set<IResolutionScope> done)
      Collects all attributes in scope and imported scopes.
      Parameters:
      scope - the scope to be considered for collecting
      attributes - the contained attributes (modified as a side effect)
      done - already processed scopes
    • findDeclaration

      public static DecisionVariableDeclaration findDeclaration(IResolutionScope scope, ModelQuery.IDeclarationSelector selector)
      Searches a given scope for a variable declaration of type and returns the declaration determined by selector.
      Parameters:
      scope - the scope to search
      selector - the variable selector
      Returns:
      the selected variable declaration or null if none was found
    • findAttribute

      public static Attribute findAttribute(IModelElement scope, String name)
      Finds an attribute within scope or enclosing parent scopes, i.e., search in inverted direction along attribute application/inheritance.
      Parameters:
      scope - the scope (may be null, leads to null as result)
      name - the name of the attribute
      Returns:
      the attribute declaration or null for none