-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into alisevych/ui_tests_correction
- Loading branch information
Showing
49 changed files
with
888 additions
and
484 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
...-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/mapper/UtModelDeepMapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package org.utbot.framework.plugin.api.mapper | ||
|
||
import org.utbot.framework.plugin.api.UtArrayModel | ||
import org.utbot.framework.plugin.api.UtAssembleModel | ||
import org.utbot.framework.plugin.api.UtClassRefModel | ||
import org.utbot.framework.plugin.api.UtCompositeModel | ||
import org.utbot.framework.plugin.api.UtCustomModel | ||
import org.utbot.framework.plugin.api.UtEnumConstantModel | ||
import org.utbot.framework.plugin.api.UtLambdaModel | ||
import org.utbot.framework.plugin.api.UtModel | ||
import org.utbot.framework.plugin.api.UtNullModel | ||
import org.utbot.framework.plugin.api.UtPrimitiveModel | ||
import org.utbot.framework.plugin.api.UtReferenceModel | ||
import org.utbot.framework.plugin.api.UtVoidModel | ||
|
||
/** | ||
* Performs deep mapping of [UtModel]s. | ||
* | ||
* NOTE: | ||
* - [shallowMapper] is invoked on models **before** mapping their sub models. | ||
* - [shallowMapper] is responsible for caching own results (it may be called repeatedly on same models). | ||
*/ | ||
class UtModelDeepMapper private constructor( | ||
private val shallowMapper: UtModelMapper | ||
) : UtModelMapper { | ||
constructor(shallowMapper: (UtModel) -> UtModel) : this(UtModelSafeCastingCachingShallowMapper(shallowMapper)) | ||
|
||
/** | ||
* Keys are models that have been shallowly mapped by [shallowMapper]. | ||
* Values are models that have been deeply mapped by this [UtModelDeepMapper]. | ||
* Models are only associated with models of the same type (i.e. the cache type is actually `MutableMap<T, T>`) | ||
*/ | ||
private val cache = mutableMapOf<UtModel, UtModel>() | ||
|
||
private val allInputtedModels get() = cache.keys | ||
private val allOutputtedModels get() = cache.values | ||
|
||
override fun <T : UtModel> map(model: T, clazz: Class<T>): T = | ||
clazz.cast(mapNestedModels(shallowMapper.map(model, clazz))) | ||
|
||
/** | ||
* Maps models contained inside [model], but not the [model] itself. | ||
*/ | ||
private fun mapNestedModels(model: UtModel): UtModel = cache.getOrPut(model) { | ||
when (model) { | ||
is UtNullModel, | ||
is UtPrimitiveModel, | ||
is UtEnumConstantModel, | ||
is UtClassRefModel, | ||
is UtVoidModel -> model | ||
is UtArrayModel -> mapNestedModels(model) | ||
is UtCompositeModel -> mapNestedModels(model) | ||
is UtLambdaModel -> mapNestedModels(model) | ||
is UtAssembleModel -> mapNestedModels(model) | ||
is UtCustomModel -> mapNestedModels(model) | ||
|
||
// PythonModel, JsUtModel may be here | ||
else -> throw UnsupportedOperationException("UtModel $this cannot be mapped") | ||
} | ||
} | ||
|
||
private fun mapNestedModels(model: UtArrayModel): UtReferenceModel { | ||
val mappedModel = UtArrayModel( | ||
id = model.id, | ||
classId = model.classId, | ||
length = model.length, | ||
constModel = model.constModel, | ||
stores = model.stores, | ||
) | ||
cache[model] = mappedModel | ||
|
||
mappedModel.constModel = model.constModel.map(this) | ||
mappedModel.stores.putAll(model.stores.mapModelValues(this)) | ||
|
||
return mappedModel | ||
} | ||
|
||
private fun mapNestedModels(model: UtCompositeModel): UtCompositeModel { | ||
val mappedModel = UtCompositeModel( | ||
id = model.id, | ||
classId = model.classId, | ||
isMock = model.isMock, | ||
) | ||
cache[model] = mappedModel | ||
|
||
mappedModel.fields.putAll(model.fields.mapModelValues(this)) | ||
mappedModel.mocks.putAll(model.mocks.mapValuesTo(mutableMapOf()) { it.value.mapModels(this@UtModelDeepMapper) }) | ||
|
||
return mappedModel | ||
} | ||
|
||
private fun mapNestedModels(model: UtLambdaModel): UtReferenceModel = UtLambdaModel( | ||
id = model.id, | ||
samType = model.samType, | ||
declaringClass = model.declaringClass, | ||
lambdaName = model.lambdaName, | ||
capturedValues = model.capturedValues.mapModels(this@UtModelDeepMapper).toMutableList() | ||
) | ||
|
||
private fun mapNestedModels(model: UtAssembleModel): UtReferenceModel = UtAssembleModel( | ||
id = model.id, | ||
classId = model.classId, | ||
modelName = model.modelName, | ||
instantiationCall = model.instantiationCall.mapModels(this), | ||
modificationsChainProvider = { | ||
cache[model] = this@UtAssembleModel | ||
model.modificationsChain.map { it.mapModels(this@UtModelDeepMapper) } | ||
}, | ||
origin = model.origin?.mapPreservingType<UtCompositeModel>(this) | ||
) | ||
|
||
private fun mapNestedModels(model: UtCustomModel): UtReferenceModel = | ||
model.shallowMap(this) | ||
|
||
companion object { | ||
/** | ||
* Creates identity deep mapper, runs [block] on it, and returns the set of all models that | ||
* were mapped (i.e. deeply collects all models reachable from models passed to `collector`). | ||
*/ | ||
fun collectAllModels(block: (collector: UtModelDeepMapper) -> Unit): Set<UtModel> = | ||
UtModelDeepMapper(UtModelNoopMapper).also(block).allInputtedModels | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/mapper/UtModelMapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.utbot.framework.plugin.api.mapper | ||
|
||
import org.utbot.framework.plugin.api.UtCompositeModel | ||
import org.utbot.framework.plugin.api.UtModel | ||
import org.utbot.framework.plugin.api.UtModelWithCompositeOrigin | ||
|
||
interface UtModelMapper { | ||
/** | ||
* Performs depending on the implementation deep or shallow mapping of the [model]. | ||
* | ||
* In some cases (e.g. when mapping [UtModelWithCompositeOrigin.origin]) you may want to get result | ||
* of some specific type (e.g. [UtCompositeModel]), only then you should specify specific value for [clazz]. | ||
* | ||
* NOTE: if you are fine with result model and [model] having different types, then you should | ||
* use `UtModel::class.java` as a value for [clazz] or just use [UtModel.map]. | ||
*/ | ||
fun <T : UtModel> map(model: T, clazz: Class<T>): T | ||
} |
7 changes: 7 additions & 0 deletions
7
...-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/mapper/UtModelNoopMapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.utbot.framework.plugin.api.mapper | ||
|
||
import org.utbot.framework.plugin.api.UtModel | ||
|
||
object UtModelNoopMapper : UtModelMapper { | ||
override fun <T : UtModel> map(model: T, clazz: Class<T>): T = model | ||
} |
15 changes: 15 additions & 0 deletions
15
...in/kotlin/org/utbot/framework/plugin/api/mapper/UtModelSafeCastingCachingShallowMapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.utbot.framework.plugin.api.mapper | ||
|
||
import org.utbot.framework.plugin.api.UtModel | ||
|
||
class UtModelSafeCastingCachingShallowMapper( | ||
val mapper: (UtModel) -> UtModel | ||
) : UtModelMapper { | ||
private val cache = mutableMapOf<UtModel, UtModel>() | ||
|
||
override fun <T : UtModel> map(model: T, clazz: Class<T>): T { | ||
val mapped = cache.getOrPut(model) { mapper(model) } | ||
return if (clazz.isInstance(mapped)) clazz.cast(mapped) | ||
else model | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/mapper/Utils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package org.utbot.framework.plugin.api.mapper | ||
|
||
import org.utbot.framework.plugin.api.EnvironmentModels | ||
import org.utbot.framework.plugin.api.UtDirectGetFieldModel | ||
import org.utbot.framework.plugin.api.UtDirectSetFieldModel | ||
import org.utbot.framework.plugin.api.UtExecutableCallModel | ||
import org.utbot.framework.plugin.api.UtExecution | ||
import org.utbot.framework.plugin.api.UtInstrumentation | ||
import org.utbot.framework.plugin.api.UtModel | ||
import org.utbot.framework.plugin.api.UtNewInstanceInstrumentation | ||
import org.utbot.framework.plugin.api.UtReferenceModel | ||
import org.utbot.framework.plugin.api.UtStatementCallModel | ||
import org.utbot.framework.plugin.api.UtStatementModel | ||
import org.utbot.framework.plugin.api.UtStaticMethodInstrumentation | ||
|
||
inline fun <reified T : UtModel> T.mapPreservingType(mapper: UtModelMapper): T = | ||
mapper.map(this, T::class.java) | ||
|
||
fun UtModel.map(mapper: UtModelMapper) = mapPreservingType<UtModel>(mapper) | ||
|
||
fun List<UtModel>.mapModels(mapper: UtModelMapper): List<UtModel> = | ||
map { model -> model.map(mapper) } | ||
|
||
fun <K> Map<K, UtModel>.mapModelValues(mapper: UtModelMapper): Map<K, UtModel> = | ||
mapValues { (_, model) -> model.map(mapper) } | ||
|
||
fun UtStatementModel.mapModels(mapper: UtModelMapper): UtStatementModel = | ||
when(this) { | ||
is UtStatementCallModel -> mapModels(mapper) | ||
is UtDirectSetFieldModel -> UtDirectSetFieldModel( | ||
instance = instance.mapPreservingType<UtReferenceModel>(mapper), | ||
fieldId = fieldId, | ||
fieldModel = fieldModel.map(mapper) | ||
) | ||
} | ||
|
||
fun UtStatementCallModel.mapModels(mapper: UtModelMapper): UtStatementCallModel = | ||
when(this) { | ||
is UtDirectGetFieldModel -> UtDirectGetFieldModel( | ||
instance = instance.mapPreservingType<UtReferenceModel>(mapper), | ||
fieldAccess = fieldAccess, | ||
) | ||
is UtExecutableCallModel -> UtExecutableCallModel( | ||
instance = instance?.mapPreservingType<UtReferenceModel>(mapper), | ||
executable = executable, | ||
params = params.mapModels(mapper) | ||
) | ||
} | ||
|
||
fun EnvironmentModels.mapModels(mapper: UtModelMapper) = EnvironmentModels( | ||
thisInstance = thisInstance?.map(mapper), | ||
statics = statics.mapModelValues(mapper), | ||
parameters = parameters.mapModels(mapper), | ||
executableToCall = executableToCall, | ||
) | ||
|
||
fun UtInstrumentation.mapModels(mapper: UtModelMapper) = when (this) { | ||
is UtNewInstanceInstrumentation -> copy(instances = instances.mapModels(mapper)) | ||
is UtStaticMethodInstrumentation -> copy(values = values.mapModels(mapper)) | ||
} | ||
|
||
fun UtExecution.mapStateBeforeModels(mapper: UtModelMapper) = copy( | ||
stateBefore = stateBefore.mapModels(mapper) | ||
) |
Oops, something went wrong.