Class TypeContext

  • All Implemented Interfaces:
    IResolutionScope

    public class TypeContext
    extends java.lang.Object
    implements IResolutionScope
    Defines a type context which consists of all variables defined in a project and additionally variables which can be added to the context in a layered manner. While IVML currently may not really require layered type contexts (shadowing variables), we just introduce it here for future use. The typical use is context.pushLayer(); context.addToContext(...); try { // resolve some types or expressions } catch (TranslatorException e) { throw e; } finally { context.popLayer(); } Please note that layers must properly be cleared (popLayer() . Further, a type context provides a resolution scope for its implicit definitions (without providing interfaces or a name).
    This class provides support for sorting elements on project level as well as on compound level according to the input.
    Author:
    Holger Eichelberger
    • Constructor Detail

      • TypeContext

        public TypeContext​(Project project,
                           MessageReceiver messageReceiver)
        Creates a type context for a given project.
        Parameters:
        project - the project representing the root resolution scope of this type context
        messageReceiver - instance for receiving, storing and passing messages to Eclipse
      • TypeContext

        public TypeContext​(TypeContext context)
        Creates a local type context.
        Parameters:
        context - the global type context to take basic information from
    • Method Detail

      • pushLayer

        public void pushLayer​(IModelElement parent)
        Push a resolution layer in case that intermediary variables shell be considered, e.g. within a compound. Please note that the parent itself is not added to the layer/context and needs a specific subsequent add call.
        Parameters:
        parent - the parent (scope, may be null - in this case the parent scope of the previous layer is considered)
      • pushParent

        public int pushParent​(DecisionVariableDeclaration decVar)
        Pushes the enclosing parents of decVar.
        Parameters:
        decVar - the variable to push
        Returns:
        the number of layers pushed
      • pushParentRec

        private int pushParentRec​(IModelElement elt)
        Pushes the enclosing parents of elt in a recursive manner.
        Parameters:
        elt - the element to push the parents for
        Returns:
        the number of layers pushed
      • popLayer

        public void popLayer()
        Pop a resolution layer in case that intermediary variables shell be considered, e.g. at the end of a compound. Please consider proper handling of exceptions so that this method is always called at the end of a pushed section so that further semantic analysis after the error is possible!
      • addToContext

        public void addToContext​(Compound comp)
        Adds a compound to the current context (layer). Please call #pushLayer() before.
        Parameters:
        comp - the compound for which all contained variables shall be added
      • addToContext

        private void addToContext​(Compound comp,
                                  java.util.Set<Compound> done)
        Adds a compound to the current context (layer). Please call #pushLayer() before.
        Parameters:
        comp - the compound for which all contained variables shall be added
        done - already added compounds
      • addToContext

        public void addToContext​(AttributeAssignment assignment)
        Adds an attribute assignment to the current context (layer). Please call #pushLayer() before.
        Parameters:
        assignment - the assignment for which all contained variables shall be added
      • addToContext

        void addToContext​(Compound compound,
                          java.lang.String unresolvedParentCompound)
        Stores a refined compound where the parent/super compound could not be loaded at the current time.
        Parameters:
        compound - A refined compound where the super compound was not set so far (i.e. is set to null).
        unresolvedParentCompound - The name of the super/parent compound, which was not found so far.
      • getUnresolvedCompoundRefinments

        java.util.List<Compound> getUnresolvedCompoundRefinments​(java.lang.String parentCompound)
        Returns a list of compounds where the given compound was specified as a super/parent compound, but could not be set, as it was not available during parsing the child compound.
        Parameters:
        parentCompound - The parent/super compound of a refinement.
        Returns:
        null or a list of incomplete compounds.
      • clearUnresolvedCompounds

        void clearUnresolvedCompounds​(java.lang.String parentCompound)
        Clears the list of incomplete compounds, where the parent/super compound of a refinement was missing. Should be done after the refinements where resolved.
        Parameters:
        parentCompound - The name of the super/parent compound.
      • addToContext

        public void addToContext​(OperationDefinition opDef)
        Adds an operation definition to the current context (layer). Please call #pushLayer() before.
        Parameters:
        opDef - the operation definition which shall be added
      • addToContext

        public void addToContext​(DecisionVariableDeclaration var)
        Adds a variable declaration to the current context (layer). Please call #pushLayer() before.
        Parameters:
        var - the decision variable declaration which shall be added
      • getProject

        public Project getProject()
        Returns the project providing the root resolution scope.
        Returns:
        the project
      • obtainVariable

        public final Variable obtainVariable​(AbstractVariable decl)
        Obtains a CTS variable node for a given variable declaration, i.e., creates it or returns it from the variable pool of this context.
        Parameters:
        decl - the declaration
        Returns:
        the variable
      • resolveType

        public IDatatype resolveType​(de.uni_hildesheim.sse.ivml.Type type)
                              throws TranslatorException
        Resolves a given type to a type representation in the IVML object model.
        Parameters:
        type - the type to be resolved
        Returns:
        the representing type
        Throws:
        TranslatorException - in case that the given type cannot be resolved
      • findRefType

        public IDatatype findRefType​(IDatatype contained)
        Finds a reference type (declaration or defines an implicit type) for a given contained type.
        Parameters:
        contained - the contained type
        Returns:
        the reference type declaration
      • findElementByName

        public ContainableModelElement findElementByName​(java.lang.String name,
                                                         java.lang.Class<? extends ModelElement> type)
                                                  throws ModelQueryException
        Searches for a specified element.
        Parameters:
        name - the name of the element to search for (may be qualified)
        type - the specific element to be returned
        Returns:
        the corresponding variable or null
        Throws:
        ModelQueryException - in case of semantic problems
      • findVariable

        public AbstractVariable findVariable​(java.lang.String name,
                                             java.lang.Class<? extends AbstractVariable> type)
                                      throws ModelQueryException
        Searches for a specified variable.
        Parameters:
        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 semantic problems
      • findActualParent

        private IModelElement findActualParent​(IModelElement elt)
        Returns the actual project or compound the element is located in.
        Parameters:
        elt - the element
        Returns:
        the actual parent
      • findVariableUse

        public IModelElement findVariableUse​(java.lang.String name)
                                      throws ModelQueryException
        Searches for the use of a specified variable (declaration or compound access).
        Parameters:
        name - the name of the variable to search for (may be qualified)
        Returns:
        the corresponding variable or null
        Throws:
        ModelQueryException - in case of semantic problems
      • findType

        public IDatatype findType​(java.lang.String name,
                                  java.lang.Class<? extends IDatatype> type)
                           throws ModelQueryException
        Finds the specified data type definition.
        Parameters:
        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 semantic problems
      • findCompounds

        public Compound[] findCompounds​(java.util.List<java.lang.String> names,
                                        boolean nullOnFail)
                                 throws ModelQueryException
        Finds compounds according to the given names.
        Parameters:
        names - the names of the compounds
        nullOnFail - if null shall be returned on failing resolution
        Returns:
        the found compounds (null if there was nothing to resolve or individuals were not resolved and nullOnFail, else an array with null entries)
        Throws:
        ModelQueryException - in case of semantic problems
      • allResolved

        public static boolean allResolved​(IDatatype[] types)
        Returns whether all types in types are resolved.
        Parameters:
        types - the types to inspect
        Returns:
        true for all resolved, false for at least one not resolved or null==types
      • getElement

        public ContainableModelElement getElement​(int index)
        Returns a contained model element specified by index.
        Specified by:
        getElement in interface IResolutionScope
        Parameters:
        index - a 0-based index specifying the operation to be returned
        Returns:
        the contained element
        Throws:
        java.lang.IndexOutOfBoundsException - if index<0 || index>=getElementCount()
      • getElementCount

        public int getElementCount()
        Returns the number of contained elements.
        Specified by:
        getElementCount in interface IResolutionScope
        Returns:
        the number of contained elements
      • resolveValue

        public ConstraintSyntaxTree resolveValue​(de.uni_hildesheim.sse.ivml.Value value,
                                                 IModelElement parent,
                                                 org.eclipse.emf.ecore.EObject object,
                                                 org.eclipse.emf.ecore.EStructuralFeature feature)
                                          throws TranslatorException
        Resolves a value and returns the corresponding syntax tree (one node).
        Parameters:
        value - the value to be converted
        parent - the model parent
        object - the grammar object this method is called for
        feature - the grammar feature this method is called for
        Returns:
        the corresponding syntax tree
        Throws:
        TranslatorException - in case of any translation problem
      • createConstantValue

        private static ConstantValue createConstantValue​(org.eclipse.emf.ecore.EObject object,
                                                         org.eclipse.emf.ecore.EStructuralFeature feature,
                                                         IDatatype type,
                                                         java.lang.Object... values)
                                                  throws TranslatorException
        Creates a constant value.
        Parameters:
        object - the grammar object this method is called for
        feature - the grammar feature this method is called for
        type - the type to create the value for
        values - the actual value(s) (may be null)
        Returns:
        the constant value
        Throws:
        TranslatorException - if creating the constant value fails
      • resolveSelf

        private ConstraintSyntaxTree resolveSelf​(IModelElement parent,
                                                 org.eclipse.emf.ecore.EObject object,
                                                 org.eclipse.emf.ecore.EStructuralFeature feature)
                                          throws TranslatorException
        Resolves "self" and returns the corresponding syntax tree (one node).
        Parameters:
        parent - the model parent
        object - the grammar object this method is called for
        feature - the grammar feature this method is called for
        Returns:
        the corresponding syntax tree
        Throws:
        TranslatorException - in case of any translation problem
      • getSelf

        private Self getSelf​(Compound comp)
        Returns a self instance from the self pool.
        Parameters:
        comp - the compond to return the self instance for
        Returns:
        the self instance
      • processQValue

        ConstraintSyntaxTree processQValue​(java.lang.String sValue,
                                           org.eclipse.emf.ecore.EObject object,
                                           org.eclipse.emf.ecore.EStructuralFeature feature)
                                    throws TranslatorException
        Processes a qualified name as value.
        Parameters:
        sValue - the qualified name as string
        object - the grammar object this method is called for
        feature - the grammar feature this method is called for
        Returns:
        the corresponding syntax tree
        Throws:
        TranslatorException - in case of any translation problem
      • checkEnumOclCompliance

        public void checkEnumOclCompliance​(java.lang.String qName,
                                           org.eclipse.emf.ecore.EObject object,
                                           org.eclipse.emf.ecore.EStructuralFeature feature)
        Checks a (potentially) qualified enum literal name for OCL compliance if enabled.
        Parameters:
        qName - the qualified name
        object - the grammar object this method is called for
        feature - the grammar feature this method is called for
      • createValueTree

        private ConstraintSyntaxTree createValueTree​(java.lang.String sValue,
                                                     IDatatype type,
                                                     org.eclipse.emf.ecore.EObject object,
                                                     org.eclipse.emf.ecore.EStructuralFeature feature)
                                              throws TranslatorException
        Creates a value tree for a given type.
        Parameters:
        sValue - the string representation of the value
        type - the target type
        object - the grammar object this method is called for
        feature - the grammar feature this method is called for
        Returns:
        the value tree
        Throws:
        TranslatorException - in case of any translation problem
      • getImport

        public ProjectImport getImport​(int index)
        Description copied from interface: IResolutionScope
        Returns the project import specified by index.
        Specified by:
        getImport in interface IResolutionScope
        Parameters:
        index - a 0-based index specifying the import to be returned
        Returns:
        the project import
      • getName

        public java.lang.String getName()
        Description copied from interface: IResolutionScope
        Returns the (unqualified) name of the scope.
        Specified by:
        getName in interface IResolutionScope
        Returns:
        the unqualified name of the scope
      • hasInterfaces

        public boolean hasInterfaces()
        Description copied from interface: IResolutionScope
        Returns whether this scope has interfaces.
        Specified by:
        hasInterfaces in interface IResolutionScope
        Returns:
        true if it has interfaces, false else
      • isInterface

        public boolean isInterface()
        Description copied from interface: IResolutionScope
        Returns whether this scope is an interfaces.
        Specified by:
        isInterface in interface IResolutionScope
        Returns:
        true if it is an interface, false else
      • addToProject

        public boolean addToProject​(org.eclipse.emf.ecore.EObject key,
                                    Comment comment,
                                    ContainableModelElement element)
        Adds a containable element to the root resolution scope as well as a mapping from key to comment (optional) and element.
        Parameters:
        key - the key object as parsed by xText from the input file (may be null then no mapping is added, e.g. when adding a comment only)
        comment - the comment instance (may be null)
        element - the element instance
        Returns:
        true if the addition was successful, false else due to duplicated names
        See Also:
        ContainableElementsMapAndComparator.put(EObject, Comment, ContainableModelElement)
      • sortProjectElements

        public void sortProjectElements​(java.util.List<org.eclipse.emf.ecore.EObject> topLevelElements)
        Sorts the elements in the root resolution scope according to the the given list of top-level elements.
        Parameters:
        topLevelElements - the top-level elements from the grammar
      • setIndexes

        private static void setIndexes​(java.util.List<org.eclipse.emf.ecore.EObject> elements,
                                       ContainableElementsMapAndComparator map)
        Transfers the index positions of elements to map.
        Parameters:
        elements - the list representing the positions
        map - the target map to set the values to
      • registerSorter

        public void registerSorter​(IContainableElementsSorter sorter,
                                   org.eclipse.emf.ecore.EObject key,
                                   Comment comment,
                                   ContainableModelElement element)
        Registers an element to be sorted (later).
        Parameters:
        sorter - the element being sorted later
        key - the key object as parsed by xText from the input file (may be null then no mapping is added, e.g. when adding a comment only)
        comment - the comment instance (may be null)
        element - the element instance
      • closeSorter

        public void closeSorter​(IContainableElementsSorter sorter,
                                java.util.List<org.eclipse.emf.ecore.EObject> elements)
        Closes an elements sorter (regarding the stored data) and sorts the elements within the compound.
        Parameters:
        sorter - the element being sorted
        elements - the grammar elements within the compound defining the sort order
      • clear

        public void clear()
        Clears this type context (for possible reuse).
      • getElement

        public ContainableModelElement getElement​(java.lang.String name)
        Description copied from interface: IResolutionScope
        Returns a contained model element specified by its name.
        Specified by:
        getElement in interface IResolutionScope
        Parameters:
        name - the qualified or unqualified name to search for
        Returns:
        the related element or null if no such element is known