Skip to content

Commit 7aaa230

Browse files
committed
moving copyValueFrom close to setValueBasedOn before refactoring
1 parent 496c0bc commit 7aaa230

59 files changed

Lines changed: 665 additions & 615 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ class BooleanGene(
6262
return value.toString()
6363
}
6464

65+
override fun containsSameValueAs(other: Gene): Boolean {
66+
if (other !is BooleanGene) {
67+
throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
68+
}
69+
return this.value == other.value
70+
}
71+
72+
6573
override fun copyValueFrom(other: Gene): Boolean {
6674
if (other !is BooleanGene) {
6775
throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
@@ -76,14 +84,6 @@ class BooleanGene(
7684
return true
7785
}
7886

79-
override fun containsSameValueAs(other: Gene): Boolean {
80-
if (other !is BooleanGene) {
81-
throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
82-
}
83-
return this.value == other.value
84-
}
85-
86-
8787
override fun setValueBasedOn(gene: Gene): Boolean {
8888
if (gene is SeededGene<*>){
8989
return this.setValueBasedOn(gene.getPhenotype()as Gene)

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

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -189,41 +189,7 @@ class ObjectGene(
189189
return additionalFields!!.any { it.first.value == fieldToAdd.first.value}
190190
}
191191

192-
override fun copyValueFrom(other: Gene): Boolean {
193-
if (other !is ObjectGene) {
194-
throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
195-
}
196-
197-
if (other.isFixed != isFixed)
198-
throw IllegalArgumentException("cannot copy value for ObjectGene if their isFixed is different")
199-
200-
if (!isFixed && !template!!.possiblySame(other.template!!))
201-
throw IllegalArgumentException("different template ${other.template.javaClass}")
202192

203-
//TODO what if they have a different number of fields, or name not match???
204-
// semantic of this function is unclear, really need TODO refactoring
205-
206-
val updateOk = updateValueOnlyIfValid(
207-
{
208-
var ok = true
209-
210-
for (i in fixedFields.indices) {
211-
ok = ok && this.fixedFields[i].copyValueFrom(other.fixedFields[i])
212-
}
213-
214-
if(!isFixed){
215-
//TODO what if there is a mismatch here? semantic of this function is unclear
216-
for (i in additionalFields!!.indices){
217-
ok = ok && this.additionalFields!![i].copyValueFrom(other.additionalFields!![i])
218-
}
219-
}
220-
221-
ok
222-
}, true
223-
)
224-
225-
return updateOk
226-
}
227193

228194

229195
/**
@@ -278,6 +244,42 @@ class ObjectGene(
278244
)
279245
}
280246

247+
override fun copyValueFrom(other: Gene): Boolean {
248+
if (other !is ObjectGene) {
249+
throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
250+
}
251+
252+
if (other.isFixed != isFixed)
253+
throw IllegalArgumentException("cannot copy value for ObjectGene if their isFixed is different")
254+
255+
if (!isFixed && !template!!.possiblySame(other.template!!))
256+
throw IllegalArgumentException("different template ${other.template.javaClass}")
257+
258+
//TODO what if they have a different number of fields, or name not match???
259+
// semantic of this function is unclear, really need TODO refactoring
260+
261+
val updateOk = updateValueOnlyIfValid(
262+
{
263+
var ok = true
264+
265+
for (i in fixedFields.indices) {
266+
ok = ok && this.fixedFields[i].copyValueFrom(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].copyValueFrom(other.additionalFields!![i])
273+
}
274+
}
275+
276+
ok
277+
}, true
278+
)
279+
280+
return updateOk
281+
}
282+
281283
override fun setValueBasedOn(gene: Gene): Boolean {
282284
if (gene is ObjectGene
283285
&& (fixedFields.indices).all { fixedFields[it].possiblySame(gene.fixedFields[it]) }

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,21 @@ class SeededGene<T>(
9595
gene.getValueAsPrintableString(mode = mode, targetFormat = targetFormat)
9696
}
9797

98+
99+
100+
override fun containsSameValueAs(other: Gene): Boolean {
101+
if (other !is SeededGene<*>)
102+
throw IllegalArgumentException("Invalid gene ${other::class.java}")
103+
return this.employSeeded == other.employSeeded
104+
&& (if (employSeeded) this.seeded.containsSameValueAs(other.seeded)
105+
else this.gene.containsSameValueAs(other.gene as Gene))
106+
}
107+
108+
109+
110+
override fun possiblySame(gene : Gene) : Boolean =
111+
super.possiblySame(gene) && this.gene.possiblySame((gene as SeededGene<*>).gene as Gene)
112+
98113
override fun copyValueFrom(other: Gene): Boolean {
99114
if (other !is SeededGene<*>)
100115
throw IllegalArgumentException("Invalid gene ${other::class.java}")
@@ -115,19 +130,6 @@ class SeededGene<T>(
115130
)
116131
}
117132

118-
override fun containsSameValueAs(other: Gene): Boolean {
119-
if (other !is SeededGene<*>)
120-
throw IllegalArgumentException("Invalid gene ${other::class.java}")
121-
return this.employSeeded == other.employSeeded
122-
&& (if (employSeeded) this.seeded.containsSameValueAs(other.seeded)
123-
else this.gene.containsSameValueAs(other.gene as Gene))
124-
}
125-
126-
127-
128-
override fun possiblySame(gene : Gene) : Boolean =
129-
super.possiblySame(gene) && this.gene.possiblySame((gene as SeededGene<*>).gene as Gene)
130-
131133
override fun setValueBasedOn(gene: Gene): Boolean {
132134
// only allow bind value for gene
133135
if (gene is SeededGene<*> && isEmploySeededMutable){

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,25 +79,23 @@ class UUIDGene(
7979

8080
fun getValueAsUUID(): UUID = UUID(mostSigBits.value, leastSigBits.value)
8181

82-
override fun copyValueFrom(other: Gene): Boolean {
82+
override fun containsSameValueAs(other: Gene): Boolean {
8383
if (other !is UUIDGene) {
8484
throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
8585
}
86-
return updateValueOnlyIfValid(
87-
{this.mostSigBits.copyValueFrom(other.mostSigBits) && this.leastSigBits.copyValueFrom(other.leastSigBits)}, true
88-
)
86+
return this.mostSigBits.containsSameValueAs(other.mostSigBits)
87+
&& this.leastSigBits.containsSameValueAs(other.leastSigBits)
8988
}
9089

91-
override fun containsSameValueAs(other: Gene): Boolean {
90+
override fun copyValueFrom(other: Gene): Boolean {
9291
if (other !is UUIDGene) {
9392
throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
9493
}
95-
return this.mostSigBits.containsSameValueAs(other.mostSigBits)
96-
&& this.leastSigBits.containsSameValueAs(other.leastSigBits)
94+
return updateValueOnlyIfValid(
95+
{this.mostSigBits.copyValueFrom(other.mostSigBits) && this.leastSigBits.copyValueFrom(other.leastSigBits)}, true
96+
)
9797
}
9898

99-
100-
10199
override fun setValueBasedOn(gene: Gene): Boolean {
102100
return when{
103101
gene is UUIDGene ->{

core/src/main/kotlin/org/evomaster/core/search/gene/collection/ArrayGene.kt

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -133,26 +133,7 @@ class ArrayGene<T>(
133133
return copy
134134
}
135135

136-
override fun copyValueFrom(other: Gene): Boolean {
137-
if (other !is ArrayGene<*>) {
138-
throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
139-
}
140-
141-
if (this.template::class.simpleName != other.template::class.simpleName) return false
142136

143-
return updateValueOnlyIfValid(
144-
{
145-
killAllChildren()
146-
// check maxSize
147-
val elements = (if(maxSize!= null && other.elements.size > maxSize!!)
148-
other.elements.subList(0, maxSize!!) else other.elements).map { e -> e.copy() as T }.toMutableList()
149-
// build parents for [element]
150-
addChildren(elements)
151-
true
152-
},
153-
false
154-
)
155-
}
156137

157138
override fun containsSameValueAs(other: Gene): Boolean {
158139
if (other !is ArrayGene<*>) {
@@ -296,6 +277,27 @@ class ArrayGene<T>(
296277
return false
297278
}
298279

280+
override fun copyValueFrom(other: Gene): Boolean {
281+
if (other !is ArrayGene<*>) {
282+
throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
283+
}
284+
285+
if (this.template::class.simpleName != other.template::class.simpleName) return false
286+
287+
return updateValueOnlyIfValid(
288+
{
289+
killAllChildren()
290+
// check maxSize
291+
val elements = (if(maxSize!= null && other.elements.size > maxSize!!)
292+
other.elements.subList(0, maxSize!!) else other.elements).map { e -> e.copy() as T }.toMutableList()
293+
// build parents for [element]
294+
addChildren(elements)
295+
true
296+
},
297+
false
298+
)
299+
}
300+
299301
@Deprecated("Do not call directly outside this package. Call setFromStringValue")
300302
/**
301303
* To set the Array children from a String.

core/src/main/kotlin/org/evomaster/core/search/gene/collection/EnumGene.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,15 @@ class EnumGene<T : Comparable<T>>(
197197
return valueNames?.get(index)
198198
}
199199

200+
201+
override fun containsSameValueAs(other: Gene): Boolean {
202+
if (other !is EnumGene<*>) {
203+
throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
204+
}
205+
//FIXME what if compared to another enum with different values???
206+
return this.index == other.index
207+
}
208+
200209
override fun copyValueFrom(other: Gene): Boolean {
201210
if (other !is EnumGene<*>) {
202211
throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
@@ -211,15 +220,6 @@ class EnumGene<T : Comparable<T>>(
211220
return true
212221
}
213222

214-
override fun containsSameValueAs(other: Gene): Boolean {
215-
if (other !is EnumGene<*>) {
216-
throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
217-
}
218-
//FIXME what if compared to another enum with different values???
219-
return this.index == other.index
220-
}
221-
222-
223223
override fun setValueBasedOn(gene: Gene): Boolean {
224224
when {
225225
gene is EnumGene<*> -> index == gene.index

core/src/main/kotlin/org/evomaster/core/search/gene/collection/FixedMapGene.kt

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,7 @@ class FixedMapGene<K, V>(
3939
)
4040
}
4141

42-
override fun copyValueFrom(other: Gene): Boolean {
43-
if (other !is FixedMapGene<*, *>) {
44-
throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
45-
}
4642

47-
return updateValueOnlyIfValid({
48-
killAllChildren()
49-
// maxSize
50-
val copy = (if (maxSize!=null && other.elements.size > maxSize!!)
51-
other.elements.subList(0, maxSize!!)
52-
else other.elements)
53-
.map { e -> e.copy() as PairGene<K, V> }
54-
.toMutableList()
55-
addChildren(copy)
56-
true
57-
},false)
58-
}
5943

6044
override fun containsSameValueAs(other: Gene): Boolean {
6145
if (other !is FixedMapGene<*, *>) {
@@ -89,4 +73,22 @@ class FixedMapGene<K, V>(
8973
)
9074
return false
9175
}
76+
77+
override fun copyValueFrom(other: Gene): Boolean {
78+
if (other !is FixedMapGene<*, *>) {
79+
throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
80+
}
81+
82+
return updateValueOnlyIfValid({
83+
killAllChildren()
84+
// maxSize
85+
val copy = (if (maxSize!=null && other.elements.size > maxSize!!)
86+
other.elements.subList(0, maxSize!!)
87+
else other.elements)
88+
.map { e -> e.copy() as PairGene<K, V> }
89+
.toMutableList()
90+
addChildren(copy)
91+
true
92+
},false)
93+
}
9294
}

core/src/main/kotlin/org/evomaster/core/search/gene/collection/FlexibleMapGene.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,7 @@ where T : Gene {
3737
)
3838
}
3939

40-
override fun copyValueFrom(other: Gene): Boolean {
41-
//TODO
4240

43-
return false
44-
}
4541

4642
override fun containsSameValueAs(other: Gene): Boolean {
4743
if (other !is FlexibleMapGene<*>) {
@@ -58,6 +54,12 @@ where T : Gene {
5854
return false
5955
}
6056

57+
override fun copyValueFrom(other: Gene): Boolean {
58+
//TODO
59+
60+
return false
61+
}
62+
6163
override fun isPrintable(): Boolean {
6264
return elements.all { it.isPrintable() }
6365
}

core/src/main/kotlin/org/evomaster/core/search/gene/collection/PairGene.kt

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,7 @@ class PairGene<F,S>(
5959

6060

6161

62-
override fun copyValueFrom(other: Gene): Boolean {
63-
if (other !is PairGene<*, *>) {
64-
throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
65-
}
66-
return updateValueOnlyIfValid(
67-
{
68-
first.copyValueFrom(other.first) && second.copyValueFrom(other.second)
69-
}, true
70-
)
71-
}
62+
7263

7364
override fun containsSameValueAs(other: Gene): Boolean {
7465
if (other !is PairGene<*, *>) {
@@ -85,6 +76,16 @@ class PairGene<F,S>(
8576
return first.setValueBasedOn(gene.first) && second.setValueBasedOn(gene.second)
8677
}
8778

79+
override fun copyValueFrom(other: Gene): Boolean {
80+
if (other !is PairGene<*, *>) {
81+
throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
82+
}
83+
return updateValueOnlyIfValid(
84+
{
85+
first.copyValueFrom(other.first) && second.copyValueFrom(other.second)
86+
}, true
87+
)
88+
}
8889

8990
override fun copyContent(): Gene {
9091
return PairGene(name, first.copy(), second.copy(), allowedToMutateFirst)

0 commit comments

Comments
 (0)