-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity_test.py
63 lines (47 loc) · 1.84 KB
/
entity_test.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
from __future__ import unicode_literals
import os
from os.path import dirname, join
from textx.metamodel import metamodel_from_file
from textx.export import metamodel_export, model_export
this_folder = dirname(__file__)
class SimpleType(object):
"""
We are registering user SimpleType class to support
simple types (integer, string) in our entity models
Thus, user doesn't need to provide integer and string
types in the model but can reference them in attribute types nevertheless.
"""
def __init__(self, parent, name):
self.parent = parent
self.name = name
def __str__(self):
return self.name
def get_entity_mm(debug=False):
"""
Builds and returns a meta-model for Entity language.
"""
# Built-in simple types
# Each model will have this simple types during reference resolving but
# these will not be a part of `types` list of EntityModel.
type_builtins = {
'integer': SimpleType(None, 'integer'),
'string': SimpleType(None, 'string')
}
entity_mm = metamodel_from_file(join(this_folder, 'entity.tx'),
classes=[SimpleType],
builtins=type_builtins,
debug=debug)
return entity_mm
def main(debug=False):
entity_mm = get_entity_mm(debug)
# Export to .dot file for visualization
dot_folder = join(this_folder, 'dotexport')
if not os.path.exists(dot_folder):
os.mkdir(dot_folder)
metamodel_export(entity_mm, join(dot_folder, 'entity_meta.dot'))
# Build Person model from person.ent file
person_model = entity_mm.model_from_file(join(this_folder, 'person.ent'))
# Export to .dot file for visualization
model_export(person_model, join(dot_folder, 'person.dot'))
if __name__ == "__main__":
main()