org.eclipse.emf.ocl.parser
Class EcoreEnvironment

java.lang.Object
  extended byorg.eclipse.emf.ocl.parser.EcoreEnvironment
All Implemented Interfaces:
Environment, PersistentEnvironment

public class EcoreEnvironment
extends java.lang.Object
implements PersistentEnvironment

An Environment stores the variables created while evaluating an OCL expression, including self. It also stores the package, if the OCL package statement is used. Methods are provided for querying whether variable, property, operation, and association class reference names belong to the environment.

This environment provides persistence of dynamically-generated types via its TypeResolver's resource. The default resolver implementation uses the most basic resource implementation, which does not support persistence, so clients requiring persistence should initialize it with their own resource. Persistence of Variables is virtual: the variables are stored in a resource that reconstructs variables on-the-fly from data encoded in their URI fragments.


Field Summary
 
Fields inherited from interface org.eclipse.emf.ocl.parser.Environment
ANNOTATION_URI_BODY, ANNOTATION_URI_DEFINE, ANNOTATION_URI_DERIVE, ANNOTATION_URI_INIT
 
Constructor Summary
EcoreEnvironment(Environment parent)
           
EcoreEnvironment(org.eclipse.emf.ecore.EPackage pkg)
          Create an environment, set the default package from a package statement, and set the package registry to the global package registry.
EcoreEnvironment(org.eclipse.emf.ecore.EPackage pkg, org.eclipse.emf.ecore.EPackage.Registry reg)
          Create an environment, set the default package from a package statement, and set the package registry
 
Method Summary
 boolean addElement(java.lang.String name, Variable elem, boolean exp)
          Adds a variable declaration to the environment.
protected  void collectStates(org.eclipse.emf.ecore.EClassifier owner, java.util.List pathPrefix, java.util.List states)
          Implemented by subclasses to find all states in the specified owner type that match the given path name prefix and add them to the accumulator list.
protected  TypeResolver createTypeResolver()
          Creates a new type resolver for use with this environment.
 void deleteElement(java.lang.String name)
          Removes a name when it goes out of scope.
static org.eclipse.emf.ecore.EPackage findPackage(java.util.List packageNames)
          Looks in the EMF registry for a package with the specified qualified package name.
static org.eclipse.emf.ecore.EPackage findPackage(java.util.List packageNames, org.eclipse.emf.ecore.EPackage.Registry registry)
          Looks in the given registry for a package with the specified qualified package name.
 org.eclipse.emf.ecore.EClass getAssociationClass(org.eclipse.emf.ecore.EReference reference)
          Obtains the association class that the specified reference represents, as an EClass, if any.
 org.eclipse.emf.ecore.EClassifier getContextClassifier()
          Obtains the context classifier of this environment.
 org.eclipse.emf.ecore.EOperation getContextOperation()
          Obtains the context operation of this environment, if it is an operation context.
 org.eclipse.emf.ecore.EStructuralFeature getContextProperty()
          Obtains the context property of this environment, if it is a property context.
 java.lang.String getDescription(org.eclipse.emf.ecore.ENamedElement namedElement)
          Default implementation just gets the name of the named element's eClass or, if it is an ETypedElement, the name of its type.
 EnvironmentFactory getFactory()
          Obtains the factory that created me, or an appropriate default factory if I was not created using a factory.
 org.eclipse.emf.common.util.EList getMemberEnds(org.eclipse.emf.ecore.EClass associationClass)
          Obtains the ends of the specified association class as a list of EReferences.
static org.eclipse.emf.ecore.EClassifier getOCLCollectionType(org.eclipse.emf.ecore.EClassifier type, boolean isOrdered, boolean isUnique)
          Obtains the appropriate OCL collection type for an EClassifier, according to the collection's orderedness and uniqueness.
static org.eclipse.emf.ecore.EClassifier getOCLType(org.eclipse.emf.ecore.EClassifier type)
          Obtains the appropriate OCL type for an EClassifier, if it corresponds to an OCL primitive type.
