Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_xmi_ecore_save_heterogeneous_metamodel(tmpdir):
f = tmpdir.mkdir('pyecore-tmp').join('heterogeneous.xmi')
# Build a simple metamodel
Root = Ecore.EClass('Root')
Root.eStructuralFeatures.append(Ecore.EReference('element', Ecore.EObject))
pack = Ecore.EPackage('mypack', nsURI='http://mypack/1.0',
nsPrefix='mypack_pref')
pack.eClassifiers.append(Root)
rset = ResourceSet()
ecore_path = os.path.join('tests', 'xmi', 'xmi-tests', 'My.ecore')
resource = rset.get_resource(ecore_path)
root = resource.contents[0]
rset.metamodel_registry[root.nsURI] = root
# We open a first model with a special metamodel
xmi_path = os.path.join('tests', 'xmi', 'xmi-tests', 'MyRoot.xmi')
resource = rset.get_resource(xmi_path)
root1 = resource.contents[0]
MetaEClass, EInteger
name = 'library'
nsPrefix = 'lib'
nsURI = 'http://emf.wikipedia.org/2011/Library'
# Do not remove
eClass = Ecore.EPackage(name=name, nsURI=nsURI, nsPrefix=nsPrefix)
BookCategory = EEnum('BookCategory', literals=['ScienceFiction',
'Biography',
'Mistery'])
class Book(EObject, metaclass=MetaEClass):
title = EAttribute(eType=EString)
pages = EAttribute(eType=EInteger)
category = EAttribute(eType=BookCategory,
default_value=BookCategory.ScienceFiction)
def __init__(self):
super().__init__()
class Writer(EObject, metaclass=MetaEClass):
name = EAttribute(eType=EString)
books = EReference(eType=Book, lower=1, upper=-1)
Book.authors = EReference('authors', Writer, lower=1, upper=-1,
eOpposite=Writer.books)
def test_create_writer():
smith = library.Writer()
assert smith and isinstance(smith, Ecore.EObject)
assert smith.name is None
assert smith.books == []
def __init__(self):
super().__init__()
class Writer(EObject, metaclass=MetaEClass):
name = EAttribute(eType=EString)
books = EReference(eType=Book, lower=1, upper=-1)
Book.authors = EReference('authors', Writer, lower=1, upper=-1,
eOpposite=Writer.books)
Book.eClass.eStructuralFeatures.append(Book.authors)
class Employee(EObject, metaclass=MetaEClass):
name = EAttribute(eType=EString)
age = EAttribute(eType=EInteger)
class Library(EObject, metaclass=MetaEClass):
name = EAttribute(eType=EString)
address = EAttribute(eType=EString)
employees = EReference(eType=Employee, upper=-1, containment=True)
writers = EReference(eType=Writer, upper=-1, containment=True)
books = EReference(eType=Book, upper=-1, containment=True)
# ==
# Warning, do not remove
# ==
eURIFragment = Ecore.default_eURIFragment
def __init__(self, owner=None, feature=None, value=None, label=None):
if owner and not isinstance(owner, EObject):
raise BadValueError(got=owner, expected=EObject)
self.owner = owner
self.resource = owner.eResource
self.feature = feature
self.value = value
self.previous_value = None
self.label = label
self._is_prepared = False
self._is_executable = False
if not raw_types:
raw_types = object
return raw_types
def __instancecheck__(self, instance):
raw_types = self.raw_types()
return isinstance(instance, raw_types)
def __str__(self):
raw_types = self.raw_types()
return '<{}[{}] object at {}>'.format(self.__class__.__name__,
raw_types,
hex(id(self)))
class EGenericType(EObject):
def __init__(self, eTypeParameter=None, eClassifier=None, **kwargs):
super().__init__(**kwargs)
self.eTypeParameter = eTypeParameter
self.eClassifier = eClassifier
@property
def eRawType(self):
return self.eClassifier or self.eTypeParameter
# class SpecialEClassifier(Metasubinstance):
# def __instancecheck__(cls, instance):
# if cls is not EClassifier:
# return type.__instancecheck__(cls, instance)
# return isinstance(instance, Metasubinstance) or \
# isinstance(instance, (EClass, EDataType, EPackage))
result = kwargs.get(self_var_name, args[index])
elif f.result_eclass is Ecore.EClass:
result = f.result_eclass('')
else:
result = f.result_eclass()
inputs = [a for a in args if isinstance(a, Ecore.EObject)]
print('CREATE', result, 'FROM', inputs, 'BY', f.__name__)
# Create object for the trace
rule = self.trace[f.__name__]
g = f.__globals__
marker = object()
oldvalue = g.get(result_var_name, marker)
g[result_var_name] = result
observer = ResultObserver(notifier=result)
new_args = [EObjectProxy(obj)
if isinstance(obj, Ecore.EObject)
else obj
for obj in args]
for key, value in kwargs.items():
if isinstance(value, Ecore.EObject):
kwargs[key] = EObjectProxy(value)
try:
f(*new_args, **kwargs)
finally:
if oldvalue is marker:
del g[result_var_name]
else:
g[result_var_name] = oldvalue
result.listeners.remove(observer)
if f.output_def and \
result not in self.outputs[f.output_def].contents:
def to_dict(self, obj, is_ref=False):
if isinstance(obj, type) and issubclass(obj, EObject):
if is_ref:
fun = self._to_ref_from_obj
return fun(obj.eClass, self.options, self.use_uuid, self)
# else:
# cls = obj.python_class
# mapper = next((self.mappers[k] for k in self.mappers
# if issubclass(cls, k)), self.default_mapper)
# fun = mapper.to_dict_from_obj
elif isinstance(obj, EObject):
if is_ref:
fun = self._to_ref_from_obj
else:
cls = obj.eClass.python_class
mapper = next((self.mappers[k] for k in self.mappers
if issubclass(cls, k)), self.default_mapper)
fun = mapper.to_dict_from_obj
return fun(obj, self.options, self.use_uuid, self)
elif isinstance(obj, ECollection):
fun = self._to_ref_from_obj if is_ref else self.to_dict
result = []
for x in obj:
write_object = fun(x)
if write_object is NO_OBJECT:
continue
def append(self, root):
if not isinstance(root, Ecore.EObject):
raise ValueError('The resource requires an EObject type, '
'but received {0} instead.'.format(type(root)))
self.contents.append(root)
root._eresource = self
def eRoot(self):
if not self.eContainer():
return self
if not isinstance(self.eContainer(), EObject):
return self.eContainer()
return self.eContainer().eRoot()
def __dir__(self):
eclass = self.eClass
relevant = [x.name for x in eclass.eAllStructuralFeatures()]
relevant.extend([x.name for x in eclass.eAllOperations()
if not x.name.startswith('_')])
return relevant
class EModelElement(EObject):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def eURIFragment(self):
if not self.eContainer():
if not self.eResource or len(self.eResource.contents) == 1:
return '#/'
else:
return '#/{}'.format(self.eResource.contents.index(self))
parent = self.eContainer()
if hasattr(self, 'name'):
return '{0}/{1}'.format(parent.eURIFragment(), self.name)
else:
return super().eURIFragment()
def getEAnnotation(self, source):