Skip to content
Permalink
Browse files

creating cache for EnumGene

  • Loading branch information...
arcuri82 committed Oct 7, 2019
1 parent 02ed558 commit c0ee7eaeb15a43a66d240bb670cf9ea00f03cfd2
@@ -177,8 +177,8 @@ class DbActionGeneBuilder {
private fun handleCharColumn(column: Column): Gene {
// TODO How to discover if it is a char or a char[] of 255 elements?
return if (column.enumValuesAsStrings != null) {
Companion.checkNotEmpty(column.enumValuesAsStrings)
EnumGene(name = column.name, values = column.enumValuesAsStrings)
checkNotEmpty(column.enumValuesAsStrings)
EnumGene(name = column.name, data = column.enumValuesAsStrings)
} else {
StringGene(name = column.name, value = "f", minLength = 0, maxLength = 1)
}
@@ -187,8 +187,8 @@ class DbActionGeneBuilder {
private fun handleDoubleColumn(column: Column): Gene {
// TODO How to discover if the source field is a float/Float field?
return if (column.enumValuesAsStrings != null) {
Companion.checkNotEmpty(column.enumValuesAsStrings)
EnumGene(name = column.name, values = column.enumValuesAsStrings.map { it.toDouble() })
checkNotEmpty(column.enumValuesAsStrings)
EnumGene(name = column.name, data = column.enumValuesAsStrings.map { it.toDouble() })
} else {
DoubleGene(column.name)
}
@@ -227,7 +227,7 @@ class DbActionGeneBuilder {
if (column.enumValuesAsStrings.isEmpty()) {
throw IllegalArgumentException("the list of enumerated values cannot be empty")
} else {
EnumGene(name = column.name, values = column.enumValuesAsStrings)
EnumGene(name = column.name, data = column.enumValuesAsStrings)
}
} else {
if (column.similarToPatterns != null && column.similarToPatterns.isNotEmpty()) {
@@ -316,8 +316,8 @@ class DbActionGeneBuilder {

private fun handleCLOBColumn(column: Column): Gene {
return if (column.enumValuesAsStrings != null) {
Companion.checkNotEmpty(column.enumValuesAsStrings)
EnumGene(name = column.name, values = column.enumValuesAsStrings)
checkNotEmpty(column.enumValuesAsStrings)
EnumGene(name = column.name, data = column.enumValuesAsStrings)

} else {
StringGene(name = column.name, minLength = 0, maxLength = column.size)
@@ -326,8 +326,8 @@ class DbActionGeneBuilder {

private fun handleBLOBColumn(column: Column): Gene {
return if (column.enumValuesAsStrings != null) {
Companion.checkNotEmpty(column.enumValuesAsStrings)
EnumGene(name = column.name, values = column.enumValuesAsStrings)
checkNotEmpty(column.enumValuesAsStrings)
EnumGene(name = column.name, data = column.enumValuesAsStrings)
} else {
StringGene(name = column.name, minLength = 0, maxLength = 8)
}
@@ -339,8 +339,8 @@ class DbActionGeneBuilder {
* TODO How to discover if the source field is a float/Float field?
*/
return if (column.enumValuesAsStrings != null) {
Companion.checkNotEmpty(column.enumValuesAsStrings)
EnumGene(name = column.name, values = column.enumValuesAsStrings.map { it.toDouble() })
checkNotEmpty(column.enumValuesAsStrings)
EnumGene(name = column.name, data = column.enumValuesAsStrings.map { it.toDouble() })

} else {
DoubleGene(column.name)
@@ -352,16 +352,16 @@ class DbActionGeneBuilder {
* TODO: DECIMAL precision is lower than a float gene
*/
return if (column.enumValuesAsStrings != null) {
Companion.checkNotEmpty(column.enumValuesAsStrings)
EnumGene(name = column.name, values = column.enumValuesAsStrings.map { it.toFloat() })
checkNotEmpty(column.enumValuesAsStrings)
EnumGene(name = column.name, data = column.enumValuesAsStrings.map { it.toFloat() })
} else {
FloatGene(column.name)
}
}

private fun handleBooleanColumn(column: Column): Gene {
return if (column.enumValuesAsStrings != null) {
Companion.checkNotEmpty(column.enumValuesAsStrings)
checkNotEmpty(column.enumValuesAsStrings)
EnumGene(column.name, column.enumValuesAsStrings.map { it.toBoolean() })

} else {
@@ -4,17 +4,54 @@ import org.evomaster.core.output.OutputFormat
import org.evomaster.core.search.service.AdaptiveParameterControl
import org.evomaster.core.search.service.Randomness


class EnumGene<T>(
/**
* Gene in which 1 out of N constant values is chosen.
*
* Not only the type in an enumeration must be sortable, but also
* must be immutable.
* This is fine for String and numeric values.
*/
class EnumGene<T : Comparable<T>>(
name: String,
val values: List<T>,
data: Collection<T>,
var index: Int = 0
) : Gene(name) {

companion object {

/**
* WARNING: mutable static state. But as it is just a cache, it is not a problem.
* Furthermore, although the set is mutable, the lists inside are not (more specifically,
* they are read-only copies).
*/
private val cache : MutableSet<List<*>> = mutableSetOf()
}

val values : List<T>

init {
if (values.isEmpty()) {

if (data.isEmpty()) {
throw IllegalArgumentException("Empty list of values")
}

val list = data
.toSet() // we want no duplicate
.toList() // need ordering to specify index of selection, so Set would not do
.sorted() // sort, to make meaningful list comparisons
.map { if(it is String) it.intern() as T else it} //if strings, make sure to intern them

/*
we need to make sure that, if we are adding a list that has content equal to
an already present list in the cache, we only use this latter
*/
values = if(cache.contains(list)){
cache.find { it == list }!! as List<T> // equality based on content, not reference
} else {
cache.add(list)
list
}

if (index < 0 || index >= values.size) {
throw IllegalArgumentException("Invalid index: $index")
}
@@ -26,8 +63,7 @@ class EnumGene<T>(

override fun copy(): Gene {
//recall: "values" is immutable
val copy = EnumGene<T>(name, values, index)
return copy
return EnumGene(name, values, index)
}

override fun randomize(randomness: Randomness, forceNewValue: Boolean, allGenes: List<Gene>) {

0 comments on commit c0ee7ea

Please sign in to comment.
You can’t perform that action at this time.