Beispiel #1
0
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()

    resource = XMIResource(URI(str(f)))
    resource.load()
    root = resource.contents[0]
    assert root.name == 'test'
    assert root.getEClassifier('A') is not None
 def loadModelDDM(self, input_file):
     rset = ResourceSet()
     # register the metamodel (available in the generated files)
     rset.metamodel_registry[ddm.nsURI] = ddm
     rset.resource_factory['ddm'] = lambda uri: XMIResource(uri)
     resource = rset.get_resource(URI(input_file))
     model = resource.contents[0]
     return model
Beispiel #3
0
def test_resource_createset(tmpdir, lib):
    f = tmpdir.mkdir('pyecore-tmp').join('test.xmi')
    resource = XMIResource(URI(str(f)))

    # we create some instances
    root = lib.MyRoot()
    a1 = lib.A()
    suba1 = lib.SubA()
    root.a_container.extend([a1, suba1])

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

    # we read the model
    resource = XMIResource(URI(str(f)))
    resource.load()
    assert resource.contents != []
    assert len(resource.contents[0].eContents) == 2
def saveModel(model):
    # create a resourceSet that hold the contents of the gdm.ecore model and the instances we use/create
    rset = ResourceSet()
    # register the metamodel (available in the generated files)
    rset.metamodel_registry[gdm.nsURI] = gdm
    rset.resource_factory['gdm'] = lambda uri: XMIResource(uri)

    resource = rset.create_resource(URI('Static_model_test.xmi'))
    resource.append(model)
    resource.save()
    return
Beispiel #5
0
def test_xmiresource_load_ecore_testEMF():
    xmi_file = path.join('tests', 'xmi', 'xmi-tests', 'testEMF.xmi')
    resource = XMIResource(URI(xmi_file))
    resource.load()
    assert resource.contents != []
    root = resource.contents[0]
    A = root.getEClassifier('A')
    assert A
    B = root.getEClassifier('B')
    assert B
    TInterface = root.getEClassifier('TInterface')
    assert TInterface
    TClass = root.getEClassifier('TClass')
    assert TClass
    a = A()
    assert Ecore.EcoreUtils.isinstance(a, TClass)
    assert Ecore.EcoreUtils.isinstance(a, TInterface)
    assert A.findEStructuralFeature('abstract')
    assert A.findEStructuralFeature('isAbs')
    assert a.isAbs is False
    assert a.abstract is False
    assert a.eResource is None
    assert A.eResource is resource
def test_xmiresource_load_ecore_testEMF():
    global_registry[Ecore.nsURI] = Ecore
    resource = XMIResource(URI('tests/xmi/xmi-tests/testEMF.xmi'))
    resource.load()
    assert resource.contents != []
    root = resource.contents[0]
    A = root.getEClassifier('A')
    assert A
    B = root.getEClassifier('B')
    assert B
    TInterface = root.getEClassifier('TInterface')
    assert TInterface
    TClass = root.getEClassifier('TClass')
    assert TClass
    a = A()
    assert Ecore.EcoreUtils.isinstance(a, TClass)
    assert Ecore.EcoreUtils.isinstance(a, TInterface)
    assert A.findEStructuralFeature('abstract')
    assert A.findEStructuralFeature('isAbs')
    assert a.isAbs is False
    assert a.abstract is False
    assert a.eResource is None
    assert A.eResource is resource
Beispiel #7
0
def test_resource_swap(simplemm):
    root = simplemm.Root()
    a = simplemm.A()
    b = simplemm.B()
    root.a.append(a)
    root.b.append(b)

    r1 = XMIResource(URI('resource1.xmi'))
    r2 = XMIResource(URI('resource2.xmi'))
    r1.append(root)

    assert root.eResource is r1
    assert a.eResource is root.eResource
    assert b.eResource is root.eResource

    r2.append(root)
    assert root.eResource is r2
    assert a.eResource is root.eResource
    assert b.eResource is root.eResource
