Class ContextStack
- java.lang.Object
-
- net.ssehub.easy.reasoning.sseReasoner.model.ContextStack
-
public final class ContextStack extends java.lang.ObjectImplements 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, wherebyvariable mapping lookupstarts 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 fromcomposeExpression(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 contextswith their variable in the parent context for lateractivationanddeactivationas 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 classContextStack.ContextA linked stack entry.static classContextStack.TranslateMode(Compound) Translation mode for active type caches.
-
Field Summary
Fields Modifier and Type Field Description private ContextStack.ContextcurrentContextprivate java.util.Set<? extends IDatatype>globalExcludesprivate static Pool<ContextStack.Context>POOLprivate TypeCachetypeCache
-
Constructor Summary
Constructors Constructor Description ContextStack()Creates a context stack with implicit top-most (project) context.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddAsParentCache(IDatatype type, TypeCache.IConstraintTarget target, IDecisionVariable register, AbstractVariable var)Adds the type cache oftypeas parent to the type cache that is currently in construction.voidaddConstraint(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.booleanalreadyProcessed(IDatatype type)Returns whethertypewas already processed.voidclear()Clears the stack, i.e., pops all contexts and clears the top-most (project) context for re-use.(package private) voidcollectAnnotationAssignments(AttributeAssignment assng)Collects annotation assignment names in this context.ConstraintSyntaxTreecomposeExpression(ConstraintSyntaxTree cst)Composes an all-quantified expression over the contexts of the stack.booleanconstraintVarOnly(boolean clear)Returns the flag of the current context whether only constraint variables shall be translated.booleancontainsMapping(AbstractVariable var)Returns whether the current context contains a mapping forvar.ConstraintSyntaxTreegetCurrentContainer()Returns the current container expression from the top-most context.DecisionVariableDeclarationgetCurrentIterator()Returns the current iterator variable from the top-most context.IDatatypegetCurrentType()Returns the type stored in the current context.TypeCache.EntrygetInConstruction(boolean clear)Returns whether the current context is currently constructing a type cache.ConstraintSyntaxTreegetLocalMapping(java.lang.String name)Returns the local mapping for the given variablenamejust from the current context.ConstraintSyntaxTreegetMapping(AbstractVariable var)Returns the mapped access expression forvartaking into account all current contexts of the stack starting with the current top-most one.ContextStack.TranslateModegetMappingMode(IDatatype type)Returns the type cache mapping mode when trying to register a new type.booleanisElementTypeExcluded(IModelElement element)Returns whether the givenelementif it is a type is excluded in the current context.booleanisKnownAnnotationAssignment(java.lang.String name)Returns whethernameis a known annotation assignment (annotation variable) in this context.booleanisTypeExcluded(IDatatype type)Returns whether the giventypeis excluded in the current context.voidnotifyCashMapping()Notifies the current context that mappings shall be cached uponpopContext(IDatatype).voidpopContext()Removes (and clears) the current context from the stack (except for the initial project context which remains).voidpopContext(IDatatype type)Pops a context and transfers the cache information.private ContextStack.ContextpopContextImpl()Pops the actual context if not the initial (project) context but does not clean up the popped context.voidpushContext(AbstractVariable decl, boolean recordProcessedTypes)Pushes a new (compound) context to the stack.voidpushContext(AbstractVariable decl, ConstraintSyntaxTree container, DecisionVariableDeclaration iterator, boolean recordProcessedTypes)Pushes a new container context to the stack.private voidpushContextImpl(ContextStack.Context context)Just pushes the givencontextwithout changing it.voidrecordAnnotationAssignments(Compound type)Records annotation assignments for the given compoundtypefor this context.private voidrecordProcessed(java.util.Set<IDatatype> processed, Compound type)Records a processed compound.voidrecordProcessed(IDatatype type)Records a processed type (in case of compounds also all refined types) in the closest actual context that allows recording (seeContextStack.Context.recordProcessedTypesandpushContext(AbstractVariable, ConstraintSyntaxTree, DecisionVariableDeclaration, boolean)).voidregisterForTypeCache(IDatatype type, AbstractVariable var)Registers the given type and the prototypical template variablevaras type in the type cache.voidregisterMapping(AbstractVariable var, ConstraintSyntaxTree acc)Registers a mapping between the variablevarand its actual access expressionaccinto the current top-most context.voidsetConstraintVarOnly(boolean constraintVarOnly)Changes the flag of the current context whether only constraint variables shall be translated.voidsetInConstruction(TypeCache.Entry inConstruction)Changes the type cache the current context is currently constructing.voidsetTypeExcludes(java.util.Set<? extends IDatatype> excludes)Sets type excludes on the current context.intsize()Returns the size of the context stack.booleantransferConstraints(IDatatype type, TypeCache.IConstraintTarget target, IDecisionVariable register, AbstractVariable var)Transfers all constraints from the associated type cache entry into their respective target constraint sets.voidtransferToContext(IDatatype type, AbstractVariable var)Indicates that mapping information from the type cache shall be transferred into the current context.voidtransferTypeExcludes(IDatatype type)Transfers the type excludes from the next enclosing context defining type excludes to the current context.voidunregisterMapping(AbstractVariable var)Unregisters a mapping for the variablevarfrom the current context.
-
-
-
Field Detail
-
POOL
private static final Pool<ContextStack.Context> POOL
-
currentContext
private ContextStack.Context currentContext
-
globalExcludes
private java.util.Set<? extends IDatatype> globalExcludes
-
typeCache
private transient TypeCache typeCache
-
-
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 ifenabled, may be null to explicitly prevent registrationrecordProcessedTypes- 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 compoundtypefor 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 whethernameis a known annotation assignment (annotation variable) in this context.- Parameters:
name- the variable name- Returns:
trueif the annotation variable is known,falseelse
-
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 ifenabled, may be null to explicitly prevent registrationcontainer- the container expression (may be null)iterator- a container iterator variable forcontainer, may be null but only ifcontaineris nullrecordProcessedTypes- whether processed types indicated by#recordType(IDatatype)shall be recorded or not (false)
-
pushContextImpl
private void pushContextImpl(ContextStack.Context context)
Just pushes the givencontextwithout changing it.- Parameters:
context- the context to be pushed
-
popContext
public void popContext(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 inContextStack.Context.inConstructionand cashing is neededContextStack.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(AbstractVariable var, ConstraintSyntaxTree acc)
Registers a mapping between the variablevarand its actual access expressionaccinto 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 variableacc- the access expression
-
unregisterMapping
public void unregisterMapping(AbstractVariable var)
Unregisters a mapping for the variablevarfrom the current context.- Parameters:
var- the variable to be unregistered
-
containsMapping
public boolean containsMapping(AbstractVariable var)
Returns whether the current context contains a mapping forvar.- Parameters:
var- the variable to look for- Returns:
trueif there is a mapping,falseelse
-
getMapping
public ConstraintSyntaxTree getMapping(AbstractVariable var)
Returns the mapped access expression forvartaking 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 variablenamejust 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 listfirst- add to the front or to the end oftargetconstraint- the (template) constraint to be storedregister- shall the constraint later be registered withVariablesMap
-
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 byvar. [type cache]- Parameters:
type- the type to transfer the constraints fortarget- the constraint targetregister- the variable to register the new constraints withVariablesMapvar- the actual variable for substitution- Returns:
trueif the transfer was done even if no constraint was transferred as no one was registered and no further translation shall happen,falseif 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 tofalse(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 variablevaras 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 ofvarwill be substituted by a given variable. [type cache]- Parameters:
type- the type to relate tovar- 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 tonull(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 benullfor none
-
notifyCashMapping
public void notifyCashMapping()
Notifies the current context that mappings shall be cached uponpopContext(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 onTypeCache.ON_DEMAND_ACCESSORS. [type cache]- Parameters:
type- the type to do the transfer forvar- the variable replacing the template variable
-
addAsParentCache
public void addAsParentCache(IDatatype type, TypeCache.IConstraintTarget target, IDecisionVariable register, AbstractVariable var)
Adds the type cache oftypeas parent to the type cache that is currently in construction. [type cache]- Parameters:
type- the type to addtarget- the target container for immediately transferring constraintsregister- whether constraints shall be registeredvar- 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,cstwill 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
cstif there is nothing to compose - Throws:
CSTSemanticException- if the composed expression is not valid
-
recordProcessed
public void recordProcessed(IDatatype type)
Records a processed type (in case of compounds also all refined types) in the closest actual context that allows recording (seeContextStack.Context.recordProcessedTypesandpushContext(AbstractVariable, ConstraintSyntaxTree, DecisionVariableDeclaration, boolean)). Storestypein the current context for retrieval viagetCurrentType().- Parameters:
type- the type to record
-
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 whethertypewas already processed. All current contexts with enabled recording are considered.- Parameters:
type- the type to look for- Returns:
trueif the type was already processed,falseif not
-
size
public int size()
Returns the size of the context stack.- Returns:
- the size
-
setTypeExcludes
public void setTypeExcludes(java.util.Set<? extends 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 betransferred.- Parameters:
excludes- the type excludes, null for none- See Also:
isTypeExcluded(IDatatype),isElementTypeExcluded(IModelElement)
-
transferTypeExcludes
public void transferTypeExcludes(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:
setTypeExcludes(Set),isTypeExcluded(IDatatype),isElementTypeExcluded(IModelElement)
-
isElementTypeExcluded
public boolean isElementTypeExcluded(IModelElement element)
Returns whether the givenelementif it is a type is excluded in the current context.- Parameters:
element- the element to check for- Returns:
trueif excluded,falseelse
-
isTypeExcluded
public boolean isTypeExcluded(IDatatype type)
Returns whether the giventypeis excluded in the current context.- Parameters:
type- the type to check for- Returns:
trueif excluded,falseelse
-
getCurrentType
public IDatatype getCurrentType()
Returns the type stored in the current context.- Returns:
- the type, may be null if there is none
-
-