main.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import re
  2. def extract_float(text):
  3. # Define a regex pattern to match the float value
  4. pattern = r"Current Time:\s+([-+]?\d*\.\d+|\d+)"
  5. # Search for the pattern in the text
  6. match = re.search(pattern, text)
  7. if match:
  8. # Extract the matched float value
  9. float_value = float(match.group(1))
  10. return float_value
  11. else:
  12. # Handle the case where no match is found
  13. return None
  14. def extract_type(text):
  15. # Define a regex pattern to match the type value after 'Type:'
  16. pattern = r"Type:\s*(\w+)"
  17. # Search for the pattern in the text
  18. match = re.search(pattern, text)
  19. if match:
  20. # Extract the matched type value
  21. type_value = match.group(1)
  22. return type_value
  23. else:
  24. # Handle the case where no match is found
  25. return None
  26. def extract_event_details(text):
  27. #pattern = r"event name:\s*([^;]+);\s*port:\s*([^;]+)(?:;\s*parameters:\s*(\[.*\]))?"
  28. pattern = r"event name:\s*([^;]+);\s*port:\s*([^\);]+)(?:;\s*parameters:\s*(\[.*\]))?"
  29. match = re.search(pattern, text, re.IGNORECASE)
  30. if match:
  31. event_name = match.group(1).strip()
  32. port = match.group(2).strip()
  33. parameters = match.group(3).strip() if match.group(3) else 'None'
  34. return event_name, port, parameters
  35. else:
  36. return None, None, None
  37. def generate_input_trace(input_trace, output_file):
  38. # Open the file in read mode
  39. with open(input_trace, 'r') as file:
  40. # Read all lines into a list
  41. lines = file.readlines()
  42. # Open the file in write mode ('w')
  43. with open(output_file, 'w') as file:
  44. # Traverse each line in the file
  45. i = 0
  46. current_time = None
  47. while i < len(lines):
  48. if lines[i].startswith('__'):
  49. current_time = extract_float(lines[i].strip())
  50. # Check if the line starts with 'INPUT'
  51. if lines[i].startswith('INPUT'):
  52. # Print the line that starts with 'INPUT'
  53. print(lines[i].strip())
  54. # Check if the next two lines exist and print them
  55. the_type = extract_type(lines[i + 1].strip())
  56. name, port, parameters = extract_event_details(lines[i + 2].strip())
  57. if name == None:
  58. name = 'None'
  59. if port == None:
  60. port = 'None'
  61. if parameters == None:
  62. parameters = 'None'
  63. if the_type is not None:
  64. to_write = str(current_time) + " " + the_type + " Event(\"" + name + "\",\"" + port + "\"," + parameters + ")\n"
  65. file.write(to_write)
  66. # Skip the next two lines to avoid reprocessing them
  67. i += 2
  68. # Move to the next line
  69. i += 1
  70. def compare_traces(SCCDTrace, DEVSTrace):
  71. pass
  72. if __name__ == '__main__':
  73. SCCDFile = "./examples/BouncingBalls/Python/trace.txt"
  74. DEVSFile = "./examples/BouncingBalls/PyDEVS/trace.txt"
  75. inputTrace = "./examples/BouncingBalls/input_trace.txt"
  76. option = 1
  77. if option == 1:
  78. generate_input_trace(SCCDFile, inputTrace)
  79. if option == 2:
  80. compare_traces(SCCDFile, DEVSFile)