From da4a9ebe9d3b0440979b9c0a56111c5209ea1d23 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Fri, 5 Jul 2019 18:49:36 +0300 Subject: [PATCH] Simplified Provider API --- build.gradle.kts | 2 +- .../hep/dataforge/context/AbstractPlugin.kt | 4 +-- .../kotlin/hep/dataforge/context/Context.kt | 18 +++------- .../kotlin/hep/dataforge/provider/Provider.kt | 33 ++++++------------- .../hep/dataforge/context/ContextTest.kt | 15 +++------ .../kotlin/hep/dataforge/provider/Types.kt | 4 +-- .../dataforge/workspace/SimpleWorkspace.kt | 1 - .../hep/dataforge/workspace/Workspace.kt | 32 ++++-------------- .../dataforge/workspace/WorkspacePlugin.kt | 17 +++------- 9 files changed, 32 insertions(+), 94 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 4fda7bec..00c7dfea 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -3,7 +3,7 @@ plugins { id("scientifik.publish") apply false } -val dataforgeVersion by extra("0.1.3-dev-8") +val dataforgeVersion by extra("0.1.3-dev-9") val bintrayRepo by extra("dataforge") val vcs by extra("https://github.com/mipt-npm/dataforge-core") diff --git a/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/AbstractPlugin.kt b/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/AbstractPlugin.kt index 9a091ad1..25f10232 100644 --- a/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/AbstractPlugin.kt +++ b/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/AbstractPlugin.kt @@ -18,7 +18,5 @@ abstract class AbstractPlugin(override val meta: Meta = EmptyMeta) : Plugin { this._context = null } - override fun provideTop(target: String, name: Name): Any? = null - - override fun listNames(target: String): Sequence = emptySequence() + override fun provideTop(target: String): Map = emptyMap() } \ No newline at end of file diff --git a/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/Context.kt b/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/Context.kt index 1309eea8..9db32a01 100644 --- a/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/Context.kt +++ b/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/Context.kt @@ -59,19 +59,11 @@ open class Context(final override val name: String, val parent: Context? = Globa override val defaultTarget: String get() = Plugin.PLUGIN_TARGET - override fun provideTop(target: String, name: Name): Any? { - return when (target) { - Plugin.PLUGIN_TARGET -> plugins[PluginTag.fromString(name.toString())] - Value.TYPE -> properties[name]?.value - else -> null - } - } - - override fun listNames(target: String): Sequence { - return when (target) { - Plugin.PLUGIN_TARGET -> plugins.asSequence().map { it.name.toName() } - Value.TYPE -> properties.values().map { it.first } - else -> emptySequence() + override fun provideTop(target: String): Map { + return when(target){ + Value.TYPE -> properties.sequence().toMap() + Plugin.PLUGIN_TARGET -> plugins.sequence(true).associateBy { it.name.toName() } + else-> emptyMap() } } diff --git a/dataforge-context/src/commonMain/kotlin/hep/dataforge/provider/Provider.kt b/dataforge-context/src/commonMain/kotlin/hep/dataforge/provider/Provider.kt index 35f1aca0..b1d769e2 100644 --- a/dataforge-context/src/commonMain/kotlin/hep/dataforge/provider/Provider.kt +++ b/dataforge-context/src/commonMain/kotlin/hep/dataforge/provider/Provider.kt @@ -17,7 +17,6 @@ package hep.dataforge.provider import hep.dataforge.names.Name import hep.dataforge.names.toName -import kotlin.jvm.JvmName /** * A marker utility interface for providers. @@ -42,29 +41,21 @@ interface Provider { /** - * Provide a top level element for this [Provider] or return null if element is not present + * A map of direct children for specific target */ - fun provideTop(target: String, name: Name): Any? - - /** - * [Sequence] of available names with given target. Only top level names are listed, no chain path. - * - * @param target - * @return - */ - fun listNames(target: String): Sequence + fun provideTop(target: String): Map } fun Provider.provide(path: Path, targetOverride: String? = null): Any? { if (path.length == 0) throw IllegalArgumentException("Can't provide by empty path") val first = path.first() - val top = provideTop(targetOverride ?: first.target ?: defaultTarget, first.name) + val target = targetOverride ?: first.target ?: defaultTarget + val res = provideTop(target)[first.name] ?: return null return when (path.length) { - 1 -> top + 1 -> res else -> { - when (top) { - null -> null - is Provider -> top.provide(path.tail!!, targetOverride = defaultChainTarget) + when (res) { + is Provider -> res.provide(path.tail!!, targetOverride = defaultChainTarget) else -> throw IllegalStateException("Chain path not supported: child is not a provider") } } @@ -85,16 +76,12 @@ inline fun Provider.provide(target: String, name: Name): T? { inline fun Provider.provide(target: String, name: String): T? = provide(target, name.toName()) - -fun Provider.top(target: String): Map = top(target) - /** - * A top level content with names + * Typed top level content */ -@JvmName("typedTop") inline fun Provider.top(target: String): Map { - return listNames(target).associate { - it to (provideTop(target, it) as? T ?: error("The element $it is declared but not provided")) + return provideTop(target).mapValues { + it.value as? T ?: error("The type of element $it is ${it::class} but ${T::class} is expected") } } diff --git a/dataforge-context/src/commonTest/kotlin/hep/dataforge/context/ContextTest.kt b/dataforge-context/src/commonTest/kotlin/hep/dataforge/context/ContextTest.kt index 1d37c69e..c77439d6 100644 --- a/dataforge-context/src/commonTest/kotlin/hep/dataforge/context/ContextTest.kt +++ b/dataforge-context/src/commonTest/kotlin/hep/dataforge/context/ContextTest.kt @@ -12,17 +12,10 @@ class ContextTest { class DummyPlugin : AbstractPlugin() { override val tag get() = PluginTag("test") - override fun provideTop(target: String, name: Name): Any? { - return when (target) { - "test" -> return name - else -> super.provideTop(target, name) - } - } - - override fun listNames(target: String): Sequence { - return when (target) { - "test" -> sequenceOf("a", "b", "c.d").map { it.toName() } - else -> super.listNames(target) + override fun provideTop(target: String): Map { + return when(target){ + "test" -> listOf("a", "b", "c.d").associate { it.toName() to it.toName() } + else -> emptyMap() } } } diff --git a/dataforge-context/src/jvmMain/kotlin/hep/dataforge/provider/Types.kt b/dataforge-context/src/jvmMain/kotlin/hep/dataforge/provider/Types.kt index d6ed723d..dfe81ce0 100644 --- a/dataforge-context/src/jvmMain/kotlin/hep/dataforge/provider/Types.kt +++ b/dataforge-context/src/jvmMain/kotlin/hep/dataforge/provider/Types.kt @@ -34,9 +34,7 @@ inline fun Provider.provideByType(name: Name): T? { inline fun Provider.top(): Map { val target = Types[T::class] - return listNames(target).associate { name -> - name to (provideByType(name) ?: error("The element $name is declared but not provided")) - } + return top(target) } /** diff --git a/dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/SimpleWorkspace.kt b/dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/SimpleWorkspace.kt index d707545d..68438440 100644 --- a/dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/SimpleWorkspace.kt +++ b/dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/SimpleWorkspace.kt @@ -7,7 +7,6 @@ import hep.dataforge.data.DataNode import hep.dataforge.meta.Meta import hep.dataforge.names.Name import hep.dataforge.names.toName -import hep.dataforge.provider.top /** diff --git a/dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/Workspace.kt b/dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/Workspace.kt index 1945c74e..58fe081e 100644 --- a/dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/Workspace.kt +++ b/dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/Workspace.kt @@ -29,27 +29,16 @@ interface Workspace : ContextAware, Provider { */ val tasks: Map> - override fun provideTop(target: String, name: Name): Any? { + override fun provideTop(target: String): Map { return when (target) { - "target", Meta.TYPE -> targets[name.toString()] - Task.TYPE -> tasks[name] - Data.TYPE -> data[name] - DataNode.TYPE -> data.getNode(name) - else -> null + "target", Meta.TYPE -> targets.mapKeys { it.key.toName() } + Task.TYPE -> tasks + Data.TYPE -> data.data().toMap() + DataNode.TYPE -> data.nodes().toMap() + else -> emptyMap() } } - override fun listNames(target: String): Sequence { - return when (target) { - "target", Meta.TYPE -> targets.keys.asSequence().map { it.toName() } - Task.TYPE -> tasks.keys.asSequence().map { it } - Data.TYPE -> data.data().map { it.first } - DataNode.TYPE -> data.nodes().map { it.first } - else -> emptySequence() - } - } - - /** * Invoke a task in the workspace utilizing caching if possible */ @@ -64,15 +53,6 @@ interface Workspace : ContextAware, Provider { } } -// /** -// * Invoke a task in the workspace utilizing caching if possible -// */ -// operator fun Task.invoke(targetName: String): DataNode { -// val target = targets[targetName] ?: error("A target with name $targetName not found in ${this@Workspace}") -// context.logger.info { "Running ${this.name} on $target" } -// return invoke(target) -// } - companion object { const val TYPE = "workspace" } diff --git a/dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/WorkspacePlugin.kt b/dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/WorkspacePlugin.kt index 7f808166..2fefd4f8 100644 --- a/dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/WorkspacePlugin.kt +++ b/dataforge-workspace/src/commonMain/kotlin/hep/dataforge/workspace/WorkspacePlugin.kt @@ -10,19 +10,10 @@ import hep.dataforge.names.toName abstract class WorkspacePlugin : AbstractPlugin() { abstract val tasks: Collection> - override fun provideTop(target: String, name: Name): Any? { - return if (target == Task.TYPE) { - tasks.find { it.name == name.toString() } - } else { - super.provideTop(target, name) - } - } - - override fun listNames(target: String): Sequence { - return if (target == Task.TYPE) { - tasks.asSequence().map { it.name.toName() } - } else { - return super.listNames(target) + override fun provideTop(target: String): Map { + return when(target){ + Task.TYPE -> tasks.associateBy { it.name.toName() } + else -> emptyMap() } } } \ No newline at end of file