-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.py
4498 lines (3748 loc) · 193 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
""" IM-FIT """
#!/usr/bin/env python3
import os
import re
import sys
import time
import ast
import json
import subprocess
import xml.etree.ElementTree as ET
import random
import pytest
import astunparse
from datetime import datetime
import matplotlib.pyplot as plt
import numpy as np
import squarify
from PySide6.QtWidgets import QApplication, QMainWindow, QFileDialog, QMessageBox
from PySide6 import QtGui
from PySide6 import *
from modules import *
from widgets import *
from ui_main import Ui_MainWindow
import imfit_database_function
import database_connection
import scan_process
import monitoring_process
class MainWindow(QMainWindow):
"""IM-FIT UI Window"""
global connection
connection = database_connection.connect("postgres", "imfit_database", "admin")
connection.commit()
global RUN_ORDER_LIST_JUST_PATH
RUN_ORDER_LIST_JUST_PATH = []
global ROS_SOURCE_CODE
ROS_SOURCE_CODE = ""
global POSSIBLE_MUTANT_LIST
POSSIBLE_MUTANT_LIST = []
global ZIPPED_LIST
ZIPPED_LIST = []
global ZIPPED_ROS_LIST
ZIPPED_ROS_LIST = []
global ROS_SOURCE_MUTANT
ROS_SOURCE_MUTANT = []
global source_and_mutate_code
source_and_mutate_code = []
global fault_name_list
fault_name_list = []
global fi_plan_directory_list
fi_plan_directory_list = []
global fault_plan_json_format_for_db
fault_plan_json_format = ""
global fiplan_name_for_db
fiplan_name_for_db = ""
global all_mutant_list_for_db
all_mutant_list_for_db = ""
global killed_mutants_list_for_db
killed_mutants_list_for_db = ""
global equivalent_mutants_list_for_db
equivalent_mutants_list_for_db = ""
global survived_mutants_list_for_db
survived_mutants_list_for_db = ""
global metric_list
metric_list = ""
global killed_mutant_ros_fi_plan_line
killed_mutant_ros_fi_plan_line = ""
global survived_mutant_ros_fi_plan_line
survived_mutant_ros_fi_plan_line = ""
global mutation_score_ros_fi_plan_line
mutation_score_ros_fi_plan_line = ""
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
Settings.ENABLE_CUSTOM_TITLE_BAR = True
# APP NAME
title = "IM-FIT"
description = "IM-FIT"
# APPLY TEXTS
self.setWindowTitle(title)
self.ui.titleRightInfo.setText(description)
# TOGGLE MENU
self.ui.toggleButton.clicked.connect(lambda: UIFunctions.toggleMenu(self, True))
# SET UI DEFINITIONS
UIFunctions.uiDefinitions(self)
# DATA
self.code_part_for_mutation = []
### FUNCTIONS
### TAKE FILE FUNCTIONS
def control_file_directory(file_name):
"""Check the file directory for the unwanted characters"""
banned_characters = ["(", ")", "[", "]", "?", "<", ">", "\\", "|"]
for i in file_name:
if i in banned_characters:
message_box = QMessageBox()
message_box.setIcon(QMessageBox.Warning)
message_box.setText("File directory must be edited!")
message_box.setWindowTitle("Warning!")
message_box.setStandardButtons(QMessageBox.Ok)
message_box.exec()
return False
return True
# Take ".py" file for source code
def get_file_py_for_source_code():
"""Take Python-based source code to use for V&V process in IM-FIT"""
dialog = QFileDialog()
dialog.setFileMode(QFileDialog.AnyFile)
dialog.setNameFilter(("*.py"))
if dialog.exec():
file_name = dialog.selectedFiles()
control_status = control_file_directory(file_name[0])
# .py file is choosed by the user
if file_name[0].endswith(".py") and control_status == True:
# file_name[0] is directory of file
with open(file_name[0], mode="r", encoding="utf-8") as json_file:
source_code_data = json_file.read()
# Source code directory is added to the text
self.ui.source_code_directory_text.setPlainText(
str(file_name[0])
)
# source adds to the UI
self.ui.source_code_content.setPlainText(source_code_data)
# IM-FIT DB
source_code_file_full_directory = file_name[0].split("/")
source_code_file_name = source_code_file_full_directory[-1]
# Take ".py" file for test case
def get_file_py_for_test_case():
"""Take Python-based test cases to use for V&V process in IM-FIT"""
dialog = QFileDialog()
dialog.setFileMode(QFileDialog.AnyFile)
dialog.setNameFilter(("*.py"))
if dialog.exec():
file_name = dialog.selectedFiles()
control_status = control_file_directory(file_name[0])
if file_name[0].endswith(".py") and control_status == True:
with open(file_name[0], mode="r", encoding="utf-8") as json_file:
data = json_file.read()
self.ui.test_case_directory_text.setPlainText(str(file_name[0]))
self.ui.test_case_content.setPlainText(data)
# Take ".py" file for Docker Page
def get_file_py_for_docker_page():
""" Take Python-based ROS Source Code """
dialog = QFileDialog()
dialog.setFileMode(QFileDialog.AnyFile)
dialog.setNameFilter(("*.py"))
if dialog.exec():
file_name = dialog.selectedFiles()
control_status = control_file_directory(file_name[0])
if file_name[0].endswith(".py") and control_status == True:
with open(file_name[0], mode="r", encoding="utf-8") as json_file:
data = json_file.read()
self.ui.textEdit_48.setPlainText(str(file_name[0]))
self.ui.textEdit_49.setPlainText(data)
# Take ".json" file for workload
def workload_get_file_json():
"""Take JSON-based workload files to use for V&V process in IM-FIT"""
dialog = QFileDialog()
dialog.setFileMode(QFileDialog.AnyFile)
dialog.setNameFilter(("*.json"))
if dialog.exec():
file_name = dialog.selectedFiles()
control_status = control_file_directory(file_name[0])
if file_name[0].endswith(".json") and control_status == True:
with open(file_name[0], mode="r", encoding="utf-8") as file:
data = file.read()
self.ui.textEdit_46.setPlainText(str(file_name[0]))
self.ui.textEdit_3.setPlainText(data)
# Take ".json" file function
def fiplan_get_file_json():
"""Take FI Plan to use for V&V process in IM-FIT"""
dialog = QFileDialog()
dialog.setFileMode(QFileDialog.AnyFile)
dialog.setNameFilter(("*.json"))
if dialog.exec():
file_name = dialog.selectedFiles()
control_status = control_file_directory(file_name[0])
if file_name[0].endswith(".json") and control_status == True:
self.ui.listWidget_11.addItem(str(file_name[0]))
def get_file_json_for_workload():
"""Take JSON-based workload files to use for V&V process in IM-FIT"""
dialog = QFileDialog()
dialog.setFileMode(QFileDialog.AnyFile)
dialog.setNameFilter(("*.json"))
if dialog.exec():
file_name = dialog.selectedFiles()
control_status = control_file_directory(file_name[0])
if file_name[0].endswith(".json") and control_status == True:
with open(file_name[0], mode="r", encoding="utf-8") as file:
data = file.read()
self.ui.textEdit_3.setPlainText(data)
# Take ".rosbag" file function
def get_file_rosbag():
"""Take rosbag files to use for V&V process in IM-FIT"""
dialog = QFileDialog()
dialog.setFileMode(QFileDialog.AnyFile)
dialog.setNameFilter(("*.bag"))
if dialog.exec():
file_name = dialog.selectedFiles()
control_status = control_file_directory(file_name[0])
if file_name[0].endswith(".bag") and control_status == True:
with open(file_name[0], mode="r", encoding="utf-8") as file:
data = file.read()
self.ui.textEditor.setPlainText(data)
# EDIT BUTTONS FOR START PAGE
# Source Code Edit Checkbox on START PAGE
def edit_source_code():
if self.ui.checkBox_8.isChecked() is True:
self.ui.source_code_content.setReadOnly(False)
else:
self.ui.source_code_content.setReadOnly(True)
# Workload Edit Checkbox on START PAGE
def edit_workload():
if self.ui.checkBox_5.isChecked() is True:
self.ui.textEdit_3.setReadOnly(False)
else:
self.ui.textEdit_3.setReadOnly(True)
# Select Code Snippets wit One Click
def code_snippet_select_with_one_clicked():
try:
clicked_item_text = self.ui.code_snippet_list.currentItem().text()
with open(
"code_snippets.json", encoding="utf-8"
) as code_snippets_from_json:
code_snippets_name_list = json.load(code_snippets_from_json)
added_snippet_regex_length = len(
code_snippets_name_list["code_snippets"]
)
for i in range(0, added_snippet_regex_length):
code_snippet_name = code_snippets_name_list["code_snippets"][i][
"Snippets"
]["Snippet_Name"]
if clicked_item_text == code_snippet_name:
code_snippet_description = code_snippets_name_list[
"code_snippets"
][i]["Snippets"]["Process"]
self.ui.textEdit_24.setPlainText(code_snippet_description)
except AttributeError:
pass
# Select Snippet Button on Start Page
def code_snippet_selection():
code_snippet_list_is_empty = True
len_of_selected_code_snippets = self.ui.listWidget_8.count()
selected_row = self.ui.code_snippet_list.currentItem().text()
row = selected_row.split("\n")
### Check Double Enter
if len_of_selected_code_snippets:
for i in range(len_of_selected_code_snippets):
code_snippet_list_element = self.ui.listWidget_8.item(i).text()
if code_snippet_list_element == selected_row:
code_snippet_list_is_empty = False
if code_snippet_list_is_empty is True:
self.ui.listWidget_8.addItems(row)
else:
self.ui.listWidget_8.addItems(row)
# Select Task on Create on Workload Page
def task_selection():
selected_row = self.ui.listWidget_21.currentItem().text()
row = selected_row.split("\n")
self.ui.listWidget_22.addItems(row)
# Select Task with One Click on Workload Page
def task_info_with_one_click():
clicked_item_text = self.ui.listWidget_21.currentItem().text()
self.ui.textEdit_14.clear()
json_type_task = self.ui.plainTextEdit.toPlainText()
tasks_list_json = json.loads(json_type_task)
for i in range(0, len(tasks_list_json["Tasks"])):
task_name = str(tasks_list_json["Tasks"][i]["Task"]["Task_ID"])
if task_name == clicked_item_text:
task_detail = str(tasks_list_json["Tasks"][i]["Task"])
for j in task_detail:
self.ui.textEdit_14.setPlainText(
self.ui.textEdit_14.toPlainText() + j
)
def possible_mutation_line_selection():
"""Select Line For Mutation on FI Plan Page"""
possible_mutation_line = self.ui.listWidget.currentItem().text()
self.ui.textEdit_13.setPlainText(possible_mutation_line)
def fault_selection_from_library():
"""Select Fault From Fault Library on FI Plan Page"""
global ZIPPED_LIST
ZIPPED_LIST = []
text_check = self.ui.textEdit_13.toPlainText()
if text_check:
selected_line = self.ui.textEdit_13.toPlainText()
selected_fault = self.ui.listWidget_3.currentItem().text()
text_selected_line_and_fault = (
selected_line + " #==># " + selected_fault
)
ZIPPED_LIST.append(text_selected_line_and_fault)
line_and_fault = text_selected_line_and_fault.split("\n")
self.ui.listWidget_7.addItems(line_and_fault)
# This Function Shows Fault's Information from Fault Library on FI Plan Page
def show_fault_info_with_one_click():
try:
clicked_item_text = self.ui.listWidget_3.currentItem().text()
with open("faultLibrary.json", encoding="utf-8") as file:
fault_list = json.load(file)
fault_list_size = self.ui.listWidget_3.count()
for i in range(fault_list_size):
fault_name = fault_list["all_faults"][i]["fault"]["Fault_Name"]
if clicked_item_text == fault_name:
fault_description = fault_list["all_faults"][i]["fault"][
"Explanation"
]
self.ui.textEdit_17.setPlainText(fault_description)
except:
IndexError()
def nth_replace(string, old, new, replace_num=1):
"""Replace Funtion"""
left_join = old
right_join = old
if not old:
old = " "
groups = string.split(old)
nth_split = [
left_join.join(groups[:replace_num]),
right_join.join(groups[replace_num:]),
]
return new.join(nth_split)
def mutation_process_function(target_text, start_json_value, finish_json_value):
global source_and_mutate_code
global fault_name_list
# try:
# Fault Lİbrary JSON is opened
with open("faultLibrary.json", encoding="utf-8") as file:
fault_list = json.load(file)
for fault_number in range(start_json_value, finish_json_value):
target_operators_from_library = fault_list["all_faults"][fault_number][
"fault"
]["Target_to_Change"]
operators = target_operators_from_library.split(",")
changed_operators_from_library = fault_list["all_faults"][fault_number][
"fault"
]["Changed"]
using_fault_name = fault_list["all_faults"][fault_number]["fault"][
"Fault_Name"
]
split_operators = changed_operators_from_library.split(",")
for k in operators:
find_k_in_text = re.findall(k, target_text)
if find_k_in_text:
found_target = find_k_in_text[0]
number_of_pattern = len(find_k_in_text)
for changing_number in range(1, number_of_pattern + 1):
for changed_line in split_operators:
mutated_line = nth_replace(
target_text,
found_target,
changed_line,
replace_num=changing_number,
)
if mutated_line != target_text:
# Mutated line is added to mutation list on IM-FIT UI
self.ui.listWidget_4.addItem(mutated_line)
# Original source code line and mutated line are added to
# list to use for execution
source_and_mutate_code.append(target_text)
source_and_mutate_code.append(mutated_line)
fault_name_list.append(using_fault_name)
total_mut = self.ui.listWidget_4.count()
self.ui.label_17.setText(str(total_mut))
# except:
# message_box = QMessageBox()
# message_box.setIcon(QMessageBox.Critical)
# message_box.setText("Failed to create mutant!")
# message_box.setWindowTitle("Caution!")
# message_box.setStandardButtons(QMessageBox.Ok)
# message_box.exec()
def fault_lib_size():
with open("faultLibrary.json", encoding="utf-8") as file:
fault_list = json.load(file)
fault_library_size = len(fault_list["all_faults"])
return fault_library_size
def start_mutation():
global fault_name_list
with open("faultLibrary.json", encoding="utf-8") as file:
fault_list = json.load(file)
fault_library_size = fault_lib_size()
if self.ui.checkBox_4.isChecked() is True:
self.ui.listWidget_7.clear()
self.ui.listWidget_7.setEnabled(False)
len_selected_line_mutation = self.ui.listWidget.count()
for i in range(0, len_selected_line_mutation):
line = self.ui.listWidget.item(i).text()
mutation_process_function(line, 0, fault_library_size)
else:
len_selected_line_mutation = self.ui.listWidget_7.count()
for i in range(0, len_selected_line_mutation):
hata_ve_line = self.ui.listWidget_7.item(i).text()
split_hata_ve_line = hata_ve_line.split(" #==># ")
hata_ismi = split_hata_ve_line[1]
print(hata_ismi)
fault_name_list.append(hata_ismi)
line = split_hata_ve_line[0]
for fault_number in range(fault_library_size):
fault_name_from_library = fault_list["all_faults"][
fault_number
]["fault"]["Fault_Name"]
if hata_ismi == fault_name_from_library:
mutation_process_function(
line, fault_number, fault_number + 1
)
def random_hata_bas():
fault_library_size = fault_lib_size()
random_fault = random.randint(0, fault_library_size)
target_text = self.ui.listWidget.currentItem().text()
mutation_process_function(target_text, random_fault, random_fault + 1)
def execution_module_function():
if self.ui.checkBox_10.isChecked() is True:
self.execute_docker_containers()
global ROS_SOURCE_MUTANT
global ROS_SOURCE_CODE
# print("ROS_SOURCE_CODE",ROS_SOURCE_CODE)
# print("ROS_SOURCE_MUTANT",ROS_SOURCE_MUTANT)
global fault_name_list
global all_mutant_list_for_db
global killed_mutants_list_for_db
global equivalent_mutants_list_for_db
global survived_mutants_list_for_db
global killed_mutant_ros_fi_plan_line
global survived_mutant_ros_fi_plan_line
global mutation_score_ros_fi_plan_line
killed_mutants_list = []
survived_mutants_list = []
equivalent_mutants_list = []
all_mutant_list = []
error_code_list = []
killed_mutants = 0
survived_mutants = 0
equivalent_mutants = 0
execution_timeout_list = []
original_source_code_output = ""
execution_fiplan_list_length = self.ui.listWidget_6.count()
for execution_step in range(0, execution_fiplan_list_length):
execution_fiplan_directory = self.ui.listWidget_6.item(
execution_step
).text()
file_type_keywords = [
"_type_python.json",
"_type_launch.json",
"_type_rospy.json",
]
for file_type_pattern in file_type_keywords:
find_file_type = re.findall(
file_type_pattern, execution_fiplan_directory
)
if find_file_type:
# ROS-Py execution settings
if find_file_type[0] == "_type_rospy.json":
killed_counter = 0
survived_counter = 0
# Length of ROS source mutant list
len_ros_source_mutant = len(ROS_SOURCE_MUTANT)
ros_working_directory = self.ui.textEdit_20.toPlainText()
working_package_full_path_split = (
ros_working_directory.split("/")
)
working_package_full_path_split.pop(-1)
working_directory = ""
for i in working_package_full_path_split:
if not i == "":
working_directory += "/" + i
for i in range(len_ros_source_mutant):
if i % 3 == 2:
new_data = ROS_SOURCE_CODE.replace(
ROS_SOURCE_MUTANT[i - 1], ROS_SOURCE_MUTANT[i]
)
fname = "mutant" + str(int(i / 3)) + ".py"
complete_name = os.path.join(
working_directory, fname
)
with open(
complete_name, mode="w", encoding="utf-8"
) as file1:
file1.write(new_data)
subprocess.Popen(
["chmod", "+x", fname], cwd=working_directory
)
print("#" * 32)
print("Mutant:", fname)
print("#" * 32)
try:
roscore_process = subprocess.Popen(["roscore"])
time.sleep(5)
# print("working_package_full_path_split[-3]",working_package_full_path_split[-2])
print("fname",fname)
status_output = subprocess.Popen(
[
"rosrun",
working_package_full_path_split[-2],
fname,
]
)
status_output.wait(timeout=5)
except subprocess.TimeoutExpired:
status_output.terminate()
roscore_process.terminate()
survived_counter += 1
print("#" * 32)
print("# Survived Mutant #")
print("#" * 32)
print("\n\n")
print("-" * 10)
else:
status_output.terminate()
roscore_process.terminate()
killed_counter += 1
print("#" * 32)
print("# Killed Mutant #")
print("#" * 32)
print("\n\n")
print("-" * 10)
print("#" * 32)
print("# Results #")
print("#" * 32)
print("\n\n")
print("Total Killed Mutant(s):", killed_counter)
print("Total Survived Mutant(s):", survived_counter)
print(
"\nMutation Score:",
(killed_counter / (killed_counter + survived_counter))
* 100,
)
print("-" * 32)
print("\n\n")
killed_mutant_ros_fi_plan_line = killed_counter
survived_mutant_ros_fi_plan_line = survived_counter
mutation_score_ros_fi_plan_line = (killed_counter / (killed_counter + survived_counter)) * 100
elif find_file_type[0] == "_type_launch.json":
killed_counter = 0
survived_counter = 0
fiplan_directory = self.ui.listWidget_36.item(0).text()
with open(fiplan_directory, encoding="utf-8") as json_file:
fault_list = json.load(json_file)
for i in range(len(fault_list)):
directory = fault_list[i]["Fault"]["File_Directory"]
split_directory = directory.split("/")
folder_name = split_directory[-3]
ros_exe_file_name = fault_list[i]["Fault"]["Exe_File"]
with open(
directory, mode="r", encoding="utf-8"
) as file:
python_file_content = file.read()
source_code = fault_list[i]["Fault"]["Source_Code"]
mutant_code = fault_list[i]["Fault"]["Mutate_Code"]
mutation_process = python_file_content.replace(
source_code, mutant_code
)
with open(
directory, mode="w", encoding="utf-8"
) as file:
file.write(mutation_process)
print("#" * 32)
print("Mutant:", str(i))
print("#" * 32)
try:
status_output = subprocess.Popen(
[
"roslaunch",
folder_name,
ros_exe_file_name,
]
)
status_output.wait(timeout=60)
except subprocess.TimeoutExpired:
status_output.terminate()
survived_counter += 1
print("#" * 32)
print("# Survived Mutant #")
print("#" * 32)
print("\n\n")
print("#" * 32)
else:
status_output.terminate()
killed_counter += 1
print("#" * 32)
print("# Killed Mutant #")
print("#" * 32)
print("\n\n")
print("#" * 32)
with open(
directory, mode="w", encoding="utf-8"
) as file:
file.write(python_file_content)
print("#" * 32)
print("# Results #")
print("#" * 32)
print("\n\n")
print("Total Killed Mutant(s):", killed_counter)
print("Total Survived Mutant(s):", survived_counter)
print(
"\nMutation Score:",
(killed_counter / (killed_counter + survived_counter))
* 100,
)
print("#" * 32)
print("\n\n")
else:
is_empty_dependent = self.ui.textEdit_40.toPlainText()
if is_empty_dependent == "":
start_time = time.time()
timeout_counter = 0
source_code_text = (
self.ui.source_code_content.toPlainText()
)
fname = "original_code.py"
data = source_code_text
with open(fname, mode="w", encoding="utf-8") as file:
file.write(data)
mutation_list_size = self.ui.listWidget_4.count()
time_limit_per_process = (
self.ui.textEdit_18.toPlainText()
)
subprocess.Popen("python3 original_code.py", shell=True)
print("#" * 32)
print("# Original Code #")
print("#" * 32)
fiplan_directory = self.ui.listWidget_6.item(0).text()
with open(
fiplan_directory, encoding="utf-8"
) as json_file:
fault_list = json.load(json_file)
file_name = (
self.ui.source_code_directory_text.toPlainText()
)
file_name_split = file_name.split("/")
file_name_for_creating_mutant_file = file_name_split[-1]
for i in range(0, mutation_list_size):
target_line_from_json = fault_list[i]["Fault"][
"Source_Code"
]
mutated_code_from_json = fault_list[i]["Fault"][
"Mutate_Code"
]
main_mutation_process = source_code_text.replace(
target_line_from_json, mutated_code_from_json
)
fname = (
"Mutant_no_"
+ str(i + 1)
+ "_"
+ file_name_for_creating_mutant_file
)
# fname = "fault" + str(i) + ".py"
data = main_mutation_process
mutant_save_location = self.ui.textEdit_52.toPlainText()
if mutant_save_location != "":
mutant_save_location_and_mutant_file = os.path.join(
mutant_save_location, fname
)
with open(
mutant_save_location_and_mutant_file, mode="w", encoding="utf-8"
) as file:
file.write(data)
else:
with open(
fname, mode="w", encoding="utf-8"
) as file:
file.write(data)
print("\n\nFault Number: ", i)
print("\n############")
if mutant_save_location != "":
cmd = ["python3", fname]
else:
cmd = ["python3", fname]
mutant_save_location = os.getcwd()
all_mutant_list.append(fname)
try:
subprocess.run(
cmd, cwd = mutant_save_location ,timeout=int(time_limit_per_process)
)
except subprocess.TimeoutExpired:
timeout_counter += 1
execution_timeout_list.append(
"python3 " + fname
)
killed_mutants += 1
killed_mutants_list.append("python3 " + fname)
# error_code_list.append(mutant_code_output)
continue
else:
os.chdir(mutant_save_location)
mutant_code_execution = "python3 " + fname
# This subprocess function runs mutant codes and takes their output.
subprocess_output_status = (
subprocess.getstatusoutput(
mutant_code_execution
)
)
mutant_code_output = subprocess_output_status[1]
error_code_list.append(mutant_code_output)
# This command detects coverage rate for the faults.
coverage_run_process = (
"coverage run fault" + str(i) + ".py"
)
coverage_run_process_report = "coverage report"
# Faults are run individually.
os.system(coverage_run_process)
# Generates the coverage report of the run the fault.
os.system(coverage_run_process_report)
if subprocess_output_status[0] != 0:
print(mutant_code_output)
print("#" * 32)
print("# Killed Mutant #")
print("#" * 32)
killed_mutants += 1
killed_mutants_list.append(
mutant_code_execution
)
error_code_list.append(mutant_code_output)
else:
if (
mutant_code_output
== original_source_code_output
):
print(mutant_code_output)
print("#" * 32)
print(
"# Equivalent Mutant #"
)
print("#" * 32)
equivalent_mutants += 1
equivalent_mutants_list.append(
mutant_code_execution
)
else:
print(mutant_code_output)
print("#" * 32)
print(
"# Survived Mutant #"
)
print("#" * 32)
survived_mutants += 1
survived_mutants_list.append(
mutant_code_execution
)
end_time = time.time()
print("\n\n")
print("#" * 32)
print(
"# RESULTS #"
)
print("#" * 32)
print("\n")
print("Process Time: ", end_time - start_time)
self.ui.listWidget_9.addItem(
"Process Time:" + str(end_time - start_time)
)
print("\n")
mutation_score = (
killed_mutants / (killed_mutants + survived_mutants)
) * 100
print("Mutation Score: %", mutation_score)
self.ui.listWidget_9.addItem(
"Mutation Score: %" + str(mutation_score)
)
print("\n")
print(
"Number of Total Mutants:",
killed_mutants
+ survived_mutants
+ equivalent_mutants,
)
self.ui.listWidget_9.addItem(
"All Mutants: "
+ str(
killed_mutants
+ survived_mutants
+ equivalent_mutants
)
)
print("\n")
print("Killed Mutants: ", killed_mutants)
self.ui.listWidget_9.addItem(
"Killed: " + str(killed_mutants)
)
print("Survived Mutants: ", survived_mutants)
self.ui.listWidget_9.addItem(
"Survived: " + str(survived_mutants)
)
print("Equivalent Mutants: ", equivalent_mutants)
self.ui.listWidget_9.addItem(
"Equivalent: " + str(equivalent_mutants)
)
print("Timeout: ", timeout_counter)
self.ui.listWidget_9.addItem(
"Timeout: " + str(timeout_counter)
)
# Create graphics about mutation process
self.create_bar_chart(
mutation_score,
killed_mutants,
survived_mutants,
equivalent_mutants,
)
self.create_pie_chart(
killed_mutants, survived_mutants, equivalent_mutants
)
print("\n")
print("#" * 32)
print(
"# DETAILS #"
)
print("#" * 32)
print("\n")
killed_mutant_list_size = len(killed_mutants_list)
print("Killed Mutants List: ")
self.ui.listWidget_16.addItem("Killed Mutants List: ")
for i in range(killed_mutant_list_size):
print(killed_mutants_list[i])
self.ui.listWidget_16.addItem(
str(killed_mutants_list[i])
)