No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

graphviz.py 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # -*- coding: utf-8 -*-
  2. import datetime
  3. import EditorialModel
  4. from EditorialModel.classtypes import EmClassType
  5. from EditorialModel.types import EmType
  6. from EditorialModel.fields import EmField
  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_type {\nstyle="invis"\n')
  39. for c in em.components(EmType):
  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="dotted" ]\n'
  44. for nat, sups in c.superiors().items():
  45. for sup in sups:
  46. self.edges += cid+' -> '+self._component_id(sup)+' [ label="%s" color="green" ]'%nat
  47. #dotfp.write("}\n")
  48. for rf in [ f for f in em.components(EmField) if f.fieldtype == 'rel2type']:
  49. dotfp.write(self._component_node(rf, em))
  50. cid = self._component_id(rf)
  51. self.edges += cid+' -> '+self._component_id(rf.em_class)+'\n'
  52. self.edges += cid+' -> '+self._component_id(em.component(rf.rel_to_type_id))+'\n'
  53. dotfp.write(self.edges)
  54. dotfp.write("\n}")
  55. pass
  56. @staticmethod
  57. def _component_id(c):
  58. return 'emcomp%d'%c.uid
  59. def _component_node(self, c, em):
  60. #ret = 'emcomp%d '%c.uid
  61. ret = "\t"+EmBackendGraphviz._component_id(c)
  62. cn = c.__class__.__name__
  63. rel_field = ""
  64. if cn == 'EmClass':
  65. ret += '[ label="%s", shape="%s" ]'%(c.name.replace('"','\\"'), 'doubleoctagon')
  66. elif cn == 'EmField':
  67. str_def = '[ label="Rel2Type {fname}|{{{records}}}", shape="record"]'
  68. records = ' | '.join([f.name for f in em.components('EmField') if f.rel_field_id == c.uid])
  69. ret += str_def.format(fname=c.name.replace('"','\\"'), records=records)
  70. elif cn == 'EmType':
  71. ret += '[ label="%s %s '%(cn, c.name.replace('"','\\"'))
  72. cntref = 0
  73. first = True
  74. for f in [ f for f in c.fields() if f.name not in c.em_class.default_fields_list().keys()]:
  75. if cn == 'EmType' and f.rel_field_id is None:
  76. #if not (f.rel_to_type_id is None):
  77. if isinstance(f, EditorialModel.fieldtypes.rel2type.EmFieldType):
  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.EmFieldType):
  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