Skip to content

Commit a2e910c

Browse files
committed
first full pass for refactoring
1 parent 1c735bd commit a2e910c

36 files changed

Lines changed: 180 additions & 546 deletions

core/src/main/kotlin/org/evomaster/core/search/gene/BooleanGene.kt

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,23 +71,9 @@ class BooleanGene(
7171

7272

7373
override fun unsafeCopyValueFrom(other: Gene): Boolean {
74-
if (other !is BooleanGene) {
75-
throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
76-
}
77-
val current = this.value
78-
this.value = other.value
79-
if (!isLocallyValid()){
80-
this.value = current
81-
return false
82-
}
8374

84-
return true
85-
}
75+
val gene = other.getPhenotype()
8676

87-
override fun unsafeSetFromStringValue(gene: Gene): Boolean {
88-
if (gene is SeededGene<*>){
89-
return this.unsafeSetFromStringValue(gene.getPhenotype()as Gene)
90-
}
9177
if (gene !is BooleanGene){
9278
LoggingUtil.uniqueWarn(log, "Do not support to bind boolean gene with the type: ${gene::class.java.simpleName}")
9379
return false

core/src/main/kotlin/org/evomaster/core/search/gene/Gene.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import org.evomaster.core.problem.enterprise.EnterpriseIndividual
1616
import org.evomaster.core.problem.enterprise.SampleType
1717
import org.evomaster.core.search.RootElement
1818
import org.evomaster.core.search.gene.utils.GeneUtils
19-
import org.evomaster.core.search.gene.wrapper.WrapperGene
19+
import org.evomaster.core.search.gene.interfaces.WrapperGene
2020
import org.evomaster.core.search.service.SearchGlobalState
2121
import org.evomaster.core.search.service.monitor.ProcessMonitorExcludeField
2222

core/src/main/kotlin/org/evomaster/core/search/gene/ObjectGene.kt

Lines changed: 14 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -246,71 +246,36 @@ class ObjectGene(
246246

247247
override fun unsafeCopyValueFrom(other: Gene): Boolean {
248248
if (other !is ObjectGene) {
249-
throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
249+
return false
250250
}
251251

252252
if (other.isFixed != isFixed)
253-
throw IllegalArgumentException("cannot copy value for ObjectGene if their isFixed is different")
253+
return false
254254

255255
if (!isFixed && !template!!.possiblySame(other.template!!))
256-
throw IllegalArgumentException("different template ${other.template.javaClass}")
256+
return false
257257

258258
//TODO what if they have a different number of fields, or name not match???
259259
// semantic of this function is unclear, really need TODO refactoring
260260

261-
val updateOk = updateValueOnlyIfValid(
262-
{
263-
var ok = true
264-
265-
for (i in fixedFields.indices) {
266-
ok = ok && this.fixedFields[i].unsafeCopyValueFrom(other.fixedFields[i])
267-
}
268-
269-
if(!isFixed){
270-
//TODO what if there is a mismatch here? semantic of this function is unclear
271-
for (i in additionalFields!!.indices){
272-
ok = ok && this.additionalFields!![i].unsafeCopyValueFrom(other.additionalFields!![i])
273-
}
274-
}
275-
276-
ok
277-
}, true
278-
)
261+
var ok = true
279262

280-
return updateOk
281-
}
263+
for (i in fixedFields.indices) {
264+
ok = ok && this.fixedFields[i].unsafeCopyValueFrom(other.fixedFields[i])
265+
}
282266

283-
override fun unsafeSetFromStringValue(gene: Gene): Boolean {
284-
if (gene is ObjectGene
285-
&& (fixedFields.indices).all { fixedFields[it].possiblySame(gene.fixedFields[it]) }
286-
&& isFixed == gene.isFixed
287-
&& (isFixed || template!!.possiblySame(gene.template!!))) {
288-
289-
var result = true
290-
(fixedFields.indices).forEach {
291-
val r = fixedFields[it].unsafeSetFromStringValue(gene.fixedFields[it])
292-
if (!r)
293-
LoggingUtil.uniqueWarn(log, "cannot bind the field ${fixedFields[it].name}")
294-
result = result && r
295-
}
296-
if(!isFixed){
297-
(additionalFields!!.indices).forEach {
298-
val r = additionalFields!![it].unsafeSetFromStringValue(gene.additionalFields!![it])
299-
if (!r)
300-
LoggingUtil.uniqueWarn(log, "cannot bind the field ${additionalFields!![it].name}")
301-
result = result && r
302-
}
267+
if(!isFixed){
268+
//TODO what if there is a mismatch here? semantic of this function is unclear
269+
for (i in additionalFields!!.indices){
270+
ok = ok && this.additionalFields!![i].unsafeCopyValueFrom(other.additionalFields!![i])
303271
}
304-
if (!result)
305-
LoggingUtil.uniqueWarn(log, "fail to fully bind field values with the ObjectGene")
306-
307-
return result
308272
}
309273

310-
LoggingUtil.uniqueWarn(log, "cannot bind the ${this::class.java.simpleName} with ${gene::class.java.simpleName}")
311-
return false
274+
return ok
312275
}
313276

277+
278+
314279
override fun adaptiveSelectSubsetToMutate(randomness: Randomness, internalGenes: List<Gene>, mwc: MutationWeightControl, additionalGeneMutationInfo: AdditionalGeneMutationInfo): List<Pair<Gene, AdditionalGeneMutationInfo?>> {
315280

316281
if (additionalGeneMutationInfo.impact != null

core/src/main/kotlin/org/evomaster/core/search/gene/SeededGene.kt

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -112,39 +112,18 @@ class SeededGene<T>(
112112

113113
override fun unsafeCopyValueFrom(other: Gene): Boolean {
114114
if (other !is SeededGene<*>)
115-
throw IllegalArgumentException("Invalid gene ${other::class.java}")
116-
117-
return updateValueOnlyIfValid(
118-
{
119-
val ok = if (employSeeded)
120-
this.seeded.unsafeCopyValueFrom(other.seeded)
121-
else
122-
this.gene.unsafeCopyValueFrom(other.gene as Gene)
123-
124-
if (ok){
125-
this.employSeeded = other.employSeeded
126-
this.isEmploySeededMutable = other.isEmploySeededMutable
127-
}
128-
ok
129-
}, false
130-
)
131-
}
115+
return false
132116

133-
override fun unsafeSetFromStringValue(gene: Gene): Boolean {
134-
// only allow bind value for gene
135-
if (gene is SeededGene<*> && isEmploySeededMutable){
136-
employSeeded = gene.employSeeded
137-
if (!employSeeded)
138-
return ParamUtil.getValueGene(this.gene).unsafeSetFromStringValue(ParamUtil.getValueGene(gene.gene as Gene))
139-
else
140-
return seeded.unsafeSetFromStringValue(gene.seeded)
141-
}
117+
val ok = if (employSeeded)
118+
this.seeded.unsafeCopyValueFrom(other.seeded)
119+
else
120+
this.gene.unsafeCopyValueFrom(other.gene as Gene)
142121

143-
if (gene !is SeededGene<*> && !employSeeded){
144-
return ParamUtil.getValueGene(this.gene).unsafeSetFromStringValue(ParamUtil.getValueGene(gene))
122+
if (ok){
123+
this.employSeeded = other.employSeeded
124+
this.isEmploySeededMutable = other.isEmploySeededMutable
145125
}
146-
147-
return false
126+
return ok
148127
}
149128

150129
override fun adaptiveSelectSubsetToMutate(randomness: Randomness, internalGenes: List<Gene>, mwc: MutationWeightControl, additionalGeneMutationInfo: AdditionalGeneMutationInfo): List<Pair<Gene, AdditionalGeneMutationInfo?>> {

core/src/main/kotlin/org/evomaster/core/search/gene/UUIDGene.kt

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -88,27 +88,13 @@ class UUIDGene(
8888
}
8989

9090
override fun unsafeCopyValueFrom(other: Gene): Boolean {
91-
if (other !is UUIDGene) {
92-
throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
93-
}
94-
return updateValueOnlyIfValid(
95-
{this.mostSigBits.unsafeCopyValueFrom(other.mostSigBits) && this.leastSigBits.unsafeCopyValueFrom(other.leastSigBits)}, true
96-
)
97-
}
9891

99-
override fun unsafeSetFromStringValue(gene: Gene): Boolean {
100-
return when{
101-
gene is UUIDGene ->{
102-
mostSigBits.unsafeSetFromStringValue(gene.mostSigBits) && leastSigBits.unsafeSetFromStringValue(gene.leastSigBits)
103-
}
104-
gene is StringGene && gene.getSpecializationGene() != null ->{
105-
unsafeSetFromStringValue(gene.getSpecializationGene()!!)
106-
}
107-
else->{
108-
LoggingUtil.uniqueWarn(log, "cannot bind UUIDGene with ${gene::class.java.simpleName}")
109-
false
110-
}
92+
val gene = other.getPhenotype()
93+
94+
if (gene !is UUIDGene) {
95+
throw IllegalArgumentException("Invalid gene type ${gene.javaClass}")
11196
}
97+
return this.mostSigBits.unsafeCopyValueFrom(gene.mostSigBits)
98+
&& this.leastSigBits.unsafeCopyValueFrom(gene.leastSigBits)
11299
}
113-
114100
}

core/src/main/kotlin/org/evomaster/core/search/gene/wrapper/WrapperGene.kt renamed to core/src/main/kotlin/org/evomaster/core/search/gene/interfaces/WrapperGene.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.evomaster.core.search.gene.wrapper
1+
package org.evomaster.core.search.gene.interfaces
22

33
/**
44
* Some genes are wrappers to provide extra functionality and control on how genes are sampled, mutated and

core/src/main/kotlin/org/evomaster/core/search/gene/sql/SqlAutoIncrementGene.kt

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,8 @@ class SqlAutoIncrementGene(name: String) : SimpleGene(name) {
3636
* Man: need to check with Andrea, copyValueFrom of [ImmutableDataHolderGene] throw an exception
3737
*/
3838
override fun unsafeCopyValueFrom(other: Gene): Boolean {
39-
if (other !is SqlAutoIncrementGene) {
40-
throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
41-
}
4239
//do nothing
43-
return true
44-
}
45-
46-
override fun unsafeSetFromStringValue(gene: Gene): Boolean {
47-
// do nothing, cannot bind with others
48-
return true
40+
return containsSameValueAs(other)
4941
}
5042

5143
/**
@@ -55,7 +47,7 @@ class SqlAutoIncrementGene(name: String) : SimpleGene(name) {
5547
*/
5648
override fun containsSameValueAs(other: Gene): Boolean {
5749
if (other !is SqlAutoIncrementGene) {
58-
throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
50+
return false
5951
}
6052
return this === other
6153
}

core/src/main/kotlin/org/evomaster/core/search/gene/sql/SqlBinaryStringGene.kt

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,11 @@ class SqlBinaryStringGene(
7878

7979
override fun unsafeCopyValueFrom(other: Gene): Boolean {
8080
if (other !is SqlBinaryStringGene) {
81-
throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
81+
return false
8282
}
83-
return updateValueOnlyIfValid(
84-
{binaryArrayGene.unsafeCopyValueFrom(other.binaryArrayGene)}, false
85-
)
83+
return binaryArrayGene.unsafeCopyValueFrom(other.binaryArrayGene)
8684
}
8785

88-
override fun unsafeSetFromStringValue(gene: Gene): Boolean {
89-
if (gene is SqlBinaryStringGene) {
90-
return binaryArrayGene.unsafeSetFromStringValue(gene.binaryArrayGene)
91-
}
92-
LoggingUtil.uniqueWarn(log, "cannot bind SqlBitstringGene with ${gene::class.java.simpleName}")
93-
return false
94-
}
9586

9687
override fun copyContent() = SqlBinaryStringGene(name,
9788
minSize = minSize,

core/src/main/kotlin/org/evomaster/core/search/gene/sql/SqlBitStringGene.kt

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,11 @@ class SqlBitStringGene(
6868

6969
override fun unsafeCopyValueFrom(other: Gene): Boolean {
7070
if (other !is SqlBitStringGene) {
71-
throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
71+
return false
7272
}
73-
return updateValueOnlyIfValid(
74-
{booleanArrayGene.unsafeCopyValueFrom(other.booleanArrayGene)}, false
75-
)
73+
return booleanArrayGene.unsafeCopyValueFrom(other.booleanArrayGene)
7674
}
7775

78-
override fun unsafeSetFromStringValue(gene: Gene): Boolean {
79-
if (gene is SqlBitStringGene) {
80-
return booleanArrayGene.unsafeSetFromStringValue(gene.booleanArrayGene)
81-
}
82-
LoggingUtil.uniqueWarn(log, "cannot bind SqlBitstringGene with ${gene::class.java.simpleName}")
83-
return false
84-
}
85-
86-
87-
8876
override fun copyContent() = SqlBitStringGene(name, minSize = minSize, maxSize = maxSize, booleanArrayGene.copy() as ArrayGene<BooleanGene>)
8977

9078
override fun customShouldApplyShallowMutation(

core/src/main/kotlin/org/evomaster/core/search/gene/sql/SqlCompositeGene.kt

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -78,39 +78,14 @@ class SqlCompositeGene(
7878

7979
override fun unsafeCopyValueFrom(other: Gene): Boolean {
8080
if (other !is SqlCompositeGene) {
81-
throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
81+
return false
8282
}
8383

84-
return updateValueOnlyIfValid(
85-
{
86-
var ok = true
87-
for (i in fields.indices) {
88-
ok = ok && this.fields[i].unsafeCopyValueFrom(other.fields[i])
89-
}
90-
ok
91-
}, true
92-
)
93-
}
94-
95-
96-
97-
98-
override fun unsafeSetFromStringValue(gene: Gene): Boolean {
99-
if (gene is SqlCompositeGene && (fields.indices).all { fields[it].possiblySame(gene.fields[it]) }) {
100-
var result = true
101-
(fields.indices).forEach {
102-
val r = fields[it].unsafeSetFromStringValue(gene.fields[it])
103-
if (!r)
104-
LoggingUtil.uniqueWarn(log, "cannot bind the field ${fields[it].name}")
105-
result = result && r
106-
}
107-
if (!result)
108-
LoggingUtil.uniqueWarn(log, "cannot bind the ${this::class.java.simpleName} (with the refType ${compositeTypeName ?: "null"}) with the object gene (with the refType ${gene.compositeTypeName ?: "null"})")
109-
return result
84+
var ok = true
85+
for (i in fields.indices) {
86+
ok = ok && this.fields[i].unsafeCopyValueFrom(other.fields[i])
11087
}
111-
// might be cycle object gene
112-
LoggingUtil.uniqueWarn(log, "cannot bind the ${this::class.java.simpleName} (with the refType ${compositeTypeName ?: "null"}) with ${gene::class.java.simpleName}")
113-
return false
88+
return ok
11489
}
11590

11691
override fun copyContent() = SqlCompositeGene(this.name, fields.map { it.copy() }.toList(), this.compositeTypeName)

0 commit comments

Comments
 (0)