main 2.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 filter_lines(lines, filter_func):
  71. """
  72. Filter lines based on the provided filter function.
  73. """
  74. return [line for line in lines if not filter_func(line)]
  75. def read_and_filter_file(file_path, filter_func):
  76. """
  77. Read a file and filter its lines using the filter function.
  78. """
  79. with open(file_path, 'r') as file:
  80. lines = file.readlines()
  81. return filter_lines(lines, filter_func)
  82. # Fitler, filtering what should be ignored
  83. temp = False
  84. def devs_filter(line):
  85. global temp
  86. if line.startswith('\tEXTERNAL'):
  87. temp = True
  88. if temp and line.startswith('\n'):
  89. temp = False
  90. condition = not line.startswith('\tEXTERNAL') and not temp
  91. return condition
  92. i = 0
  93. def sccd_filter(line):
  94. global i
  95. if line.startswith('INPUT'):
  96. i = 2
  97. condition = (not line.startswith('INPUT') and not line.startswith('__'))
  98. if not line.startswith('INPUT') and i != 0:
  99. condition = False
  100. i -= 1
  101. return condition
  102. def write_filtered_lines_to_file(filtered_lines, output_path):
  103. """
  104. Write the filtered lines to a file.
  105. """
  106. with open(output_path, 'w') as file:
  107. file.writelines(filtered_lines)
  108. def compare_traces(file1_path, file2_path):
  109. """
  110. Compare two files line by line after filtering them.
  111. """
  112. filtered_lines1 = read_and_filter_file(file1_path, sccd_filter)
  113. filtered_lines2 = read_and_filter_file(file2_path, devs_filter)
  114. # Write the filtered lines to output files
  115. write_filtered_lines_to_file(filtered_lines1, "sccd_test.txt")
  116. write_filtered_lines_to_file(filtered_lines2, "devs_test.txt")
  117. line_num = 1
  118. differences = []
  119. max_lines = max(len(filtered_lines1), len(filtered_lines2))
  120. for i in range(max_lines):
  121. line1 = filtered_lines1[i] if i < len(filtered_lines1) else ''
  122. line2 = filtered_lines2[i] if i < len(filtered_lines2) else ''
  123. if line1 != line2:
  124. differences.append((line_num, line1, line2))
  125. line_num += 1
  126. return differences
  127. def print_differences(differences):
  128. """
  129. Print the differences between the filtered files.
  130. """
  131. if not differences:
  132. print("The files are identical after filtering.")
  133. else:
  134. for line_num, line1, line2 in differences:
  135. print(f"Difference at line {line_num}:")
  136. print(f"File1: {line1.strip()}")
  137. print(f"File2: {line2.strip()}")
  138. print()
  139. # Example filter function to exclude lines starting with a certain keyword
  140. def example_filter_func(line):
  141. return line.startswith('IGNORE')
  142. if __name__ == '__main__':
  143. SCCDFile = "./examples/BouncingBalls/Python/trace.txt"
  144. DEVSFile = "./examples/BouncingBalls/PyDEVS/trace.txt"
  145. inputTrace = "./examples/BouncingBalls/input_trace.txt"
  146. option = 2
  147. if option == 1:
  148. generate_input_trace(SCCDFile, inputTrace)
  149. if option == 2:
  150. compare_traces(SCCDFile, DEVSFile)