def test_resource_createSaveModifyRead(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 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
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)
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 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()
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]
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
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
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
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
# 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) # transfo.run(ghmde=a)