/
EMConfig.kt
465 lines (350 loc) · 16.4 KB
/
EMConfig.kt
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
package org.evomaster.core
import joptsimple.BuiltinHelpFormatter
import joptsimple.OptionDescriptor
import joptsimple.OptionParser
import joptsimple.OptionSet
import org.evomaster.clientJava.controllerApi.ControllerConstants
import org.evomaster.core.output.OutputFormat
import kotlin.reflect.KMutableProperty
import kotlin.reflect.jvm.javaType
/**
* Class used to hold all the main configuration properties
* of EvoMaster.
*
*/
class EMConfig {
/*
Code here does use the JOptSimple library
https://pholser.github.io/jopt-simple/
*/
companion object {
fun validateOptions(args: Array<String>): OptionParser {
val config = EMConfig()
val parser = EMConfig.getOptionParser()
val options = parser.parse(*args)
if (!options.has("help")) {
//actual validation is done here
config.updateProperties(options)
}
return parser
}
/**
* Having issue with types/kotlin/reflection...
* Therefore, need custom output formatting.
* However, easier way (for now) is to just override
* what we want to change
*
* TODO: groups and ordering
*/
private class MyHelpFormatter : BuiltinHelpFormatter(80, 2) {
override fun extractTypeIndicator(descriptor: OptionDescriptor): String? {
return null
}
}
/**
* Get all available "console options" for the annotated properties
*/
fun getOptionParser(): OptionParser {
val defaultInstance = EMConfig()
var parser = OptionParser()
parser.accepts("help", "Print this help documentation")
.forHelp()
getConfigurationProperties().forEach { m ->
/*
Note: here we could use typing in the options,
instead of converting everything to string.
But it looks bit cumbersome to do it in Kotlin,
at least for them moment
Until we make a complete MyHelpFormatter, here
for the types we "hack" the default one, ie,
we set the type description to "null", but then
the argument description will contain the type
*/
val argTypeName = m.returnType.toString()
.run { substring(lastIndexOf('.') + 1) }
parser.accepts(m.name, getDescription(m))
.withRequiredArg()
.describedAs(argTypeName)
.defaultsTo("" + m.call(defaultInstance))
}
parser.formatHelpWith(MyHelpFormatter())
return parser
}
private fun getDescription(m: KMutableProperty<*>): String {
val cfg = (m.annotations.find { it is Cfg } as? Cfg)
?: throw IllegalArgumentException("Property ${m.name} is not annotated with @Cfg")
val text = cfg.description.trim().run {
when {
isBlank() -> "No description."
!endsWith(".") -> this + "."
else -> this
}
}
val min = (m.annotations.find { it is Min } as? Min)?.min
val max = (m.annotations.find { it is Max } as? Max)?.max
var constraints = ""
if (min != null || max != null) {
constraints += " [Constraints: "
if (min != null) {
constraints += "min=$min"
}
if (max != null) {
if (min != null) constraints += ", "
constraints += "max=$max"
}
constraints += "]."
}
var enumValues = ""
val returnType = m.returnType.javaType as Class<*>
if (returnType.isEnum) {
val elements = returnType.getDeclaredMethod("values")
.invoke(null) as Array<*>
enumValues = " [Values: " + elements.joinToString(", ") + "]"
}
var description = "$text$constraints$enumValues"
val experimental = (m.annotations.find { it is Experimental } as? Experimental)
if(experimental != null){
/*
TODO: For some reasons, coloring is not working here.
Could open an issue at:
https://github.com/jopt-simple/jopt-simple
*/
//description = AnsiColor.inRed("EXPERIMENTAL: $description")
description = "EXPERIMENTAL: $description"
}
return description
}
fun getConfigurationProperties(): List<KMutableProperty<*>> {
return EMConfig::class.members
.filterIsInstance(KMutableProperty::class.java)
.filter { it.annotations.any { it is Cfg } }
}
}
/**
* Update the values of the properties based on the options
* chosen on the command line
*
*
* @throws IllegalArgumentException if there are constraint violations
*/
fun updateProperties(options: OptionSet) {
getConfigurationProperties().forEach { m ->
val opt = options.valueOf(m.name)?.toString() ?:
throw IllegalArgumentException("Value not found for property ${m.name}")
val returnType = m.returnType.javaType as Class<*>
/*
TODO: ugly checks. But not sure yet if can be made better in Kotlin.
Could be improved with isSubtypeOf from 1.1?
http://stackoverflow.com/questions/41553647/kotlin-isassignablefrom-and-reflection-type-checks
*/
try {
if (Integer.TYPE.isAssignableFrom(returnType)) {
m.setter.call(this, Integer.parseInt(opt))
} else if (java.lang.Long.TYPE.isAssignableFrom(returnType)) {
m.setter.call(this, java.lang.Long.parseLong(opt))
} else if (java.lang.Double.TYPE.isAssignableFrom(returnType)) {
m.setter.call(this, java.lang.Double.parseDouble(opt))
} else if (java.lang.Boolean.TYPE.isAssignableFrom(returnType)) {
m.setter.call(this, java.lang.Boolean.parseBoolean(opt))
} else if (java.lang.String::class.java.isAssignableFrom(returnType)) {
m.setter.call(this, opt)
} else if (returnType.isEnum) {
val valueOfMethod = returnType.getDeclaredMethod("valueOf",
java.lang.String::class.java)
m.setter.call(this, valueOfMethod.invoke(null, opt))
} else {
throw IllegalStateException("BUG: cannot handle type $returnType")
}
} catch (e: Exception) {
throw IllegalArgumentException("Failed to handle property '${m.name}'", e)
}
val parameterValue = m.getter.call(this).toString()
m.annotations.find { it is Min }?.also {
it as Min
if(parameterValue.toDouble() < it.min){
throw IllegalArgumentException("Failed to handle Min ${it.min} constraint for" +
" parameter '${m.name}' with value $parameterValue")
}
}
m.annotations.find { it is Max }?.also {
it as Max
if(parameterValue.toDouble() > it.max){
throw IllegalArgumentException("Failed to handle Max ${it.max} constraint for" +
" parameter '${m.name}' with value $parameterValue")
}
}
}
when(stoppingCriterion){
StoppingCriterion.TIME -> if(maxActionEvaluations != defaultMaxActionEvaluations){
throw IllegalArgumentException("Changing number of max actions, but stopping criterion is time")
}
StoppingCriterion.FITNESS_EVALUATIONS -> if(maxTimeInSeconds != defaultMaxTimeInSeconds){
throw IllegalArgumentException("Changing number of max seconds, but stopping criterion is based on fitness evaluations")
}
}
if(shouldGenerateSqlData() && ! heuristicsForSQL){
throw IllegalArgumentException("Cannot generate SQL data if you not enable " +
"collecting heuristics with 'heuristicsForSQL'")
}
}
fun shouldGenerateSqlData() = generateSqlDataWithDSE || generateSqlDataWithSearch
//------------------------------------------------------------------------
//--- custom annotations
/**
* Configuration (CFG in short) for EvoMaster.
* Properties annotated with [Cfg] can be set from
* command line.
* The code in this class uses reflection, on each property
* marked with this annotation, to build the list of available
* modifiable configurations.
*/
@Target(AnnotationTarget.PROPERTY)
@MustBeDocumented
annotation class Cfg(val description: String)
@Target(AnnotationTarget.PROPERTY)
@MustBeDocumented
annotation class Min(val min: Double)
@Target(AnnotationTarget.PROPERTY)
@MustBeDocumented
annotation class Max(val max: Double)
/**
* This annotation is used to represent properties controlling
* features that are still work in progress.
* Do not use them (yet).
*/
@Target(AnnotationTarget.PROPERTY)
@MustBeDocumented
annotation class Experimental
//------------------------------------------------------------------------
//--- properties
enum class Algorithm {
MIO, RANDOM, WTS, MOSA
}
@Cfg("The algorithm used to generate test cases")
var algorithm = Algorithm.MIO
enum class ProblemType {
REST
}
@Cfg("The type of SUT we want to generate tests for, e.g., a RESTful API")
var problemType = ProblemType.REST
@Cfg("Specify in which format the tests should be outputted")
var outputFormat = OutputFormat.JAVA_JUNIT_4
@Cfg("Specify if test classes should be created as output of the tool. " +
"Usually, you would put it to 'false' only when debugging EvoMaster itself")
var createTests = true
@Cfg("The path directory of where the generated test classes should be saved to")
//TODO check if can be created
var outputFolder = "src/em"
@Cfg("The name of generated file with the test cases, without file type extension. " +
"In JVM languages, if the name contains '.', folders will be created to represent " +
"the given package structure")
//TODO constrain of no spaces or weird characters, eg use regular expression
var testSuiteFileName = "EvoMasterTest"
@Cfg("The seed for the random generator used during the search. " +
"A negative value means the CPU clock time will be rather used as seed")
var seed: Long = -1
@Cfg("TCP port of where the SUT REST controller is listening on")
@Min(0.0) @Max(65535.0)
var sutControllerPort = ControllerConstants.DEFAULT_CONTROLLER_PORT
@Cfg("Host name or IP address of where the SUT REST controller is listening on")
var sutControllerHost = ControllerConstants.DEFAULT_CONTROLLER_HOST
@Cfg("Limit of number of individuals per target to keep in the archive")
@Min(1.0)
var archiveTargetLimit = 10
@Cfg("Probability of sampling a new individual at random")
@Min(0.0) @Max(1.0)
var probOfRandomSampling = 0.5
@Cfg("The percentage of passed search before starting a more focused, less exploratory one")
@Min(0.0) @Max(1.0)
var focusedSearchActivationTime = 0.5
@Cfg("Number of applied mutations on sampled individuals, at the start of the search")
@Min(0.0)
var startNumberOfMutations = 1
@Cfg("Number of applied mutations on sampled individuals, by the end of the search")
@Min(0.0)
var endNumberOfMutations = 10
enum class StoppingCriterion {
TIME,
FITNESS_EVALUATIONS
}
@Cfg("Stopping criterion for the search")
var stoppingCriterion = StoppingCriterion.TIME
val defaultMaxActionEvaluations = 1000
@Cfg("Maximum number of action evaluations for the search." +
" A fitness evaluation can be composed of 1 or more actions," +
" like for example REST calls or SQL setups." +
" The more actions are allowed, the better results one can expect." +
" But then of course the test generation will take longer." +
" Only applicable depending on the stopping criterion.")
@Min(1.0)
var maxActionEvaluations = defaultMaxActionEvaluations
val defaultMaxTimeInSeconds = 60
@Cfg("Maximum number of seconds allowed for the search." +
" The more time is allowed, the better results one can expect." +
" But then of course the test generation will take longer." +
" Only applicable depending on the stopping criterion.")
@Min(1.0)
var maxTimeInSeconds = defaultMaxTimeInSeconds
@Cfg("Whether or not writing statistics of the search process. " +
"This is only needed when running experiments with different parameter settings")
var writeStatistics = false
@Cfg("Where the statistics file (if any) is going to be written (in CSV format)")
var statisticsFile = "statistics.csv"
@Cfg("Whether should add to an existing statistics file, instead of replacing it")
var appendToStatisticsFile = false
@Cfg("If positive, check how often, in percentage % of the budget, to collect statistics snapshots." +
" For example, every 5% of the time.")
@Max(50.0)
var snapshotInterval = -1.0
@Cfg("Where the snapshot file (if any) is going to be written (in CSV format)")
var snapshotStatisticsFile = "snapshot.csv"
@Cfg("An id that will be part as a column of the statistics file (if any is generated)")
var statisticsColumnId = "-"
@Cfg("Probability of applying a mutation that can change the structure of a test")
@Min(0.0) @Max(1.0)
var structureMutationProbability = 0.5
enum class FeedbackDirectedSampling {
NONE,
LAST,
FOCUSED_QUICKEST
}
@Cfg("Specify whether when we sample from archive we do look at the most promising targets for which we have had a recent improvement")
var feedbackDirectedSampling = FeedbackDirectedSampling.LAST
@Cfg("Define the population size in the search algorithms that use populations (eg, Genetic Algorithms)")
@Min(1.0)
var populationSize = 30
@Cfg("Define the maximum number of tests in a suite in the search algorithms that evolve whole suites, e.g. WTS")
@Min(1.0)
var maxSearchSuiteSize = 50
@Cfg("Probability of applying crossover operation (if any is used in the search algorithm)")
@Min(0.0) @Max(1.0)
var xoverProbability = 0.7
@Cfg("Number of elements to consider in a Tournament Selection (if any is used in the search algorithm)")
@Min(1.0)
var tournamentSize = 10
@Cfg("When sampling new test cases to evaluate, probability of using some smart strategy instead of plain random")
@Min(0.0) @Max(1.0)
var probOfSmartSampling = 0.5
@Cfg("Max number of 'actions' (e.g., RESTful calls or SQL commands) that can be done in a single test")
@Min(1.0)
var maxTestSize = 10
@Experimental
@Cfg("Tracking of SQL commands to improve test generation")
var heuristicsForSQL = false
@Experimental
@Cfg("Enable EvoMaster to generate SQL data with direct accesses to the database. Use Dynamic Symbolic Execution")
var generateSqlDataWithDSE = false
@Experimental
@Cfg("Enable EvoMaster to generate SQL data with direct accesses to the database. Use a search algorithm")
var generateSqlDataWithSearch = false
@Experimental
@Cfg("When generating SQL data, how many new rows (max) to generate for each specific SQL Select")
@Min(1.0)
var maxSqlInitActionsPerMissingData = 5
@Cfg("Maximum size (in bytes) that EM handles response payloads in the HTTP responses. " +
"If larger than that, a response will not be stored internally in EM during the test generation. "+
"This is needed to avoid running out of memory.")
var maxResponseByteSize = 1_000_000
@Cfg("Whether to print how much search done so far")
var showProgress = true
}