static org.eclipse.emf.ecore.EClassifier getOCLType(org.eclipse.emf.ecore.ETypedElement typedElement)
          Obtains the appropriate OCL type for an Ecore typed element, according to its type, multiplicity, orderedness, and uniqueness.
 Environment getParent()
          get the parent environment
 org.eclipse.emf.common.util.EList getQualifiers(org.eclipse.emf.ecore.EStructuralFeature property)
          Gets the qualifiers of an association end.
 Variable getSelfVariable()
          Gets the self variable, looking it up in a parent environment if necessary.
 org.eclipse.emf.common.util.EList getSignals(org.eclipse.emf.ecore.EClassifier owner)
          By default, return an empty list because Ecore does not support the modeling of signals.
 java.lang.String getStateName(org.eclipse.emf.ecore.EObject state)
          Ecore does not model states, so this method returns null.
 org.eclipse.emf.common.util.EList getStates(org.eclipse.emf.ecore.EClassifier owner, java.util.List pathPrefix)
          Obtains the states matching the specified path prefix in the owner type by trying the EcoreEnvironment.collectStates(org.eclipse.emf.ecore.EClassifier, java.util.List, java.util.List) method on it and, recursively, its supertypes to find all matches.
 TypeResolver getTypeResolver()
          Obtains a type resolver to be used to create (and cache) concrete bindings for the generic OCL types, based on the user model types provided by this environment.
 java.util.Collection getVariables()
          Returns the Variables registered in me explicitly.
static java.lang.String initialLower(org.eclipse.emf.ecore.ENamedElement elem)
          Gets the name of a named element with its initial character in lower case.
 boolean isAssociationClass(org.eclipse.emf.ecore.EClass eclass)
          Determines whether the specified eclass is an association class.
 boolean isEmpty()
          Is the environment empty?
 boolean isQuery(org.eclipse.emf.ecore.EOperation operation)
          Determines whether the specified operation is a query operation.
 Variable lookup(java.lang.String name)
          returns the VariableDeclaration for a variable Looks in parent environment scope if not found in current scope.
 org.eclipse.emf.ecore.EClass lookupAssociationClassReference(org.eclipse.emf.ecore.EClassifier parentClassifier, java.lang.String name)
          Find reference in the specified class to the named association class.
 Variable lookupImplicitSourceForAssociationClass(java.lang.String name)
          Return the variable declaration in namedElements which references the named association class.
 Variable lookupImplicitSourceForOperation(java.lang.String name, org.eclipse.emf.common.util.EList params)
          Return the Variable declaration in namedElements which contains the name as an EOperation.
 Variable lookupImplicitSourceForProperty(java.lang.String name)
          Return the most appropriate matching variable to use as the implicit source of a call to the specified property.
 Variable lookupLocal(java.lang.String name)
          Returns the VariableDeclaration for a variable name.
 org.eclipse.emf.ecore.EOperation lookupOperation(org.eclipse.emf.ecore.EClassifier owner, java.lang.String name, org.eclipse.emf.common.util.EList args)
          Find the operation in the specified class.
 org.eclipse.emf.ecore.EPackage lookupPackage(java.util.List path)
          Finds the EPackage corresponding to the path: name::...
 org.eclipse.emf.ecore.EClassifier lookupPathName(java.util.List names)
          Finds the EClassifier corresponding to the path: name::...
 org.eclipse.emf.ecore.EStructuralFeature lookupProperty(org.eclipse.emf.ecore.EClassifier owner, java.lang.String name)
          Find a property defined or inherited by the specified classifier.
 org.eclipse.emf.ecore.EClass lookupSignal(org.eclipse.emf.ecore.EClassifier owner, java.lang.String name, org.eclipse.emf.common.util.EList args)
          Find a received signal in the specified classifier.
 void setContextOperation(org.eclipse.emf.ecore.EOperation operation)
           
 void setContextProperty(org.eclipse.emf.ecore.EStructuralFeature property)
           
