graphToDot.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # coding: utf-8
  2. """
  3. Author: Sten Vercamman
  4. Univeristy of Antwerp
  5. Example code for paper: Efficient model transformations for novices
  6. url: http://msdl.cs.mcgill.ca/people/hv/teaching/MSBDesign/projects/Sten.Vercammen
  7. The main goal of this code is to give an overview, and an understandable
  8. implementation, of known techniques for pattern matching and solving the
  9. sub-graph homomorphism problem. The presented techniques do not include
  10. performance adaptations/optimizations. It is not optimized to be efficient
  11. but rather for the ease of understanding the workings of the algorithms.
  12. The paper does list some possible extensions/optimizations.
  13. It is intended as a guideline, even for novices, and provides an in-depth look
  14. at the workings behind various techniques for efficient pattern matching.
  15. """
  16. import graph as mg
  17. def printGraph(fileName, graph, matched_v={}, matched_e={}):
  18. if not isinstance(graph, mg.Graph):
  19. raise TypeError('Can only print Graph Graphs')
  20. with open(fileName, 'w') as f:
  21. f.write('digraph randomGraph {\n\n')
  22. for str_type, plan_vertices in graph.vertices.items():
  23. for plan_vertex in plan_vertices:
  24. vertex_str = str(id(plan_vertex)) + ' [label="'+str(str_type)+'"'
  25. if plan_vertex in list(matched_v.values()):
  26. vertex_str += ', style=dashed, style=filled]\n'
  27. else:
  28. vertex_str += ']\n'
  29. f.write(vertex_str)
  30. for out_edge in plan_vertex.outgoing_edges:
  31. edge_str = str(id(plan_vertex)) + ' -> ' + str(id(out_edge.tgt)) + ' [label="'+str(out_edge.type)+'"'
  32. if out_edge in list(matched_e.values()):
  33. edge_str += ', style=dashed, penwidth = 4]\n'
  34. else:
  35. edge_str += ']\n'
  36. f.write(edge_str)
  37. f.write('\n}')