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

Integrate Solver with ApiWsStructureMutator #1078

Merged
merged 38 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
67b018a
Integrate Solver with ApiWsStructureMutator
agusaldasoro Sep 16, 2024
894e41c
Merge branch 'master' into feat/integrate-with-builder
agusaldasoro Sep 25, 2024
a67d1ff
Merge branch 'master' into feat/integrate-with-builder
agusaldasoro Oct 3, 2024
0bf9750
Create test to execute Z3Solver
agusaldasoro Oct 6, 2024
faddcd6
Remove target folder
agusaldasoro Oct 6, 2024
c861967
Add target to gitignore
agusaldasoro Oct 6, 2024
641bbaf
Working test
agusaldasoro Oct 9, 2024
839e252
Merge branch 'master' into feat/integrate-with-builder
agusaldasoro Oct 9, 2024
f174859
Merge branch 'master' into feat/integrate-with-builder
agusaldasoro Oct 13, 2024
885eb9e
Discard unncessary changes
agusaldasoro Oct 14, 2024
dae59cb
Format code
agusaldasoro Oct 14, 2024
8ba8ba8
Add support for more than one
agusaldasoro Oct 14, 2024
9cca01f
fix gene id
agusaldasoro Oct 14, 2024
80a102b
Fix tests
agusaldasoro Oct 14, 2024
ca2826b
Fix injection
agusaldasoro Nov 22, 2024
39f4035
Merge branch 'master' into feat/integrate-with-builder
agusaldasoro Nov 22, 2024
490ba37
Fix conflicts
agusaldasoro Nov 22, 2024
dcc29f7
Fix build
agusaldasoro Nov 25, 2024
5853681
fix test
agusaldasoro Nov 25, 2024
2485432
Fix version
agusaldasoro Nov 25, 2024
953409e
Update core/src/main/kotlin/org/evomaster/core/problem/api/service/Ap…
agusaldasoro Nov 25, 2024
3ffffc8
Merge branch 'master' into feat/integrate-with-builder
agusaldasoro Dec 14, 2024
45f6f01
Merge branch 'master' into feat/integrate-with-builder
agusaldasoro Dec 19, 2024
874722c
Test something
agusaldasoro Dec 19, 2024
a6a5dff
Update core/src/main/kotlin/org/evomaster/core/problem/api/service/Ap…
agusaldasoro Dec 19, 2024
ad59b44
Merge branch 'master' into feat/integrate-with-builder
agusaldasoro Jan 14, 2025
c728bc9
Fix deps
agusaldasoro Jan 16, 2025
256d5e7
Merge branch 'master' into feat/integrate-with-builder
agusaldasoro Jan 16, 2025
81672ee
Fix write to file error
agusaldasoro Jan 16, 2025
5879985
Add logs info
agusaldasoro Jan 17, 2025
b231482
Print error while saving file
agusaldasoro Jan 20, 2025
4eb8a31
Use another tmp folder
agusaldasoro Jan 20, 2025
616f22b
Add tests
agusaldasoro Jan 21, 2025
4871900
Merge branch 'master' into feat/integrate-with-builder
agusaldasoro Jan 21, 2025
826787c
Remove trial test
agusaldasoro Jan 21, 2025
0c914ce
Merge branch 'master' into feat/integrate-with-builder
agusaldasoro Jan 24, 2025
b32ef13
Merge branch 'master' into feat/integrate-with-builder
agusaldasoro Jan 24, 2025
72a72ce
Add javadoc
agusaldasoro Jan 24, 2025
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ Migrations/
/e2e-tests/spring-rest-mysql-column-types/target/
/e2e-tests/spring-rest-postgres-column-types/target/
/e2e-tests/spring-rest-h2-column-types/target/
/e2e-tests/spring-rest-h2-z3solver/target/
/test-old-libraries/target/
/e2e-tests/spring-web/target/
/e2e-tests/spring-rest-mongo/target/
Expand Down Expand Up @@ -166,5 +167,7 @@ Migrations/
/e2e-tests/spring-rest-bb/maven/target/
/target/
/wfc/target/

