|
@@ -0,0 +1,126 @@
|
|
1
|
+# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+import datetime
|
|
4
|
+from EditorialModel.classtypes import EmClassType
|
|
5
|
+from EditorialModel.fieldgroups import EmFieldGroup
|
|
6
|
+from EditorialModel.types import EmType
|
|
7
|
+from Lodel.utils.mlstring import MlString
|
|
8
|
+
|
|
9
|
+class EmBackendGraphviz(object):
|
|
10
|
+
|
|
11
|
+ ## @brief Constructor
|
|
12
|
+ # @param dot_fname str : The filename where we want to save the dot repr of the EM
|
|
13
|
+ def __init__(self, dot_fname):
|
|
14
|
+ self.edges = ""
|
|
15
|
+ self.dot_fname = dot_fname
|
|
16
|
+ #with open(dot_file, 'w+') as dot_fp:
|
|
17
|
+
|
|
18
|
+ ## @brief Not implementend
|
|
19
|
+ # @warning Not implemented
|
|
20
|
+ def load(self):
|
|
21
|
+ raise NotImplementedError(self.__class__.__name__+' cannot load an EM')
|
|
22
|
+
|
|
23
|
+ ## @brief Save an EM in a dot file
|
|
24
|
+ # @param em model : The EM to save
|
|
25
|
+ # @warning hardcoded classtype
|
|
26
|
+ def save(self, em):
|
|
27
|
+ self.edges = ""
|
|
28
|
+ with open(self.dot_fname, 'w') as dotfp:
|
|
29
|
+ dotfp.write("digraph G {\n\trankdir = BT\n")
|
|
30
|
+
|
|
31
|
+ dotfp.write('subgraph cluster_classtype {\nstyle="invis"\n')
|
|
32
|
+ for ct in [ 'entity', 'entry', 'person' ]:
|
|
33
|
+ dotfp.write('\n\nct%s [ label="classtype %s" shape="tripleoctagon" ]\n'%(ct, ct))
|
|
34
|
+ dotfp.write("}\n")
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+ dotfp.write('subgraph cluster_class {\nstyle="invis"\n')
|
|
38
|
+ for c in em.classes():
|
|
39
|
+
|
|
40
|
+ dotfp.write(self._component_node(c, em))
|
|
41
|
+ cn = c.__class__.__name__
|
|
42
|
+ cid = self._component_id(c)
|
|
43
|
+ self.edges += cid+' -> ct%s [ style="dashed" ]\n'%c.classtype
|
|
44
|
+ dotfp.write("}\n")
|
|
45
|
+
|
|
46
|
+ #dotfp.write('subgraph cluster_fieldgroup {\nstyle="invis"\n')
|
|
47
|
+ for c in em.components(EmFieldGroup):
|
|
48
|
+ dotfp.write(self._component_node(c, em))
|
|
49
|
+ cn = c.__class__.__name__
|
|
50
|
+ cid = self._component_id(c)
|
|
51
|
+ self.edges += cid+' -> '+self._component_id(c.em_class)+' [ style="dashed" ]\n'
|
|
52
|
+ #dotfp.write("}\n")
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+ #dotfp.write('subgraph cluster_type {\nstyle="invis"\n')
|
|
56
|
+ for c in em.components(EmType):
|
|
57
|
+ dotfp.write(self._component_node(c, em))
|
|
58
|
+ cn = c.__class__.__name__
|
|
59
|
+ cid = self._component_id(c)
|
|
60
|
+ self.edges += cid+' -> '+self._component_id(c.em_class)+' [ style="dotted" ]\n'
|
|
61
|
+ for fg in c.fieldgroups():
|
|
62
|
+ self.edges += cid+' -> '+self._component_id(fg)+' [ style="dashed" ]\n'
|
|
63
|
+ for nat in c.superiors():
|
|
64
|
+ self.edges += cid+' -> '+self._component_id(c.superiors()[nat])+' [ label="%s" color="green" ]'%nat
|
|
65
|
+ #dotfp.write("}\n")
|
|
66
|
+
|
|
67
|
+ dotfp.write(self.edges)
|
|
68
|
+
|
|
69
|
+ dotfp.write("\n}")
|
|
70
|
+ pass
|
|
71
|
+
|
|
72
|
+ @staticmethod
|
|
73
|
+ def _component_id(c):
|
|
74
|
+ return 'emcomp%d'%c.uid
|
|
75
|
+
|
|
76
|
+ def _component_node(self, c, em):
|
|
77
|
+ #ret = 'emcomp%d '%c.uid
|
|
78
|
+ ret = "\t"+EmBackendGraphviz._component_id(c)
|
|
79
|
+ cn = c.__class__.__name__
|
|
80
|
+ rel_field = ""
|
|
81
|
+ if cn == 'EmClass':
|
|
82
|
+ ret += '[ label="%s", shape="%s" ]'%(c.name, 'doubleoctagon')
|
|
83
|
+ elif cn == 'EmType' or cn == 'EmFieldGroup':
|
|
84
|
+ ret += '[ label="%s %s '%(cn, c.name)
|
|
85
|
+
|
|
86
|
+ cntref = 0
|
|
87
|
+ first = True
|
|
88
|
+ for f in c.fields():
|
|
89
|
+ if ((cn == 'EmType' and f.optional) or (cn == 'EmFieldGroup' and not f.optional)) and f.rel_field_id is None:
|
|
90
|
+
|
|
91
|
+ if not (f.rel_to_type_id is None):
|
|
92
|
+ rel_node_id = '%s%s'%(EmBackendGraphviz._component_id(c), EmBackendGraphviz._component_id(em.component(f.rel_to_type_id)))
|
|
93
|
+
|
|
94
|
+ rel_node = '\t%s [ label="rel_to_type'%rel_node_id
|
|
95
|
+
|
|
96
|
+ if len(f.rel_to_type_fields()) > 0:
|
|
97
|
+ #rel_node += '| {'
|
|
98
|
+ first = True
|
|
99
|
+ for rf in f.rel_to_type_fields():
|
|
100
|
+ rel_node += ' | '
|
|
101
|
+ if first:
|
|
102
|
+ rel_node += '{ '
|
|
103
|
+ first = False
|
|
104
|
+ rel_node += rf.name
|
|
105
|
+ rel_node += '}" shape="record" style="dashed"]\n'
|
|
106
|
+
|
|
107
|
+ rel_field += rel_node
|
|
108
|
+
|
|
109
|
+ ref_node = EmBackendGraphviz._component_id(em.component(f.rel_to_type_id))
|
|
110
|
+ self.edges += '%s:f%d -> %s [ color="purple" ]\n'%(EmBackendGraphviz._component_id(c), cntref, rel_node_id)
|
|
111
|
+ self.edges += '%s -> %s [color="purple"]\n'%(rel_node_id, ref_node)
|
|
112
|
+
|
|
113
|
+ ret += '|'
|
|
114
|
+ if first:
|
|
115
|
+ ret += ' { '
|
|
116
|
+ first = False
|
|
117
|
+ if not (f.rel_to_type_id is None):
|
|
118
|
+ ret += '<f%d> '%cntref
|
|
119
|
+ cntref += 1
|
|
120
|
+ ret += f.name
|
|
121
|
+ ret += '}" shape="record" color="%s" ]'%('blue' if cn == 'EmType' else 'red')
|
|
122
|
+ else:
|
|
123
|
+ return ""
|
|
124
|
+ ret +="\n"+rel_field
|
|
125
|
+ return ret
|
|
126
|
+
|