transformation.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. """
  2. A test TCore transformation: an FSA to a PN, based on
  3. https://homepages.ecs.vuw.ac.nz/~tk/publications/papers/explicitly-modeling-transformations.pdf
  4. """
  5. from core.himesis import Himesis, HimesisPreConditionPatternNAC, HimesisPreConditionPatternLHS, HimesisPostConditionPattern
  6. from tcore.messages import Packet
  7. from rules.frule import FRule
  8. from state import MvSKernel, getMvSBackend
  9. def print_results(graph):
  10. print("GRAPH:")
  11. for node in graph.node_guid_iter():
  12. print(" ", node)
  13. for k, v in graph.node_get_attributes(node).items():
  14. print(" ", k, "=", v)
  15. for edge in graph.edge_iter():
  16. label = graph.edge_get_label(edge)
  17. s, t = graph.get_edge(edge)
  18. print(" ", s, "-[ %s ]->" % str(label), t)
  19. # TODO: Read from DrawIO
  20. # TODO: Select graph based on Formalism name and instance name
  21. FSA = Himesis("FSA", getMvSBackend(MvSKernel.IGRAPH))
  22. # Create the nodes
  23. s1 = FSA.add_node("State", False)
  24. FSA.node_set_attribute(s1, "name", "S1")
  25. s2 = FSA.add_node("State", False)
  26. FSA.node_set_attribute(s2, "name", "S2")
  27. s3 = FSA.add_node("State", False)
  28. FSA.node_set_attribute(s3, "name", "S3")
  29. # Create the edges
  30. FSA.link_nodes(s1, s2, "y")
  31. FSA.link_nodes(s2, s2, "e")
  32. FSA.link_nodes(s2, s3, "s")
  33. # TODO: conformance check w.r.t. Formalism
  34. # Create the rules
  35. # TODO: Also obtain these from DrawIO
  36. # RULE 1: State2Place
  37. r1LHS = HimesisPreConditionPatternLHS("r1LHS", getMvSBackend(MvSKernel.IGRAPH))
  38. r1n1 = r1LHS.add_node("State", False)
  39. r1LHS.node_set_attribute(r1n1, Himesis.Constants.MT_LABEL, '1')
  40. r1NAC = HimesisPreConditionPatternNAC(None, "r1NAC", getMvSBackend(MvSKernel.IGRAPH))
  41. r1n2 = r1NAC.add_node("State", False)
  42. r1NAC.node_set_attribute(r1n2, Himesis.Constants.MT_LABEL, '1')
  43. r1n3 = r1NAC.add_node("Place", False)
  44. r1NAC.node_set_attribute(r1n3, Himesis.Constants.MT_LABEL, '2')
  45. r1NAC.link_nodes(r1n2, r1n3)
  46. r1LHS.addNAC(r1NAC)
  47. r1RHS = HimesisPostConditionPattern("r1RHS", getMvSBackend(MvSKernel.IGRAPH))
  48. r1n4 = r1RHS.add_node("State", False)
  49. r1RHS.node_set_attribute(r1n4, Himesis.Constants.MT_LABEL, '1')
  50. r1n5 = r1RHS.add_node("Place", False)
  51. r1RHS.node_set_attribute(r1n5, Himesis.Constants.MT_LABEL, '2')
  52. r1RHS.link_nodes(r1n4, r1n5)
  53. def action(mapper, H: Himesis):
  54. attr = H.node_get_attribute(mapper['1'], "name")
  55. H.node_set_attribute(mapper['2'], "name", "P(%s)" % attr)
  56. return {} # TODO?
  57. r1RHS[Himesis.Constants.MT_ACTION] = action
  58. # print("NAC:")
  59. # print_results(r1NAC)
  60. # print("\nLHS:")
  61. # print_results(r1LHS)
  62. # print("\nRHS:")
  63. # print_results(r1RHS)
  64. print("Applying ARule")
  65. r1NAC[Himesis.Constants.MT_CONSTRAINT] = lambda a, b: True
  66. r1LHS[Himesis.Constants.MT_CONSTRAINT] = lambda a, b: True
  67. r1RHS.pre = r1LHS
  68. r1RHS.pre_labels = []
  69. for v in r1LHS.node_guid_iter():
  70. r1RHS.pre_labels.append(r1LHS.node_get_attribute(v, Himesis.Constants.MT_LABEL))
  71. arule = FRule(r1LHS, r1RHS, sendAndApplyDeltaFunc=lambda x: None)
  72. FSA2PN = arule.packet_in(Packet(FSA))
  73. if arule.exception:
  74. print(arule.exception)
  75. else:
  76. # import igraph as ig
  77. # ig.plot(FSA2PN.graph.state.graph, "result.png")
  78. print_results(FSA2PN.graph)