protected  void setFactory(EnvironmentFactory factory)
          Sets the factory that created me.
 void setParent(Environment env)
          Set the parent environment
 void setSelfVariable(Variable var)
          Sets the "self" variable that is the implicit source of any attribute, association end, or operation call.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

EcoreEnvironment

public EcoreEnvironment(org.eclipse.emf.ecore.EPackage pkg)
Create an environment, set the default package from a package statement, and set the package registry to the global package registry.

Parameters:
pkg -

EcoreEnvironment

public EcoreEnvironment(org.eclipse.emf.ecore.EPackage pkg,
                        org.eclipse.emf.ecore.EPackage.Registry reg)
Create an environment, set the default package from a package statement, and set the package registry

Parameters:
pkg -
reg -

EcoreEnvironment

public EcoreEnvironment(Environment parent)
Method Detail

getOCLType

public static org.eclipse.emf.ecore.EClassifier getOCLType(org.eclipse.emf.ecore.ETypedElement typedElement)
Obtains the appropriate OCL type for an Ecore typed element, according to its type, multiplicity, orderedness, and uniqueness. The mapping is as follows:

Parameters:
typedElement - the ECore typed element to get the OCL type for
Returns:
the corresponding OCL type
See Also:
EcoreEnvironment.getOCLType(EClassifier), EcoreEnvironment.getOCLCollectionType(EClassifier, boolean, boolean)

getOCLType

public static org.eclipse.emf.ecore.EClassifier getOCLType(org.eclipse.emf.ecore.EClassifier type)
Obtains the appropriate OCL type for an EClassifier, if it corresponds to an OCL primitive type.

Parameters:
type - the type to convert to an OCL type
Returns:
the corresponding OCL type

getOCLCollectionType

public static org.eclipse.emf.ecore.EClassifier getOCLCollectionType(org.eclipse.emf.ecore.EClassifier type,
                                                                     boolean isOrdered,
                                                                     boolean isUnique)
Obtains the appropriate OCL collection type for an EClassifier, according to the collection's orderedness and uniqueness. The mapping is as follows:

Note that the collection's element type is mapped to an OCL primitive type where possible.

Parameters:
type - the type to get the corresponding OCL collection type for
isOrdered - whether the OCL type should be ordered
isUnique - whether the OCL type should be unique
Returns:
the corresponding OCL type
See Also:
EcoreEnvironment.getOCLType(EClassifier)

getFactory

public EnvironmentFactory getFactory()
Description copied from interface: Environment
Obtains the factory that created me, or an appropriate default factory if I was not created using a factory. This factory can be used to create nested environments within me.

Specified by:
getFactory in interface Environment
Returns:
my originating factory
See Also:
EnvironmentFactory.createEnvironment(Environment)

setFactory

protected void setFactory(EnvironmentFactory factory)
Sets the factory that created me. This method should only be invoked by that factory.

Parameters:
factory - my originating factory

setParent

public void setParent(Environment env)
Set the parent environment

Specified by:
setParent in interface Environment
Parameters:
env -

getParent

public Environment getParent()
get the parent environment

Specified by:
getParent in interface Environment
Returns:
Environment

getContextClassifier

public org.eclipse.emf.ecore.EClassifier getContextClassifier()
Description copied from interface: Environment
Obtains the context classifier of this environment.

Specified by:
getContextClassifier in interface Environment
Returns:
the context classifier

getContextOperation

public org.eclipse.emf.ecore.EOperation getContextOperation()
Description copied from interface: Environment
Obtains the context operation of this environment, if it is an operation context.

Specified by:
getContextOperation in interface Environment
Returns:
the context operation, or null if this is not an operation environment

setContextOperation

public void setContextOperation(org.eclipse.emf.ecore.EOperation operation)

getContextProperty

public org.eclipse.emf.ecore.EStructuralFeature getContextProperty()
Description copied from interface: Environment
Obtains the context property of this environment, if it is a property context.

Specified by:
getContextProperty in interface Environment
Returns:
the context property, or null if this is not a property environment

