forked from yuqiChen94/Swat_Simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutation_operator.py
186 lines (173 loc) · 5.7 KB
/
mutation_operator.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
import random
import os
class operator:
path_list = os.getcwd() + "\plc\plc6_list.txt"
line_no = 0
check = 0
ori = []
def __init__(self, path):
self.path = path
self.ori = open(path, "rU").readlines()
self.line_no = random.randrange(0, len(self.ori))
self.line = self.ori[self.line_no].strip("\n")
def indices(self, element):
result = []
offset = -1
while True:
try:
offset = self.line.index(element, offset + 1)
except ValueError:
return result
result.append(offset)
def choose(self):
print(self.line)
def operator_ABS(self):
numlist = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
result = []
i = []
# print self.line
for num in numlist:
result.append(self.indices(num))
for j in range(0, len(numlist)):
if result[j] != []:
i.append(j)
# print j
# print i
if i != []:
numpos = i[random.randrange(0, len(i))]
numresult = result[numpos]
cpos = numresult[random.randrange(0, len(numresult))]
# print "numpos",numpos
# print numresult
# print cpos
newlist = numlist
del newlist[numpos]
choosenum = newlist[random.randrange(0, len(newlist))]
newline = self.line[0:cpos] + choosenum + self.line[cpos + 1 :]
self.check = 1
print(newline)
print(self.line)
self.line = newline
else:
self.check = 0
print("error")
def operator_AOR(self):
Aolist = ["+", "-", "*", "/", "%"]
result = []
i = []
# print self.line
for op in Aolist:
result.append(self.indices(op))
for j in range(0, len(Aolist)):
if result[j] != []:
i.append(j)
# print j
# print i
if i != []:
oppos = i[random.randrange(0, len(i))]
opresult = result[oppos]
cpos = opresult[random.randrange(0, len(opresult))]
# print "oppos",oppos
# print opresult
# print cpos
newlist = Aolist
del newlist[oppos]
chooseop = newlist[random.randrange(0, len(newlist))]
newline = self.line[0:cpos] + chooseop + self.line[cpos + 1 :]
print(newline)
print(self.line)
self.line = newline
self.check = 1
else:
print("error")
def operator_ASR(self):
Asolist = ["+=", "-=", "*=", "/=", "%="]
result = []
i = []
# print self.line
for op in Asolist:
result.append(self.indices(op))
for j in range(0, len(Asolist)):
if result[j] != []:
i.append(j)
# print j
# print i
if i != []:
oppos = i[random.randrange(0, len(i))]
opresult = result[oppos]
cpos = opresult[random.randrange(0, len(opresult))]
# print "oppos",oppos
# print opresult
# print cpos
newlist = Asolist
del newlist[oppos]
chooseop = newlist[random.randrange(0, len(newlist))]
newline = self.line[0:cpos] + chooseop + self.line[cpos + 1 :]
print(newline)
print(self.line)
self.line = newline
self.check = 1
else:
print("error")
def operator_ROR(self):
Asolist = [">", "<", ">=", "<=", "==", "!=", "<>"]
result = []
i = []
# print self.line
for op in Asolist:
result.append(self.indices(op))
for j in range(0, len(Asolist)):
if result[j] != []:
i.append(j)
# print j
# print i
if i != []:
oppos = i[random.randrange(0, len(i))]
opresult = result[oppos]
cpos = opresult[random.randrange(0, len(opresult))]
# print "oppos",oppos
# print opresult
# print cpos
newlist = Asolist
del newlist[oppos]
chooseop = newlist[random.randrange(0, len(newlist))]
newline = self.line[0:cpos] + chooseop + self.line[cpos + 1 :]
print(newline)
print(self.line)
self.line = newline
self.check = 1
else:
print("error")
def operator_COR(self):
try:
cpos = self.line.index("if")
num = random.randrange(0, 2)
if num == 0:
newline = self.line[0:cpos] + "if True:"
else:
newline = self.line[0:cpos] + "if False:"
print(self.line)
print(newline)
self.line = newline
self.check = 1
except:
print("error")
def gen(self, mutantnum):
if self.check == 1:
with open(self.path_list, "a") as f2:
f2.write("Mutation number:" + str(mutantnum) + "\r\n")
f2.write(self.ori[self.line_no].strip("\n"))
f2.write(" >>>>> ")
f2.write(self.line)
f2.write("\n\r")
path_new = self.path[0 : len(self.path) - 3] + "_" + str(mutantnum) + ".py"
count1 = len(open(self.path, "rU").readlines())
self.ori[self.line_no] = self.line + "\r\n"
with open(path_new, "a") as f1:
for i in range(0, len(self.ori)):
f1.write(self.ori[i])
return True
else:
print("No generation")
return False
# +,-,*,/