How to use pyecore - 10 common examples

To help you get started, we’ve selected a few pyecore examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pyecore / pyecoregen / tests / test_templates.py View on Github external
def test_user_module_derived_from_mixin_(pygen_output_dir):
    rootpkg = EPackage('derived_from_mixin')
    c1 = EClass('MyClass')
    c1.eOperations.append(EOperation('do_it'))
    c1.eStructuralFeatures.append(EAttribute('any', EString, derived=True))
    rootpkg.eClassifiers.append(c1)
    c2 = EClass('MyOtherClass')
    c2.eStructuralFeatures.append(EAttribute('other', EString, derived=True))
    c2.eStructuralFeatures.append(EReference('toc', c1, derived=True))
    c2.eSuperTypes.append(c1)
    rootpkg.eClassifiers.append(c2)

    mm = generate_meta_model(rootpkg, pygen_output_dir, user_module='user_provided.module')

    c = mm.MyOtherClass(any='any', other='other')
    assert isinstance(c, MyClassMixin)
    assert isinstance(c, MyOtherClassMixin)
    assert isinstance(c, mm.MyClass)
    assert c.any == 'any'
    c.mock_other.assert_called_once_with('other')

    assert not c.do_it.called
    c.do_it()
    assert c.do_it.called
github pyecore / pyecoregen / tests / test_templates.py View on Github external
def test_user_module_derived_from_mixin_(pygen_output_dir):
    rootpkg = EPackage('derived_from_mixin')
    c1 = EClass('MyClass')
    c1.eOperations.append(EOperation('do_it'))
    c1.eStructuralFeatures.append(EAttribute('any', EString, derived=True))
    rootpkg.eClassifiers.append(c1)
    c2 = EClass('MyOtherClass')
    c2.eStructuralFeatures.append(EAttribute('other', EString, derived=True))
    c2.eStructuralFeatures.append(EReference('toc', c1, derived=True))
    c2.eSuperTypes.append(c1)
    rootpkg.eClassifiers.append(c2)

    mm = generate_meta_model(rootpkg, pygen_output_dir, user_module='user_provided.module')

    c = mm.MyOtherClass(any='any', other='other')
    assert isinstance(c, MyClassMixin)
    assert isinstance(c, MyOtherClassMixin)
    assert isinstance(c, mm.MyClass)
    assert c.any == 'any'
    c.mock_other.assert_called_once_with('other')

    assert not c.do_it.called
    c.do_it()
github pyecore / pyecore / tests / wikilibrary / wikilib.py View on Github external
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
eModule = sys.modules[__name__]
otherClassifiers = [BookCategory]
github pyecore / pyecore / tests / json / test_json_save.py View on Github external
child = Ecore.EReference(containment=True, upper=-1)
    imply = Ecore.EReference()
    ref_by = Ecore.EReference()
    distant = Ecore.EReference(eType=Ecore.EObject)


A.child.eType = A
A.imply.eType = A
A.ref_by.eType = A


@Ecore.EMetaclass
class Point(object):
    x = Ecore.EAttribute(eType=Ecore.EDouble)
    y = Ecore.EAttribute(eType=Ecore.EDouble)
    z = Ecore.EAttribute(eType=Ecore.EDouble)


def test_json_resource_save_static_metamodel(tmpdir):
    f = tmpdir.mkdir('pyecore-tmp').join('test.json')
    resource = JsonResource(URI(str(f)))

    # we add the elements to the resource
    resource.append(eClass)
    resource.save()

    # we read the model
    resource = JsonResource(URI(str(f)))
    resource.load()
    assert resource.contents != []
    assert len(resource.contents[0].eContents) == 2
github pyecore / pyecoregen / tests / test_generated_library.py View on Github external
def test_meta_attribute_access(generated_library):
    assert isinstance(generated_library.Book.category, Ecore.EAttribute)
    attribute = generated_library.Book.category
    assert attribute.name == 'category'
    assert attribute.upperBound == 1
    assert attribute.lowerBound == 0
    assert isinstance(attribute.eType, Ecore.EDataType)
    assert attribute.eType is generated_library.BookCategory
github pyecore / pyecore / tests / xmi / xmi_experiment.py View on Github external
if feat.many:
        index = parent.__getattribute__(name).index(obj)
        return '{0}/@{1}.{2}'.format(_build_path(parent), name, str(index))
    else:
        return '{0}/{1}'.format(_build_path(parent), name)

