Class ContextStack


  • public final class ContextStack
    extends java.lang.Object
    Implements a context stack for nested translations. The top-most context corresponds to the project, further nested contexts to visited compounds or containers. Each context contains the recent part of the variable mapping, whereby variable mapping lookup starts with the current context and takes also previous contexts into account. Each context may parts of a complex constraint accessing the path to contained compound variables (the complete constraint over all stack contexts can be obtained from composeExpression(ConstraintSyntaxTree).

    However, due to IVML assignment blocks, a translation context stack is not really straightforward. In assignment blocks, additional implicit constraints to initialize annotations are introduced and translated after the actual variable has been translated, i.e., outside the context of a compound or container variable. Therefore, we allow registering contexts with their variable in the parent context for later activation and deactivation as long as the parent context exists. If re-activated, an already popped context (which must not be cleaned up then) is pushed back onto the stack. If de-activated, the re-activated context is popped again but not cleaned up. Registered contexts are cleaned up when the parent context is popped.

    Author:
    Holger Eichelberger
    • Constructor Detail

      • ContextStack

        public ContextStack()
        Creates a context stack with implicit top-most (project) context.
    • Method Detail

      • clear

        public void clear()
        Clears the stack, i.e., pops all contexts and clears the top-most (project) context for re-use.
      • pushContext

        public void pushContext​(AbstractVariable decl,
                                boolean recordProcessedTypes)
        Pushes a new (compound) context to the stack. No processed types are recorded.
        Parameters:
        decl - the variable to register the new context with if enabled, may be null to explicitly prevent registration
        recordProcessedTypes - whether processed types indicated by #recordType(IDatatype) shall be recorded or not (false)
      • recordAnnotationAssignments

        public void recordAnnotationAssignments​(Compound type)
        Records annotation assignments for the given compound type for this context.
        Parameters:
        type - the compound type
      • collectAnnotationAssignments

        void collectAnnotationAssignments​(AttributeAssignment assng)
        Collects annotation assignment names in this context.
        Parameters:
        assng - the assignment to collect for
      • isKnownAnnotationAssignment

        public boolean isKnownAnnotationAssignment​(java.lang.String name)
        Returns whether name is a known annotation assignment (annotation variable) in this context.
        Parameters:
        name - the variable name
        Returns:
        true if the annotation variable is known, false else
      • pushContext

        public void pushContext​(AbstractVariable decl,
                                ConstraintSyntaxTree container,
                                DecisionVariableDeclaration iterator,
                                boolean recordProcessedTypes)
        Pushes a new container context to the stack. All parameters may be null.
        Parameters:
        decl - the variable to register the new context with if enabled, may be null to explicitly prevent registration
        container - the container expression (may be null)
        iterator - a container iterator variable for container, may be null but only if container is null
        recordProcessedTypes - whether processed types indicated by #recordType(IDatatype) shall be recorded or not (false)
      • pushContextImpl

        private void pushContextImpl​(ContextStack.Context context)
        Just pushes the given context without changing it.
        Parameters:
        context - the context to be pushed
      • popContext

        public void popContext()
        Removes (and clears) the current context from the stack (except for the initial project context which remains). Contexts are only cleaned up and released to the pool if they are not registered with a parent context.
      • popContextImpl

        private ContextStack.Context popContextImpl()
        Pops the actual context if not the initial (project) context but does not clean up the popped context.
        Returns:
        the popped context, null in case of the initial (project) context
      • registerMapping

        public void registerMapping​(AbstractVariable var,
                                    ConstraintSyntaxTree acc)
        Registers a mapping between the variable var and its actual access expression acc into the current top-most context. Overrides any existing mapping in the top-most context. Preceeds any existing mapping in a previous still active context.
        Parameters:
        var - the variable
        acc - the access expression
      • unregisterMapping

        public void unregisterMapping​(AbstractVariable var)
        Unregisters a mapping for the variable var from the current context.
        Parameters:
        var - the variable to be unregistered
      • containsMapping

        public boolean containsMapping​(AbstractVariable var)
        Returns whether the current context contains a mapping for var.
        Parameters:
        var - the variable to look for
        Returns:
        true if there is a mapping, false else
      • getMapping

        public ConstraintSyntaxTree getMapping​(AbstractVariable var)
        Returns the mapped access expression for var taking into account all current contexts of the stack starting with the current top-most one.
        Parameters:
        var - the variable to return the mapping for
        Returns:
        the mapped access expression or null if there is no registered mapping
      • getLocalMapping

        public ConstraintSyntaxTree getLocalMapping​(java.lang.String name)
        Returns the local mapping for the given variable name just from the current context. Names are registered once, to the most local (non-shadowed) variable.
        Parameters:
        name - the name of the variable
        Returns:
        the mapped access expression or null if there is no registered mapping
      • addConstraint

        public void addConstraint​(ConstraintList target,
                                  boolean first,
                                  Constraint constraint,
                                  boolean register)
        Adds a constraint to the type cache that is currently in construction for the actual context frame. [type cache]
        Parameters:
        target - the target constraint list
        first - add to the front or to the end of target
        constraint - the (template) constraint to be stored
        register - shall the constraint later be registered with VariablesMap
      • transferConstraints

        public boolean transferConstraints​(IDatatype type,
                                           TypeCache.IConstraintTarget target,
                                           IDecisionVariable register,
                                           AbstractVariable var)
        Transfers all constraints from the associated type cache entry into their respective target constraint sets. Substitute the original template variable by var. [type cache]
        Parameters:
        type - the type to transfer the constraints for
        target - the constraint target
        register - the variable to register the new constraints with VariablesMap
        var - the actual variable for substitution
        Returns:
        true if the transfer was done even if no constraint was transferred as no one was registered and no further translation shall happen, false if translation can go on, e.g. the type cache for type is currently in construction
      • constraintVarOnly

        public boolean constraintVarOnly​(boolean clear)
        Returns the flag of the current context whether only constraint variables shall be translated. [type cache]
        Parameters:
        clear - clears the original value to false (e.g., for temporary use)
        Returns:
        the original value
      • setConstraintVarOnly

        public void setConstraintVarOnly​(boolean constraintVarOnly)
        Changes the flag of the current context whether only constraint variables shall be translated. [type cache]
        Parameters:
        constraintVarOnly - the new value of the flag
      • registerForTypeCache

        public void registerForTypeCache​(IDatatype type,
                                         AbstractVariable var)
        Registers the given type and the prototypical template variable var as type in the type cache. As long as the containing context is active, all constraints added through {@link #addConstraint(ConstraintList, boolean, Constraint, boolean))} will be related to the created type cache entry. Upon {@link #transferConstraints(IDatatype, IConstraintTarget, IDecisionVariable, AbstractVariable))} all occurrences of var will be substituted by a given variable. [type cache]
        Parameters:
        type - the type to relate to
        var - the template variable
      • getInConstruction

        public TypeCache.Entry getInConstruction​(boolean clear)
        Returns whether the current context is currently constructing a type cache. [type cache]
        Parameters:
        clear - clears the original value to null (e.g., for temporary use)
        Returns:
        the type cache entry
      • setInConstruction

        public void setInConstruction​(TypeCache.Entry inConstruction)
        Changes the type cache the current context is currently constructing. [type cache]
        Parameters:
        inConstruction - the type cache being in construction, may be null for none
      • notifyCashMapping

        public void notifyCashMapping()
        Notifies the current context that mappings shall be cached upon popContext(IDatatype). [type cache]
      • getMappingMode

        public ContextStack.TranslateMode getMappingMode​(IDatatype type)
        Returns the type cache mapping mode when trying to register a new type. [type cache]
        Parameters:
        type - the type the mode shall be returned for
        Returns:
        the mode
      • transferToContext

        public void transferToContext​(IDatatype type,
                                      AbstractVariable var)
        Indicates that mapping information from the type cache shall be transferred into the current context. This may be done on-demand or in one step depending on TypeCache.ON_DEMAND_ACCESSORS. [type cache]
        Parameters:
        type - the type to do the transfer for
        var - the variable replacing the template variable
      • addAsParentCache

        public void addAsParentCache​(IDatatype type,
                                     TypeCache.IConstraintTarget target,
                                     IDecisionVariable register,
                                     AbstractVariable var)
        Adds the type cache of type as parent to the type cache that is currently in construction. [type cache]
        Parameters:
        type - the type to add
        target - the target container for immediately transferring constraints
        register - whether constraints shall be registered
        var - the replacing variable
      • getCurrentContainer

        public ConstraintSyntaxTree getCurrentContainer()
        Returns the current container expression from the top-most context.
        Returns:
        the container expression (may be null for the initial project context or a compound context)
      • getCurrentIterator

        public DecisionVariableDeclaration getCurrentIterator()
        Returns the current iterator variable from the top-most context.
        Returns:
        the iterator variable (may be null for the initial project context or a compound context)
      • composeExpression

        public ConstraintSyntaxTree composeExpression​(ConstraintSyntaxTree cst)
                                               throws CSTSemanticException
        Composes an all-quantified expression over the contexts of the stack. If no stack contains container/iterator, cst will be returned.
        Parameters:
        cst - the constraint expression to be composed, shall contain the access expression of the top-most context to be effective
        Returns:
        the composed expression or cst if there is nothing to compose
        Throws:
        CSTSemanticException - if the composed expression is not valid
      • recordProcessed

        private void recordProcessed​(java.util.Set<IDatatype> processed,
                                     Compound type)
        Records a processed compound.
        Parameters:
        processed - the set of already processed types (may be modified as a side effect)
        type - the type to be recorded
      • alreadyProcessed

        public boolean alreadyProcessed​(IDatatype type)
        Returns whether type was already processed. All current contexts with enabled recording are considered.
        Parameters:
        type - the type to look for
        Returns:
        true if the type was already processed, false if not
      • size

        public int size()
        Returns the size of the context stack.
        Returns:
        the size
      • isElementTypeExcluded

        public boolean isElementTypeExcluded​(IModelElement element)
        Returns whether the given element if it is a type is excluded in the current context.
        Parameters:
        element - the element to check for
        Returns:
        true if excluded, false else
      • isTypeExcluded

        public boolean isTypeExcluded​(IDatatype type)
        Returns whether the given type is excluded in the current context.
        Parameters:
        type - the type to check for
        Returns:
        true if excluded, false else
      • getCurrentType

        public IDatatype getCurrentType()
        Returns the type stored in the current context.
        Returns:
        the type, may be null if there is none