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

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