Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
fb0adca
Draft: model synthesis
Jun 20, 2022
11c47d9
m
AbdullinAM Jun 20, 2022
4aaa8e5
first prototype, needs debugging and tuning
AbdullinAM Jun 22, 2022
9c63ba0
prototype
AbdullinAM Jun 28, 2022
707fa6d
refactor
AbdullinAM Jun 29, 2022
10c4193
very early prototype
AbdullinAM Jul 8, 2022
4285a5c
UtConstraintModel implemented
AbdullinAM Jul 12, 2022
22e2266
partial UtConstraint to UtExpression convertion
AbdullinAM Jul 14, 2022
6b47a9a
working prototypes
AbdullinAM Jul 19, 2022
a75a840
bugfixes + expression support in constraints
AbdullinAM Jul 21, 2022
fb392f6
m
AbdullinAM Jul 26, 2022
5f35311
everything kind of works, but some constraints need to be added
AbdullinAM Jul 27, 2022
ba9bb62
first working protoype with arrays
AbdullinAM Aug 1, 2022
065924f
renaming
AbdullinAM Aug 1, 2022
8cc7515
first tests and fixes
AbdullinAM Aug 2, 2022
4a14606
more expressions supported
AbdullinAM Aug 3, 2022
bbf525b
some cleanup + more expressions supported
AbdullinAM Aug 8, 2022
850b530
cleanup
AbdullinAM Aug 8, 2022
8799691
support for multidimensional arrays + fix for array generation
AbdullinAM Aug 9, 2022
60df4a4
first support of lists
AbdullinAM Aug 10, 2022
8963a7e
support sets
AbdullinAM Aug 10, 2022
1578c96
small refactorings
AbdullinAM Aug 11, 2022
1dd4b25
maps
AbdullinAM Aug 12, 2022
313757d
some cleanup and parameters
AbdullinAM Aug 12, 2022
faa0047
test write fix
AbdullinAM Aug 12, 2022
94740c9
m
AbdullinAM Aug 22, 2022
9920283
Merge branch 'main' into abdullin/constraint-model-synthesis
AbdullinAM Aug 22, 2022
fd22077
merge with master
AbdullinAM Aug 24, 2022
4e97b5f
first prototype of constraint scoring selector
AbdullinAM Aug 26, 2022
2544b31
m
AbdullinAM Aug 30, 2022
e585371
fixes
AbdullinAM Aug 31, 2022
d2564e9
Merge branch 'main' into abdullin/constraint-model-synthesis
AbdullinAM Sep 7, 2022
bb1acbc
Merge branch 'main' into abdullin/constraint-model-synthesis
AbdullinAM Sep 7, 2022
c6fbeb0
Split constraint models into a set of non-intersecting subsets before…
AbdullinAM Sep 12, 2022
2553ef7
m
AbdullinAM Sep 12, 2022
e943a16
some cleanup
AbdullinAM Sep 13, 2022
cd5fff8
more cleanup
AbdullinAM Sep 13, 2022
c2a7d31
order models in the subsets
AbdullinAM Sep 13, 2022
d1b09c8
simple caching of synthesis unit contexts
AbdullinAM Sep 13, 2022
eedccb9
option to enable/disable caching of synthesis contexts
AbdullinAM Sep 14, 2022
11eea4d
m
AbdullinAM Sep 14, 2022
9712e70
tests
AbdullinAM Sep 14, 2022
1c6b679
unit tests fixed
AbdullinAM Sep 19, 2022
4e37763
Merge branch 'main' into abdullin/constraint-model-synthesis
AbdullinAM Sep 28, 2022
34ed6fe
merge with main
AbdullinAM Sep 28, 2022
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
Prev Previous commit
Next Next commit
simple caching of synthesis unit contexts
  • Loading branch information