setContextProperty

public void setContextProperty(org.eclipse.emf.ecore.EStructuralFeature property)

getTypeResolver

public TypeResolver getTypeResolver()
Description copied from interface: PersistentEnvironment
Obtains a type resolver to be used to create (and cache) concrete bindings for the generic OCL types, based on the user model types provided by this environment.

Specified by:
getTypeResolver in interface PersistentEnvironment
Returns:
the type resolver

createTypeResolver

protected TypeResolver createTypeResolver()
Creates a new type resolver for use with this environment.

Returns:
a new type resolver

isEmpty

public boolean isEmpty()
Is the environment empty?

Specified by:
isEmpty in interface Environment
Returns:
boolean

lookupLocal

public Variable lookupLocal(java.lang.String name)
Returns the VariableDeclaration for a variable name. Does not look in parent environment scopes.

Specified by:
lookupLocal in interface Environment
Parameters:
name -
Returns:
VariableDeclaration

lookup

public Variable lookup(java.lang.String name)
returns the VariableDeclaration for a variable Looks in parent environment scope if not found in current scope.

Specified by:
lookup in interface Environment
Parameters:
name -
Returns:
VariableDeclaration

lookupPackage

public org.eclipse.emf.ecore.EPackage lookupPackage(java.util.List path)
Description copied from interface: Environment
Finds the EPackage corresponding to the path: name::...::name If no EPackage is found, return null.

Specified by:
lookupPackage in interface Environment
Parameters:
path - the path name
Returns:
the package, or null if not found

lookupPathName

public org.eclipse.emf.ecore.EClassifier lookupPathName(java.util.List names)
Finds the EClassifier corresponding to the path: name::...::name If no EClassifier is found, return null.

Specified by:
lookupPathName in interface Environment
Parameters:
names -
Returns:
EClassifier

setSelfVariable

public void setSelfVariable(Variable var)
Description copied from interface: Environment
Sets the "self" variable that is the implicit source of any attribute, association end, or operation call.

Specified by:
setSelfVariable in interface Environment
Parameters:
var - the "self" variable

getSelfVariable

public Variable getSelfVariable()
Description copied from interface: Environment
Gets the self variable, looking it up in a parent environment if necessary.

Specified by:
getSelfVariable in interface Environment
Returns:
the self variable, or null if none (which should not happen as there is always some classifier context)

addElement

public boolean addElement(java.lang.String name,
                          Variable elem,
                          boolean exp)
Adds a variable declaration to the environment. If the name is null, then a new unique temporary name is generated.

Specified by:
addElement in interface Environment
Parameters:
name -
elem - -- variable declaration
exp - -- is the variable implicit?
Returns:
- boolean

deleteElement

public void deleteElement(java.lang.String name)
Removes a name when it goes out of scope.

Specified by:
deleteElement in interface Environment
Parameters:
name -

getVariables

public java.util.Collection getVariables()
Description copied from interface: Environment
Returns the Variables registered in me explicitly.

Specified by:
getVariables in interface Environment
Returns:
my variable declarations

lookupOperation

public org.eclipse.emf.ecore.EOperation lookupOperation(org.eclipse.emf.ecore.EClassifier owner,
                                                        java.lang.String name,
                                                        org.eclipse.emf.common.util.EList args)
Description copied from interface: Environment
Find the operation in the specified class.

Specified by:
lookupOperation in interface Environment
Parameters:
owner - the owner type of the called operation
name - the name of the called operation
args - the arguments (as OCLExpressions) passed to the operation
Returns:
the matching operation, or null if not found

lookupSignal

public org.eclipse.emf.ecore.EClass lookupSignal(org.eclipse.emf.ecore.EClassifier owner,
                                                 java.lang.String name,
                                                 org.eclipse.emf.common.util.EList args)
Description copied from interface: Environment
Find a received signal in the specified classifier.

