From 1b29e377cab411f3436ae8616b31b9f38a1e6204 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sat, 18 Nov 2023 22:43:53 +0300 Subject: [PATCH] Type -> DfId Add descriptor to MetaConverter --- CHANGELOG.md | 1 + .../kscience/dataforge/context/Plugin.kt | 4 +- .../dataforge/context/PluginFactory.kt | 4 +- .../kscience/dataforge/provider/dfType.kt | 18 ++--- .../space/kscience/dataforge/data/Data.kt | 4 +- .../space/kscience/dataforge/data/DataTree.kt | 4 +- .../kscience/dataforge/io/EnvelopeFormat.kt | 4 +- .../space/kscience/dataforge/io/IOFormat.kt | 4 +- .../space/kscience/dataforge/io/MetaFormat.kt | 4 +- .../space/kscience/dataforge/meta/Meta.kt | 4 +- .../meta/descriptors/MetaDescriptor.kt | 20 ++--- .../meta/descriptors/MetaDescriptorBuilder.kt | 49 +++++++------ .../meta/transformations/MetaConverter.kt | 73 +++++++++++++++++++ .../kscience/dataforge/workspace/Task.kt | 4 +- .../kscience/dataforge/workspace/Workspace.kt | 4 +- 15 files changed, 141 insertions(+), 60 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4bcc9cef..9f2cdff8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased ### Added +- Obligatory `type: KType` and `descriptor` property for `MetaConverters` - Added separate `Meta`, `SealedMeta` and `ObservableMutableMeta` builders. ### Changed diff --git a/dataforge-context/src/commonMain/kotlin/space/kscience/dataforge/context/Plugin.kt b/dataforge-context/src/commonMain/kotlin/space/kscience/dataforge/context/Plugin.kt index 0621604e..152f5a76 100644 --- a/dataforge-context/src/commonMain/kotlin/space/kscience/dataforge/context/Plugin.kt +++ b/dataforge-context/src/commonMain/kotlin/space/kscience/dataforge/context/Plugin.kt @@ -3,8 +3,8 @@ package space.kscience.dataforge.context import space.kscience.dataforge.context.Plugin.Companion.TARGET import space.kscience.dataforge.meta.Meta import space.kscience.dataforge.meta.MetaRepr +import space.kscience.dataforge.misc.DfId import space.kscience.dataforge.misc.Named -import space.kscience.dataforge.misc.Type import space.kscience.dataforge.names.Name import space.kscience.dataforge.names.parseAsName import space.kscience.dataforge.provider.Provider @@ -18,7 +18,7 @@ import space.kscience.dataforge.provider.Provider * * create - configure - attach - detach - destroy */ -@Type(TARGET) +@DfId(TARGET) public interface Plugin : Named, ContextAware, Provider, MetaRepr { /** diff --git a/dataforge-context/src/commonMain/kotlin/space/kscience/dataforge/context/PluginFactory.kt b/dataforge-context/src/commonMain/kotlin/space/kscience/dataforge/context/PluginFactory.kt index 308e3bbb..0273d327 100644 --- a/dataforge-context/src/commonMain/kotlin/space/kscience/dataforge/context/PluginFactory.kt +++ b/dataforge-context/src/commonMain/kotlin/space/kscience/dataforge/context/PluginFactory.kt @@ -1,9 +1,9 @@ package space.kscience.dataforge.context import space.kscience.dataforge.meta.Meta -import space.kscience.dataforge.misc.Type +import space.kscience.dataforge.misc.DfId -@Type(PluginFactory.TYPE) +@DfId(PluginFactory.TYPE) public interface PluginFactory : Factory { public val tag: PluginTag diff --git a/dataforge-context/src/jvmMain/kotlin/space/kscience/dataforge/provider/dfType.kt b/dataforge-context/src/jvmMain/kotlin/space/kscience/dataforge/provider/dfType.kt index b44e509b..ab34ea50 100644 --- a/dataforge-context/src/jvmMain/kotlin/space/kscience/dataforge/provider/dfType.kt +++ b/dataforge-context/src/jvmMain/kotlin/space/kscience/dataforge/provider/dfType.kt @@ -4,29 +4,29 @@ import space.kscience.dataforge.context.Context import space.kscience.dataforge.context.PluginBuilder import space.kscience.dataforge.context.gather import space.kscience.dataforge.misc.DFExperimental +import space.kscience.dataforge.misc.DfId import space.kscience.dataforge.misc.Named -import space.kscience.dataforge.misc.Type import space.kscience.dataforge.names.Name import kotlin.reflect.KClass import kotlin.reflect.full.findAnnotation @DFExperimental -public val KClass<*>.dfType: String - get() = findAnnotation()?.id ?: simpleName ?: "" +public val KClass<*>.dfId: String + get() = findAnnotation()?.id ?: simpleName ?: "" /** - * Provide an object with given name inferring target from its type using [Type] annotation + * Provide an object with given name inferring target from its type using [DfId] annotation */ @DFExperimental public inline fun Provider.provideByType(name: String): T? { - val target = T::class.dfType + val target = T::class.dfId return provide(target, name) } @DFExperimental public inline fun Provider.top(): Map { - val target = T::class.dfType + val target = T::class.dfId return top(target) } @@ -35,15 +35,15 @@ public inline fun Provider.top(): Map { */ @DFExperimental public inline fun Context.gather(inherit: Boolean = true): Map = - gather(T::class.dfType, inherit) + gather(T::class.dfId, inherit) @DFExperimental public inline fun PluginBuilder.provides(items: Map) { - provides(T::class.dfType, items) + provides(T::class.dfId, items) } @DFExperimental public inline fun PluginBuilder.provides(vararg items: Named) { - provides(T::class.dfType, *items) + provides(T::class.dfId, *items) } diff --git a/dataforge-data/src/commonMain/kotlin/space/kscience/dataforge/data/Data.kt b/dataforge-data/src/commonMain/kotlin/space/kscience/dataforge/data/Data.kt index e169b4a5..984582e5 100644 --- a/dataforge-data/src/commonMain/kotlin/space/kscience/dataforge/data/Data.kt +++ b/dataforge-data/src/commonMain/kotlin/space/kscience/dataforge/data/Data.kt @@ -5,7 +5,7 @@ import space.kscience.dataforge.meta.Meta import space.kscience.dataforge.meta.MetaRepr import space.kscience.dataforge.meta.isEmpty import space.kscience.dataforge.misc.DFInternal -import space.kscience.dataforge.misc.Type +import space.kscience.dataforge.misc.DfId import kotlin.coroutines.CoroutineContext import kotlin.coroutines.EmptyCoroutineContext import kotlin.reflect.KType @@ -14,7 +14,7 @@ import kotlin.reflect.typeOf /** * A data element characterized by its meta */ -@Type(Data.TYPE) +@DfId(Data.TYPE) public interface Data : Goal, MetaRepr { /** * Type marker for the data. The type is known before the calculation takes place so it could be checked. diff --git a/dataforge-data/src/commonMain/kotlin/space/kscience/dataforge/data/DataTree.kt b/dataforge-data/src/commonMain/kotlin/space/kscience/dataforge/data/DataTree.kt index b70744ce..bafcbea2 100644 --- a/dataforge-data/src/commonMain/kotlin/space/kscience/dataforge/data/DataTree.kt +++ b/dataforge-data/src/commonMain/kotlin/space/kscience/dataforge/data/DataTree.kt @@ -2,7 +2,7 @@ package space.kscience.dataforge.data import space.kscience.dataforge.meta.Meta import space.kscience.dataforge.misc.DFInternal -import space.kscience.dataforge.misc.Type +import space.kscience.dataforge.misc.DfId import space.kscience.dataforge.names.* import kotlin.collections.component1 import kotlin.collections.component2 @@ -31,7 +31,7 @@ public val DataTreeItem.type: KType /** * A tree-like [DataSet] grouped into the node. All data inside the node must inherit its type */ -@Type(DataTree.TYPE) +@DfId(DataTree.TYPE) public interface DataTree : DataSet { /** diff --git a/dataforge-io/src/commonMain/kotlin/space/kscience/dataforge/io/EnvelopeFormat.kt b/dataforge-io/src/commonMain/kotlin/space/kscience/dataforge/io/EnvelopeFormat.kt index 8a2a09b4..0df5ab27 100644 --- a/dataforge-io/src/commonMain/kotlin/space/kscience/dataforge/io/EnvelopeFormat.kt +++ b/dataforge-io/src/commonMain/kotlin/space/kscience/dataforge/io/EnvelopeFormat.kt @@ -4,7 +4,7 @@ import kotlinx.io.Source import space.kscience.dataforge.context.Context import space.kscience.dataforge.io.EnvelopeFormatFactory.Companion.ENVELOPE_FORMAT_TYPE import space.kscience.dataforge.meta.Meta -import space.kscience.dataforge.misc.Type +import space.kscience.dataforge.misc.DfId import space.kscience.dataforge.names.Name import space.kscience.dataforge.names.asName import kotlin.reflect.KType @@ -17,7 +17,7 @@ public interface EnvelopeFormat : IOFormat { public fun EnvelopeFormat.read(input: Source): Envelope = readFrom(input) -@Type(ENVELOPE_FORMAT_TYPE) +@DfId(ENVELOPE_FORMAT_TYPE) public interface EnvelopeFormatFactory : IOFormatFactory, EnvelopeFormat { override val type: KType get() = typeOf() diff --git a/dataforge-io/src/commonMain/kotlin/space/kscience/dataforge/io/IOFormat.kt b/dataforge-io/src/commonMain/kotlin/space/kscience/dataforge/io/IOFormat.kt index 36d28d17..ffcadf1a 100644 --- a/dataforge-io/src/commonMain/kotlin/space/kscience/dataforge/io/IOFormat.kt +++ b/dataforge-io/src/commonMain/kotlin/space/kscience/dataforge/io/IOFormat.kt @@ -7,8 +7,8 @@ import space.kscience.dataforge.context.Context import space.kscience.dataforge.context.Factory import space.kscience.dataforge.io.IOFormatFactory.Companion.IO_FORMAT_TYPE import space.kscience.dataforge.meta.Meta +import space.kscience.dataforge.misc.DfId import space.kscience.dataforge.misc.Named -import space.kscience.dataforge.misc.Type import space.kscience.dataforge.names.Name import space.kscience.dataforge.names.asName import kotlin.reflect.KType @@ -72,7 +72,7 @@ public fun Sink.writeWith(format: IOWriter, obj: T): Unit = format.writeTo(this, obj) -@Type(IO_FORMAT_TYPE) +@DfId(IO_FORMAT_TYPE) public interface IOFormatFactory : Factory>, Named { /** * Explicit type for dynamic type checks diff --git a/dataforge-io/src/commonMain/kotlin/space/kscience/dataforge/io/MetaFormat.kt b/dataforge-io/src/commonMain/kotlin/space/kscience/dataforge/io/MetaFormat.kt index dcffd657..cadf87ca 100644 --- a/dataforge-io/src/commonMain/kotlin/space/kscience/dataforge/io/MetaFormat.kt +++ b/dataforge-io/src/commonMain/kotlin/space/kscience/dataforge/io/MetaFormat.kt @@ -9,7 +9,7 @@ import space.kscience.dataforge.context.Global import space.kscience.dataforge.io.MetaFormatFactory.Companion.META_FORMAT_TYPE import space.kscience.dataforge.meta.Meta import space.kscience.dataforge.meta.descriptors.MetaDescriptor -import space.kscience.dataforge.misc.Type +import space.kscience.dataforge.misc.DfId import space.kscience.dataforge.names.Name import space.kscience.dataforge.names.asName import space.kscience.dataforge.names.plus @@ -38,7 +38,7 @@ public interface MetaFormat : IOFormat { public fun readMeta(source: Source, descriptor: MetaDescriptor? = null): Meta } -@Type(META_FORMAT_TYPE) +@DfId(META_FORMAT_TYPE) public interface MetaFormatFactory : IOFormatFactory, MetaFormat { public val shortName: String diff --git a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/Meta.kt b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/Meta.kt index 83e4ee0a..356ef4f6 100644 --- a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/Meta.kt +++ b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/Meta.kt @@ -2,7 +2,7 @@ package space.kscience.dataforge.meta import kotlinx.serialization.Serializable import kotlinx.serialization.json.Json -import space.kscience.dataforge.misc.Type +import space.kscience.dataforge.misc.DfId import space.kscience.dataforge.misc.unsafeCast import space.kscience.dataforge.names.* import kotlin.jvm.JvmName @@ -31,7 +31,7 @@ public fun interface MetaProvider : ValueProvider { * TODO add documentation * Same name siblings are supported via elements with the same [Name] but different indices. */ -@Type(Meta.TYPE) +@DfId(Meta.TYPE) @Serializable(MetaSerializer::class) public interface Meta : MetaRepr, MetaProvider { public val value: Value? diff --git a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/descriptors/MetaDescriptor.kt b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/descriptors/MetaDescriptor.kt index b6a91631..742b89ed 100644 --- a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/descriptors/MetaDescriptor.kt +++ b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/descriptors/MetaDescriptor.kt @@ -7,7 +7,7 @@ import space.kscience.dataforge.names.* /** * Restrictions on value in the node */ -public enum class ValueRequirement { +public enum class ValueRestriction { /** * No restrictions */ @@ -26,26 +26,25 @@ public enum class ValueRequirement { /** * The descriptor for a meta - * @param info description text + * @param description description text * @param children child descriptors for this node * @param multiple True if same name siblings with this name are allowed - * @param valueRequirement The requirements for node content + * @param valueRestriction The requirements for node content * @param valueTypes list of allowed types for [Meta.value], null if all values are allowed. * Empty list means that no value should be present in this node. * @param indexKey An index field by which this node is identified in case of same name siblings construct * @param defaultValue the default [Meta.value] for the node - * @param attributes additional attributes of this descriptor. For example validation and widget parameters + * @param attributes additional attributes of this descriptor. For example, validation and widget parameters */ @Serializable public data class MetaDescriptor( - public val info: String? = null, + public val description: String? = null, public val children: Map = emptyMap(), public val multiple: Boolean = false, - public val valueRequirement: ValueRequirement = ValueRequirement.NONE, + public val valueRestriction: ValueRestriction = ValueRestriction.NONE, public val valueTypes: List? = null, public val indexKey: String = Meta.INDEX_KEY, public val defaultValue: Value? = null, - public val readOnly: Boolean = false, public val attributes: Meta = Meta.EMPTY, ) { /** @@ -63,11 +62,12 @@ public data class MetaDescriptor( } public companion object { + public val EMPTY: MetaDescriptor = MetaDescriptor("Generic meta tree") internal const val ALLOWED_VALUES_KEY = "allowedValues" } } -public val MetaDescriptor.required: Boolean get() = valueRequirement == ValueRequirement.REQUIRED || children.values.any { required } +public val MetaDescriptor.required: Boolean get() = valueRestriction == ValueRestriction.REQUIRED || children.values.any { required } public val MetaDescriptor.allowedValues: List? get() = attributes[MetaDescriptor.ALLOWED_VALUES_KEY]?.value?.list @@ -80,9 +80,9 @@ public operator fun MetaDescriptor.get(name: Name): MetaDescriptor? = when (name public operator fun MetaDescriptor.get(name: String): MetaDescriptor? = get(name.parseAsName(true)) public fun MetaDescriptor.validate(value: Value?): Boolean = if (value == null) { - valueRequirement != ValueRequirement.REQUIRED + valueRestriction != ValueRestriction.REQUIRED } else { - if (valueRequirement == ValueRequirement.ABSENT) false + if (valueRestriction == ValueRestriction.ABSENT) false else { (valueTypes == null || value.type in valueTypes) && (allowedValues?.let { value in it } ?: true) } diff --git a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/descriptors/MetaDescriptorBuilder.kt b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/descriptors/MetaDescriptorBuilder.kt index 524342b0..ae6c171a 100644 --- a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/descriptors/MetaDescriptorBuilder.kt +++ b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/descriptors/MetaDescriptorBuilder.kt @@ -8,20 +8,26 @@ import space.kscience.dataforge.names.first import space.kscience.dataforge.names.length import kotlin.collections.set -public class MetaDescriptorBuilder @PublishedApi internal constructor() { +public class MetaDescriptorBuilder @PublishedApi internal constructor() { public var info: String? = null public var children: MutableMap = linkedMapOf() public var multiple: Boolean = false - public var valueRequirement: ValueRequirement = ValueRequirement.NONE - public var readOnly: Boolean = false + public var valueRestriction: ValueRestriction = ValueRestriction.NONE - public var type: List? = null + public var valueTypes: List? = null - public fun type(primaryType: ValueType, vararg otherTypes: ValueType) { - type = listOf(primaryType, *otherTypes) + public fun valueType(primaryType: ValueType, vararg otherTypes: ValueType) { + valueTypes = listOf(primaryType, *otherTypes) } + /** + * A key for indexing values. Should be changed in case of the name clash. + */ public var indexKey: String = Meta.INDEX_KEY + + /** + * The default value + */ public var default: Value? = null public fun default(value: Any?) { @@ -42,6 +48,7 @@ public class MetaDescriptorBuilder @PublishedApi internal constructor() { children[name.first().body] = target target } + else -> { children.getOrPut(name.first().body) { MetaDescriptorBuilder() }.item(name.cutFirst(), block) } @@ -51,16 +58,17 @@ public class MetaDescriptorBuilder @PublishedApi internal constructor() { public fun node( name: Name, descriptor: MetaDescriptor, - block: MetaDescriptorBuilder.() -> Unit = {} + block: MetaDescriptorBuilder.() -> Unit = {}, ): MetaDescriptorBuilder = when (name.length) { 0 -> error("Can't set descriptor to root") 1 -> { val item = descriptor.toBuilder().apply { - valueRequirement = ValueRequirement.ABSENT + valueRestriction = ValueRestriction.ABSENT }.apply(block) children[name.first().body] = item item } + else -> children.getOrPut(name.first().body) { MetaDescriptorBuilder() }.node(name.cutFirst(), descriptor, block) @@ -79,14 +87,13 @@ public class MetaDescriptorBuilder @PublishedApi internal constructor() { @PublishedApi internal fun build(): MetaDescriptor = MetaDescriptor( - info = info, + description = info, children = children.mapValues { it.value.build() }, multiple = multiple, - valueRequirement = valueRequirement, - valueTypes = type, + valueRestriction = valueRestriction, + valueTypes = valueTypes, indexKey = indexKey, defaultValue = default, - readOnly = readOnly, attributes = attributes ) } @@ -104,9 +111,9 @@ public fun MetaDescriptorBuilder.value( name: Name, type: ValueType, vararg additionalTypes: ValueType, - block: MetaDescriptorBuilder.() -> Unit = {} + block: MetaDescriptorBuilder.() -> Unit = {}, ): MetaDescriptorBuilder = item(name) { - type(type, *additionalTypes) + valueType(type, *additionalTypes) block() } @@ -114,16 +121,16 @@ public fun MetaDescriptorBuilder.value( name: String, type: ValueType, vararg additionalTypes: ValueType, - block: MetaDescriptorBuilder.() -> Unit = {} + block: MetaDescriptorBuilder.() -> Unit = {}, ): MetaDescriptorBuilder = value(Name.parse(name), type, additionalTypes = additionalTypes, block) /** * Create and configure child value descriptor */ public fun MetaDescriptorBuilder.node( - name: Name, block: MetaDescriptorBuilder.() -> Unit + name: Name, block: MetaDescriptorBuilder.() -> Unit, ): MetaDescriptorBuilder = item(name) { - valueRequirement = ValueRequirement.ABSENT + valueRestriction = ValueRestriction.ABSENT block() } @@ -142,7 +149,7 @@ public fun MetaDescriptorBuilder.node( } public fun MetaDescriptorBuilder.required() { - valueRequirement = ValueRequirement.REQUIRED + valueRestriction = ValueRestriction.REQUIRED } public inline fun > MetaDescriptorBuilder.enum( @@ -158,11 +165,11 @@ public inline fun > MetaDescriptorBuilder.enum( } private fun MetaDescriptor.toBuilder(): MetaDescriptorBuilder = MetaDescriptorBuilder().apply { - info = this@toBuilder.info + info = this@toBuilder.description children = this@toBuilder.children.mapValuesTo(LinkedHashMap()) { it.value.toBuilder() } multiple = this@toBuilder.multiple - valueRequirement = this@toBuilder.valueRequirement - type = this@toBuilder.valueTypes + valueRestriction = this@toBuilder.valueRestriction + valueTypes = this@toBuilder.valueTypes indexKey = this@toBuilder.indexKey default = defaultValue attributes = this@toBuilder.attributes.toMutableMeta() diff --git a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/transformations/MetaConverter.kt b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/transformations/MetaConverter.kt index 2d66daf2..0dfb63d7 100644 --- a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/transformations/MetaConverter.kt +++ b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/transformations/MetaConverter.kt @@ -1,12 +1,25 @@ package space.kscience.dataforge.meta.transformations import space.kscience.dataforge.meta.* +import space.kscience.dataforge.meta.descriptors.MetaDescriptor +import kotlin.reflect.KType +import kotlin.reflect.typeOf /** * A converter of generic object to and from [Meta] */ public interface MetaConverter { + /** + * Runtime type of [T] + */ + public val type: KType + + /** + * A descriptor for resulting meta + */ + public val descriptor: MetaDescriptor get() = MetaDescriptor.EMPTY + /** * Attempt conversion of [meta] to an object or return null if conversion failed */ @@ -20,51 +33,105 @@ public interface MetaConverter { public companion object { public val meta: MetaConverter = object : MetaConverter { + override val type: KType = typeOf() + override fun metaToObjectOrNull(meta: Meta): Meta = meta override fun objectToMeta(obj: Meta): Meta = obj } public val value: MetaConverter = object : MetaConverter { + override val type: KType = typeOf() + override fun metaToObjectOrNull(meta: Meta): Value? = meta.value override fun objectToMeta(obj: Value): Meta = Meta(obj) } public val string: MetaConverter = object : MetaConverter { + override val type: KType = typeOf() + + override val descriptor: MetaDescriptor = MetaDescriptor { + valueType(ValueType.STRING) + } + + override fun metaToObjectOrNull(meta: Meta): String? = meta.string override fun objectToMeta(obj: String): Meta = Meta(obj.asValue()) } public val boolean: MetaConverter = object : MetaConverter { + override val type: KType = typeOf() + + override val descriptor: MetaDescriptor = MetaDescriptor { + valueType(ValueType.BOOLEAN) + } + override fun metaToObjectOrNull(meta: Meta): Boolean? = meta.boolean override fun objectToMeta(obj: Boolean): Meta = Meta(obj.asValue()) } public val number: MetaConverter = object : MetaConverter { + override val type: KType = typeOf() + + override val descriptor: MetaDescriptor = MetaDescriptor { + valueType(ValueType.NUMBER) + } + override fun metaToObjectOrNull(meta: Meta): Number? = meta.number override fun objectToMeta(obj: Number): Meta = Meta(obj.asValue()) } public val double: MetaConverter = object : MetaConverter { + override val type: KType = typeOf() + + override val descriptor: MetaDescriptor = MetaDescriptor { + valueType(ValueType.NUMBER) + } + override fun metaToObjectOrNull(meta: Meta): Double? = meta.double override fun objectToMeta(obj: Double): Meta = Meta(obj.asValue()) } public val float: MetaConverter = object : MetaConverter { + override val type: KType = typeOf() + + override val descriptor: MetaDescriptor = MetaDescriptor { + valueType(ValueType.NUMBER) + } + override fun metaToObjectOrNull(meta: Meta): Float? = meta.float override fun objectToMeta(obj: Float): Meta = Meta(obj.asValue()) } public val int: MetaConverter = object : MetaConverter { + override val type: KType = typeOf() + + override val descriptor: MetaDescriptor = MetaDescriptor { + valueType(ValueType.NUMBER) + } + override fun metaToObjectOrNull(meta: Meta): Int? = meta.int override fun objectToMeta(obj: Int): Meta = Meta(obj.asValue()) } public val long: MetaConverter = object : MetaConverter { + override val type: KType = typeOf() + + override val descriptor: MetaDescriptor = MetaDescriptor { + valueType(ValueType.NUMBER) + } + override fun metaToObjectOrNull(meta: Meta): Long? = meta.long override fun objectToMeta(obj: Long): Meta = Meta(obj.asValue()) } public inline fun > enum(): MetaConverter = object : MetaConverter { + override val type: KType = typeOf() + + override val descriptor: MetaDescriptor = MetaDescriptor { + valueType(ValueType.STRING) + allowedValues(enumValues()) + } + @Suppress("USELESS_CAST") override fun metaToObjectOrNull(meta: Meta): E = meta.enum() as? E ?: error("The Item is not a Enum") @@ -76,6 +143,12 @@ public interface MetaConverter { reader: (Value) -> T, ): MetaConverter> = object : MetaConverter> { + override val type: KType = typeOf>() + + override val descriptor: MetaDescriptor = MetaDescriptor { + valueType(ValueType.LIST) + } + override fun metaToObjectOrNull(meta: Meta): List? = meta.value?.list?.map(reader) override fun objectToMeta(obj: List): Meta = Meta(obj.map(writer).asValue()) diff --git a/dataforge-workspace/src/commonMain/kotlin/space/kscience/dataforge/workspace/Task.kt b/dataforge-workspace/src/commonMain/kotlin/space/kscience/dataforge/workspace/Task.kt index 782b925f..a1ef7be2 100644 --- a/dataforge-workspace/src/commonMain/kotlin/space/kscience/dataforge/workspace/Task.kt +++ b/dataforge-workspace/src/commonMain/kotlin/space/kscience/dataforge/workspace/Task.kt @@ -9,7 +9,7 @@ import space.kscience.dataforge.meta.MetaRepr import space.kscience.dataforge.meta.Specification import space.kscience.dataforge.meta.descriptors.Described import space.kscience.dataforge.meta.descriptors.MetaDescriptor -import space.kscience.dataforge.misc.Type +import space.kscience.dataforge.misc.DfId import space.kscience.dataforge.names.Name import space.kscience.dataforge.workspace.Task.Companion.TYPE import kotlin.reflect.KType @@ -19,7 +19,7 @@ import kotlin.reflect.typeOf * A configurable task that could be executed on a workspace. The [TaskResult] represents a lazy result of the task. * In general no computations should be made until the result is called. */ -@Type(TYPE) +@DfId(TYPE) public interface Task : Described { /** diff --git a/dataforge-workspace/src/commonMain/kotlin/space/kscience/dataforge/workspace/Workspace.kt b/dataforge-workspace/src/commonMain/kotlin/space/kscience/dataforge/workspace/Workspace.kt index 52f92037..ee00f539 100644 --- a/dataforge-workspace/src/commonMain/kotlin/space/kscience/dataforge/workspace/Workspace.kt +++ b/dataforge-workspace/src/commonMain/kotlin/space/kscience/dataforge/workspace/Workspace.kt @@ -6,7 +6,7 @@ import space.kscience.dataforge.data.DataSet import space.kscience.dataforge.data.asSequence import space.kscience.dataforge.meta.Meta import space.kscience.dataforge.meta.MutableMeta -import space.kscience.dataforge.misc.Type +import space.kscience.dataforge.misc.DfId import space.kscience.dataforge.names.Name import space.kscience.dataforge.provider.Provider @@ -18,7 +18,7 @@ public interface DataSelector{ /** * An environment for pull-mode computation */ -@Type(Workspace.TYPE) +@DfId(Workspace.TYPE) public interface Workspace : ContextAware, Provider { /** * The whole data node for current workspace