if __name__ == '__main__':
    rset = ResourceSet()

    # UMLPrimitiveTypes Creation
    umltypes = Ecore.EPackage('umltypes')
    String = Ecore.EDataType('String', str)
    Boolean = Ecore.EDataType('Boolean', bool, False)
    Integer = Ecore.EDataType('Integer', int, 0)
    UnlimitedNatural = Ecore.EDataType('UnlimitedNatural', int, 0)
    Real = Ecore.EDataType('Real', float, 0.0)
    umltypes.eClassifiers.extend([String, Boolean, Integer, UnlimitedNatural, Real])
    rset.resources['platform:/plugin/org.eclipse.uml2.types/model/Types.ecore'] = umltypes

    # Register Ecore metamodel instance
    resource = rset.get_resource(URI('xmi-tests/Ecore.ecore'))
    rset.resources['platform:/plugin/org.eclipse.emf.ecore/model/Ecore.ecore'] = resource.contents[0]

    resource = rset.get_resource(URI('xmi-tests/UML.ecore'))
github pyecore / pyecore / tests / xmi / test_xmi_serialization.py View on Github external
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]
github pyecore / pyecore / tests / xmi / test_xmi_serialization.py View on Github external
def lib():
    package = Ecore.EPackage('mypackage')
    package.nsURI = 'http://simplemetamodel/1.0'
    package.nsPrefix = 'simplemm'
    AbsA = Ecore.EClass('AbsA', abstract=True)
    AbsA.eStructuralFeatures.append(Ecore.EReference('toa', AbsA))
    A = Ecore.EClass('A', superclass=(AbsA,))
    SubA = Ecore.EClass('SubA', superclass=(A,))
    MyRoot = Ecore.EClass('MyRoot')
    MyRoot.a_container = Ecore.EReference('a_container', eType=AbsA, upper=-1,
                                          containment=True)
    MyRoot.eStructuralFeatures.append(MyRoot.a_container)
    package.eClassifiers.extend([MyRoot, A, SubA, AbsA])
    package.MyRoot = MyRoot
    package.SubA = SubA
    package.A = A
    # we register the metamodel first
    global_registry[package.nsURI] = package
    return package
github pyecore / pyecore / tests / json / test_json_save.py View on Github external
def lib():
    package = Ecore.EPackage('mypackage')
    package.nsURI = 'http://simplemetamodel/1.0'
    package.nsPrefix = 'simplemm'
    AbsA = Ecore.EClass('AbsA', abstract=True)
    A = Ecore.EClass('A', superclass=(AbsA,))
    SubA = Ecore.EClass('SubA', superclass=(A,))
    MyRoot = Ecore.EClass('MyRoot')
    MyRoot.a_container = Ecore.EReference('a_container', eType=AbsA, upper=-1,
                                          containment=True)
    MyRoot.eStructuralFeatures.append(MyRoot.a_container)
    MyRoot.eStructuralFeatures.append(Ecore.EAttribute('trans',
                                                       eType=Ecore.EString,
                                                       transient=True))
    package.eClassifiers.extend([MyRoot, A, SubA, AbsA])
    package.MyRoot = MyRoot
    package.SubA = SubA
    package.A = A
    # we register the metamodel first
    global_registry[package.nsURI] = package
    return package
github pyecore / pyecore / tests / json / test_json_save.py View on Github external
def lib():
    package = Ecore.EPackage('mypackage')
    package.nsURI = 'http://simplemetamodel/1.0'
    package.nsPrefix = 'simplemm'
    AbsA = Ecore.EClass('AbsA', abstract=True)
    A = Ecore.EClass('A', superclass=(AbsA,))
    SubA = Ecore.EClass('SubA', superclass=(A,))
    MyRoot = Ecore.EClass('MyRoot')
    MyRoot.a_container = Ecore.EReference('a_container', eType=AbsA, upper=-1,
                                          containment=True)
    MyRoot.eStructuralFeatures.append(MyRoot.a_container)
    MyRoot.eStructuralFeatures.append(Ecore.EAttribute('trans',
                                                       eType=Ecore.EString,
                                                       transient=True))
    package.eClassifiers.extend([MyRoot, A, SubA, AbsA])
    package.MyRoot = MyRoot
    package.SubA = SubA
    package.A = A
    # we register the metamodel first
    global_registry[package.nsURI] = package
    return package