-
Notifications
You must be signed in to change notification settings - Fork 1
/
MutationClasses.py
295 lines (209 loc) · 8.88 KB
/
MutationClasses.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
from matlab.engine import matlabengine
def parse_str(number):
try:
return int(number)
except ValueError:
return float(number)
###################################################################
#
#
#
###################################################################
class MutationIterator:
def load(self, block: str, eng: matlabengine.MatlabEngine):
"""Load Original Block"""
pass
def __iter__(self):
return self
def __next__(self):
return self.next_mutant()
def next_mutant(self):
pass
def get_original(self):
return ()
class SingleParameterMutationIterator(MutationIterator):
mutant_types = []
param_name = ""
unique_original_function = False
block_type = ""
current_index = -99
start_index = -99
original_function = None
initialized = False
def __iter__(self):
self.current_index = 0 if self.unique_original_function else 1 # just skip start_index if this is not a unique_orginal_function like if
return self
def load(self, block: str, eng: matlabengine.MatlabEngine):
self.initialized = True
self.original_function = eng.get_param(block, self.param_name)
try:
self.start_index = self.mutant_types.index(self.original_function)
except ValueError:
# some blocks has unique original values just store it.
if self.unique_original_function:
self.start_index = -1
return True
return False
return True
def next_mutant(self):
if self.current_index < len(self.mutant_types):
index = (self.start_index + self.current_index) % len(self.mutant_types)
self.current_index += 1
return (self.param_name, self.mutant_types[index])
else:
raise StopIteration
def get_original(self):
return (self.param_name, self.original_function)
def __str__(self):
return self.__repr__()
def __repr__(self):
if not self.initialized:
return f"{self.__class__.__name__}({self.param_name}, {len(self.mutant_types)})"
current_mutation_index = (self.start_index + self.current_index - 1) % len(self.mutant_types)
current_mutation = self.mutant_types[current_mutation_index]
return f"{self.__class__.__name__}(parameter_name='{self.param_name}', original_value='{self.original_function}', mutation='{current_mutation}', n='{self.current_index-1}/{len(self.mutant_types)-1}')"
class MinMaxMutationIterator(SingleParameterMutationIterator):
mutant_types = ["max", "min"]
param_name = "function"
block_type = "MinMax"
class DoubleInputRelationalOperatorMutationIterator(SingleParameterMutationIterator):
mutant_types = ["==", "~=", "<", "<=", ">=", ">"]
param_name = "Operator"
block_type = "RelationalOperator"
#class SingleInputRelationalOperatorMutationIterator(SingleParameterMutationIterator):
# mutant_types = ["isInf", "isNaN", "isFinite"]
# param_name = "function"
# TODO: there can be more than two sign or less
class SumMutationIterator(SingleParameterMutationIterator):
mutant_types = ["++", "+-", "-+", "--"]
param_name = "Inputs"
block_type = "Sum"
class OneSpaceSumMutationIterator(SingleParameterMutationIterator):
mutant_types = ["|++", "|+-", "|-+", "|--"]
param_name = "Inputs"
block_type = "Sum"
# TODO: "NOT" function is not supporting because have only 1 input
class LogicalOperatorMutationIterator(SingleParameterMutationIterator):
mutant_types = ['AND', 'OR', 'NAND', 'NOR', 'XOR', 'NXOR']
param_name = "Operator"
block_type = "Logic"
class IfMutationIterator(SingleParameterMutationIterator):
mutant_types = ["1 == 1", "1 == 0"] #instad of true or false
param_name = "IfExpression"
unique_original_function = True
block_type = "If"
class TrigonometryMutationIterator(SingleParameterMutationIterator):
mutant_types = ['sin', 'cos', 'tan','asin','acos','atan','sinh', 'cosh','tanh','asinh','acosh','atanh','cos + jsin']
param_name = "Operator"
block_type = "Trigonometry"
class SingleInputMathMutationIterator(SingleParameterMutationIterator):
mutant_types = ['exp', 'log','10^u','log10','magnitude^2','square','conj','reciprocal','transpose' ,'hermitian']
param_name = "Operator"
block_type = "Math"
class DoubleInputMathMutationIterator(SingleParameterMutationIterator):
mutant_types = ['pow','hypot','rem','mod']
param_name = "Operator"
block_type = "Math"
class ProductMutationIterator(SingleParameterMutationIterator):
mutant_types = ['Element-wise(.*)', 'Matrix(*)']
param_name = "Multiplication"
block_type = "Product"
class ProductInputsIterator(SingleParameterMutationIterator):
mutant_types = ['2', '*/', '/*', '//']
param_name = "Inputs"
block_type = "Product"
class ForIteratorMutationIterator(SingleParameterMutationIterator):
mutant_types = ['Zero-based', 'One-based']
param_name = "IndexMode"
block_type = "ForIterator"
class UnitDelayMutationIterator(SingleParameterMutationIterator):
mutant_types = ['Columns as channels (frame based)', 'Elements as channels (sample based)']
param_name = "InputProcessing"
block_type = "UnitDelay"
class SwitchMutationIterator(SingleParameterMutationIterator):
mutant_types = ['u2 >= Threshold', 'u2 ~= 0'] # discarded. u2 > Threshold
param_name = "Criteria"
block_type = "Switch"
###################################################################
#
#
#
###################################################################
class SingleParameterLambdaMutationIterator(MutationIterator):
mutant_types = []
param_name = ""
block_type = ""
current_index = -99
original_function = None
initialized = False
def __iter__(self):
self.current_index = 0
return self
def load(self, block: str, eng: matlabengine.MatlabEngine):
self.initialized = True
self.original_function = eng.get_param(block, self.param_name)
return self.check(eng)
def check(self, eng):
return True
def next_mutant(self):
if self.current_index < len(self.mutant_types):
index = self.current_index
self.current_index += 1
val = self.get_val(index)
return (self.param_name, val)
else:
raise StopIteration
def get_val(self,index):
return str(self.mutant_types[index](parse_str(self.original_function)))
def get_original(self):
return (self.param_name, self.original_function)
def __str__(self):
return self.__repr__()
def __repr__(self):
if not self.initialized:
return f"{self.__class__.__name__}({self.param_name}, {len(self.mutant_types)})"
current_mutation_index = self.current_index-1
current_mutation = self.get_val(current_mutation_index)
return f"{self.__class__.__name__}(parameter_name='{self.param_name}', original_value='{self.original_function}', mutation='{current_mutation}', n='{self.current_index}/{len(self.mutant_types)}')"
class ForIteratorLamdaMutationIterator(SingleParameterLambdaMutationIterator):
mutant_types = [lambda x: x*2]
param_name = "IterationLimit"
block_type = "ForIterator"
class UnitDelayLamdaMutationIterator(SingleParameterLambdaMutationIterator):
mutant_types = [lambda x: x+10]
param_name = "InitialCondition"
block_type = "UnitDelay"
class SwitchLamdaMutationIterator(SingleParameterLambdaMutationIterator):
mutant_types = [lambda x: x+10]
param_name = "Threshold"
block_type = "Switch"
def check(self, eng):
# check block is not u2 ~= 0
return self.original_function != "u2 ~= 0"
class ConstantLamdaMutationIterator(SingleParameterLambdaMutationIterator):
mutant_types = [
lambda x: x+1, lambda x: x+10, lambda x: x+100,
lambda x: x-1, lambda x: x-10, lambda x: x-100,
]
param_name = "Value"
block_type = "Constant"
def check(self, eng):
# check value is int or float
try:
parse_str(self.original_function)
return True
except ValueError:
return False
###################################################################
#
#
#
###################################################################
class MutationApplier:
def apply(self, block: str, eng: matlabengine.MatlabEngine, args):
eng.set_param(block, *args, nargout=0)
###################################################################
#
#
#
###################################################################