暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

graphviz.py 5.3KB

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