Class ModelQuery

  • Direct Known Subclasses:
    IvmlModelQuery

    public class ModelQuery
    extends java.lang.Object
    Provides an interface to query models.
    Author:
    Holger Eichelberger
    • Constructor Detail

      • ModelQuery

        public ModelQuery()
    • Method Detail

      • 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 java.lang.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 java.lang.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 java.lang.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 java.util.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,
                                         java.util.List<Compound> result,
                                         java.util.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,
                                          java.lang.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,
                                           java.lang.String name,
                                           java.lang.String path,
                                           java.util.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,
                                         java.lang.String name,
                                         java.lang.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(IResolutionScope, String, boolean, Class)
      • findType

        private static IDatatype findType​(IResolutionScope elements,
                                          java.lang.String name,
                                          boolean considerVariableDeclarations,
                                          java.lang.Class<? extends IDatatype> type,
                                          java.util.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,
                                                      java.lang.String typeName,
                                                      java.lang.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,
                                                      java.lang.String typeName,
                                                      java.lang.Class<? extends IDatatype> type,
                                                      java.util.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,
                                                      java.lang.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,
                                                 java.lang.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,
                                                         java.lang.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​(java.lang.String namespace,
                                                     IResolutionScope scope,
                                                     java.util.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,
                                                             java.lang.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​(java.lang.String name,
                                                   ProjectImport imp,
                                                   java.lang.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 java.lang.String removeNamespace​(java.lang.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,
                                                    java.lang.String name,
                                                    java.lang.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,
                                                    java.lang.String name,
                                                    java.lang.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,
                                                     java.lang.String name,
                                                     java.lang.Class<?> type,
                                                     java.util.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,
                                                                   java.lang.String name,
                                                                   java.lang.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,
                                                     java.lang.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,
                                                     java.lang.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,
                                                                java.lang.String name,
                                                                java.lang.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,
                                                                 java.lang.String name,
                                                                 java.lang.Class<?> type,
                                                                 java.util.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,
                                                            java.lang.String name,
                                                            java.lang.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,
                                    java.lang.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,
                                               java.lang.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 java.util.List<SearchResult> getElementsByNamePrefix​(ModelElement element,
                                                                           java.lang.String namePrefix,
                                                                           DatatypeVisitor datatypeVisitor,
                                                                           SearchContext context,
                                                                           java.lang.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 java.util.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,
                                             java.util.List<Attribute> attributes,
                                             java.util.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,
                                              java.lang.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