예제 #1
0
def test_resourceset_getresource_ecore_UML():
    global_registry[Ecore.nsURI] = Ecore
    rset = ResourceSet()
    # UMLPrimitiveTypes Metaclasses 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.metamodel_registry[
        'platform:/plugin/org.eclipse.uml2.types/model/Types.ecore'] = umltypes
    # Register Ecore metamodel instance
    resource = rset.get_resource(URI('tests/xmi/xmi-tests/Ecore.ecore'))
    rset.metamodel_registry[
        'platform:/plugin/org.eclipse.emf.ecore/model/Ecore.ecore'] = resource.contents[
            0]
    # Load the UML metamodel
    resource = rset.get_resource(URI('tests/xmi/xmi-tests/UML.ecore'))
    root = resource.contents[0]
    assert root.getEClassifier('Class')
    assert root.getEClassifier('Interface')
    assert root.getEClassifier('State')
    assert root.eResource is resource
예제 #2
0
def test_xmi_save_none_value(tmpdir):
    f = tmpdir.mkdir('pyecore-tmp').join('default_none_value.xmi')

    # Build a simple metamodel
    Root = Ecore.EClass('Root')
    Root.eStructuralFeatures.append(Ecore.EReference('element', Ecore.EObject))
    Root.eStructuralFeatures.append(Ecore.EAttribute('name', Ecore.EString))
    Root.eStructuralFeatures.append(Ecore.EAttribute('ints', Ecore.EInt,
                                                     upper=-1))
    pack = Ecore.EPackage('mypack', nsURI='http://mypack/1.0',
                          nsPrefix='mypack_pref')
    pack.eClassifiers.append(Root)

    r = Root()
    r.element = None
    r.name = None
    r.ints.extend([3, 4, 5])

    rset = ResourceSet()
    resource = rset.create_resource(URI(str(f)))
    resource.append(r)
    resource.save(options={XMIOptions.SERIALIZE_DEFAULT_VALUES: True})

    with open(str(f), 'r') as f:
        tree = etree.parse(f)
        xmlroot = tree.getroot()
        assert xmlroot[0].tag in ('name', 'element')
        assert xmlroot[1].tag in ('name', 'element')
        assert xmlroot[0].tag != xmlroot[1].tag
예제 #3
0
def test_xmi_save_load_EDate(tmpdir):
    from datetime import datetime
    f = tmpdir.mkdir('pyecore-tmp').join('default_date.xmi')

    # Build a simple metamodel
    Root = Ecore.EClass('Root')
    Root.eStructuralFeatures.append(Ecore.EAttribute('date', Ecore.EDate))
    pack = Ecore.EPackage('mypack',
                          nsURI='http://mypack/1.0',
                          nsPrefix='mypack_pref')
    pack.eClassifiers.append(Root)

    date = datetime.utcnow()
    r = Root()
    r.date = date

    rset = ResourceSet()
    resource = rset.create_resource(URI(str(f)))
    resource.append(r)
    resource.save()

    rset = ResourceSet()
    rset.metamodel_registry[pack.nsURI] = pack
    resource = rset.get_resource(URI(str(f)))
    assert resource.contents[0].date == date
예제 #4
0
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()

    resource = rset.get_resource('tests/xmi/xmi-tests/My.ecore')
    root = resource.contents[0]
    rset.metamodel_registry[root.nsURI] = root

    # We open a first model with a special metamodel
    resource = rset.get_resource('tests/xmi/xmi-tests/MyRoot.xmi')
    root1 = resource.contents[0]

    r = Root(element=root1)
    resource = rset.create_resource(URI(str(f)))
    resource.append(r)
    resource.save()

    with open(str(f), 'r') as f:
        tree = etree.parse(f)
        xmlroot = tree.getroot()
        assert 'mypack_pref' in xmlroot.nsmap
        assert 'myprefix' in xmlroot.nsmap
예제 #5
0
def test__jsonresource_load_multiple_root(rset):
    A = Ecore.EClass('A')
    pack = Ecore.EPackage('pack', 'packuri', 'pack')
    pack.eClassifiers.append(A)

    rset.metamodel_registry[pack.nsURI] = pack
    json_file = path.join('tests', 'json', 'data', 'multiple_root.json')
    resource = rset.get_resource(json_file)

    assert len(resource.contents) == 2
    assert resource.contents[0] != resource.contents[1]
    A = Ecore.EClass('A')
    pack = Ecore.EPackage('pack', 'packuri', 'pack')
    pack.eClassifiers.append(A)

    rset.metamodel_registry[pack.nsURI] = pack
    json_file = path.join('tests', 'json', 'data', 'multiple_root.json')
    resource = rset.get_resource(json_file)

    assert len(resource.contents) == 2
    assert resource.contents[0] != resource.contents[1]
