Skip to content

Commit 2d4aa5c

Browse files
committed
keeping on with refactoring
1 parent a41f6cf commit 2d4aa5c

12 files changed

Lines changed: 40 additions & 178 deletions

File tree

core/src/main/kotlin/org/evomaster/core/search/gene/numeric/BigDecimalGene.kt

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -245,23 +245,11 @@ class BigDecimalGene(
245245

246246

247247
override fun unsafeCopyValueFrom(other: Gene): Boolean {
248-
if (other !is BigDecimalGene)
249-
throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
250-
// since bigdecimal is immutable, just refer to the value of other gene
251-
val current = this.value
252-
this.value = other.value
253-
if (!isLocallyValid()){
254-
this.value = current
255-
return false
256-
}
257248

258-
return true
259-
}
249+
val gene = other.getPhenotype()
260250

261-
override fun unsafeSetFromStringValue(gene: Gene): Boolean {
262251
val bd = when(gene){
263-
is SeededGene<*> -> return this.unsafeSetFromStringValue(gene.getPhenotype() as Gene)
264-
is NumericStringGene -> return this.unsafeSetFromStringValue(gene.number)
252+
is NumericStringGene -> return this.unsafeCopyValueFrom(gene.number)
265253
is LongGene -> BigDecimal(gene.value)
266254
is FloatGene -> BigDecimal(gene.value.toDouble())
267255
is IntegerGene -> BigDecimal(gene.value)
@@ -435,4 +423,4 @@ class BigDecimalGene(
435423
}
436424

437425
override fun getZero(): BigDecimal = BigDecimal.ZERO
438-
}
426+
}

core/src/main/kotlin/org/evomaster/core/search/gene/numeric/BigIntegerGene.kt

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -137,25 +137,11 @@ class BigIntegerGene(
137137

138138

139139
override fun unsafeCopyValueFrom(other: Gene): Boolean {
140-
if (other !is BigIntegerGene) {
141-
throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
142-
}
143-
//BigInteger is immutable, just refer to the value of other gene
144-
val current = this.value
145-
this.value = other.value
146-
if (!isLocallyValid()){
147-
this.value = current
148-
return false
149-
}
150-
151-
return true
152-
}
153140

141+
val gene = other.getPhenotype()
154142

155-
override fun unsafeSetFromStringValue(gene: Gene): Boolean {
156143
when(gene){
157-
is SeededGene<*> -> return this.unsafeSetFromStringValue(gene.getPhenotype() as Gene)
158-
is NumericStringGene -> return this.unsafeSetFromStringValue(gene.number)
144+
is NumericStringGene -> return this.unsafeCopyValueFrom(gene.number)
159145
is LongGene -> setValueWithLong(gene.value)
160146
is FloatGene -> setValueWithLong(gene.value.toLong())
161147
is IntegerGene -> setValueWithLong(gene.value.toLong())
@@ -251,4 +237,4 @@ class BigIntegerGene(
251237

252238
override fun getZero(): BigInteger = BigInteger.ZERO
253239

254-
}
240+
}

core/src/main/kotlin/org/evomaster/core/search/gene/numeric/DoubleGene.kt

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -94,45 +94,21 @@ class DoubleGene(name: String,
9494
}
9595

9696
override fun unsafeCopyValueFrom(other: Gene): Boolean {
97-
if (other !is DoubleGene) {
98-
throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
99-
}
100-
val current = this.value
101-
this.value = other.value
102-
if (!isLocallyValid()){
103-
this.value = current
104-
return false
105-
}
10697

107-
return true
108-
}
98+
val gene = other.getPhenotype()
10999

110-
override fun unsafeSetFromStringValue(gene: Gene) : Boolean{
111100
when(gene){
112101
is DoubleGene -> value = gene.value
113102
is FloatGene -> value = gene.value.toDouble()
114103
is IntegerGene -> value = gene.value.toDouble()
115104
is LongGene -> value = gene.value.toDouble()
116105
is BigDecimalGene -> value = try { gene.value.toDouble() } catch (e: Exception) { return false }
117106
is BigIntegerGene -> value = try { gene.value.toDouble() } catch (e: Exception) { return false }
118-
is StringGene -> {
119-
value = gene.value.toDoubleOrNull() ?: return false
120-
}
121-
is Base64StringGene ->{
122-
value = gene.data.value.toDoubleOrNull() ?: return false
123-
}
124-
is ImmutableDataHolderGene -> {
125-
value = gene.value.toDoubleOrNull() ?: return false
126-
}
127-
is SqlPrimaryKeyGene ->{
128-
value = gene.uniqueId.toDouble()
129-
}
130-
is SeededGene<*> ->{
131-
return this.unsafeSetFromStringValue(gene.getPhenotype() as Gene)
132-
}
133-
is NumericStringGene ->{
134-
return this.unsafeSetFromStringValue(gene.number)
135-
}
107+
is StringGene -> { value = gene.value.toDoubleOrNull() ?: return false }
108+
is Base64StringGene ->{ value = gene.data.value.toDoubleOrNull() ?: return false }
109+
is ImmutableDataHolderGene -> { value = gene.value.toDoubleOrNull() ?: return false }
110+
is SqlPrimaryKeyGene ->{ value = gene.uniqueId.toDouble() }
111+
is NumericStringGene ->{ return this.unsafeCopyValueFrom(gene.number) }
136112
else -> {
137113
LoggingUtil.uniqueWarn(
138114
log,
@@ -184,4 +160,4 @@ class DoubleGene(name: String,
184160
}
185161

186162
override fun getZero(): Double = 0.0
187-
}
163+
}

core/src/main/kotlin/org/evomaster/core/search/gene/numeric/FloatGene.kt

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -93,20 +93,9 @@ class FloatGene(name: String,
9393
}
9494

9595
override fun unsafeCopyValueFrom(other: Gene): Boolean {
96-
if (other !is FloatGene) {
97-
throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
98-
}
99-
val current = this.value
100-
this.value = other.value
101-
if (!isLocallyValid()){
102-
this.value = current
103-
return false
104-
}
10596

106-
return true
107-
}
97+
val gene = other.getPhenotype()
10898

109-
override fun unsafeSetFromStringValue(gene: Gene): Boolean {
11099
when(gene){
111100
is FloatGene -> value = gene.value
112101
is DoubleGene -> value = gene.value.toFloat()
@@ -126,11 +115,8 @@ class FloatGene(name: String,
126115
is SqlPrimaryKeyGene ->{
127116
value = gene.uniqueId.toFloat()
128117
}
129-
is SeededGene<*> ->{
130-
return this.unsafeSetFromStringValue(gene.getPhenotype() as Gene)
131-
}
132118
is NumericStringGene ->{
133-
return this.unsafeSetFromStringValue(gene.number)
119+
return this.unsafeCopyValueFrom(gene.number)
134120
}
135121
else -> {
136122
LoggingUtil.uniqueWarn(

core/src/main/kotlin/org/evomaster/core/search/gene/numeric/IntegerGene.kt

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -169,20 +169,9 @@ class IntegerGene(
169169

170170

171171
override fun unsafeCopyValueFrom(other: Gene): Boolean {
172-
if (other !is IntegerGene) {
173-
throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
174-
}
175-
val current = this.value
176-
this.value = other.value
177-
if (!isLocallyValid()){
178-
this.value = current
179-
return false
180-
}
181172

182-
return true
183-
}
173+
val gene = other.getPhenotype()
184174

185-
override fun unsafeSetFromStringValue(gene: Gene): Boolean {
186175
when (gene) {
187176
is IntegerGene -> value = gene.value
188177
is FloatGene -> value = gene.value.toInt()
@@ -202,11 +191,8 @@ class IntegerGene(
202191
is SqlPrimaryKeyGene -> {
203192
value = gene.uniqueId.toInt()
204193
}
205-
is SeededGene<*> ->{
206-
return this.unsafeSetFromStringValue(gene.getPhenotype() as Gene)
207-
}
208194
is NumericStringGene ->{
209-
return this.unsafeSetFromStringValue(gene.number)
195+
return this.unsafeCopyValueFrom(gene.number)
210196
}
211197
else -> {
212198
LoggingUtil.uniqueWarn(log, "cannot bind Integer with ${gene::class.java.simpleName}")
@@ -240,4 +226,4 @@ class IntegerGene(
240226
}
241227

242228
override fun getZero(): Int = 0
243-
}
229+
}

core/src/main/kotlin/org/evomaster/core/search/gene/numeric/LongGene.kt

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,9 @@ class LongGene(
9191
}
9292

9393
override fun unsafeCopyValueFrom(other: Gene): Boolean {
94-
if (other !is LongGene) {
95-
throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
96-
}
97-
val current = this.value
98-
this.value = other.value
99-
if (!isLocallyValid()){
100-
this.value = current
101-
return false
102-
}
10394

104-
return true
105-
}
95+
val gene = other.getPhenotype()
10696

107-
override fun unsafeSetFromStringValue(gene: Gene): Boolean {
10897
when(gene){
10998
is LongGene -> value = gene.value
11099
is FloatGene -> value = gene.value.toLong()
@@ -124,11 +113,8 @@ class LongGene(
124113
is SqlPrimaryKeyGene ->{
125114
value = gene.uniqueId
126115
}
127-
is SeededGene<*> ->{
128-
return this.unsafeSetFromStringValue(gene.getPhenotype() as Gene)
129-
}
130116
is NumericStringGene ->{
131-
return this.unsafeSetFromStringValue(gene.number)
117+
return this.unsafeCopyValueFrom(gene.number)
132118
}
133119
else -> {
134120
log.info("Do not support to bind long gene with the type: ${gene::class.java.simpleName}")

core/src/main/kotlin/org/evomaster/core/search/gene/placeholder/CycleObjectGene.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,11 @@ class CycleObjectGene(name: String) : SimpleGene(name) {
4747

4848
override fun unsafeCopyValueFrom(other: Gene): Boolean {
4949
// do nothing
50-
return true
50+
return other is CycleObjectGene
5151
}
5252

53-
override fun unsafeSetFromStringValue(gene: Gene): Boolean {
54-
return false
55-
}
5653

5754
override fun isPrintable(): Boolean {
5855
return false
5956
}
60-
}
57+
}

core/src/main/kotlin/org/evomaster/core/search/gene/placeholder/ImmutableDataHolderGene.kt

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,8 @@ class ImmutableDataHolderGene(
6666

6767

6868
override fun unsafeCopyValueFrom(other: Gene): Boolean {
69-
throw IllegalStateException("Not supposed to modify an immutable gene")
70-
}
71-
72-
override fun unsafeSetFromStringValue(gene: Gene): Boolean {
73-
// do nothing
74-
return true
69+
//do nothing
70+
return containsSameValueAs(other)
7571
}
7672

77-
}
73+
}

core/src/main/kotlin/org/evomaster/core/search/gene/placeholder/LimitObjectGene.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,8 @@ class LimitObjectGene(name: String) : SimpleGene(name) {
4343
}
4444

4545
override fun unsafeCopyValueFrom(other: Gene): Boolean {
46-
return true
46+
//do nothing
47+
return containsSameValueAs(other)
4748
}
4849

49-
override fun unsafeSetFromStringValue(gene: Gene): Boolean {
50-
return false
51-
}
52-
}
50+
}

core/src/main/kotlin/org/evomaster/core/search/gene/regex/AnyCharacterRxGene.kt

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -69,34 +69,19 @@ class AnyCharacterRxGene : RxAtom, SimpleGene("."){
6969
}
7070

7171
override fun unsafeCopyValueFrom(other: Gene): Boolean {
72-
if (other !is AnyCharacterRxGene) {
73-
throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
74-
}
75-
val current = this.value
76-
this.value = other.value
77-
if (!isLocallyValid()){
78-
this.value = current
79-
return false
80-
}
8172

82-
return true
83-
}
73+
val gene = other.getPhenotype()
8474

85-
override fun unsafeSetFromStringValue(gene: Gene): Boolean {
8675
when(gene){
87-
is AnyCharacterRxGene -> {
88-
value = gene.value
89-
}
76+
is AnyCharacterRxGene -> { value = gene.value }
9077
is IntegerGene -> value = gene.value.toChar()
91-
is DoubleGene -> value = gene.value.toChar()
92-
is FloatGene -> value = gene.value.toChar()
93-
is LongGene -> value = gene.value.toChar()
78+
is DoubleGene -> value = gene.value.toInt().toChar()
79+
is FloatGene -> value = gene.value.toInt().toChar()
80+
is LongGene -> value = gene.value.toInt().toChar()
9481
else -> {
95-
if (gene is StringGene && gene.value.length == 1)
82+
if (gene is StringGene && gene.value.length == 1) {
9683
value = gene.value.first()
97-
else if(gene is StringGene && gene.getSpecializationGene() != null){
98-
return unsafeSetFromStringValue(gene.getSpecializationGene()!!)
99-
}else{
84+
} else{
10085
LoggingUtil.uniqueWarn(log, "cannot bind AnyCharacterRxGene with ${gene::class.java.simpleName}")
10186
return false
10287
}
@@ -105,4 +90,4 @@ class AnyCharacterRxGene : RxAtom, SimpleGene("."){
10590
return true
10691
}
10792

108-
}
93+
}

0 commit comments

Comments
 (0)