Class ContextStack
java.lang.Object
net.ssehub.easy.reasoning.sseReasoner.model.ContextStack
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 ClassesModifier and TypeClassDescription(package private) static classA linked stack entry.static enum(Compound) Translation mode for active type caches. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate ContextStack.Contextprivate Set<? extends net.ssehub.easy.varModel.model.datatypes.IDatatype> private static final net.ssehub.easy.basics.pool.Pool<ContextStack.Context> private TypeCache -
Constructor Summary
ConstructorsConstructorDescriptionCreates a context stack with implicit top-most (project) context. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddAsParentCache(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 oftypeas parent to the type cache that is currently in construction.voidaddConstraint(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.booleanalreadyProcessed(net.ssehub.easy.varModel.model.datatypes.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(net.ssehub.easy.varModel.model.AttributeAssignment assng) Collects annotation assignment names in this context.net.ssehub.easy.varModel.cst.ConstraintSyntaxTreecomposeExpression(net.ssehub.easy.varModel.cst.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(net.ssehub.easy.varModel.model.AbstractVariable var) Returns whether the current context contains a mapping forvar.net.ssehub.easy.varModel.cst.ConstraintSyntaxTreeReturns the current container expression from the top-most context.net.ssehub.easy.varModel.model.DecisionVariableDeclarationReturns the current iterator variable from the top-most context.net.ssehub.easy.varModel.model.datatypes.IDatatypeReturns 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.ConstraintSyntaxTreegetLocalMapping(String name) Returns the local mapping for the given variablenamejust from the current context.net.ssehub.easy.varModel.cst.ConstraintSyntaxTreegetMapping(net.ssehub.easy.varModel.model.AbstractVariable var) Returns the mapped access expression forvartaking 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.booleanisElementTypeExcluded(net.ssehub.easy.varModel.model.IModelElement element) Returns whether the givenelementif it is a type is excluded in the current context.booleanReturns whethernameis a known annotation assignment (annotation variable) in this context.booleanisTypeExcluded(net.ssehub.easy.varModel.model.datatypes.IDatatype type) Returns whether the giventypeis excluded in the current context.voidNotifies the current context that mappings shall be cached uponpopContext(IDatatype).voidRemoves (and clears) the current context from the stack (except for the initial project context which remains).voidpopContext(net.ssehub.easy.varModel.model.datatypes.IDatatype type) Pops a context and transfers the cache information.private ContextStack.ContextPops the actual context if not the initial (project) context but does not clean up the popped context.voidpushContext(net.ssehub.easy.varModel.model.AbstractVariable decl, boolean recordProcessedTypes) Pushes a new (compound) context to the stack.voidpushContext(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 voidpushContextImpl(ContextStack.Context context) Just pushes the givencontextwithout changing it.voidrecordAnnotationAssignments(net.ssehub.easy.varModel.model.datatypes.Compound type) Records annotation assignments for the given compoundtypefor this context.private voidrecordProcessed(Set<net.ssehub.easy.varModel.model.datatypes.IDatatype> processed, net.ssehub.easy.varModel.model.datatypes.Compound type) Records a processed compound.voidrecordProcessed(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 (seeContextStack.Context.recordProcessedTypesandpushContext(AbstractVariable, ConstraintSyntaxTree, DecisionVariableDeclaration, boolean)).voidregisterForTypeCache(net.ssehub.easy.varModel.model.datatypes.IDatatype type, net.ssehub.easy.varModel.model.AbstractVariable var) Registers the given type and the prototypical template variablevaras type in the type cache.voidregisterMapping(net.ssehub.easy.varModel.model.AbstractVariable var, net.ssehub.easy.varModel.cst.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(Set<? extends net.ssehub.easy.varModel.model.datatypes.IDatatype> excludes) Sets type excludes on the current context.intsize()Returns the size of the context stack.booleantransferConstraints(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.voidtransferToContext(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.voidtransferTypeExcludes(net.ssehub.easy.varModel.model.datatypes.IDatatype type) Transfers the type excludes from the next enclosing context defining type excludes to the current context.voidunregisterMapping(net.ssehub.easy.varModel.model.AbstractVariable var) Unregisters a mapping for the variablevarfrom the current context.
-
Field Details
-
POOL
-
currentContext
-
globalExcludes
-
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 registrationrecordProcessedTypes- 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 compoundtypefor 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
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(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 registrationcontainer- the container expression (may be null)iterator- a container iterator variable forcontainer, may be null but only ifcontaineris nullrecordProcessedTypes- whether processed types shall be recorded or not (false)
-
pushContextImpl
Just pushes the givencontextwithout 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 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
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 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(net.ssehub.easy.varModel.model.AbstractVariable var) Unregisters a mapping for the variablevarfrom 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 forvar.- Parameters:
var- the variable to look for- Returns:
trueif there is a mapping,falseelse
-
getMapping
public net.ssehub.easy.varModel.cst.ConstraintSyntaxTree getMapping(net.ssehub.easy.varModel.model.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
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(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 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(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 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(net.ssehub.easy.varModel.model.datatypes.IDatatype type, net.ssehub.easy.varModel.model.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 throughaddConstraint(ConstraintList, boolean, Constraint, boolean)will be related to the created type cache entry. UpontransferConstraints(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
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
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(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 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(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 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 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,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:
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 (seeContextStack.Context.recordProcessedTypesandpushContext(AbstractVariable, ConstraintSyntaxTree, DecisionVariableDeclaration, boolean)). Storestypein the current context for retrieval viagetCurrentType().- 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 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(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 betransferred.- 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 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(net.ssehub.easy.varModel.model.datatypes.IDatatype type) Returns whether the giventypeis excluded in the current context.- Parameters:
type- the type to check for- Returns:
trueif excluded,falseelse
-
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
-