Specified by:
lookupSignal in interface Environment
Parameters:
owner - the owner type of the signal reception
name - the name of the signal
args - the arguments (as OCLExpressions) passed in the signal
Returns:
the matching signal, or null if not found

getSignals

public org.eclipse.emf.common.util.EList getSignals(org.eclipse.emf.ecore.EClassifier owner)
By default, return an empty list because Ecore does not support the modeling of signals.

Specified by:
getSignals in interface Environment
Parameters:
owner - the owner type of the signal receptions
Returns:
the available signals (as EClasses), or an empty list if none

lookupProperty

public org.eclipse.emf.ecore.EStructuralFeature lookupProperty(org.eclipse.emf.ecore.EClassifier owner,
                                                               java.lang.String name)
Description copied from interface: Environment
Find a property defined or inherited by the specified classifier.

Specified by:
lookupProperty in interface Environment
Parameters:
name - the property name
Returns:
the property, or null if it could not be found

lookupAssociationClassReference

public org.eclipse.emf.ecore.EClass lookupAssociationClassReference(org.eclipse.emf.ecore.EClassifier parentClassifier,
                                                                    java.lang.String name)
Description copied from interface: Environment
Find reference in the specified class to the named association class.

Specified by:
lookupAssociationClassReference in interface Environment
Parameters:
parentClassifier - the referencing class to search
name - the association class name (with an initial lower case as per the OCL convention)
Returns:
the association class, or null if the specified parent classifier does not have any references implemented by the named association class

initialLower

public static java.lang.String initialLower(org.eclipse.emf.ecore.ENamedElement elem)
Gets the name of a named element with its initial character in lower case.

Parameters:
elem - a named element
Returns:
the element's name, with an initial lower case letter

getStates

public org.eclipse.emf.common.util.EList getStates(org.eclipse.emf.ecore.EClassifier owner,
                                                   java.util.List pathPrefix)
Obtains the states matching the specified path prefix in the owner type by trying the EcoreEnvironment.collectStates(org.eclipse.emf.ecore.EClassifier, java.util.List, java.util.List) method on it and, recursively, its supertypes to find all matches. For implicit (null) owners, looks up the innermost-scoped variable as the implicit source and tries again on this variable's type.

To extend this implementation, override the EcoreEnvironment.collectStates(org.eclipse.emf.ecore.EClassifier, java.util.List, java.util.List) method.

Specified by:
getStates in interface Environment
Parameters:
owner - the classifier for which states are being sought. Can be null in the case of an oclIsInState() call on an implicit target
pathPrefix - partial path name of the states being sought. This can be empty to find the first level of state names
Returns:
the list of all possible states directly contained in the namespace indicated by the path prefix (i.e., only one level of state nesting)

collectStates

protected void collectStates(org.eclipse.emf.ecore.EClassifier owner,
                             java.util.List pathPrefix,
                             java.util.List states)
Implemented by subclasses to find all states in the specified owner type that match the given path name prefix and add them to the accumulator list. The default implementation does nothing, as Ecore does not model states.

Implementors must only provide the states defined directly in the namespace indicated by the path prefix (i.e., only one level).

Parameters:
owner - the owner type
pathPrefix - partial
states - a list of states directly owned by the namespace indicated by path prefix, within the owner type

getStateName

public java.lang.String getStateName(org.eclipse.emf.ecore.EObject state)
Ecore does not model states, so this method returns null. Environments that support UML states must override this implementation.

Specified by:
getStateName in interface Environment
Parameters:
state - representation of a UML state
Returns:
its simple name

lookupImplicitSourceForProperty

public Variable lookupImplicitSourceForProperty(java.lang.String name)
Description copied from interface: Environment
Return the most appropriate matching variable to use as the implicit source of a call to the specified property. Variables are returned based on inner-most scope first.

Specified by:
lookupImplicitSourceForProperty in interface Environment
Parameters:
name - the property name
Returns:
the matching variable

lookupImplicitSourceForAssociationClass

public Variable lookupImplicitSourceForAssociationClass(java.lang.String name)
Description copied from interface: Environment
Return the variable declaration in namedElements which references the named association class. Variables are returned based on inner-most scope first.