Beispiel #8
0
def save(pkg, targetfolder, name=PKGNAME):
    """
    Serialize an EPackage.

    For now, produce both XMI and EMFJSON formats on each call.
    """
    xmipath = str((targetfolder / (name + '.xmi')).resolve())
    jsonpath = str((targetfolder / (name + '.json')).resolve())
    xmi = XMIResource(URI(xmipath))
    json = JsonResource(URI(jsonpath), indent=4)
    xmi.append(pkg)
    json.append(pkg)
    xmi.save()
    json.save()
Beispiel #9
0
def test_resource_multiroot_container_changement():
    resource = XMIResource('testResource')
    A = EClass('A')
    A.eStructuralFeatures.append(EReference('toa', A, containment=True))

    a1 = A()
    a2 = A()

    resource = XMIResource('test')
    resource.append(a1)
    resource.append(a2)

    assert resource.contents == [a1, a2]

    a1.toa = a2
    assert resource.contents == [a1]
Beispiel #10
0
def test_xmi_ecore_save_option_xmitype(tmpdir):
    f = tmpdir.mkdir('pyecore-tmp').join('xmitype.xmi')
    resource = XMIResource(URI(str(f)))

    resource.append(eClass)
    resource.save(options={XMIOptions.OPTION_USE_XMI_TYPE: True})

    has_xmi_type = False
    with open(str(f)) as output:
        for line in output:
            if 'xmi:type="' in line:
                has_xmi_type = True
                break
    assert has_xmi_type

    resource.save()
    has_xmi_type = False
    with open(str(f)) as output:
        for line in output:
            if 'xmi:type="' in line:
                has_xmi_type = True
                break
    assert has_xmi_type is False
Beispiel #11
0
def test_resource_multiroot_urifragment():
    A = EClass('A')
    A.eStructuralFeatures.append(EReference('toa', A, containment=True))

    a1 = A()
    a2 = A()
    a3 = A()
    a2.toa = a3

    resource = XMIResource('test')
    resource.append(a1)
    resource.append(a2)

    assert a1.eURIFragment() == '/0'
    assert a2.eURIFragment() == '/1'
    assert a3.eURIFragment() == '/1/toa'

    resource.remove(a2)
    assert len(resource.contents) == 1
    assert a2._eresource is None
import langs.ddmLang as ddm
from pyecore.resources import ResourceSet, URI
from pyecore.resources.xmi import XMIResource

rset = ResourceSet()
# register the metamodel (available in the generated files)
rset.metamodel_registry[ddm.nsURI] = ddm
rset.resource_factory['ddm'] = lambda uri: XMIResource(uri)
resource = rset.get_resource(URI("documents.models/venuesDOC.model"))
model = resource.contents[0]

