LoLAConvert.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 place, marking in places.items():
  13. if int(marking) > 0:
  14. places_with_markings.append((place, marking))
  15. lola_file = filename.replace(".xml", ".lola")
  16. with open(lola_file, 'w') as f:
  17. f.write("PLACE\n")
  18. for i, place in enumerate(places):
  19. f.write("\t" + place)
  20. if i < len(places) -1:
  21. f.write(",\n")
  22. else:
  23. f.write(";\n")
  24. f.write("\nMARKING\n")
  25. for i, pm in enumerate(places_with_markings):
  26. f.write("\t" + pm[0] + " : " + pm[1])
  27. if i < len(places_with_markings) -1:
  28. f.write(",\n")
  29. else:
  30. f.write(";\n")
  31. for trans in transitions:
  32. f.write("\nTRANSITION " + trans + "\n")
  33. consume = {}
  34. produce = {}
  35. for arc in arcs:
  36. s, t, w = arc
  37. if t == trans:
  38. consume[s] = w
  39. elif s == trans:
  40. produce[t] = w
  41. f.write("\tCONSUME\n")
  42. for i, s in enumerate(consume):
  43. f.write("\t\t" + s + " : " + consume[s])
  44. if i < len(consume) - 1:
  45. f.write(",\n")
  46. else:
  47. f.write(";\n")
  48. f.write("\tPRODUCE\n")
  49. for i, t in enumerate(produce):
  50. f.write("\t\t" + t + " : " + produce[t])
  51. if i < len(produce) - 1:
  52. f.write(",\n")
  53. else:
  54. f.write(";\n")
  55. def parse_pnml(self, filename):
  56. tree = ET.parse(filename)
  57. root = tree.getroot()
  58. net = root[0]
  59. places = {}
  60. transitions = []
  61. arcs = []
  62. for child in net:
  63. if child.tag == "place":
  64. name_node = self.get_child_with_name(child, "name")
  65. name = self.get_child_with_name(name_node, "value").text
  66. marking_node = self.get_child_with_name(child, 'initialMarking')
  67. marking_value_node = self.get_child_with_name(marking_node, 'value')
  68. marking = marking_value_node.text.split(",")[1]
  69. places[name] = marking
  70. elif child.tag == "transition":
  71. name_node = self.get_child_with_name(child, "name")
  72. name = self.get_child_with_name(name_node, "value").text
  73. transitions.append(name)
  74. elif child.tag == "arc":
  75. source = child.get("source")
  76. target = child.get("target")
  77. inscription_node = self.get_child_with_name(child, 'inscription')
  78. inscription_value_node = self.get_child_with_name(inscription_node, 'value')
  79. inscription = inscription_value_node.text.split(",")[1]
  80. arcs.append((source, target, inscription))
  81. return places, transitions, arcs
  82. def get_child_with_name(self, node, name):
  83. for child in node:
  84. if child.tag == name:
  85. return child
  86. raise Exception("On node: " + str(node) +", the child " + name + " was not found!")
  87. if __name__ == "__main__":
  88. filename = "dining_philo.xml"
  89. lc = LoLAConvert()
  90. lc.convert_file(filename)