Specified by:
lookupImplicitSourceForAssociationClass in interface Environment
Parameters:
name - the association class name to seek
Returns:
the matching variable declaration, or null if none is found

findPackage

public static org.eclipse.emf.ecore.EPackage findPackage(java.util.List packageNames)
Looks in the EMF registry for a package with the specified qualified package name. Uses the global package registry.

Parameters:
packageNames - the qualified package name
Returns:
the matching EPackage, or null if not found

findPackage

public static org.eclipse.emf.ecore.EPackage findPackage(java.util.List packageNames,
                                                         org.eclipse.emf.ecore.EPackage.Registry registry)
Looks in the given registry for a package with the specified qualified package name.

Parameters:
packageNames - the qualified package name
registry - the EPackage.Registry to look in
Returns:
the matching EPackage, or null if not found

lookupImplicitSourceForOperation

public Variable lookupImplicitSourceForOperation(java.lang.String name,
                                                 org.eclipse.emf.common.util.EList params)
                                          throws SemanticException
Description copied from interface: Environment
Return the Variable declaration in namedElements which contains the name as an EOperation. Variables are returned based on inner-most scope first.

Specified by:
lookupImplicitSourceForOperation in interface Environment
Parameters:
name -
Returns:
the matching variable declaration
Throws:
SemanticException

getDescription

public java.lang.String getDescription(org.eclipse.emf.ecore.ENamedElement namedElement)
Default implementation just gets the name of the named element's eClass or, if it is an ETypedElement, the name of its type.

Specified by:
getDescription in interface Environment
Parameters:
namedElement - a named element obtained by adaptation of a user model element
Returns:
the named element's description

isQuery

public boolean isQuery(org.eclipse.emf.ecore.EOperation operation)
Determines whether the specified operation is a query operation. Note that an environment that correctly supports query operations would need to supply appropriately annotated EOperations or otherwise override this method.

Specified by:
isQuery in interface Environment
Parameters:
operation - an operation
Returns:
whether it is a query operation. An EOperation that is not annotated is considered to be a query by default

isAssociationClass

public boolean isAssociationClass(org.eclipse.emf.ecore.EClass eclass)
Determines whether the specified eclass is an association class. Note that an environment that correctly supports association classes would need to supply appropriately annotated EClasses.

Specified by:
isAssociationClass in interface Environment
Parameters:
eclass - an Ecore class
Returns:
whether it is an association class

getAssociationClass

public org.eclipse.emf.ecore.EClass getAssociationClass(org.eclipse.emf.ecore.EReference reference)
Obtains the association class that the specified reference represents, as an EClass, if any. This gets the "class" aspect of the association class, for which the reference represents the "association" aspect. Note that an environment that correctly supports association classes would need to supply appropriately annotated EReferences.

Specified by:
getAssociationClass in interface Environment
Parameters:
reference - a reference
Returns:
the association class, or null if the reference does not represent an association class

getMemberEnds

public org.eclipse.emf.common.util.EList getMemberEnds(org.eclipse.emf.ecore.EClass associationClass)
Obtains the ends of the specified association class as a list of EReferences. Note that an environment that correctly supports association classes would need to supply appropriately annotated EClasses.

Specified by:
getMemberEnds in interface Environment
Parameters:
associationClass - an EClass representing an association class
Returns:
the EReferences representing its member ends (the association roles)

getQualifiers

public org.eclipse.emf.common.util.EList getQualifiers(org.eclipse.emf.ecore.EStructuralFeature property)
Gets the qualifiers of an association end. Note that an environment that correctly supports association end qualifiers would need to supply appropriately annotated EReferences.

Specified by:
getQualifiers in interface Environment
Parameters:
property - a property of the model class
Returns:
the qualifiers (as a list of EStructuralFeatures, or an empty list if the property has no qualifiers

Copyright 2002, 2006 IBM Corporation and others.
All Rights Reserved.