print("pause")
def export_to_amola(statechart: Statechart, xmi_path: str=None):
    """
    Experimental support to export a statechart to AMOLA using pyecore.

    :param statechart: statechart to export
    :param xmi_path: if provided, will save the XMI
    :return: an instance of the metamodel
    """
    metamodel, _ = load_metamodel()

    # Create statechart
    e_statechart = metamodel.Model()
    e_statechart.metadata = convert_to_json({
        'name': statechart.name,
        'description': statechart.description,
        'preamble': statechart.preamble,
    })

    # Create nodes/states
    e_states = {}
    for name in statechart.states:
        state = statechart.state_for(name)

        # Create node and set metadata
        e_state = metamodel.Node(name=name)
        e_state.metadata = convert_to_json({
            'preconditions': getattr(state, 'preconditions', None),
            'postconditions': getattr(state, 'postconditions', None),
            'invariants': getattr(state, 'invariants', None),
        })

        # Actions
        actions = []
        if getattr(state, 'on_entry', None):
            actions.append('entry / {}'.format(state.on_entry.replace('\n', '\\n')))
        if getattr(state, 'on_exit', None):
            actions.append('exit / {}'.format(state.on_exit.replace('\n', '\\n')))

        # Internal transitions are actions in AMOLA
        for transition in statechart.transitions_from(state.name):
            if transition.internal:
                actions.append(export_TE(transition))

        e_state.actions = '\n'.join(actions)

        # Define its type
        if isinstance(state, CompoundState):
            e_state.type = 'OR'
        elif isinstance(state, OrthogonalState):
            e_state.type = 'AND'
        elif isinstance(state, FinalState):
            e_state.type = 'END'
        elif isinstance(state, ShallowHistoryState):
            e_state.type = 'SHALLOW_HISTORY'
        elif isinstance(state, DeepHistoryState):
            e_state.type = 'HISTORY'
        else:
            e_state.type = 'BASIC'

        e_states[name] = e_state

    # Define children now that all states are created
    for name in statechart.states:
        e_states[name].Children.extend(
            [e_states[child] for child in statechart.children_for(name)]
        )

    # Create transitions
    e_transitions = []
    for transition in statechart.transitions:
        # Internal transitions should be considered as actions (and are defined elsewhere)
        if transition.internal:
            continue

        e_transition = metamodel.Transition(
            source=e_states[transition.source],
            target=e_states[transition.target],
        )

        e_transition.metadata = convert_to_json({
            'preconditions': getattr(transition, 'preconditions', []),
            'postconditions': getattr(transition, 'postconditions', []),
            'invariants': getattr(transition, 'invariants', []),
        })
        e_transition.TE = export_TE(transition)

        e_transitions.append(e_transition)

    # Create initial states for compound states
    # Create transition for history state memory
    for name in statechart.states:
        state = statechart.state_for(name)

        if isinstance(state, CompoundState) and state.initial:
            e_state = metamodel.Node(type='START')
            e_states[state.name].Children.add(e_state)
            e_transition = metamodel.Transition(
                source=e_state,
                target=e_states[state.initial],
            )
            e_states[len(e_states)] = e_state  # len(e_states) ensures a new key
            e_transitions.append(e_transition)
        elif isinstance(state, HistoryStateMixin) and state.memory:
            e_transition = metamodel.Transition(
                source=e_states[state.name],
                target=e_states[state.memory],
            )
            e_transitions.append(e_transition)

    e_statechart.nodes.add(e_states[statechart.root])
    e_statechart.transitions.extend(e_transitions)

    if xmi_path:
        resource = XMIResource(URI(xmi_path))
        resource.append(e_statechart)
        resource.save()

    return e_statechart
Beispiel #14
0
def test_resource_crossref_uuid(tmpdir, lib):
    f1 = tmpdir.mkdir('pyecore-tmp2').join('main_uuid1.xmi')
    f2 = tmpdir.join('pyecore-tmp2', 'href_uuid2.xmi')
    r1 = XMIResource(URI(str(f1)))
    r2 = XMIResource(URI(str(f2)))
    r2.use_uuid = True

    # we create some instances
    root = lib.MyRoot()
    a1 = lib.A()
    a2 = lib.SubA()
    a1.toa = a2
    root.a_container.append(a1)

    # we add the elements to the first resource
    r1.append(root)

    # we create and add element to the second resource
    root2 = lib.MyRoot()
    root2.a_container.append(a2)
    r2.append(root2)

    r1.save()
    r2.save()

    # we read the model
    rset = ResourceSet()
    resource = rset.get_resource(URI(str(f1)))
    resource.load()
    assert resource.contents != []
    assert len(resource.contents[0].eContents) == 1
    a_obj = resource.contents[0].eContents[0]
    a_obj.toa.force_resolve()
    assert isinstance(a_obj.toa, lib.SubA)
Beispiel #15
0
from transfo_example import ghmde2graph as transfo

# generated using
# https://github.com/kolovos/datasets/blob/master/github-mde/ghmde.ecore
# as input metamodel
import ghmde


# quick model def
a = ghmde.Repository(name='repo')
f = ghmde.File(path='test')
a.files.append(f)

# b = ghmde.Repository(name='repo2')

resource = XMIResource()
resource.append(a)
# resource.append(b)

# run transfo (multi-root)
transfo.run(ghmde_model=resource)

print(transfo.inputs.ghmde_model.contents[0])
print(transfo.outputs.graph_model.contents)
print(transfo.outputs.graph_model.contents[0].nodes)

print(transfo.trace)
for rule in transfo.trace.rules:
    print('*', rule.name)

# # run transfo (single direct root)