Skip to content

Commit dce7039

Browse files
Models in stateBefore should be assembled in concrete executor #2539 (#2544)
1 parent 15d624f commit dce7039

File tree

1 file changed

+35
-22
lines changed

1 file changed

+35
-22
lines changed

utbot-framework/src/main/kotlin/org/utbot/framework/util/UtConcreteExecutionResultUtils.kt

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,54 @@ import org.utbot.framework.plugin.api.UtModel
88
import org.utbot.instrumentation.instrumentation.execution.UtConcreteExecutionResult
99
import java.util.IdentityHashMap
1010

11+
/**
12+
* Tries to convert all models from [UtExecutionResult] to [UtAssembleModel] if possible.
13+
*
14+
* @return [UtConcreteExecutionResult] with converted models.
15+
*/
16+
fun UtConcreteExecutionResult.convertToAssemble(packageName: String): UtConcreteExecutionResult {
17+
val allModels = collectAllModels()
18+
19+
val modelsToAssembleModels = AssembleModelGenerator(packageName).createAssembleModels(allModels)
20+
return updateWithAssembleModels(modelsToAssembleModels)
21+
}
22+
1123
private fun UtConcreteExecutionResult.updateWithAssembleModels(
1224
assembledUtModels: IdentityHashMap<UtModel, UtModel>
1325
): UtConcreteExecutionResult {
1426
val toAssemble: (UtModel) -> UtModel = { assembledUtModels.getOrDefault(it, it) }
1527

16-
val resolvedStateAfter = if (stateAfter is MissingState) MissingState else stateAfter.copy(
17-
thisInstance = stateAfter.thisInstance?.let { toAssemble(it) },
18-
parameters = stateAfter.parameters.map { toAssemble(it) },
19-
statics = stateAfter.statics.mapValues { toAssemble(it.value) },
20-
)
21-
val resolvedResult =
22-
(result as? UtExecutionSuccess)?.model?.let { UtExecutionSuccess(toAssemble(it)) } ?: result
28+
val resolvedStateBefore = stateBefore.resolveState(toAssemble)
29+
val resolvedStateAfter = stateAfter.resolveState(toAssemble)
30+
val resolvedResult = (result as? UtExecutionSuccess)?.model?.let { UtExecutionSuccess(toAssemble(it)) } ?: result
2331

2432
return copy(
33+
stateBefore = resolvedStateBefore,
2534
stateAfter = resolvedStateAfter,
2635
result = resolvedResult,
2736
)
2837
}
2938

30-
/**
31-
* Tries to convert all models from [UtExecutionResult] to [UtAssembleModel] if possible.
32-
*
33-
* @return [UtConcreteExecutionResult] with converted models.
34-
*/
35-
fun UtConcreteExecutionResult.convertToAssemble(packageName: String): UtConcreteExecutionResult {
36-
val allModels = collectAllModels()
37-
38-
val modelsToAssembleModels = AssembleModelGenerator(packageName).createAssembleModels(allModels)
39-
return updateWithAssembleModels(modelsToAssembleModels)
40-
}
39+
private fun EnvironmentModels.resolveState(toAssemble: (UtModel) -> UtModel): EnvironmentModels =
40+
if (this is MissingState) {
41+
MissingState
42+
} else {
43+
copy(
44+
thisInstance = thisInstance?.let { toAssemble(it) },
45+
parameters = parameters.map { toAssemble(it) },
46+
statics = statics.mapValues { toAssemble(it.value) },
47+
)
48+
}
4149

4250
private fun UtConcreteExecutionResult.collectAllModels(): List<UtModel> {
43-
val allModels = listOfNotNull(stateAfter.thisInstance).toMutableList()
44-
allModels += stateAfter.parameters
45-
allModels += stateAfter.statics.values
51+
val allModels = mutableListOf<UtModel>()
52+
53+
allModels += stateBefore.utModels
54+
allModels += stateAfter.utModels
4655
allModels += listOfNotNull((result as? UtExecutionSuccess)?.model)
56+
4757
return allModels
48-
}
58+
}
59+
60+
private val EnvironmentModels.utModels: List<UtModel>
61+
get() = listOfNotNull(thisInstance) + parameters + statics.values

0 commit comments

Comments
 (0)