Class ContextStack

java.lang.Object
net.ssehub.easy.reasoning.sseReasoner.model.ContextStack

public final class ContextStack extends 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 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
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    (package private) static class 
    A linked stack entry.
    static enum 
    (Compound) Translation mode for active type caches.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
     
    private Set<? extends net.ssehub.easy.varModel.model.datatypes.IDatatype>
     
    private static final net.ssehub.easy.basics.pool.Pool<ContextStack.Context>
     
    private TypeCache
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a context stack with implicit top-most (project) context.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addAsParentCache(net.ssehub.easy.varModel.model.datatypes.IDatatype type, TypeCache.IConstraintTarget target, net.ssehub.easy.varModel.confModel.IDecisionVariable register, net.ssehub.easy.varModel.model.AbstractVariable var)
    Adds the type cache of type as parent to the type cache that is currently in construction.
    void
    addConstraint(net.ssehub.easy.reasoning.core.reasoner.ConstraintList target, boolean first, net.ssehub.easy.varModel.model.Constraint constraint, boolean register)
    Adds a constraint to the type cache that is currently in construction for the actual context frame.
    boolean
    alreadyProcessed(net.ssehub.easy.varModel.model.datatypes.IDatatype type)
    Returns whether type was already processed.
    void
    Clears the stack, i.e., pops all contexts and clears the top-most (project) context for re-use.
    (package private) void
    collectAnnotationAssignments(net.ssehub.easy.varModel.model.AttributeAssignment assng)
    Collects annotation assignment names in this context.
    net.ssehub.easy.varModel.cst.ConstraintSyntaxTree
    composeExpression(net.ssehub.easy.varModel.cst.ConstraintSyntaxTree cst)
    Composes an all-quantified expression over the contexts of the stack.
    boolean
    constraintVarOnly(boolean clear)
    Returns the flag of the current context whether only constraint variables shall be translated.
    boolean
    containsMapping(net.ssehub.easy.varModel.model.AbstractVariable var)
    Returns whether the current context contains a mapping for var.
    net.ssehub.easy.varModel.cst.ConstraintSyntaxTree
    Returns the current container expression from the top-most context.
    net.ssehub.easy.varModel.model.DecisionVariableDeclaration
    Returns the current iterator variable from the top-most context.
    net.ssehub.easy.varModel.model.datatypes.IDatatype
    Returns the type stored in the current context.
    getInConstruction(boolean clear)
    Returns whether the current context is currently constructing a type cache.
    net.ssehub.easy.varModel.cst.ConstraintSyntaxTree
    Returns the local mapping for the given variable name just from the current context.
    net.ssehub.easy.varModel.cst.ConstraintSyntaxTree
    getMapping(net.ssehub.easy.varModel.model.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.
    getMappingMode(net.ssehub.easy.varModel.model.datatypes.IDatatype type)
    Returns the type cache mapping mode when trying to register a new type.
    boolean
    isElementTypeExcluded(net.ssehub.easy.varModel.model.IModelElement element)
    Returns whether the given element if it is a type is excluded in the current context.
    boolean
    Returns whether name is a known annotation assignment (annotation variable) in this context.
    boolean
    isTypeExcluded(net.ssehub.easy.varModel.model.datatypes.IDatatype type)
    Returns whether the given type is excluded in the current context.
    void
    Notifies the current context that mappings shall be cached upon popContext(IDatatype).
    void
    Removes (and clears) the current context from the stack (except for the initial project context which remains).
    void
    popContext(net.ssehub.easy.varModel.model.datatypes.IDatatype type)
    Pops a context and transfers the cache information.
    Pops the actual context if not the initial (project) context but does not clean up the popped context.
    void
    pushContext(net.ssehub.easy.varModel.model.AbstractVariable decl, boolean recordProcessedTypes)
    Pushes a new (compound) context to the stack.
    void
    pushContext(net.ssehub.easy.varModel.model.AbstractVariable decl, net.ssehub.easy.varModel.cst.ConstraintSyntaxTree container, net.ssehub.easy.varModel.model.DecisionVariableDeclaration iterator, boolean recordProcessedTypes)
    Pushes a new container context to the stack.
    private void
    Just pushes the given context without changing it.
    void
    recordAnnotationAssignments(net.ssehub.easy.varModel.model.datatypes.Compound type)
    Records annotation assignments for the given compound type for this context.
    private void
    recordProcessed(Set<net.ssehub.easy.varModel.model.datatypes.IDatatype> processed, net.ssehub.easy.varModel.model.datatypes.Compound type)
    Records a processed compound.
    void
    recordProcessed(net.ssehub.easy.varModel.model.datatypes.IDatatype type)
    Records a processed type (in case of compounds also all refined types) in the closest actual context that allows recording (see ContextStack.Context.recordProcessedTypes and pushContext(AbstractVariable, ConstraintSyntaxTree, DecisionVariableDeclaration, boolean)).
    void
    registerForTypeCache(net.ssehub.easy.varModel.model.datatypes.IDatatype type, net.ssehub.easy.varModel.model.AbstractVariable var)
    Registers the given type and the prototypical template variable var as type in the type cache.
    void
    registerMapping(net.ssehub.easy.varModel.model.AbstractVariable var, net.ssehub.easy.varModel.cst.ConstraintSyntaxTree acc)
    Registers a mapping between the variable var and its actual access expression acc into the current top-most context.
    void
    setConstraintVarOnly(boolean constraintVarOnly)
    Changes the flag of the current context whether only constraint variables shall be translated.
    void
    Changes the type cache the current context is currently constructing.
    void
    setTypeExcludes(Set<? extends net.ssehub.easy.varModel.model.datatypes.IDatatype> excludes)
    Sets type excludes on the current context.
    int
    Returns the size of the context stack.
    boolean
    transferConstraints(net.ssehub.easy.varModel.model.datatypes.IDatatype type, TypeCache.IConstraintTarget target, net.ssehub.easy.varModel.confModel.IDecisionVariable register, net.ssehub.easy.varModel.model.AbstractVariable var)
    Transfers all constraints from the associated type cache entry into their respective target constraint sets.
    void
    transferToContext(net.ssehub.easy.varModel.model.datatypes.IDatatype type, net.ssehub.easy.varModel.model.AbstractVariable var)
    Indicates that mapping information from the type cache shall be transferred into the current context.
    void
    transferTypeExcludes(net.ssehub.easy.varModel.model.datatypes.IDatatype type)
    Transfers the type excludes from the next enclosing context defining type excludes to the current context.
    void
    unregisterMapping(net.ssehub.easy.varModel.model.AbstractVariable var)
    Unregisters a mapping for the variable var from the current context.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • POOL

      private static final net.ssehub.easy.basics.pool.Pool<ContextStack.Context> POOL
    • currentContext

      private ContextStack.Context currentContext
    • globalExcludes

      private Set<? extends net.ssehub.easy.varModel.model.datatypes.IDatatype> globalExcludes
    • typeCache

      private transient TypeCache typeCache
  • Constructor Details

    • ContextStack

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

    • 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(net.ssehub.easy.varModel.model.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 shall be recorded or not (false)
    • recordAnnotationAssignments

      public void recordAnnotationAssignments(net.ssehub.easy.varModel.model.datatypes.Compound type)
      Records annotation assignments for the given compound type for this context.
      Parameters:
      type - the compound type
    • collectAnnotationAssignments

      void collectAnnotationAssignments(net.ssehub.easy.varModel.model.AttributeAssignment assng)
      Collects annotation assignment names in this context.
      Parameters:
      assng - the assignment to collect for
    • isKnownAnnotationAssignment

      public boolean isKnownAnnotationAssignment(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(net.ssehub.easy.varModel.model.AbstractVariable decl, net.ssehub.easy.varModel.cst.ConstraintSyntaxTree container, net.ssehub.easy.varModel.model.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 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(net.ssehub.easy.varModel.model.datatypes.IDatatype type)
      Pops a context and transfers the cache information. [type cache]
      Parameters:
      type - the type to transfer the data to if the actual context is in ContextStack.Context.inConstruction and cashing is needed ContextStack.Context.cashMapping.
    • 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(net.ssehub.easy.varModel.model.AbstractVariable var, net.ssehub.easy.varModel.cst.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(net.ssehub.easy.varModel.model.AbstractVariable var)
      Unregisters a mapping for the variable var from the current context.
      Parameters:
      var - the variable to be unregistered
    • containsMapping

      public boolean containsMapping(net.ssehub.easy.varModel.model.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 net.ssehub.easy.varModel.cst.ConstraintSyntaxTree getMapping(net.ssehub.easy.varModel.model.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 net.ssehub.easy.varModel.cst.ConstraintSyntaxTree getLocalMapping(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(net.ssehub.easy.reasoning.core.reasoner.ConstraintList target, boolean first, net.ssehub.easy.varModel.model.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(net.ssehub.easy.varModel.model.datatypes.IDatatype type, TypeCache.IConstraintTarget target, net.ssehub.easy.varModel.confModel.IDecisionVariable register, net.ssehub.easy.varModel.model.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(net.ssehub.easy.varModel.model.datatypes.IDatatype type, net.ssehub.easy.varModel.model.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 addConstraint(ConstraintList, boolean, Constraint, boolean) will be related to the created type cache entry. Upon 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(net.ssehub.easy.varModel.model.datatypes.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(net.ssehub.easy.varModel.model.datatypes.IDatatype type, net.ssehub.easy.varModel.model.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(net.ssehub.easy.varModel.model.datatypes.IDatatype type, TypeCache.IConstraintTarget target, net.ssehub.easy.varModel.confModel.IDecisionVariable register, net.ssehub.easy.varModel.model.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 net.ssehub.easy.varModel.cst.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 net.ssehub.easy.varModel.model.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 net.ssehub.easy.varModel.cst.ConstraintSyntaxTree composeExpression(net.ssehub.easy.varModel.cst.ConstraintSyntaxTree cst) throws net.ssehub.easy.varModel.cst.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:
      net.ssehub.easy.varModel.cst.CSTSemanticException - if the composed expression is not valid
    • recordProcessed

      public void recordProcessed(net.ssehub.easy.varModel.model.datatypes.IDatatype type)
      Records a processed type (in case of compounds also all refined types) in the closest actual context that allows recording (see ContextStack.Context.recordProcessedTypes and pushContext(AbstractVariable, ConstraintSyntaxTree, DecisionVariableDeclaration, boolean)). Stores type in the current context for retrieval via getCurrentType().
      Parameters:
      type - the type to record
    • recordProcessed

      private void recordProcessed(Set<net.ssehub.easy.varModel.model.datatypes.IDatatype> processed, net.ssehub.easy.varModel.model.datatypes.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(net.ssehub.easy.varModel.model.datatypes.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
    • setTypeExcludes

      public void setTypeExcludes(Set<? extends net.ssehub.easy.varModel.model.datatypes.IDatatype> excludes)
      Sets type excludes on the current context. Type excludes are only valid on the given context, but defined type excludes on the next enclosing context may be transferred.
      Parameters:
      excludes - the type excludes, null for none
      See Also:
    • transferTypeExcludes

      public void transferTypeExcludes(net.ssehub.easy.varModel.model.datatypes.IDatatype type)
      Transfers the type excludes from the next enclosing context defining type excludes to the current context. Clears the type excludes in the originating context.
      Parameters:
      type - the type causing the transfer, to be stored in the current context (@link #getCurrentType()}
      See Also:
    • isElementTypeExcluded

      public boolean isElementTypeExcluded(net.ssehub.easy.varModel.model.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(net.ssehub.easy.varModel.model.datatypes.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 net.ssehub.easy.varModel.model.datatypes.IDatatype getCurrentType()
      Returns the type stored in the current context.
      Returns:
      the type, may be null if there is none