Skip to content

Commit

Permalink
use static factories for Libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
gciatto committed Sep 24, 2020
1 parent 39a1677 commit 0acf286
Show file tree
Hide file tree
Showing 22 changed files with 87 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fun gt(request: Solve.Request<ExecutionContext>): Sequence<Solve.Response> {
fun main() {
prolog {
val solver = Solver.classicWithDefaultBuiltins(
libraries = Libraries(
libraries = Libraries.of(
Library.aliased(
primitives = mapOf(gtSignature to ::gt),
alias = "it.unibo.lrizzato.myprimives"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import kotlin.collections.Set as KtSet

data class ClassicExecutionContext(
override val procedure: Struct? = null,
override val libraries: Libraries = Libraries(),
override val flags: FlagStore = FlagStore.EMPTY,
override val libraries: Libraries = Libraries.empty(),
override val flags: FlagStore = FlagStore.empty(),
override val staticKb: Theory = Theory.empty(),
override val dynamicKb: Theory = Theory.empty(),
override val operators: OperatorSet = getAllOperators(libraries, staticKb, dynamicKb).toOperatorSet(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import it.unibo.tuprolog.solve.library.Libraries
import it.unibo.tuprolog.theory.Theory

internal open class ClassicSolver(
libraries: Libraries = Libraries(),
flags: FlagStore = FlagStore.EMPTY,
libraries: Libraries = Libraries.empty(),
flags: FlagStore = FlagStore.empty(),
staticKb: Theory = Theory.empty(),
dynamicKb: Theory = Theory.empty(),
inputChannels: InputStore<*> = ExecutionContextAware.defaultInputChannels(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import it.unibo.tuprolog.theory.RetractResult
import it.unibo.tuprolog.theory.Theory

internal class MutableClassicSolver(
libraries: Libraries = Libraries(),
flags: FlagStore = FlagStore.EMPTY,
libraries: Libraries = Libraries.empty(),
flags: FlagStore = FlagStore.empty(),
staticKb: Theory = Theory.empty(),
dynamicKb: Theory = Theory.empty(),
inputChannels: InputStore<*> = ExecutionContextAware.defaultInputChannels(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import it.unibo.tuprolog.theory.Theory
* @author Enrico
*/
internal class StreamsSolver constructor(
libraries: Libraries = Libraries(),
flags: FlagStore = FlagStore.EMPTY,
libraries: Libraries = Libraries.empty(),
flags: FlagStore = FlagStore.empty(),
staticKb: Theory = Theory.empty(),
dynamicKb: Theory = Theory.empty(),
inputChannels: InputStore<*> = ExecutionContextAware.defaultInputChannels(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import it.unibo.tuprolog.theory.Theory
* @author Enrico
*/
internal data class StreamsExecutionContext(
override val libraries: Libraries = Libraries(),
override val flags: FlagStore = FlagStore.EMPTY,
override val libraries: Libraries = Libraries.empty(),
override val flags: FlagStore = FlagStore.empty(),
override val staticKb: Theory = Theory.empty(),
override val dynamicKb: Theory = Theory.empty(),
override val operators: OperatorSet = getAllOperators(libraries, staticKb, dynamicKb).toOperatorSet(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ internal class StreamsSolverTest {
fun defaultConstructorParameters() {
val toBeTested = StreamsSolver()

assertEquals(Libraries(), toBeTested.libraries)
assertEquals(FlagStore.EMPTY, toBeTested.flags)
assertEquals(Libraries.empty(), toBeTested.libraries)
assertEquals(FlagStore.empty(), toBeTested.flags)
assertEquals(Theory.empty(), toBeTested.staticKb)
assertEquals(Theory.empty(), toBeTested.dynamicKb)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ internal class SolverUtilsTest {
dynamicKb = Theory.empty(),
staticKb = aContext.staticKb.assertA(aClause),
flags = FlagStore.of("someFlag" to Atom.of("someFlagValue")),
libraries = Libraries()
libraries = Libraries.empty()
)

val toBeTested = solveRequest.newSolveRequest(solveRequest.query, toPropagateContextData = modifiedContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal class StateRuleSelectionTest {
theQuery.extractSignature(),
theQuery.argsList,
StreamsExecutionContext(
libraries = Libraries(
libraries = Libraries.of(
Library.aliased(
alias = "testLib",
theory = prolog { theory({ "f"("a") }) }
Expand Down Expand Up @@ -104,7 +104,7 @@ internal class StateRuleSelectionTest {
@Test
fun stateRuleSelectionUsesCombinationOfStaticAndDynamicKBWhenLibraryTheoriesDoesntProvideMatches() {
val dynamicAndStaticKBSolveRequest =
with(threeDBSolveRequest) { copy(context = context.copy(libraries = Libraries())) }
with(threeDBSolveRequest) { copy(context = context.copy(libraries = Libraries.empty())) }
val correctSubstitutions = prolog {
ktListOf(
theQueryVariable to "b",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ internal class ConjunctionTest {
goal.argsList,
StreamsExecutionContext(
substitution = preRequestSubstitution,
libraries = Libraries(
libraries = Libraries.of(
Library.aliased(
alias = "conjunction.test",
primitives = mapOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal object SolverTestUtils {
query.extractSignature(),
query.argsList,
StreamsExecutionContext(
libraries = Libraries(
libraries = Libraries.of(
Library.aliased(
alias = "solve.solver.test",
theory = database,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ class FlagStore private constructor(private val flags: Map<String, Term>) : Map<
DoubleQuotes
)

@JsName("empty")
@JvmStatic
fun empty() = EMPTY

@JsName("ofMap")
@JvmStatic
fun of(flags: Map<String, Term>) = FlagStore(flags)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ sealed class SideEffect {
}

data class AddLibraries(val libraries: Libraries) : SideEffect() {
constructor(libraries: Iterable<AliasedLibrary>) : this(Libraries(libraries))
constructor(libraries: Sequence<AliasedLibrary>) : this(Libraries(libraries))
constructor(vararg libraries: AliasedLibrary) : this(Libraries(*libraries))
constructor(libraries: Iterable<AliasedLibrary>) : this(Libraries.of(libraries))
constructor(libraries: Sequence<AliasedLibrary>) : this(Libraries.of(libraries))
constructor(vararg libraries: AliasedLibrary) : this(Libraries.of(*libraries))
}

data class ResetLibraries(val libraries: Libraries) : SideEffect() {
constructor(libraries: Iterable<AliasedLibrary>) : this(Libraries(libraries))
constructor(libraries: Sequence<AliasedLibrary>) : this(Libraries(libraries))
constructor(vararg libraries: AliasedLibrary) : this(Libraries(*libraries))
constructor(libraries: Iterable<AliasedLibrary>) : this(Libraries.of(libraries))
constructor(libraries: Sequence<AliasedLibrary>) : this(Libraries.of(libraries))
constructor(vararg libraries: AliasedLibrary) : this(Libraries.of(*libraries))
}

data class SetOperators(val operators: Iterable<Operator>) : SideEffect() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface SolverFactory {

@JsName("defaultLibraries")
val defaultLibraries: Libraries
get() = Libraries()
get() = Libraries.empty()

@JsName("defaultBuiltins")
val defaultBuiltins: AliasedLibrary
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ interface AliasedLibrary : Library {

/** Creates a library group from this library aliased and the "added" one */
operator fun <L : AliasedLibrary> L.plus(library: L): LibraryGroup<AliasedLibrary> =
Libraries(this, library)
Libraries.of(this, library)
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ import it.unibo.tuprolog.solve.library.exception.NoSuchALibraryException
import it.unibo.tuprolog.solve.primitive.Primitive
import it.unibo.tuprolog.theory.Theory
import kotlin.js.JsName
import kotlin.jvm.JvmStatic

/** A class representing an agglomerate of libraries with an alias */
class Libraries(libraries: Sequence<AliasedLibrary>) :
class Libraries private constructor(libraries: Sequence<AliasedLibrary>) :
LibraryGroup<AliasedLibrary>,
Map<String, AliasedLibrary> by (libraries.map { it.alias to it }.toMap()) {

constructor(vararg library: AliasedLibrary) : this(library.asSequence())
constructor(libraries: Iterable<AliasedLibrary>) : this(libraries.asSequence())

/** All library aliases of libraries included in this library group */
@JsName("libraryAliases")
val libraryAliases: Set<String>
Expand Down Expand Up @@ -121,5 +119,21 @@ class Libraries(libraries: Sequence<AliasedLibrary>) :

private fun noSuchALibraryError(alias: String): Nothing =
throw NoSuchALibraryException("No library with alias `$alias` has been loaded")

@JsName("empty")
@JvmStatic
fun empty() = Libraries(emptySequence())

@JsName("of")
@JvmStatic
fun of(vararg library: AliasedLibrary) = Libraries(sequenceOf(*library))

@JsName("ofIterable")
@JvmStatic
fun of(libraries: Iterable<AliasedLibrary>) = Libraries(libraries.asSequence())

@JsName("ofSequence")
@JvmStatic
fun of(libraries: Sequence<AliasedLibrary>) = Libraries(libraries)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal object ArithmeticEvaluatorUtils {

/** A context with [CommonBuiltins] loaded */
internal val commonFunctionsContext = object : ExecutionContext by ExpressionEvaluatorUtils.noFunctionsContext {
override val libraries: Libraries = Libraries(CommonBuiltins)
override val libraries: Libraries = Libraries.of(CommonBuiltins)
}

/** A context with [CommonBuiltins] loaded */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal object ExpressionEvaluatorUtils {

/** A context with empty functions map */
internal val noFunctionsContext = object : ExecutionContext by DummyInstances.executionContext {
override val libraries: Libraries = Libraries()
override val libraries: Libraries = Libraries.empty()
}

internal val noFunctionRequest = Solve.Request(
Expand Down Expand Up @@ -79,7 +79,7 @@ internal object ExpressionEvaluatorUtils {
/** Creates a context with provided signature-function binding */
private fun createContextWithFunctionBy(signature: Signature, function: PrologFunction): ExecutionContext =
object : ExecutionContext by DummyInstances.executionContext {
override val libraries: Libraries = Libraries(
override val libraries: Libraries = Libraries.of(
Library.aliased(
alias = "test.expression.evaluator",
functions = mapOf(signature to function)
Expand Down
Loading

0 comments on commit 0acf286

Please sign in to comment.