예제 #6
0
def test_xmi_multiroot_save(tmpdir):
    # Define a simple model that will be split in many roots
    A = Ecore.EClass('A')
    A.eStructuralFeatures.append(Ecore.EAttribute('name', Ecore.EString))
    pack1 = Ecore.EPackage('pack1', nsURI='http://pack1/1.0', nsPrefix='pack1')
    pack1.eClassifiers.append(A)

    B = Ecore.EClass('B')
    B.eStructuralFeatures.append(Ecore.EAttribute('age', Ecore.EInt))
    B.eStructuralFeatures.append(Ecore.EReference('to_a', A))
    pack2 = Ecore.EPackage('pack2', nsURI='http://pack2/1.0', nsPrefix='pack2')
    pack2.eClassifiers.append(B)

    f = tmpdir.mkdir('pyecore-tmp').join('multi_root.xmi')
    rset = ResourceSet()
    resource = rset.create_resource(URI(str(f)))
    resource.append(pack1)
    resource.append(pack2)
    resource.save()

    with open(str(f), 'r') as f:
        tree = etree.parse(f)
        xmlroot = tree.getroot()
        assert xmlroot.tag == '{{{0}}}XMI'.format(XMI_URL)
예제 #7
0
def test_json_save_multiple_roots(tmpdir):
    A = Ecore.EClass('A')
    A.eStructuralFeatures.append(Ecore.EAttribute('name', Ecore.EString))
    pack = Ecore.EPackage('pack', 'packuri', 'pack')
    pack.eClassifiers.append(A)

    f = tmpdir.mkdir('pyecore-tmp').join('multiple.json')
    resource = JsonResource(URI(str(f)))
    resource.append(A(name='root1'))
    resource.append(A(name='root2'))
    resource.save()

    dct = json.load(open(str(f)))
    assert type(dct) is list
    assert dct[0]['name'] == 'root1'
    assert dct[1]['name'] == 'root2'
예제 #8
0
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)
    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
예제 #9
0
def test_deserialize_href_uuid_ref():
    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()

    resource = rset.get_resource('tests/xmi/xmi-tests/My.ecore')
    root = resource.contents[0]
    rset.metamodel_registry[root.nsURI] = root
    rset.metamodel_registry[pack.nsURI] = pack

    resource = rset.get_resource('tests/xmi/xmi-tests/encoded.xmi')
    root1 = resource.contents[0]
    resource = rset.get_resource('tests/xmi/xmi-tests/encoded2.xmi')
    root2 = resource.contents[0]
    assert root2.element.eClass is root1.eClass
예제 #10
0
def test_json_save_multiple_roots_roundtrip(tmpdir):
    A = Ecore.EClass('A')
    A.eStructuralFeatures.append(Ecore.EAttribute('name', Ecore.EString))
    pack = Ecore.EPackage('pack', 'packuri', 'pack')
    pack.eClassifiers.append(A)

    f = tmpdir.mkdir('pyecore-tmp').join('multiple.json')
    resource = JsonResource(URI(str(f)))
    resource.append(A(name='root1'))
    resource.append(A(name='root2'))
    resource.save()

    global_registry[pack.nsURI] = pack
    resource = JsonResource(URI(str(f)))
    resource.load()
    assert len(resource.contents) == 2
    assert resource.contents[0].name == 'root1'
    assert resource.contents[1].name == 'root2'
    del global_registry[pack.nsURI]
예제 #11
0
def test_load_multipleroot_with_refs():
    rset = ResourceSet()
    multi_root = path.join('tests', 'xmi', 'xmi-tests',
                           'multiple_with_refs.xmi')

    package = Ecore.EPackage('amm', 'ammuri', 'amm')
    A = Ecore.EClass('A')
    A.eStructuralFeatures.append(Ecore.EAttribute('name', Ecore.EString))
    A.eStructuralFeatures.append(Ecore.EReference('toa', A))
    A.eStructuralFeatures.append(Ecore.EReference('contains', A,
                                                  containment=True))
    package.eClassifiers.append(A)

    rset.metamodel_registry[package.nsURI] = package
    resource = rset.get_resource(URI(str(multi_root)))

    root1, root2 = resource.contents
    assert root1.contains.toa is root2
    assert root1.name == 'root1'
    assert root2.name == 'root2'
    assert root1.contains.name == 'inner'
