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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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" ]\n'%nat
  47. #dotfp.write("}\n")
  48. """
  49. for rf in [ f for f in em.components(EmField) if f.fieldtype == 'rel2type']:
  50. dotfp.write(self._component_node(rf, em))
  51. cid = self._component_id(rf)
  52. self.edges += cid+' -> '+self._component_id(rf.em_class)+'\n'
  53. self.edges += cid+' -> '+self._component_id(em.component(rf.rel_to_type_id))+'\n'
  54. """
  55. dotfp.write(self.edges)
  56. dotfp.write("\n}")
  57. pass
  58. @staticmethod
  59. def _component_id(c):
  60. return 'emcomp%d'%c.uid
  61. def _component_node(self, c, em):
  62. ret = "\t"+EmBackendGraphviz._component_id(c)
  63. cn = c.__class__.__name__
  64. rel_field = ""
  65. if cn == 'EmClass':
  66. ret += '[ label="EmClass %s", shape="%s" ]'%(c.name.replace('"','\\"'), 'doubleoctagon')
  67. elif cn == 'EmField':
  68. str_def = '[ label="Rel2Type {fname}|{{{records}}}", shape="record"]'
  69. records = ' | '.join([f.name for f in em.components('EmField') if f.rel_field_id == c.uid])
  70. ret += str_def.format(fname=c.name.replace('"','\\"'), records=records)
  71. elif cn == 'EmType':
  72. ret += '[ label="%s %s '%(cn, c.name.replace('"','\\"'))
  73. cntref = 0
  74. first = True
  75. for f in [ f for f in c.fields() if f.name not in c.em_class.default_fields_list().keys()]:
  76. if f.rel_field_id is None:
  77. if f.fieldtype == 'rel2type':
  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"]\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" dir="both" ]\n'%(EmBackendGraphviz._component_id(c), cntref, rel_node_id)
  93. self.edges += '%s -> %s [color="purple" dir="both" ]\n'%(rel_node_id, ref_node)
  94. ret += '|'
  95. if first:
  96. ret += ' { '
  97. first = False
  98. if f.fieldtype == 'rel2type':
  99. ret += '<f%d> '%cntref
  100. cntref += 1
  101. ret += f.name
  102. ret += '}" shape="record" color="%s" ]'%('blue' if cn == 'EmType' else 'red')
  103. else:
  104. return ""
  105. ret +="\n"+rel_field
  106. return ret