LoLAConvert.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import sys
  2. import xml.etree.ElementTree as ET
  3. class LoLAConvert:
  4. def __init__(self):
  5. pass
  6. def convert_file(self, filename):
  7. places, transitions, arcs = self.parse_pnml(filename)
  8. print("Places: " + str(places))
  9. print("Transitions: " + str(transitions))
  10. print("Arcs: " + str(arcs))
  11. places_with_markings = []
  12. for p_id, p_data in places.items():
  13. p_name, marking = p_data
  14. if int(marking) > 0:
  15. places_with_markings.append((p_id, p_name, marking))
  16. lola_file = filename.replace(".xml", ".lola")
  17. with open(lola_file, 'w') as f:
  18. f.write("PLACE\n")
  19. for i, p_id in enumerate(places):
  20. p_name, marking = places[p_id]
  21. f.write("\t" + p_name)
  22. if i < len(places) - 1:
  23. f.write(",\n")
  24. else:
  25. f.write(";\n")
  26. f.write("\nMARKING\n")
  27. for i, pm in enumerate(places_with_markings):
  28. f.write("\t" + pm[1] + " : " + pm[2])
  29. if i < len(places_with_markings) - 1:
  30. f.write(",\n")
  31. else:
  32. f.write(";\n")
  33. for trans_id, trans_name in transitions.items():
  34. f.write("\nTRANSITION " + trans_name + "\n")
  35. consume = {}
  36. produce = {}
  37. for arc in arcs:
  38. s, t, w = arc
  39. if t == trans_id:
  40. consume[s] = w
  41. elif s == trans_id:
  42. produce[t] = w
  43. f.write("\tCONSUME\n")
  44. for i, s in enumerate(consume):
  45. f.write("\t\t" + places[s][0] + " : " + consume[s])
  46. if i < len(consume) - 1:
  47. f.write(",\n")
  48. else:
  49. f.write(";\n")
  50. f.write("\tPRODUCE\n")
  51. for i, t in enumerate(produce):
  52. f.write("\t\t" + places[t][0] + " : " + produce[t])
  53. if i < len(produce) - 1:
  54. f.write(",\n")
  55. else:
  56. f.write(";\n")
  57. print("Wrote to: " + str(lola_file))
  58. def parse_pnml(self, filename):
  59. tree = ET.parse(filename)
  60. root = tree.getroot()
  61. net = root[0]
  62. places = {}
  63. transitions = {}
  64. arcs = []
  65. for child in net:
  66. if child.tag == "place":
  67. p_id = child.get("id")
  68. name_node = self.get_child_with_name(child, "name")
  69. name = self.get_child_with_name(name_node, "value").text
  70. name = name.replace(" ", ".")
  71. marking_node = self.get_child_with_name(child, 'initialMarking')
  72. marking_value_node = self.get_child_with_name(marking_node, 'value')
  73. marking = marking_value_node.text.split(",")[1]
  74. places[p_id] = (name, marking)
  75. elif child.tag == "transition":
  76. t_id = child.get("id")
  77. name_node = self.get_child_with_name(child, "name")
  78. name = self.get_child_with_name(name_node, "value").text
  79. name = name.replace(" ", ".")
  80. transitions[t_id] = name
  81. elif child.tag == "arc":
  82. source = child.get("source")
  83. target = child.get("target")
  84. inscription_node = self.get_child_with_name(child, 'inscription')
  85. inscription_value_node = self.get_child_with_name(inscription_node, 'value')
  86. inscription = inscription_value_node.text.split(",")[1]
  87. arcs.append((source, target, inscription))
  88. return places, transitions, arcs
  89. def get_child_with_name(self, node, name):
  90. for child in node:
  91. if child.tag == name:
  92. return child
  93. raise Exception("On node: " + str(node) + ", the child " + name + " was not found!")
  94. if __name__ == "__main__":
  95. print("Starting LoLAConvert...")
  96. if len(sys.argv) < 2:
  97. print("Error: Please pass a filename as the first argument.")
  98. exit(1)
  99. filename = sys.argv[1]
  100. lc = LoLAConvert()
  101. lc.convert_file(filename)