예제 #12
0
def test_xmi_instanceclass_(tmpdir):
    testDT = Ecore.EDataType('TestDT', instanceClassName='int')
    Ecore.eClass.eClassifiers.append(Ecore.EDataType.eClass)  # Fix unknown why

    my_ecore_schema = Ecore.EPackage('my_ecore', nsURI='http://myecore/1.0', nsPrefix='myecore')
    my_ecore_schema.eClassifiers.append(testDT)

    rset = ResourceSet()
    rset.metamodel_registry[Ecore.nsURI] = Ecore
    ecore_path = tmpdir.mkdir('pyecore-tmp').join('instanceClassName.xmi')
    resource = rset.create_resource(URI(str(ecore_path)))
    resource.append(my_ecore_schema)
    resource.save()

    rset = ResourceSet()
    rset.metamodel_registry[Ecore.nsURI] = Ecore
    resource = rset.get_resource(URI(str(ecore_path)))
    root = resource.contents[0]

    assert root.eClassifiers is not None
    assert root.eClassifiers[0].instanceClassName == 'int'
    assert root.eClassifiers[0].eType is int
예제 #13
0
def test__pythonic_names():
    p = ecore.EPackage('MyPackage')

    c1 = ecore.EClass('MyClass')
    p.eClassifiers.append(c1)
    a1 = ecore.EAttribute('att', ecore.EString, upper=-1)
    c1.eStructuralFeatures.append(a1)

    c2 = ecore.EClass('pass')
    p.eClassifiers.append(c2)
    a2 = ecore.EAttribute('else', ecore.EString, upper=-1)
    c2.eStructuralFeatures.append(a2)

    assert c2.name == 'pass'
    assert a2.name == 'else'

    with pythonic_names():
        assert c1.name == 'MyClass'
        assert a1.name == 'att'
        assert c2.name == 'pass_'
        assert a2.name == 'else_'

    assert c2.name == 'pass'
    assert a2.name == 'else'
예제 #14
0
@when(lambda self, parent: self.name)
@mapping
def createEClass(self: Ecore.EClass, parent: Ecore.EPackage) -> Ecore.EClass:
    result.name = self.name + 'Copy'
    result.abstract = self.abstract
    for attribute in self.eAttributes:
        result.eStructuralFeatures.append(copyEAttribute(attribute))
    parent.eClassifiers.append(self)


@mapping
def copyEAttribute(self: Ecore.EAttribute) -> Ecore.EAttribute:
    result.name = self.name + 'Copy'
    result.lowerBound = self.lowerBound
    result.upperBound = self.upperBound
    result.eType = self.eType


root = Ecore.EPackage('test')
A1 = Ecore.EClass('A1')
root.eClassifiers.append(A1)
A1.eStructuralFeatures.append(Ecore.EAttribute('name', eType=Ecore.EString))

inPackage = root
result = createEPackage(inPackage)

rset = ResourceSet()
outresource = rset.create_resource(URI('testt.xmi'))
outresource.append(result)
outresource.save()
예제 #15
0
    # we add more instances
    a2 = lib.A()
    root.a_container.append(a2)

    # we save again
    resource.save()

    # we read the model
    resource = JsonResource(URI(str(f)))
    resource.load()
    assert resource.contents != []
    assert len(resource.contents[0].eContents) == 3


# Defines a small metamodel
eClass = Ecore.EPackage('pack', nsURI='http://test_pack/1.0', nsPrefix='pack')
nsURI = 'http://tst/1.0'


@Ecore.EMetaclass
class A(object):
    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
예제 #16
0
"""
Library example from the EMF wikipedia page:
https://fr.wikipedia.org/wiki/Eclipse_Modeling_Framework#/media/File:EMF_based_meta-model.png
The static metamodel had been produced by hand in this example
"""
import sys
import pyecore.ecore as Ecore
from pyecore.ecore import EObject, EAttribute, EString, EEnum, EReference, \
                          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__()

예제 #17
0
    # we add more instances
    a2 = lib.A()
    root.a_container.append(a2)

    # we save again
    resource.save()

    # we read the model
    resource = XMIResource(URI(str(f)))
    resource.load()
    assert resource.contents != []
    assert len(resource.contents[0].eContents) == 3


eClass = Ecore.EPackage(name='test', nsURI='http://test/1.0', nsPrefix='test')


@Ecore.EMetaclass
class A(object):
    name = Ecore.EAttribute('name', Ecore.EString)
    age = Ecore.EAttribute('age', Ecore.EInt)
    names = Ecore.EAttribute(eType=Ecore.EString, upper=-1)


def test_xmi_ecore_save_load(tmpdir):
    f = tmpdir.mkdir('pyecore-tmp').join('test.xmi')
    resource = XMIResource(URI(str(f)))

    resource.append(eClass)
    resource.save()