Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

first try to fix exception related to TimeOffset impact #1146

Merged
merged 3 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,10 @@ class TimeGene(
val maps = mapOf<Gene, GeneImpact>(
hour to additionalGeneMutationInfo.impact.hourGeneImpact,
minute to additionalGeneMutationInfo.impact.minuteGeneImpact,
second to additionalGeneMutationInfo.impact.secondGeneImpact
// TODO millisecond and offset
second to additionalGeneMutationInfo.impact.secondGeneImpact,
//TODO should be handle in a correct way
millisecond to additionalGeneMutationInfo.impact.millisecondGeneImpact,
offset to additionalGeneMutationInfo.impact.offsetGeneImpact
)
return mwc.selectSubGene(
internalGenes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import org.evomaster.core.search.gene.collection.EnumGene
import org.evomaster.core.search.gene.optional.ChoiceGene
import org.evomaster.core.search.gene.root.CompositeFixedGene
import org.evomaster.core.search.gene.utils.GeneUtils
import org.evomaster.core.search.impact.impactinfocollection.CompositeFixedGeneImpact
import org.evomaster.core.search.impact.impactinfocollection.GeneImpact
import org.evomaster.core.search.impact.impactinfocollection.value.date.TimeOffsetGeneImpact
import org.evomaster.core.search.service.Randomness
import org.evomaster.core.search.service.mutator.MutationWeightControl
import org.evomaster.core.search.service.mutator.genemutation.AdditionalGeneMutationInfo
import org.evomaster.core.search.service.mutator.genemutation.SubsetGeneMutationSelectionStrategy

Expand Down Expand Up @@ -86,4 +90,26 @@ class TimeOffsetGene(
return false
}

override fun adaptiveSelectSubsetToMutate(
randomness: Randomness,
internalGenes: List<Gene>,
mwc: MutationWeightControl,
additionalGeneMutationInfo: AdditionalGeneMutationInfo
): List<Pair<Gene, AdditionalGeneMutationInfo?>> {
if (additionalGeneMutationInfo.impact != null && additionalGeneMutationInfo.impact is TimeOffsetGeneImpact) {
val maps = mapOf<Gene, GeneImpact>(
type to additionalGeneMutationInfo.impact.typeImpact
)
return mwc.selectSubGene(
internalGenes,
adaptiveWeight = true,
targets = additionalGeneMutationInfo.targets,
impacts = internalGenes.map { i -> maps.getValue(i) },
individual = null,
evi = additionalGeneMutationInfo.evi
)
.map { it to additionalGeneMutationInfo.copyFoInnerGene(maps.getValue(it), it) }
}
throw IllegalArgumentException("impact is null or not TimeOffsetGeneImpact")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import org.evomaster.core.search.gene.collection.*
import org.evomaster.core.search.gene.datetime.DateGene
import org.evomaster.core.search.gene.datetime.DateTimeGene
import org.evomaster.core.search.gene.datetime.TimeGene
import org.evomaster.core.search.gene.datetime.TimeOffsetGene
import org.evomaster.core.search.gene.numeric.*
import org.evomaster.core.search.gene.optional.CustomMutationRateGene
import org.evomaster.core.search.gene.optional.OptionalGene
Expand All @@ -32,6 +33,7 @@ import org.evomaster.core.search.gene.string.NumericStringGene
import org.evomaster.core.search.gene.string.StringGene
import org.evomaster.core.search.impact.impactinfocollection.regex.*
import org.evomaster.core.search.impact.impactinfocollection.value.collection.SqlMultidimensionalArrayGeneImpact
import org.evomaster.core.search.impact.impactinfocollection.value.date.TimeOffsetGeneImpact
import org.evomaster.core.search.service.mutator.MutatedGeneSpecification
import org.slf4j.Logger
import org.slf4j.LoggerFactory
Expand Down Expand Up @@ -70,6 +72,7 @@ class ImpactUtils {
is DateGene -> DateGeneImpact(id, gene)
is DateTimeGene -> DateTimeGeneImpact(id, gene)
is TimeGene -> TimeGeneImpact(id, gene)
is TimeOffsetGene -> TimeOffsetGeneImpact(id, gene)
is SeededGene<*> -> SeededGeneImpact(id, gene)
// math
is BigDecimalGene -> BigDecimalGeneImpact(id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,28 @@ package org.evomaster.core.search.impact.impactinfocollection.value.date
import org.evomaster.core.search.gene.Gene
import org.evomaster.core.search.gene.datetime.TimeGene
import org.evomaster.core.search.impact.impactinfocollection.*
import org.evomaster.core.search.impact.impactinfocollection.value.OptionalGeneImpact
import org.evomaster.core.search.impact.impactinfocollection.value.numeric.IntegerGeneImpact

/**
* created by manzh on 2019-09-16
*/
class TimeGeneImpact(sharedImpactInfo: SharedImpactInfo, specificImpactInfo: SpecificImpactInfo,
val hourGeneImpact: IntegerGeneImpact,
val minuteGeneImpact: IntegerGeneImpact,
val secondGeneImpact : IntegerGeneImpact
class TimeGeneImpact(
sharedImpactInfo: SharedImpactInfo, specificImpactInfo: SpecificImpactInfo,
val hourGeneImpact: IntegerGeneImpact,
val minuteGeneImpact: IntegerGeneImpact,
val secondGeneImpact: IntegerGeneImpact,
val millisecondGeneImpact: OptionalGeneImpact,
val offsetGeneImpact: TimeOffsetGeneImpact
) : GeneImpact(sharedImpactInfo, specificImpactInfo){

constructor(id: String, gene : TimeGene)
: this(SharedImpactInfo(id), SpecificImpactInfo(),
hourGeneImpact = ImpactUtils.createGeneImpact(gene.hour, gene.hour.name) as? IntegerGeneImpact ?:throw IllegalStateException("IntegerGeneImpact should be created"),
minuteGeneImpact = ImpactUtils.createGeneImpact(gene.minute, gene.minute.name)as? IntegerGeneImpact ?:throw IllegalStateException("IntegerGeneImpact should be created"),
secondGeneImpact = ImpactUtils.createGeneImpact(gene.second, gene.second.name) as? IntegerGeneImpact ?:throw IllegalStateException("IntegerGeneImpact should be created")
secondGeneImpact = ImpactUtils.createGeneImpact(gene.second, gene.second.name) as? IntegerGeneImpact ?:throw IllegalStateException("IntegerGeneImpact should be created"),
millisecondGeneImpact = ImpactUtils.createGeneImpact(gene.millisecond, gene.millisecond.name) as? OptionalGeneImpact ?:throw IllegalStateException("IntegerGeneImpact should be created"),
offsetGeneImpact = ImpactUtils.createGeneImpact(gene.offset, gene.offset.name) as? TimeOffsetGeneImpact ?:throw IllegalStateException("TimeOffsetGeneImpact should be created")
)

override fun copy(): TimeGeneImpact {
Expand All @@ -27,12 +33,14 @@ class TimeGeneImpact(sharedImpactInfo: SharedImpactInfo, specificImpactInfo: Spe
specific.copy(),
hourGeneImpact = hourGeneImpact.copy(),
minuteGeneImpact = minuteGeneImpact.copy(),
secondGeneImpact = secondGeneImpact.copy())
secondGeneImpact = secondGeneImpact.copy(),
millisecondGeneImpact = millisecondGeneImpact.copy(),
offsetGeneImpact = offsetGeneImpact.copy())
}

override fun clone(): TimeGeneImpact {
return TimeGeneImpact(
shared.clone(),specific.clone(), hourGeneImpact = hourGeneImpact.clone(), minuteGeneImpact = minuteGeneImpact.clone(), secondGeneImpact = secondGeneImpact.clone()
shared.clone(),specific.clone(), hourGeneImpact = hourGeneImpact.clone(), minuteGeneImpact = minuteGeneImpact.clone(), secondGeneImpact = secondGeneImpact.clone(), millisecondGeneImpact = millisecondGeneImpact.clone(), offsetGeneImpact = offsetGeneImpact.clone()
)
}

Expand All @@ -49,7 +57,7 @@ class TimeGeneImpact(sharedImpactInfo: SharedImpactInfo, specificImpactInfo: Spe
if (gc.previous != null && gc.previous !is TimeGene)
throw IllegalStateException("gc.previous (${gc.previous::class.java.simpleName}) should be TimeGene")

val innerImpacts = mutableListOf<IntegerGeneImpact>()
val innerImpacts = mutableListOf<GeneImpact>()

if (gc.previous == null || !gc.current.hour.containsSameValueAs((gc.previous as TimeGene).hour))
innerImpacts.add(hourGeneImpact)
Expand All @@ -58,6 +66,12 @@ class TimeGeneImpact(sharedImpactInfo: SharedImpactInfo, specificImpactInfo: Spe
if (gc.previous == null || !gc.current.second.containsSameValueAs((gc.previous as TimeGene).second))
innerImpacts.add(secondGeneImpact)

if (gc.previous == null || !gc.current.millisecond.containsSameValueAs((gc.previous as TimeGene).millisecond))
innerImpacts.add(millisecondGeneImpact)

if (gc.previous == null || !gc.current.offset.containsSameValueAs((gc.previous as TimeGene).offset))
innerImpacts.add(offsetGeneImpact)

if (innerImpacts.isEmpty()) return
countImpactAndPerformance(noImpactTargets = noImpactTargets, impactTargets = impactTargets, improvedTargets = improvedTargets, onlyManipulation = onlyManipulation, num = gc.numOfMutatedGene)
innerImpacts.forEach {
Expand All @@ -70,11 +84,13 @@ class TimeGeneImpact(sharedImpactInfo: SharedImpactInfo, specificImpactInfo: Spe
return mutableMapOf(
"${getId()}-${hourGeneImpact.getId()}" to hourGeneImpact,
"${getId()}-${minuteGeneImpact.getId()}" to minuteGeneImpact,
"${getId()}-${secondGeneImpact.getId()}" to secondGeneImpact
"${getId()}-${secondGeneImpact.getId()}" to secondGeneImpact,
"${getId()}-${millisecondGeneImpact.getId()}" to millisecondGeneImpact,
"${getId()}-${offsetGeneImpact.getId()}" to offsetGeneImpact
)
}

override fun innerImpacts(): List<Impact> {
return listOf(hourGeneImpact, minuteGeneImpact, secondGeneImpact)
return listOf(hourGeneImpact, minuteGeneImpact, secondGeneImpact, millisecondGeneImpact, offsetGeneImpact)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.evomaster.core.search.impact.impactinfocollection.value.date

import org.evomaster.core.search.gene.Gene
import org.evomaster.core.search.gene.datetime.TimeGene
import org.evomaster.core.search.gene.datetime.TimeOffsetGene
import org.evomaster.core.search.gene.root.CompositeFixedGene
import org.evomaster.core.search.impact.impactinfocollection.*
import org.evomaster.core.search.impact.impactinfocollection.value.numeric.IntegerGeneImpact

//TODO should be handle in a correct way
class TimeOffsetGeneImpact(sharedImpactInfo: SharedImpactInfo, specificImpactInfo: SpecificImpactInfo,
val typeImpact: CompositeFixedGeneImpact
) : GeneImpact(sharedImpactInfo, specificImpactInfo) {

constructor(id: String, gene : TimeOffsetGene)
: this(SharedImpactInfo(id), SpecificImpactInfo(),
typeImpact = ImpactUtils.createGeneImpact(gene.type, gene.type.name) as? CompositeFixedGeneImpact ?:throw IllegalStateException("CompositeFixedGeneImpact should be created"),
)

override fun copy(): TimeOffsetGeneImpact {
return TimeOffsetGeneImpact(
shared.copy(),
specific.copy(),
typeImpact = typeImpact.copy()
)
}

override fun clone(): TimeOffsetGeneImpact {
return TimeOffsetGeneImpact(
shared.clone(),specific.clone(),
typeImpact = typeImpact.clone()
)
}

override fun validate(gene: Gene): Boolean = gene is TimeOffsetGene

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.evomaster.core.search.impact.impactinfocollection.date
import org.evomaster.core.search.gene.Gene
import org.evomaster.core.search.gene.numeric.IntegerGene
import org.evomaster.core.search.gene.datetime.TimeGene
import org.evomaster.core.search.gene.optional.OptionalGene
import org.evomaster.core.search.impact.impactinfocollection.GeneImpact
import org.evomaster.core.search.impact.impactinfocollection.GeneImpactTest
import org.evomaster.core.search.impact.impactinfocollection.ImpactOptions
Expand All @@ -16,7 +17,11 @@ import org.junit.jupiter.api.Test
class TimeGeneImpactTest : GeneImpactTest() {

override fun getGene(): Gene {
return TimeGene("d", hour = IntegerGene("h", 16), minute = IntegerGene("m", 36), second = IntegerGene("s", 9))
return TimeGene("d",
hour = IntegerGene("h", 16),
minute = IntegerGene("m", 36),
second = IntegerGene("s", 9),
millisecond = OptionalGene("millisecond", IntegerGene("millisecond", 42)))
}

override fun checkImpactType(impact: GeneImpact) {
Expand All @@ -30,6 +35,7 @@ class TimeGeneImpactTest : GeneImpactTest() {
mutationTag == 0 -> hour.value = (hour.value + 1)%24
mutationTag == 1 -> minute.value = (minute.value + 1)%60
mutationTag == 2 -> second.value = (second.value + 1)%60
mutationTag == 3 -> ((millisecond as OptionalGene).gene as IntegerGene).value = (((millisecond as OptionalGene).gene as IntegerGene).value + 1)%1000
else -> throw IllegalArgumentException("bug")
}
}
Expand All @@ -48,6 +54,8 @@ class TimeGeneImpactTest : GeneImpactTest() {
assertImpact(impact.hourGeneImpact, updatedImpact.hourGeneImpact, ImpactOptions.IMPACT_IMPROVEMENT)
assertImpact(impact.minuteGeneImpact, updatedImpact.minuteGeneImpact, ImpactOptions.NONE)
assertImpact(impact.secondGeneImpact, updatedImpact.secondGeneImpact, ImpactOptions.NONE)
assertImpact(impact.millisecondGeneImpact, updatedImpact.millisecondGeneImpact, ImpactOptions.NONE)
assertImpact(impact.offsetGeneImpact, updatedImpact.offsetGeneImpact, ImpactOptions.NONE)
}

@Test
Expand All @@ -61,6 +69,8 @@ class TimeGeneImpactTest : GeneImpactTest() {
assertImpact(impact.hourGeneImpact, updatedImpact.hourGeneImpact, ImpactOptions.NONE)
assertImpact(impact.minuteGeneImpact, updatedImpact.minuteGeneImpact, ImpactOptions.NO_IMPACT)
assertImpact(impact.secondGeneImpact, updatedImpact.secondGeneImpact, ImpactOptions.NONE)
assertImpact(impact.millisecondGeneImpact, updatedImpact.millisecondGeneImpact, ImpactOptions.NONE)
assertImpact(impact.offsetGeneImpact, updatedImpact.offsetGeneImpact, ImpactOptions.NONE)
}

@Test
Expand All @@ -74,5 +84,22 @@ class TimeGeneImpactTest : GeneImpactTest() {
assertImpact(impact.hourGeneImpact, updatedImpact.hourGeneImpact, ImpactOptions.NONE)
assertImpact(impact.minuteGeneImpact, updatedImpact.minuteGeneImpact, ImpactOptions.NONE)
assertImpact(impact.secondGeneImpact, updatedImpact.secondGeneImpact, ImpactOptions.ONLY_IMPACT)
assertImpact(impact.millisecondGeneImpact, updatedImpact.millisecondGeneImpact, ImpactOptions.NONE)
assertImpact(impact.offsetGeneImpact, updatedImpact.offsetGeneImpact, ImpactOptions.NONE)
}

@Test
fun testMillisecond(){
val gene = getGene()
val impact = initImpact(gene)
val updatedImpact = template( gene, impact, listOf(ImpactOptions.IMPACT_IMPROVEMENT), 3).second

impact as TimeGeneImpact
updatedImpact as TimeGeneImpact
assertImpact(impact.hourGeneImpact, updatedImpact.hourGeneImpact, ImpactOptions.NONE)
assertImpact(impact.minuteGeneImpact, updatedImpact.minuteGeneImpact, ImpactOptions.NONE)
assertImpact(impact.secondGeneImpact, updatedImpact.secondGeneImpact, ImpactOptions.NONE)
assertImpact(impact.millisecondGeneImpact, updatedImpact.millisecondGeneImpact, ImpactOptions.IMPACT_IMPROVEMENT)
assertImpact(impact.offsetGeneImpact, updatedImpact.offsetGeneImpact, ImpactOptions.NONE)
}
}
Loading