main.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. from generator import *
  17. from patternMatching import *
  18. import graphToDot
  19. import random
  20. debug = False
  21. if __name__ == '__main__':
  22. """
  23. The main function called when running from the command line.
  24. """
  25. random.seed(0)
  26. graph, pattern = get_random_host_and_guest(
  27. nr_vtxs = 10,
  28. nr_vtx_types = 0,
  29. nr_edges = 20,
  30. nr_edge_types = 0,
  31. )
  32. # graph, pattern = get_large_host_and_guest()
  33. # graph, pattern = get_small_host_and_guest()
  34. # override random pattern by copy pasting output from terminal to create
  35. # pattern, paste it in the createConstantPattern function in the generator.py
  36. # pattern = gg.createConstantPattern()
  37. # generate here to know pattern and graph before searching it
  38. graphToDot.printGraph('randomPattern.dot', pattern)
  39. graphToDot.printGraph('randomGraph.dot', graph)
  40. #PM = PatternMatching('naive')
  41. #PM = PatternMatching('SP')
  42. # PM = PatternMatching('Ullmann')
  43. PM = PatternMatching('VF2')
  44. matches = [m for m in PM.matchVF2(pattern, graph)]
  45. print("found", len(matches), "matches:", matches)
  46. # regenerate graph, to show matched pattern
  47. for i, (v,e) in enumerate(matches):
  48. graphToDot.printGraph(f'randomGraph-{i}.dot', graph, v, e)
  49. if debug:
  50. print(len(v))
  51. print('___')
  52. print(v)
  53. for key, value in v.items():
  54. print(value.type)
  55. print(len(e))
  56. print(e)
  57. print('___')
  58. for key, value in e.items():
  59. print(value.type)