AbdullinAM committed Sep 13, 2022
commit d1b09c8e7935518c89105a45f8200d27c4812b14
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ open class TestCaseGenerator(
return methods.map { method ->
UtMethodTestSet(
method,
minimizeExecutions(method2executions.getValue(method).toAssemble()),
minimizeExecutions(method2executions.getValue(method).toAssemble(method)),
jimpleBody(method),
method2errors.getValue(method)
)
Expand Down Expand Up @@ -384,19 +384,14 @@ open class TestCaseGenerator(
return minimizedExecutions
}

private fun List<UtExecution>.toAssemble(): List<UtExecution> =
private fun List<UtExecution>.toAssemble(method: UtMethod<*>): List<UtExecution> =
map { execution ->
val symbolicExecution = (execution as? UtSymbolicExecution) ?: return@map execution
val oldStateBefore = execution.stateBefore

val constrainedExecution = symbolicExecution.constrainedExecution ?: return@map execution
val aa = Synthesizer(this@TestCaseGenerator, constrainedExecution.modelsAfter)
val synthesizedModels = try {
aa.synthesize()
} catch (e: Throwable) {
logger.debug(e) { "Failure during constraint synthesis" }
return@map execution
}
val aa = Synthesizer(this@TestCaseGenerator, method, constrainedExecution.modelsAfter)
val synthesizedModels = aa.synthesize()

val (synthesizedThis, synthesizedParameters) = oldStateBefore.thisInstance?.let {
synthesizedModels.first() to synthesizedModels.drop(1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.utbot.framework.synthesis

import com.jetbrains.rd.util.AtomicInteger
import mu.KotlinLogging
import org.utbot.framework.PathSelectorType
import org.utbot.framework.UtSettings
Expand All @@ -14,14 +15,17 @@ class SynthesisUnitChecker(
val testCaseGenerator: TestCaseGenerator,
val declaringClass: SootClass,
) {
companion object {
private val initializerMethodId = AtomicInteger()
}

private val logger = KotlinLogging.logger("ConstrainedSynthesisUnitChecker")
private var id = 0

fun tryGenerate(synthesisUnitContext: SynthesisUnitContext, parameters: List<UtModel>): List<UtModel>? {
if (!synthesisUnitContext.isFullyDefined) return null

val synthesisMethodContext = SynthesisMethodContext(synthesisUnitContext)
val method = synthesisMethodContext.method("\$initializer_${id++}", declaringClass)
val method = synthesisMethodContext.method("\$initializer_${initializerMethodId.getAndIncrement()}", declaringClass)

val execution = run {
val executions = testCaseGenerator.generateWithPostCondition(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,21 @@ import org.utbot.framework.plugin.api.util.objectClassId

internal fun Collection<ClassId>.expandable() = filter { !it.isArray && !it.isPrimitive }.toSet()

private object SynthesisCache {
private val successfulInitializers =
mutableMapOf<UtMethod<*>, MutableMap<List<Int>, MutableList<SynthesisUnitContext>>>()

operator fun get(utMethod: UtMethod<*>, parameters: List<Int>): List<SynthesisUnitContext> =
successfulInitializers.getOrPut(utMethod, ::mutableMapOf).getOrPut(parameters, ::mutableListOf)

operator fun set(utMethod: UtMethod<*>, parameters: List<Int>, synthesisUnitContext: SynthesisUnitContext) =
successfulInitializers.getOrPut(utMethod, ::mutableMapOf).getOrPut(parameters, ::mutableListOf)
.add(synthesisUnitContext)
}

class Synthesizer(
val testCaseGenerator: TestCaseGenerator,
val method: UtMethod<*>,
val parameters: List<UtModel>,
val depth: Int = synthesisMaxDepth
) {
Expand Down Expand Up @@ -49,9 +62,8 @@ class Synthesizer(

private val unitChecker = SynthesisUnitChecker(testCaseGenerator, objectClassId.toSoot())

private fun splitModels(): Set<Set<UtModel>> {
val modelComparator = compareBy<UtModel> { parametersMap[it]!! }
val result = parameters.map { sortedSetOf(modelComparator, it) }.toMutableSet()
private fun splitModels(): Set<List<UtModel>> {
val result = parameters.map { mutableSetOf(it) }.toMutableSet()
while (true) {
var changed = false
loopExit@ for (current in result) {
Expand All @@ -72,41 +84,68 @@ class Synthesizer(
}
if (!changed) break
}
return result.map { it.sortedBy { parametersMap[it] }.toSet() }.toSet()
return result.map { models -> models.sortedBy { parametersMap[it] } }.toSet()
}

fun synthesize(timeLimit: Long = synthesisTimeoutInMillis): List<UtModel?> {
val modelSubsets = splitModels()
val currentTime = { System.currentTimeMillis() }
val startTime = currentTime()
val startTime = System.currentTimeMillis()
val timeLimitExceeded = { System.currentTimeMillis() - startTime > timeLimit }

val result = MutableList<UtModel?>(parameters.size) { null }
for (models in modelSubsets) {
val modelsList = models.toList()
val queueIterator = SynthesisUnitContextQueue(modelsList, statementStorage, depth)
val modelIndices = models.map { parametersMap[it]!! }
var found = false

for (cachedUnits in SynthesisCache[method, modelIndices]) {
if (timeLimitExceeded()) break

val assembleModel = try {
val mappedUnits = cachedUnits.copyWithNewModelsOrNull(models) ?: continue
unitChecker.tryGenerate(mappedUnits, models)
} catch (e: Throwable) {
logger.warn { "Error during assemble model generation from cached unit context" }
null
}
if (assembleModel != null) {
logger.debug { "Found $assembleModel" }
found = true
break
}
}

val queueIterator = SynthesisUnitContextQueue(models, statementStorage, depth)
while (
queueIterator.hasNext() &&
(currentTime() - startTime) < timeLimit &&
!timeLimitExceeded() &&
!found
) {
val units = queueIterator.next()
if (!units.isFullyDefined) continue

val assembleModel = unitChecker.tryGenerate(units, modelsList)
val assembleModel = try {
unitChecker.tryGenerate(units, models)
} catch (e: Throwable) {
logger.error { "Error during assemble model generation" }
logger.error(e.message)
logger.error(e.stackTraceToString())
null
}

if (assembleModel != null) {
logger.debug { "Found $assembleModel" }
success()
for ((model, assemble) in modelsList.zip(assembleModel)) {
result[parametersMap[model]!!] = assemble
for ((index, assemble) in modelIndices.zip(assembleModel)) {
result[index] = assemble
}
SynthesisCache[method, modelIndices] = units
found = true
}
}

if (found) success()
else failure()
when {
found -> success()
else -> failure()
}
}
return result
}
Expand Down Expand Up @@ -178,6 +217,16 @@ class SynthesisUnitContext(

is MethodUnit -> params.all { it.isFullyDefined() }
}

fun copyWithNewModelsOrNull(newModels: List<UtModel>): SynthesisUnitContext? {
// if current context contains some internal models -- we cannot copy it
if (mapping.size != models.size) return null

val modelMapping = models.zip(newModels).toMap()
return SynthesisUnitContext(newModels, mapping.mapKeys {
modelMapping[it.key] ?: return null
})
}
}

class SynthesisUnitContextQueue(
Expand Down Expand Up @@ -276,15 +325,15 @@ class SynthesisUnitContextQueue(
private fun produce(state: SynthesisUnit): List<SynthesisUnit> =
when (state) {
is MethodUnit -> state.params.run {
flatMapIndexed { idx, leaf ->
val newLeafs = produce(leaf)
newLeafs.map { newLeaf ->
val newParams = toMutableList()
newParams[idx] = newLeaf
state.copy(params = newParams)
}
flatMapIndexed { idx, leaf ->
val newLeafs = produce(leaf)
newLeafs.map { newLeaf ->
val newParams = toMutableList()
newParams[idx] = newLeaf
state.copy(params = newParams)
}
}
}

is ObjectUnit -> {
val leafs = leafExpander.expand(state)
Expand Down