*.DS_Store
/e2e-tests/emb-json/target/
/process_data/
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ public class SqlExecutionsDto {
* Every time there is a WHERE clause which "failed" (ie, resulted in false),
* we keep track of which tables/columns where involved in determining the
* result of the WHERE.
*
* If there is no WHERE, but still no data was returned, we consider it
* as a failed WHERE
* The key is the table name and the value is the set of columns involved in the WHERE
*/
public Map<String, Set<String>> failedWhere = new HashMap<>();

Expand Down
6 changes: 4 additions & 2 deletions core/src/main/kotlin/org/evomaster/core/BaseModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import org.evomaster.core.search.service.mutator.MutationWeightControl
import org.evomaster.core.search.service.mutator.genemutation.ArchiveGeneMutator
import org.evomaster.core.search.tracer.ArchiveMutationTrackService
import org.evomaster.core.search.tracer.TrackService


import org.evomaster.core.solver.SMTLibZ3DbConstraintSolver


/**
Expand Down Expand Up @@ -87,6 +86,9 @@ class BaseModule(val args: Array<String>, val noTests: Boolean = false) : Abstra
bind(ExecutionInfoReporter::class.java)
.asEagerSingleton()

bind(SMTLibZ3DbConstraintSolver::class.java)
.asEagerSingleton()

//no longer needed if TestSuiteWriter is moved out?
// if(noTests){
// bind(TestCaseWriter::class.java)
Expand Down
2 changes: 0 additions & 2 deletions core/src/main/kotlin/org/evomaster/core/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import org.evomaster.core.AnsiColor.Companion.inRed
import org.evomaster.core.AnsiColor.Companion.inYellow
import org.evomaster.core.config.ConfigProblemException
import org.evomaster.core.logging.LoggingUtil
import org.evomaster.core.output.OutputFormat
import org.evomaster.core.output.Termination
import org.evomaster.core.output.TestSuiteSplitter
import org.evomaster.core.output.clustering.SplitResult
import org.evomaster.core.output.service.TestSuiteWriter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import org.evomaster.core.search.gene.sql.SqlPrimaryKeyGene
import org.evomaster.core.search.impact.impactinfocollection.ImpactsOfIndividual
import org.evomaster.core.search.service.mutator.MutatedGeneSpecification
import org.evomaster.core.search.service.mutator.StructureMutator
import org.evomaster.core.solver.SMTLibZ3DbConstraintSolver
import org.evomaster.core.sql.SqlAction
import org.evomaster.core.sql.SqlActionUtils
import org.evomaster.core.sql.SqlInsertBuilder
Expand All @@ -46,6 +47,12 @@ abstract class ApiWsStructureMutator : StructureMutator() {
@Inject
protected lateinit var harvestResponseHandler: HarvestActualHttpWsResponseHandler

// TODO: This should only be initialized when config.generateSqlDataWithDSE is enabled
// TODO: Also, z3solver.close() should be invoked
// when the application is shutting down to stop the Docker container and clean the tmp folder
agusaldasoro marked this conversation as resolved.
Show resolved Hide resolved
@Inject
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arcuri82 How and where should I do that?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@agusaldasoro read docs/for_developers.md, in particular the part about Guice. So, likely would need to be defined in org.evomaster.core.BaseModule

protected lateinit var z3Solver: SMTLibZ3DbConstraintSolver

override fun addAndHarvestExternalServiceActions(
individual: EvaluatedIndividual<*>,
/**
Expand Down Expand Up @@ -268,7 +275,8 @@ abstract class ApiWsStructureMutator : StructureMutator() {

val oldSqlActions = mutableListOf<EnvironmentAction>().plus(ind.seeInitializingActions())

val addedSqlInsertions = handleFailedWhereSQL(ind, fw, mutatedGenes, sampler)
val failedWhereQueries = evaluatedIndividual.fitness.getViewOfAggregatedFailedWhereQueries()
val addedSqlInsertions = handleFailedWhereSQL(ind, fw, failedWhereQueries, mutatedGenes, sampler)

ind.repairInitializationActions(randomness)
// update impact based on added genes
Expand All @@ -289,6 +297,10 @@ abstract class ApiWsStructureMutator : StructureMutator() {
* Map of FAILED WHERE clauses. from table name key to column name values
*/
fw: Map<String, Set<String>>,
/**
* List queries with FAILED WHERE clauses
*/
failedWhereQueries: List<String>,
mutatedGenes: MutatedGeneSpecification?, sampler: ApiWsSampler<T>
): MutableList<List<SqlAction>>? {

Expand All @@ -297,7 +309,7 @@ abstract class ApiWsStructureMutator : StructureMutator() {
}

if (config.generateSqlDataWithDSE) {
return handleDSE(sampler, fw)
return handleDSE(ind, sampler, failedWhereQueries)
}

return mutableListOf()
Expand Down Expand Up @@ -385,9 +397,18 @@ abstract class ApiWsStructureMutator : StructureMutator() {
return addedSqlInsertions
}

private fun <T : ApiWsIndividual> handleDSE(sampler: ApiWsSampler<T>, fw: Map<String, Set<String>>): MutableList<List<SqlAction>> {
/* TODO: DSE should be plugged in here */
return mutableListOf()
private fun <T : ApiWsIndividual> handleDSE(ind: T, sampler: ApiWsSampler<T>, failedWhereQueries: List<String>): MutableList<List<SqlAction>> {
val schemaDto = sampler.sqlInsertBuilder?.schemaDto
?: throw IllegalStateException("No DB schema is available")

val newActions = mutableListOf<List<SqlAction>>()
for (query in failedWhereQueries) {
val newActionsForQuery = z3Solver.solve(schemaDto, query)
newActions.addAll(mutableListOf(newActionsForQuery))
ind.addInitializingDbActions(actions = newActionsForQuery)
}

return newActions
}

private fun <T : ApiWsIndividual> handleFailedFind(
Expand Down
17 changes: 15 additions & 2 deletions core/src/main/kotlin/org/evomaster/core/search/FitnessValue.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,19 @@ class FitnessValue(

/**
* When SUT does SQL commands using WHERE, keep track of when those "fails" (ie evaluate
* to false), in particular the tables and columns in them involved
* to false), in particular, the tables and columns in them involved
*/
private val aggregatedFailedWhere: MutableMap<String, Set<String>> = mutableMapOf()

/**
* When SUT does SQL commands using WHERE, keep track of when those "fails" (ie evaluate
* to false), in particular, the sql query involved
*/
private val aggregatedFailedWhereQueries: MutableList<String> = mutableListOf()

/**
* When SUT does MONGO commands using FIND, keep track of when those "fails" (ie evaluate
* to false), in particular the collection and fields in them involved
* to false), in particular, the collection and fields in them involved
*/
private val aggregatedFailedFind: MutableList<MongoFailedQuery> = mutableListOf()

Expand Down Expand Up @@ -140,6 +146,11 @@ class FitnessValue(
databaseExecutions.values,
{x -> x.failedWhere}
))

aggregatedFailedWhereQueries.clear()
aggregatedFailedWhereQueries.addAll(
databaseExecutions.values.flatMap { a -> a.executionInfo }.map{ b -> b.sqlCommand }
)
}
fun aggregateMongoDatabaseData(){
aggregatedFailedFind.clear()
Expand All @@ -166,6 +177,8 @@ class FitnessValue(

fun getViewOfAggregatedFailedWhere() = aggregatedFailedWhere

fun getViewOfAggregatedFailedWhereQueries() = aggregatedFailedWhereQueries

fun getViewOfAggregatedFailedFind() = aggregatedFailedFind

fun doesCover(target: Int): Boolean {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.evomaster.core.solver

import org.evomaster.client.java.controller.api.dto.database.schema.DbInfoDto
import org.evomaster.core.sql.SqlAction

/**
Expand All @@ -12,5 +13,5 @@ interface DbConstraintSolver : AutoCloseable {
* Solves the given constraints and returns the Db Gene to insert in the database
* @return a list of SQLAction with the inserts in the db for the given constraints
*/
fun solve(sqlQuery: String): List<SqlAction>
fun solve(schemaDto: DbInfoDto, sqlQuery: String, numberOfRows: Int = 1): List<SqlAction>
agusaldasoro marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,13 @@ class SMTConditionVisitor(
return when {
operand.contains(".") -> { // Handle column references with aliases
val parts = operand.split(".")
val tableName = tableAliases[parts[0]] ?: defaultTableName
val columnName = parts[parts.lastIndex]
getColumnReference(tableName, columnName)
if (tableAliases.containsKey(parts[0])) {
val tableName = tableAliases[parts[0]] ?: defaultTableName
val columnName = parts[parts.lastIndex]
getColumnReference(tableName, columnName)
} else {
operand
}
}
isAColumn(operand) -> { // Handle direct column references
getColumnReference(defaultTableName, operand)
Expand Down
Loading
Loading