forked from pyecore/pyecore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transfo_example.py
78 lines (60 loc) · 2.17 KB
/
transfo_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import motra
# generated using
# https://github.com/kolovos/datasets/blob/master/github-mde/ghmde.ecore
# as input metamodel
import ghmde
from pyecore.ecore import *
# Define a graph like metamodel in a static way
eClass = EPackage('graph', nsURI='http://graph/1.0', nsPrefix='graph')
@EMetaclass
class Node(object):
name = EAttribute(eType=EString)
@EMetaclass
class Graph(object):
name = EAttribute(eType=EString)
nodes = EReference(eType=Node, upper=-1, containment=True)
# Transfo definition
ghmde2graph = motra.Transformation('ghmde2graph',
inputs=['ghmde_model'],
outputs=['graph_model'])
@ghmde2graph.main
def main(ghmde_model=None, graph_model=None):
print('Transforming repository to graph', graph_model)
for repository in motra.objects_of_kind(ghmde_model, ghmde.File):
file2node(repository)
for repository in motra.objects_of_kind(ghmde_model, ghmde.Repository):
repository2graph(repository, postfix='_graph')
def does_not_starts_with(self, postfix):
return not self.name.startswith(postfix)
@ghmde2graph.mapping(when=does_not_starts_with)
def repository2graph(self: ghmde.Repository, postfix: str) -> Graph:
result.name = self.name + postfix
for repo_file in self.files:
result.nodes.append(file2node(repo_file))
@ghmde2graph.mapping
def file2node(self: ghmde.File) -> Node:
result.name = self.path
# @transfo.main
# def main(inputs, outputs):
# print('in main')
# print(inputs.ghmde.contents)
# for o in motra.objects_of_kind(inputs.ghmde, ghmde.Repository):
# test_dispatch(o)
#
#
# @transfo.mapping(when=lambda self: self.name is not None)
# def test1(self: ghmde.Repository) -> ghmde.Repository:
# print('changing name', result is self, self.name)
# result.name = self.name
# self.name += '_toto'
#
#
# @transfo.mapping(output_model='test2',
# when=lambda self: self.name is None)
# def test2(self: ghmde.Repository) -> ghmde.Repository:
# result.name = 'from_empty_' + str(self)
#
#
# @transfo.disjunct(mappings=[test1, test2])
# def test_dispatch(self: ghmde.Repository) -> ghmde.Repository:
# pass