Class TypeContext
- java.lang.Object
-
- de.uni_hildesheim.sse.translation.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 iscontext.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
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Stack<ContainableModelElementList>directContextprivate java.util.Map<IContainableElementsSorter,ContainableElementsMapAndComparator>elementSortMapsprivate ContainableModelElementListimplicitDefinitionsprivate MessageReceivermessageReceiverprivate Projectprojectprivate java.util.Map<Compound,Self>selfPoolprivate ContainableElementsMapAndComparatorsortMapprivate java.util.Map<java.lang.String,java.util.List<Compound>>unresolvedCompoundRefinementsprivate VariablePoolvariablePool
-
Constructor Summary
Constructors Constructor Description TypeContext(TypeContext context)Creates a local type context.TypeContext(Project project, MessageReceiver messageReceiver)Creates a type context for a given project.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) CustomDatatypeaddImplicitDefinition(CustomDatatype type)Adds an implicit type definition.voidaddToContext(AttributeAssignment assignment)Adds an attribute assignment to the current context (layer).voidaddToContext(Compound comp)Adds a compound to the current context (layer).(package private) voidaddToContext(Compound compound, java.lang.String unresolvedParentCompound)Stores a refined compound where the parent/super compound could not be loaded at the current time.private voidaddToContext(Compound comp, java.util.Set<Compound> done)Adds a compound to the current context (layer).voidaddToContext(DecisionVariableDeclaration var)Adds a variable declaration to the current context (layer).voidaddToContext(OperationDefinition opDef)Adds an operation definition to the current context (layer).booleanaddToProject(org.eclipse.emf.ecore.EObject key, Comment comment, ContainableModelElement element)Adds a containable element to the root resolution scope as well as a mapping fromkeytocomment(optional) andelement.static booleanallResolved(IDatatype[] types)Returns whether all types intypesare resolved.voidcheckEnumOclCompliance(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.voidclear()Clears this type context (for possible reuse).(package private) voidclearUnresolvedCompounds(java.lang.String parentCompound)Clears the list of incomplete compounds, where the parent/super compound of a refinement was missing.voidcloseSorter(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.private static ConstantValuecreateConstantValue(org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature, IDatatype type, java.lang.Object... values)Creates a constant value.private ConstraintSyntaxTreecreateValueTree(java.lang.String sValue, IDatatype type, org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature)Creates a value tree for a giventype.private IModelElementfindActualParent(IModelElement elt)Returns the actual project or compound theelementis located in.Compound[]findCompounds(java.util.List<java.lang.String> names, boolean nullOnFail)Finds compounds according to the givennames.ContainableModelElementfindElementByName(java.lang.String name, java.lang.Class<? extends ModelElement> type)Searches for a specified element.IDatatypefindRefType(IDatatype contained)Finds a reference type (declaration or defines an implicit type) for a given contained type.IDatatypefindType(java.lang.String name, java.lang.Class<? extends IDatatype> type)Finds the specified data type definition.AbstractVariablefindVariable(java.lang.String name, java.lang.Class<? extends AbstractVariable> type)Searches for a specified variable.IModelElementfindVariableUse(java.lang.String name)Searches for the use of a specified variable (declaration or compound access).ContainableModelElementgetElement(int index)Returns a contained model element specified byindex.ContainableModelElementgetElement(java.lang.String name)Returns a contained model element specified by itsname.intgetElementCount()Returns the number of contained elements.ProjectImportgetImport(int index)Returns the project import specified byindex.intgetImportsCount()Returns the number of imports.java.lang.StringgetName()Returns the (unqualified) name of the scope.IModelElementgetParent()Returns the parent model element.ProjectgetProject()Returns the project providing the root resolution scope.private SelfgetSelf(Compound comp)Returns a self instance from the self pool.(package private) 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.booleanhasInterfaces()Returns whether this scope has interfaces.booleanisInterface()Returns whether this scope is an interfaces.VariableobtainVariable(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.voidpopLayer()Pop a resolution layer in case that intermediary variables shell be considered, e.g.voidpopLayer(int count)Popscountlayers.(package private) ConstraintSyntaxTreeprocessQValue(java.lang.String sValue, org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature)Processes a qualified name as value.voidpushLayer(IModelElement parent)Push a resolution layer in case that intermediary variables shell be considered, e.g.intpushParent(DecisionVariableDeclaration decVar)Pushes the enclosing parents ofdecVar.private intpushParentRec(IModelElement elt)Pushes the enclosing parents ofeltin a recursive manner.voidregisterSorter(IContainableElementsSorter sorter, org.eclipse.emf.ecore.EObject key, Comment comment, ContainableModelElement element)Registers an element to be sorted (later).private ConstraintSyntaxTreeresolveSelf(IModelElement parent, org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature)Resolves "self" and returns the corresponding syntax tree (one node).IDatatyperesolveType(de.uni_hildesheim.sse.ivml.Type type)Resolves a given type to a type representation in the IVML object model.ConstraintSyntaxTreeresolveValue(de.uni_hildesheim.sse.ivml.Value value, IModelElement parent, org.eclipse.emf.ecore.EObject object, org.eclipse.emf.ecore.EStructuralFeature feature)Resolves a value and returns the corresponding syntax tree (one node).private static voidsetIndexes(java.util.List<org.eclipse.emf.ecore.EObject> elements, ContainableElementsMapAndComparator map)Transfers the index positions ofelementstomap.voidsortProjectElements(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.
-
-
-
Field Detail
-
implicitDefinitions
private ContainableModelElementList implicitDefinitions
-
project
private Project project
-
messageReceiver
private MessageReceiver messageReceiver
-
variablePool
private VariablePool variablePool
-
directContext
private java.util.Stack<ContainableModelElementList> directContext
-
sortMap
private ContainableElementsMapAndComparator sortMap
-
elementSortMaps
private java.util.Map<IContainableElementsSorter,ContainableElementsMapAndComparator> elementSortMaps
-
unresolvedCompoundRefinements
private java.util.Map<java.lang.String,java.util.List<Compound>> unresolvedCompoundRefinements
-
-
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 contextmessageReceiver- 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 ofdecVar.- Parameters:
decVar- the variable to push- Returns:
- the number of layers pushed
-
pushParentRec
private int pushParentRec(IModelElement elt)
Pushes the enclosing parents ofeltin a recursive manner.- Parameters:
elt- the element to push the parents for- Returns:
- the number of layers pushed
-
popLayer
public void popLayer(int count)
Popscountlayers.- Parameters:
count- the amount of layers to pop- See Also:
pushParent(DecisionVariableDeclaration)
-
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 addeddone- 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 giventypecannot 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,AbstractVariableis 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 theelementis 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,IDatatypeis 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 givennames.- Parameters:
names- the names of the compoundsnullOnFail- 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 intypesare resolved.- Parameters:
types- the types to inspect- Returns:
truefor all resolved,falsefor at least one not resolved ornull==types
-
getElement
public ContainableModelElement getElement(int index)
Returns a contained model element specified byindex.- Specified by:
getElementin interfaceIResolutionScope- Parameters:
index- a 0-based index specifying the operation to be returned- Returns:
- the contained element
- Throws:
java.lang.IndexOutOfBoundsException- ifindex<0 || index>=getElementCount()
-
getElementCount
public int getElementCount()
Returns the number of contained elements.- Specified by:
getElementCountin interfaceIResolutionScope- Returns:
- the number of contained elements
-
addImplicitDefinition
CustomDatatype addImplicitDefinition(CustomDatatype type)
Adds an implicit type definition.- Parameters:
type- the implicitly defined type- Returns:
type- See Also:
getElement(int),getElementCount()
-
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 convertedparent- the model parentobject- the grammar object this method is called forfeature- 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 forfeature- the grammar feature this method is called fortype- the type to create the value forvalues- 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 parentobject- the grammar object this method is called forfeature- 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 stringobject- the grammar object this method is called forfeature- 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 nameobject- the grammar object this method is called forfeature- 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 giventype.- Parameters:
sValue- the string representation of the valuetype- the target typeobject- the grammar object this method is called forfeature- the grammar feature this method is called for- Returns:
- the value tree
- Throws:
TranslatorException- in case of any translation problem
-
getImportsCount
public int getImportsCount()
Description copied from interface:IResolutionScopeReturns the number of imports.- Specified by:
getImportsCountin interfaceIResolutionScope- Returns:
- the number of imports
-
getImport
public ProjectImport getImport(int index)
Description copied from interface:IResolutionScopeReturns the project import specified byindex.- Specified by:
getImportin interfaceIResolutionScope- Parameters:
index- a 0-based index specifying the import to be returned- Returns:
- the project import
-
getParent
public IModelElement getParent()
Description copied from interface:IResolutionScopeReturns the parent model element.- Specified by:
getParentin interfaceIResolutionScope- Returns:
- the parent model element (may be null)
-
getName
public java.lang.String getName()
Description copied from interface:IResolutionScopeReturns the (unqualified) name of the scope.- Specified by:
getNamein interfaceIResolutionScope- Returns:
- the unqualified name of the scope
-
hasInterfaces
public boolean hasInterfaces()
Description copied from interface:IResolutionScopeReturns whether this scope has interfaces.- Specified by:
hasInterfacesin interfaceIResolutionScope- Returns:
trueif it has interfaces,falseelse
-
isInterface
public boolean isInterface()
Description copied from interface:IResolutionScopeReturns whether this scope is an interfaces.- Specified by:
isInterfacein interfaceIResolutionScope- Returns:
trueif it is an interface,falseelse
-
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 fromkeytocomment(optional) andelement.- 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:
trueif the addition was successful,falseelse 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 ofelementstomap.- Parameters:
elements- the list representing the positionsmap- 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 laterkey- 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 sortedelements- 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:IResolutionScopeReturns a contained model element specified by itsname.- Specified by:
getElementin interfaceIResolutionScope- Parameters:
name- the qualified or unqualified name to search for- Returns:
- the related element or null if no such element is known
-
-