diff --git a/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/ExecutionContext.kt b/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/ExecutionContext.kt index 0a0f76c05..7a2002bc8 100644 --- a/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/ExecutionContext.kt +++ b/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/ExecutionContext.kt @@ -96,8 +96,14 @@ interface ExecutionContext : ExecutionContextAware { is SideEffect.UpdateLibrary -> { libraries = libraries.update(sideEffect.aliasedLibrary) } - is SideEffect.UnloadLibrary -> { - libraries -= sideEffect.alias + is SideEffect.UnloadLibraries -> { + libraries -= sideEffect.aliases + } + is SideEffect.AddLibraries -> { + libraries += sideEffect.libraries + } + is SideEffect.ResetLibraries -> { + libraries = sideEffect.libraries } is SideEffect.SetOperators -> { operators += sideEffect.operatorSet diff --git a/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/SideEffect.kt b/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/SideEffect.kt index 6ef20d6ed..8973e8d36 100644 --- a/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/SideEffect.kt +++ b/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/SideEffect.kt @@ -7,6 +7,7 @@ import it.unibo.tuprolog.core.operators.OperatorSet import it.unibo.tuprolog.solve.channel.InputChannel import it.unibo.tuprolog.solve.channel.OutputChannel import it.unibo.tuprolog.solve.library.AliasedLibrary +import it.unibo.tuprolog.solve.library.Libraries import it.unibo.tuprolog.solve.library.Library import it.unibo.tuprolog.theory.Theory @@ -123,7 +124,11 @@ sealed class SideEffect { } } - data class UnloadLibrary(val alias: String) : SideEffect() + data class UnloadLibraries(val aliases: List) : SideEffect() { + constructor(aliases: Iterable) : this(aliases.toList()) + constructor(aliases: Sequence) : this(aliases.toList()) + constructor(vararg aliases: String) : this(listOf(*aliases)) + } data class UpdateLibrary(val alias: String, val library: Library) : SideEffect() { init { @@ -141,6 +146,18 @@ sealed class SideEffect { } } + data class AddLibraries(val libraries: Libraries) : SideEffect() { + constructor(libraries: Iterable) : this(Libraries(libraries)) + constructor(libraries: Sequence) : this(Libraries(libraries)) + constructor(vararg libraries: AliasedLibrary) : this(Libraries(*libraries)) + } + + data class ResetLibraries(val libraries: Libraries) : SideEffect() { + constructor(libraries: Iterable) : this(Libraries(libraries)) + constructor(libraries: Sequence) : this(Libraries(libraries)) + constructor(vararg libraries: AliasedLibrary) : this(Libraries(*libraries)) + } + data class SetOperators(val operators: Iterable) : SideEffect() { constructor(vararg operators: Operator) : this(listOf(*operators)) diff --git a/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/SideEffectFactory.kt b/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/SideEffectFactory.kt index 95fe4dba4..0332b6225 100644 --- a/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/SideEffectFactory.kt +++ b/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/SideEffectFactory.kt @@ -6,6 +6,7 @@ import it.unibo.tuprolog.core.operators.Operator import it.unibo.tuprolog.solve.channel.InputChannel import it.unibo.tuprolog.solve.channel.OutputChannel import it.unibo.tuprolog.solve.library.AliasedLibrary +import it.unibo.tuprolog.solve.library.Libraries import it.unibo.tuprolog.solve.library.Library interface SideEffectFactory { @@ -98,8 +99,14 @@ interface SideEffectFactory { fun loadLibrary(aliasedLibrary: AliasedLibrary) = SideEffect.LoadLibrary(aliasedLibrary.alias, aliasedLibrary) - fun unloadLibrary(alias: String) = - SideEffect.UnloadLibrary(alias) + fun unloadLibraries(aliases: Iterable) = + SideEffect.UnloadLibraries(aliases) + + fun unloadLibraries(aliases: Sequence) = + SideEffect.UnloadLibraries(aliases) + + fun unloadLibraries(vararg aliases: String) = + SideEffect.UnloadLibraries(*aliases) fun updateLibrary(alias: String, library: Library) = SideEffect.UpdateLibrary(alias, library) @@ -107,6 +114,30 @@ interface SideEffectFactory { fun updateLibrary(aliasedLibrary: AliasedLibrary) = SideEffect.UpdateLibrary(aliasedLibrary.alias, aliasedLibrary) + fun resetLibraries(libraries: Libraries) = + SideEffect.ResetLibraries(libraries) + + fun resetLibraries(libraries: Iterable) = + SideEffect.ResetLibraries(libraries) + + fun resetLibraries(libraries: Sequence) = + SideEffect.ResetLibraries(libraries) + + fun resetLibraries(vararg libraries: AliasedLibrary) = + SideEffect.ResetLibraries(*libraries) + + fun addLibraries(libraries: Libraries) = + SideEffect.AddLibraries(libraries) + + fun addLibraries(libraries: Iterable) = + SideEffect.AddLibraries(libraries) + + fun addLibraries(libraries: Sequence) = + SideEffect.AddLibraries(libraries) + + fun addLibraries(vararg libraries: AliasedLibrary) = + SideEffect.AddLibraries(*libraries) + fun setOperators(operators: Iterable) = SideEffect.SetOperators(operators) diff --git a/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/impl/SideEffectsBuilderImpl.kt b/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/impl/SideEffectsBuilderImpl.kt index 50a626a43..135519c22 100644 --- a/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/impl/SideEffectsBuilderImpl.kt +++ b/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/impl/SideEffectsBuilderImpl.kt @@ -8,6 +8,7 @@ import it.unibo.tuprolog.solve.SideEffectsBuilder import it.unibo.tuprolog.solve.channel.InputChannel import it.unibo.tuprolog.solve.channel.OutputChannel import it.unibo.tuprolog.solve.library.AliasedLibrary +import it.unibo.tuprolog.solve.library.Libraries import it.unibo.tuprolog.solve.library.Library internal data class SideEffectsBuilderImpl(override val sideEffects: MutableList) : SideEffectsBuilder { @@ -127,10 +128,6 @@ internal data class SideEffectsBuilderImpl(override val sideEffects: MutableList return adding { super.loadLibrary(aliasedLibrary) } } - override fun unloadLibrary(alias: String): SideEffect.UnloadLibrary { - return adding { super.unloadLibrary(alias) } - } - override fun updateLibrary(alias: String, library: Library): SideEffect.UpdateLibrary { return adding { super.updateLibrary(alias, library) } } @@ -234,4 +231,48 @@ internal data class SideEffectsBuilderImpl(override val sideEffects: MutableList override fun closeOutputChannels(vararg names: String): SideEffect.CloseOutputChannels { return adding { super.closeOutputChannels(*names) } } + + override fun unloadLibraries(aliases: Iterable): SideEffect.UnloadLibraries { + return adding { super.unloadLibraries(aliases) } + } + + override fun unloadLibraries(aliases: Sequence): SideEffect.UnloadLibraries { + return adding { super.unloadLibraries(aliases) } + } + + override fun unloadLibraries(vararg aliases: String): SideEffect.UnloadLibraries { + return adding { super.unloadLibraries(*aliases) } + } + + override fun resetLibraries(libraries: Libraries): SideEffect.ResetLibraries { + return adding { super.resetLibraries(libraries) } + } + + override fun resetLibraries(libraries: Iterable): SideEffect.ResetLibraries { + return adding { super.resetLibraries(libraries) } + } + + override fun resetLibraries(libraries: Sequence): SideEffect.ResetLibraries { + return adding { super.resetLibraries(libraries) } + } + + override fun resetLibraries(vararg libraries: AliasedLibrary): SideEffect.ResetLibraries { + return adding { super.resetLibraries(*libraries) } + } + + override fun addLibraries(libraries: Libraries): SideEffect.AddLibraries { + return adding { super.addLibraries(libraries) } + } + + override fun addLibraries(libraries: Iterable): SideEffect.AddLibraries { + return adding { super.addLibraries(libraries) } + } + + override fun addLibraries(libraries: Sequence): SideEffect.AddLibraries { + return adding { super.addLibraries(libraries) } + } + + override fun addLibraries(vararg libraries: AliasedLibrary): SideEffect.AddLibraries { + return adding { super.addLibraries(*libraries) } + } } \ No newline at end of file diff --git a/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/library/Libraries.kt b/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/library/Libraries.kt index 4f4423496..ba15d1737 100644 --- a/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/library/Libraries.kt +++ b/solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/library/Libraries.kt @@ -78,6 +78,15 @@ class Libraries(libraries: Sequence) : LibraryGroup): Libraries { + val toBeRemoved = aliases.map { + if (it in libraryAliases) { + noSuchALibraryError(it) + } + it + }.toSet() + return Libraries(libraries.asSequence().filterNot { it.alias in toBeRemoved }) + } override fun update(library: AliasedLibrary): Libraries = libraryAliases.find { library.alias in libraryAliases } diff --git a/test-solve/src/commonTest/kotlin/it/unibo/tuprolog/solve/SolveRequestTest.kt b/test-solve/src/commonTest/kotlin/it/unibo/tuprolog/solve/SolveRequestTest.kt index f3adb3d8f..f1dfe2268 100644 --- a/test-solve/src/commonTest/kotlin/it/unibo/tuprolog/solve/SolveRequestTest.kt +++ b/test-solve/src/commonTest/kotlin/it/unibo/tuprolog/solve/SolveRequestTest.kt @@ -113,12 +113,13 @@ internal class SolveRequestTest { assertEquals( createRequest().replyWith( it.solution, - differentLibraries, - differentFlags, - differentStaticKB, - differentDynamicKB, aSideEffectManager - ), + ) { + resetLibraries(differentLibraries) + resetFlags(differentFlags) + resetStaticKb(differentStaticKB) + resetDynamicKb(differentDynamicKB) + }, it ) } @@ -129,12 +130,13 @@ internal class SolveRequestTest { assertEquals( createRequest().replySuccess( solutionSubstitution, - differentLibraries, - differentFlags, - differentStaticKB, - differentDynamicKB, aSideEffectManager - ), + ) { + resetLibraries(differentLibraries) + resetFlags(differentFlags) + resetStaticKb(differentStaticKB) + resetDynamicKb(differentDynamicKB) + }, defaultRequestSuccessResponse ) } @@ -143,12 +145,13 @@ internal class SolveRequestTest { fun replyFailCreatesCorrectResponse() { assertEquals( createRequest().replyFail( - differentLibraries, - differentFlags, - differentStaticKB, - differentDynamicKB, aSideEffectManager - ), + ) { + resetLibraries(differentLibraries) + resetFlags(differentFlags) + resetStaticKb(differentStaticKB) + resetDynamicKb(differentDynamicKB) + }, defaultRequestFailedResponse ) } @@ -158,12 +161,13 @@ internal class SolveRequestTest { assertEquals( createRequest().replyException( solutionException, - differentLibraries, - differentFlags, - differentStaticKB, - differentDynamicKB, aSideEffectManager - ), + ) { + resetLibraries(differentLibraries) + resetFlags(differentFlags) + resetStaticKb(differentStaticKB) + resetDynamicKb(differentDynamicKB) + }, defaultRequestHaltedResponse ) } @@ -173,12 +177,13 @@ internal class SolveRequestTest { assertEquals( createRequest().replyWith( true, - differentLibraries, - differentFlags, - differentStaticKB, - differentDynamicKB, aSideEffectManager - ), + ) { + resetLibraries(differentLibraries) + resetFlags(differentFlags) + resetStaticKb(differentStaticKB) + resetDynamicKb(differentDynamicKB) + }, defaultRequestSuccessResponse.copy( solution = (defaultRequestSuccessResponse.solution as Solution.Yes).copy( substitution = Substitution.empty() @@ -189,12 +194,13 @@ internal class SolveRequestTest { assertEquals( createRequest().replyWith( false, - differentLibraries, - differentFlags, - differentStaticKB, - differentDynamicKB, aSideEffectManager - ), + ) { + resetLibraries(differentLibraries) + resetFlags(differentFlags) + resetStaticKb(differentStaticKB) + resetDynamicKb(differentDynamicKB) + }, defaultRequestFailedResponse ) } diff --git a/test-solve/src/commonTest/kotlin/it/unibo/tuprolog/solve/SolveResponseTest.kt b/test-solve/src/commonTest/kotlin/it/unibo/tuprolog/solve/SolveResponseTest.kt index 965828be4..7d7212927 100644 --- a/test-solve/src/commonTest/kotlin/it/unibo/tuprolog/solve/SolveResponseTest.kt +++ b/test-solve/src/commonTest/kotlin/it/unibo/tuprolog/solve/SolveResponseTest.kt @@ -7,6 +7,7 @@ import it.unibo.tuprolog.solve.testutils.SolveUtils.aSolution import it.unibo.tuprolog.solve.testutils.SolveUtils.aStaticKB import it.unibo.tuprolog.solve.testutils.SolveUtils.someFlags import it.unibo.tuprolog.solve.testutils.SolveUtils.someLibraries +import it.unibo.tuprolog.solve.testutils.SolveUtils.someSideEffects import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertNull @@ -19,26 +20,19 @@ import kotlin.test.assertNull internal class SolveResponseTest { private val aResponse = - Solve.Response(aSolution, someLibraries, someFlags, aStaticKB, aDynamicKB, aSideEffectManager) + Solve.Response(aSolution, aSideEffectManager, someSideEffects) @Test fun responseInsertedDataCorrect() { assertEquals(aSolution, aResponse.solution) - assertEquals(someLibraries, aResponse.libraries) - assertEquals(someFlags, aResponse.flags) - assertEquals(aStaticKB, aResponse.staticKb) - assertEquals(aDynamicKB, aResponse.dynamicKb) assertEquals(aSideEffectManager, aResponse.sideEffectManager) + assertEquals(someSideEffects, aResponse.sideEffects) } @Test fun responseDefaultValuesCorrect() { val toBeTested = Solve.Response(aSolution) - assertNull(toBeTested.libraries) - assertNull(toBeTested.flags) - assertNull(toBeTested.staticKb) - assertNull(toBeTested.dynamicKb) assertNull(toBeTested.sideEffectManager) } diff --git a/test-solve/src/commonTest/kotlin/it/unibo/tuprolog/solve/testutils/SolveUtils.kt b/test-solve/src/commonTest/kotlin/it/unibo/tuprolog/solve/testutils/SolveUtils.kt index 6b307aaf4..cb7ec3e45 100644 --- a/test-solve/src/commonTest/kotlin/it/unibo/tuprolog/solve/testutils/SolveUtils.kt +++ b/test-solve/src/commonTest/kotlin/it/unibo/tuprolog/solve/testutils/SolveUtils.kt @@ -53,16 +53,17 @@ internal object SolveUtils { internal val aSideEffectManager = object : SideEffectManager { override fun cut() = throw NotImplementedError() } + internal val someSideEffects = listOf(SideEffect.ResetDynamicKb()) /** The success response to default values request */ internal val defaultRequestSuccessResponse by lazy { Solve.Response( Solution.Yes(aSignature, anArgumentList, solutionSubstitution), - differentLibraries, - differentFlags, - differentStaticKB, - differentDynamicKB, - aSideEffectManager + aSideEffectManager, + SideEffect.ResetLibraries(differentLibraries), + SideEffect.ResetFlags(differentFlags), + SideEffect.ResetStaticKb(differentStaticKB), + SideEffect.ResetDynamicKb(differentDynamicKB) ) } @@ -70,11 +71,11 @@ internal object SolveUtils { internal val defaultRequestFailedResponse by lazy { Solve.Response( Solution.No(aSignature, anArgumentList), - differentLibraries, - differentFlags, - differentStaticKB, - differentDynamicKB, - aSideEffectManager + aSideEffectManager, + SideEffect.ResetLibraries(differentLibraries), + SideEffect.ResetFlags(differentFlags), + SideEffect.ResetStaticKb(differentStaticKB), + SideEffect.ResetDynamicKb(differentDynamicKB) ) } @@ -82,11 +83,11 @@ internal object SolveUtils { internal val defaultRequestHaltedResponse by lazy { Solve.Response( Solution.Halt(aSignature, anArgumentList, solutionException), - differentLibraries, - differentFlags, - differentStaticKB, - differentDynamicKB, - aSideEffectManager + aSideEffectManager, + SideEffect.ResetLibraries(differentLibraries), + SideEffect.ResetFlags(differentFlags), + SideEffect.ResetStaticKb(differentStaticKB), + SideEffect.ResetDynamicKb(differentDynamicKB) ) }