Compare commits
2 Commits
2cd1885420
...
5a75b05acd
Author | SHA1 | Date | |
---|---|---|---|
5a75b05acd | |||
3de5691c84 |
@ -3,12 +3,17 @@
|
|||||||
## Unreleased
|
## Unreleased
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
- Custom CoroutineContext during `Context` creation.
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
- Kotlin 2.0
|
||||||
|
- `MetaSpec` renamed to `MetaReader`. MetaSpec is now reserved for builder-based generation of meta descriptors.
|
||||||
|
- Add self-type for Meta. Remove unsafe cast method for meta instances.
|
||||||
|
|
||||||
### Deprecated
|
### Deprecated
|
||||||
|
|
||||||
### Removed
|
### Removed
|
||||||
|
- Automatic descriptors for schema. It is not possible to implement them without heavy reflection.
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
|
@ -3,21 +3,21 @@ import space.kscience.gradle.useApache2Licence
|
|||||||
import space.kscience.gradle.useSPCTeam
|
import space.kscience.gradle.useSPCTeam
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
id("space.kscience.gradle.project")
|
alias(spclibs.plugins.kscience.project)
|
||||||
id("org.jetbrains.kotlinx.kover") version "0.7.6"
|
alias(spclibs.plugins.kotlinx.kover)
|
||||||
}
|
}
|
||||||
|
|
||||||
allprojects {
|
allprojects {
|
||||||
group = "space.kscience"
|
group = "space.kscience"
|
||||||
version = "0.8.2"
|
version = "0.9.0-dev-1"
|
||||||
}
|
}
|
||||||
|
|
||||||
subprojects {
|
subprojects {
|
||||||
apply(plugin = "maven-publish")
|
apply(plugin = "maven-publish")
|
||||||
|
|
||||||
tasks.withType<KotlinCompile> {
|
tasks.withType<KotlinCompile> {
|
||||||
kotlinOptions {
|
compilerOptions {
|
||||||
freeCompilerArgs = freeCompilerArgs + "-Xcontext-receivers"
|
freeCompilerArgs.add("-Xcontext-receivers")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,7 @@ import space.kscience.dataforge.misc.ThreadSafe
|
|||||||
import space.kscience.dataforge.names.Name
|
import space.kscience.dataforge.names.Name
|
||||||
import space.kscience.dataforge.provider.Provider
|
import space.kscience.dataforge.provider.Provider
|
||||||
import kotlin.coroutines.CoroutineContext
|
import kotlin.coroutines.CoroutineContext
|
||||||
|
import kotlin.coroutines.EmptyCoroutineContext
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The local environment for anything being done in DataForge framework. Contexts are organized into tree structure with [Global] at the top.
|
* The local environment for anything being done in DataForge framework. Contexts are organized into tree structure with [Global] at the top.
|
||||||
@ -26,6 +27,7 @@ public open class Context internal constructor(
|
|||||||
public val parent: Context?,
|
public val parent: Context?,
|
||||||
plugins: Set<Plugin>, // set of unattached plugins
|
plugins: Set<Plugin>, // set of unattached plugins
|
||||||
meta: Meta,
|
meta: Meta,
|
||||||
|
coroutineContext: CoroutineContext = EmptyCoroutineContext,
|
||||||
) : Named, MetaRepr, Provider, CoroutineScope {
|
) : Named, MetaRepr, Provider, CoroutineScope {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -65,7 +67,7 @@ public open class Context internal constructor(
|
|||||||
|
|
||||||
override val coroutineContext: CoroutineContext by lazy {
|
override val coroutineContext: CoroutineContext by lazy {
|
||||||
(parent ?: Global).coroutineContext.let { parenContext ->
|
(parent ?: Global).coroutineContext.let { parenContext ->
|
||||||
parenContext + SupervisorJob(parenContext[Job])
|
parenContext + coroutineContext + SupervisorJob(parenContext[Job])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,6 +13,8 @@ import space.kscience.dataforge.names.plus
|
|||||||
import kotlin.collections.component1
|
import kotlin.collections.component1
|
||||||
import kotlin.collections.component2
|
import kotlin.collections.component2
|
||||||
import kotlin.collections.set
|
import kotlin.collections.set
|
||||||
|
import kotlin.coroutines.CoroutineContext
|
||||||
|
import kotlin.coroutines.EmptyCoroutineContext
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A convenience builder for context
|
* A convenience builder for context
|
||||||
@ -59,6 +61,13 @@ public class ContextBuilder internal constructor(
|
|||||||
plugin(DeFactoPluginFactory(plugin))
|
plugin(DeFactoPluginFactory(plugin))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private var coroutineContext: CoroutineContext = EmptyCoroutineContext
|
||||||
|
|
||||||
|
public fun coroutineContext(coroutineContext: CoroutineContext) {
|
||||||
|
this.coroutineContext = coroutineContext
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public fun build(): Context {
|
public fun build(): Context {
|
||||||
val contextName = name ?: NameToken("@auto", hashCode().toUInt().toString(16)).asName()
|
val contextName = name ?: NameToken("@auto", hashCode().toUInt().toString(16)).asName()
|
||||||
val plugins = HashMap<PluginTag, Plugin>()
|
val plugins = HashMap<PluginTag, Plugin>()
|
||||||
@ -86,7 +95,7 @@ public class ContextBuilder internal constructor(
|
|||||||
addPlugin(factory, meta)
|
addPlugin(factory, meta)
|
||||||
}
|
}
|
||||||
|
|
||||||
return Context(contextName, parent, plugins.values.toSet(), meta.seal())
|
return Context(contextName, parent, plugins.values.toSet(), meta.seal(), coroutineContext)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ import space.kscience.dataforge.meta.*
|
|||||||
import space.kscience.dataforge.misc.DFExperimental
|
import space.kscience.dataforge.misc.DFExperimental
|
||||||
|
|
||||||
@DFExperimental
|
@DFExperimental
|
||||||
public fun <T> ObservableMeta.asFlow(converter: MetaSpec<T>): Flow<T> = callbackFlow {
|
public fun <T> ObservableMeta.asFlow(converter: MetaReader<T>): Flow<T> = callbackFlow {
|
||||||
onChange(this){
|
onChange(this){
|
||||||
trySend(converter.read(this))
|
trySend(converter.read(this))
|
||||||
}
|
}
|
||||||
|
@ -4,18 +4,12 @@ import kotlinx.serialization.ExperimentalSerializationApi
|
|||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
import kotlinx.serialization.json.decodeFromStream
|
import kotlinx.serialization.json.decodeFromStream
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import space.kscience.dataforge.meta.Scheme
|
|
||||||
import space.kscience.dataforge.meta.SchemeSpec
|
|
||||||
import space.kscience.dataforge.meta.ValueType
|
|
||||||
import space.kscience.dataforge.meta.descriptors.MetaDescriptor
|
import space.kscience.dataforge.meta.descriptors.MetaDescriptor
|
||||||
import space.kscience.dataforge.meta.descriptors.MetaDescriptorBuilder
|
import space.kscience.dataforge.meta.descriptors.MetaDescriptorBuilder
|
||||||
import space.kscience.dataforge.meta.descriptors.node
|
|
||||||
import space.kscience.dataforge.misc.DFExperimental
|
import space.kscience.dataforge.misc.DFExperimental
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
import kotlin.reflect.KClass
|
import kotlin.reflect.KAnnotatedElement
|
||||||
import kotlin.reflect.full.isSubclassOf
|
import kotlin.reflect.KProperty
|
||||||
import kotlin.reflect.full.memberProperties
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Description text for meta property, node or whole object
|
* Description text for meta property, node or whole object
|
||||||
@ -59,18 +53,8 @@ private fun MetaDescriptorBuilder.loadDescriptorFromResource(resource: Descripto
|
|||||||
}
|
}
|
||||||
|
|
||||||
@DFExperimental
|
@DFExperimental
|
||||||
public fun MetaDescriptor.Companion.forClass(
|
public fun MetaDescriptorBuilder.forAnnotatedElement(element: KAnnotatedElement) {
|
||||||
kClass: KClass<out Any>,
|
element.annotations.forEach {
|
||||||
mod: MetaDescriptorBuilder.() -> Unit = {},
|
|
||||||
): MetaDescriptor = MetaDescriptor {
|
|
||||||
when {
|
|
||||||
kClass.isSubclassOf(Number::class) -> valueType(ValueType.NUMBER)
|
|
||||||
kClass == String::class -> ValueType.STRING
|
|
||||||
kClass == Boolean::class -> ValueType.BOOLEAN
|
|
||||||
kClass == DoubleArray::class -> ValueType.LIST
|
|
||||||
}
|
|
||||||
|
|
||||||
kClass.annotations.forEach {
|
|
||||||
when (it) {
|
when (it) {
|
||||||
is Description -> description = it.value
|
is Description -> description = it.value
|
||||||
|
|
||||||
@ -79,47 +63,70 @@ public fun MetaDescriptor.Companion.forClass(
|
|||||||
is DescriptorUrl -> loadDescriptorFromUrl(URL(it.url))
|
is DescriptorUrl -> loadDescriptorFromUrl(URL(it.url))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
kClass.memberProperties.forEach { property ->
|
|
||||||
|
|
||||||
var flag = false
|
|
||||||
|
|
||||||
val descriptor = MetaDescriptor {
|
|
||||||
//use base type descriptor as a base
|
|
||||||
(property.returnType.classifier as? KClass<*>)?.let {
|
|
||||||
from(forClass(it))
|
|
||||||
}
|
|
||||||
|
|
||||||
property.annotations.forEach {
|
|
||||||
when (it) {
|
|
||||||
is Description -> {
|
|
||||||
description = it.value
|
|
||||||
flag = true
|
|
||||||
}
|
|
||||||
|
|
||||||
is Multiple -> {
|
|
||||||
multiple = true
|
|
||||||
flag = true
|
|
||||||
}
|
|
||||||
|
|
||||||
is DescriptorResource -> {
|
|
||||||
loadDescriptorFromResource(it)
|
|
||||||
flag = true
|
|
||||||
}
|
|
||||||
|
|
||||||
is DescriptorUrl -> {
|
|
||||||
loadDescriptorFromUrl(URL(it.url))
|
|
||||||
flag = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (flag) {
|
|
||||||
node(property.name, descriptor)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
mod()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@DFExperimental
|
@DFExperimental
|
||||||
public inline fun <reified T : Scheme> SchemeSpec<T>.autoDescriptor(noinline mod: MetaDescriptorBuilder.() -> Unit = {}): MetaDescriptor =
|
public fun MetaDescriptorBuilder.forProperty(property: KProperty<*>) {
|
||||||
MetaDescriptor.forClass(T::class, mod)
|
property.annotations.forEach {
|
||||||
|
when (it) {
|
||||||
|
is Description -> description = it.value
|
||||||
|
|
||||||
|
is DescriptorResource -> loadDescriptorFromResource(it)
|
||||||
|
|
||||||
|
is DescriptorUrl -> loadDescriptorFromUrl(URL(it.url))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//
|
||||||
|
//@DFExperimental
|
||||||
|
//public fun <T : Scheme> MetaDescriptor.Companion.forScheme(
|
||||||
|
// spec: SchemeSpec<T>,
|
||||||
|
// mod: MetaDescriptorBuilder.() -> Unit = {},
|
||||||
|
//): MetaDescriptor = MetaDescriptor {
|
||||||
|
// val scheme = spec.empty()
|
||||||
|
// val kClass: KClass<T> = scheme::class as KClass<T>
|
||||||
|
// when {
|
||||||
|
// kClass.isSubclassOf(Number::class) -> valueType(ValueType.NUMBER)
|
||||||
|
// kClass == String::class -> ValueType.STRING
|
||||||
|
// kClass == Boolean::class -> ValueType.BOOLEAN
|
||||||
|
// kClass == DoubleArray::class -> ValueType.LIST
|
||||||
|
// kClass == ByteArray::class -> ValueType.LIST
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// forAnnotatedElement(kClass)
|
||||||
|
// kClass.memberProperties.forEach { property ->
|
||||||
|
// node(property.name) {
|
||||||
|
//
|
||||||
|
// (property.getDelegate(scheme) as? MetaDelegate<*>)?.descriptor?.let {
|
||||||
|
// from(it)
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// property.annotations.forEach {
|
||||||
|
// when (it) {
|
||||||
|
// is Description -> {
|
||||||
|
// description = it.value
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// is Multiple -> {
|
||||||
|
// multiple = true
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// is DescriptorResource -> {
|
||||||
|
// loadDescriptorFromResource(it)
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// is DescriptorUrl -> {
|
||||||
|
// loadDescriptorFromUrl(URL(it.url))
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
// mod()
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
//@DFExperimental
|
||||||
|
//public inline fun <reified T : Scheme> SchemeSpec<T>.autoDescriptor(
|
||||||
|
// noinline mod: MetaDescriptorBuilder.() -> Unit = {},
|
||||||
|
//): MetaDescriptor = MetaDescriptor.forScheme(this, mod)
|
@ -2,35 +2,28 @@
|
|||||||
|
|
||||||
package space.kscience.dataforge.descriptors
|
package space.kscience.dataforge.descriptors
|
||||||
|
|
||||||
import kotlinx.serialization.encodeToString
|
|
||||||
import kotlinx.serialization.json.Json
|
|
||||||
import org.junit.jupiter.api.Test
|
|
||||||
import space.kscience.dataforge.meta.Scheme
|
|
||||||
import space.kscience.dataforge.meta.SchemeSpec
|
|
||||||
import space.kscience.dataforge.meta.descriptors.MetaDescriptor
|
|
||||||
import space.kscience.dataforge.meta.int
|
|
||||||
import space.kscience.dataforge.meta.string
|
|
||||||
import space.kscience.dataforge.misc.DFExperimental
|
import space.kscience.dataforge.misc.DFExperimental
|
||||||
|
|
||||||
private class TestScheme : Scheme() {
|
//
|
||||||
|
//class TestScheme : Scheme() {
|
||||||
@Description("A")
|
//
|
||||||
val a by string()
|
// @Description("A")
|
||||||
|
// val a by string()
|
||||||
@Description("B")
|
//
|
||||||
val b by int()
|
// @Description("B")
|
||||||
|
// val b by int()
|
||||||
val c by int()
|
//
|
||||||
|
// val c by int()
|
||||||
companion object : SchemeSpec<TestScheme>(::TestScheme) {
|
//
|
||||||
override val descriptor: MetaDescriptor = autoDescriptor()
|
// companion object : SchemeSpec<TestScheme>(::TestScheme) {
|
||||||
}
|
// override val descriptor: MetaDescriptor = autoDescriptor()
|
||||||
}
|
// }
|
||||||
|
//}
|
||||||
class TestAutoDescriptors {
|
//
|
||||||
@Test
|
//class TestAutoDescriptors {
|
||||||
fun autoDescriptor() {
|
// @Test
|
||||||
val autoDescriptor = MetaDescriptor.forClass(TestScheme::class)
|
// fun autoDescriptor() {
|
||||||
println(Json { prettyPrint = true }.encodeToString(autoDescriptor))
|
// val autoDescriptor = MetaDescriptor.forScheme(TestScheme)
|
||||||
}
|
// println(Json { prettyPrint = true }.encodeToString(autoDescriptor))
|
||||||
}
|
// }
|
||||||
|
//}
|
@ -18,6 +18,24 @@ public data class NamedValueWithMeta<T>(val name: Name, val value: T, val meta:
|
|||||||
public suspend fun <T> NamedData<T>.awaitWithMeta(): NamedValueWithMeta<T> =
|
public suspend fun <T> NamedData<T>.awaitWithMeta(): NamedValueWithMeta<T> =
|
||||||
NamedValueWithMeta(name, await(), meta)
|
NamedValueWithMeta(name, await(), meta)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lazily transform this data to another data. By convention [block] should not use external data (be pure).
|
||||||
|
* @param type explicit type of the resulting [Data]
|
||||||
|
* @param coroutineContext additional [CoroutineContext] elements used for data computation.
|
||||||
|
* @param meta for the resulting data. By default equals input data.
|
||||||
|
* @param block the transformation itself
|
||||||
|
*/
|
||||||
|
@UnsafeKType
|
||||||
|
public fun <T, R> Data<T>.transform(
|
||||||
|
type: KType,
|
||||||
|
meta: Meta = this.meta,
|
||||||
|
coroutineContext: CoroutineContext = EmptyCoroutineContext,
|
||||||
|
block: suspend (T) -> R,
|
||||||
|
): Data<R> = Data(type, meta, coroutineContext, listOf(this)) {
|
||||||
|
block(await())
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Lazily transform this data to another data. By convention [block] should not use external data (be pure).
|
* Lazily transform this data to another data. By convention [block] should not use external data (be pure).
|
||||||
|
@ -43,6 +43,7 @@ internal class ActionsTest {
|
|||||||
repeat(10) {
|
repeat(10) {
|
||||||
source.updateValue(it.toString(), it)
|
source.updateValue(it.toString(), it)
|
||||||
}
|
}
|
||||||
|
|
||||||
result.updates.take(10).onEach { println(it.name) }.collect()
|
result.updates.take(10).onEach { println(it.name) }.collect()
|
||||||
|
|
||||||
assertEquals(2, result["1"]?.await())
|
assertEquals(2, result["1"]?.await())
|
||||||
|
@ -9,6 +9,8 @@ import space.kscience.dataforge.names.NameToken
|
|||||||
*/
|
*/
|
||||||
public class Laminate internal constructor(public val layers: List<Meta>) : TypedMeta<Laminate> {
|
public class Laminate internal constructor(public val layers: List<Meta>) : TypedMeta<Laminate> {
|
||||||
|
|
||||||
|
override val self: Laminate get() = this
|
||||||
|
|
||||||
override val value: Value? = layers.firstNotNullOfOrNull { it.value }
|
override val value: Value? = layers.firstNotNullOfOrNull { it.value }
|
||||||
|
|
||||||
override val items: Map<NameToken, Laminate> by lazy {
|
override val items: Map<NameToken, Laminate> by lazy {
|
||||||
|
@ -3,7 +3,6 @@ package space.kscience.dataforge.meta
|
|||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
import space.kscience.dataforge.misc.DfType
|
import space.kscience.dataforge.misc.DfType
|
||||||
import space.kscience.dataforge.misc.unsafeCast
|
|
||||||
import space.kscience.dataforge.names.*
|
import space.kscience.dataforge.names.*
|
||||||
import kotlin.jvm.JvmName
|
import kotlin.jvm.JvmName
|
||||||
|
|
||||||
@ -151,6 +150,8 @@ public interface TypedMeta<out M : TypedMeta<M>> : Meta {
|
|||||||
|
|
||||||
override val items: Map<NameToken, M>
|
override val items: Map<NameToken, M>
|
||||||
|
|
||||||
|
public val self: M
|
||||||
|
|
||||||
override fun get(name: Name): M? {
|
override fun get(name: Name): M? {
|
||||||
tailrec fun M.find(name: Name): M? = if (name.isEmpty()) {
|
tailrec fun M.find(name: Name): M? = if (name.isEmpty()) {
|
||||||
this
|
this
|
||||||
@ -164,11 +165,6 @@ public interface TypedMeta<out M : TypedMeta<M>> : Meta {
|
|||||||
override fun toMeta(): Meta = this
|
override fun toMeta(): Meta = this
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Access self as a recursive type instance
|
|
||||||
*/
|
|
||||||
public inline val <M : TypedMeta<M>> TypedMeta<M>.self: M get() = unsafeCast()
|
|
||||||
|
|
||||||
//public typealias Meta = TypedMeta<*>
|
//public typealias Meta = TypedMeta<*>
|
||||||
|
|
||||||
public operator fun <M : TypedMeta<M>> TypedMeta<M>?.get(token: NameToken): M? = this?.items?.get(token)
|
public operator fun <M : TypedMeta<M>> TypedMeta<M>?.get(token: NameToken): M? = this?.items?.get(token)
|
||||||
|
@ -11,7 +11,7 @@ import space.kscience.dataforge.misc.DFExperimental
|
|||||||
/**
|
/**
|
||||||
* A converter of generic object to and from [Meta]
|
* A converter of generic object to and from [Meta]
|
||||||
*/
|
*/
|
||||||
public interface MetaConverter<T>: MetaSpec<T> {
|
public interface MetaConverter<T>: MetaReader<T> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A descriptor for resulting meta
|
* A descriptor for resulting meta
|
||||||
|
@ -25,16 +25,16 @@ public fun MetaProvider.node(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Use [metaSpec] to read the Meta node
|
* Use [metaReader] to read the Meta node
|
||||||
*/
|
*/
|
||||||
public fun <T> MetaProvider.spec(
|
public fun <T> MetaProvider.spec(
|
||||||
metaSpec: MetaSpec<T>,
|
metaReader: MetaReader<T>,
|
||||||
key: Name? = null,
|
key: Name? = null,
|
||||||
): MetaDelegate<T?> = object : MetaDelegate<T?> {
|
): MetaDelegate<T?> = object : MetaDelegate<T?> {
|
||||||
override val descriptor: MetaDescriptor? get() = metaSpec.descriptor
|
override val descriptor: MetaDescriptor? get() = metaReader.descriptor
|
||||||
|
|
||||||
override fun getValue(thisRef: Any?, property: KProperty<*>): T? {
|
override fun getValue(thisRef: Any?, property: KProperty<*>): T? {
|
||||||
return get(key ?: property.name.asName())?.let { metaSpec.read(it) }
|
return get(key ?: property.name.asName())?.let { metaReader.read(it) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,14 +50,14 @@ public inline fun <reified T> MetaProvider.serializable(
|
|||||||
@Deprecated("Use convertable", ReplaceWith("convertable(converter, key)"))
|
@Deprecated("Use convertable", ReplaceWith("convertable(converter, key)"))
|
||||||
public fun <T> MetaProvider.node(
|
public fun <T> MetaProvider.node(
|
||||||
key: Name? = null,
|
key: Name? = null,
|
||||||
converter: MetaSpec<T>,
|
converter: MetaReader<T>,
|
||||||
): ReadOnlyProperty<Any?, T?> = spec(converter, key)
|
): ReadOnlyProperty<Any?, T?> = spec(converter, key)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Use [converter] to convert a list of same name siblings meta to object
|
* Use [converter] to convert a list of same name siblings meta to object
|
||||||
*/
|
*/
|
||||||
public fun <T> Meta.listOfSpec(
|
public fun <T> Meta.listOfSpec(
|
||||||
converter: MetaSpec<T>,
|
converter: MetaReader<T>,
|
||||||
key: Name? = null,
|
key: Name? = null,
|
||||||
): MetaDelegate<List<T>> = object : MetaDelegate<List<T>> {
|
): MetaDelegate<List<T>> = object : MetaDelegate<List<T>> {
|
||||||
override fun getValue(thisRef: Any?, property: KProperty<*>): List<T> {
|
override fun getValue(thisRef: Any?, property: KProperty<*>): List<T> {
|
||||||
|
@ -2,7 +2,7 @@ package space.kscience.dataforge.meta
|
|||||||
|
|
||||||
import space.kscience.dataforge.meta.descriptors.Described
|
import space.kscience.dataforge.meta.descriptors.Described
|
||||||
|
|
||||||
public interface MetaSpec<out T> : Described {
|
public interface MetaReader<out T> : Described {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read the source meta into an object and return null if Meta could not be interpreted as a target type
|
* Read the source meta into an object and return null if Meta could not be interpreted as a target type
|
||||||
@ -10,12 +10,12 @@ public interface MetaSpec<out T> : Described {
|
|||||||
public fun readOrNull(source: Meta): T?
|
public fun readOrNull(source: Meta): T?
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read generic read-only meta with this [MetaSpec] producing instance of the desired type.
|
* Read generic read-only meta with this [MetaReader] producing instance of the desired type.
|
||||||
* Throws an error if conversion could not be done.
|
* Throws an error if conversion could not be done.
|
||||||
*/
|
*/
|
||||||
public fun read(source: Meta): T = readOrNull(source) ?: error("Meta $source could not be interpreted by $this")
|
public fun read(source: Meta): T = readOrNull(source) ?: error("Meta $source could not be interpreted by $this")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public fun <T : Any> MetaSpec<T>.readNullable(item: Meta?): T? = item?.let { read(it) }
|
public fun <T : Any> MetaReader<T>.readNullable(item: Meta?): T? = item?.let { read(it) }
|
||||||
public fun <T> MetaSpec<T>.readValue(value: Value): T? = read(Meta(value))
|
public fun <T> MetaReader<T>.readValue(value: Value): T? = read(Meta(value))
|
@ -0,0 +1,64 @@
|
|||||||
|
package space.kscience.dataforge.meta
|
||||||
|
|
||||||
|
import space.kscience.dataforge.meta.descriptors.Described
|
||||||
|
import space.kscience.dataforge.meta.descriptors.MetaDescriptor
|
||||||
|
import space.kscience.dataforge.meta.descriptors.MetaDescriptorBuilder
|
||||||
|
import space.kscience.dataforge.misc.DFExperimental
|
||||||
|
import space.kscience.dataforge.names.Name
|
||||||
|
import space.kscience.dataforge.names.asName
|
||||||
|
import kotlin.properties.PropertyDelegateProvider
|
||||||
|
import kotlin.properties.ReadOnlyProperty
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A reference to a read-only value of type [T] inside [MetaProvider]
|
||||||
|
*/
|
||||||
|
@DFExperimental
|
||||||
|
public data class MetaRef<T>(
|
||||||
|
public val name: Name,
|
||||||
|
public val converter: MetaConverter<T>,
|
||||||
|
override val descriptor: MetaDescriptor? = converter.descriptor,
|
||||||
|
) : Described
|
||||||
|
|
||||||
|
@DFExperimental
|
||||||
|
public operator fun <T> MetaProvider.get(ref: MetaRef<T>): T? = get(ref.name)?.let { ref.converter.readOrNull(it) }
|
||||||
|
|
||||||
|
@DFExperimental
|
||||||
|
public operator fun <T> MutableMetaProvider.set(ref: MetaRef<T>, value: T) {
|
||||||
|
set(ref.name, ref.converter.convert(value))
|
||||||
|
}
|
||||||
|
|
||||||
|
@DFExperimental
|
||||||
|
public class MetaSpec(
|
||||||
|
private val configuration: MetaDescriptorBuilder.() -> Unit = {},
|
||||||
|
) : Described {
|
||||||
|
private val refs: MutableList<MetaRef<*>> = mutableListOf()
|
||||||
|
|
||||||
|
private fun registerRef(ref: MetaRef<*>) {
|
||||||
|
refs.add(ref)
|
||||||
|
}
|
||||||
|
|
||||||
|
public fun <T> item(
|
||||||
|
converter: MetaConverter<T>,
|
||||||
|
descriptor: MetaDescriptor? = converter.descriptor,
|
||||||
|
key: Name? = null,
|
||||||
|
): PropertyDelegateProvider<MetaSpec, ReadOnlyProperty<MetaSpec, MetaRef<T>>> =
|
||||||
|
PropertyDelegateProvider { _, property ->
|
||||||
|
val ref = MetaRef(key ?: property.name.asName(), converter, descriptor)
|
||||||
|
registerRef(ref)
|
||||||
|
ReadOnlyProperty { _, _ ->
|
||||||
|
ref
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override val descriptor: MetaDescriptor by lazy {
|
||||||
|
MetaDescriptor {
|
||||||
|
refs.forEach { ref ->
|
||||||
|
ref.descriptor?.let {
|
||||||
|
node(ref.name, ref.descriptor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
configuration()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -253,6 +253,9 @@ private class MutableMetaImpl(
|
|||||||
value: Value?,
|
value: Value?,
|
||||||
children: Map<NameToken, Meta> = emptyMap(),
|
children: Map<NameToken, Meta> = emptyMap(),
|
||||||
) : AbstractObservableMeta(), ObservableMutableMeta {
|
) : AbstractObservableMeta(), ObservableMutableMeta {
|
||||||
|
|
||||||
|
override val self get() = this
|
||||||
|
|
||||||
override var value = value
|
override var value = value
|
||||||
@ThreadSafe set(value) {
|
@ThreadSafe set(value) {
|
||||||
val oldValue = field
|
val oldValue = field
|
||||||
|
@ -39,6 +39,9 @@ public interface ObservableMeta : Meta {
|
|||||||
* A [Meta] which is both observable and mutable
|
* A [Meta] which is both observable and mutable
|
||||||
*/
|
*/
|
||||||
public interface ObservableMutableMeta : ObservableMeta, MutableMeta, MutableTypedMeta<ObservableMutableMeta> {
|
public interface ObservableMutableMeta : ObservableMeta, MutableMeta, MutableTypedMeta<ObservableMutableMeta> {
|
||||||
|
|
||||||
|
override val self: ObservableMutableMeta get() = this
|
||||||
|
|
||||||
override fun getOrCreate(name: Name): ObservableMutableMeta
|
override fun getOrCreate(name: Name): ObservableMutableMeta
|
||||||
|
|
||||||
override fun get(name: Name): ObservableMutableMeta? {
|
override fun get(name: Name): ObservableMutableMeta? {
|
||||||
|
@ -14,6 +14,9 @@ private class ObservableMetaWrapper(
|
|||||||
val nodeName: Name,
|
val nodeName: Name,
|
||||||
val listeners: MutableSet<MetaListener>,
|
val listeners: MutableSet<MetaListener>,
|
||||||
) : ObservableMutableMeta {
|
) : ObservableMutableMeta {
|
||||||
|
|
||||||
|
override val self get() = this
|
||||||
|
|
||||||
override val items: Map<NameToken, ObservableMutableMeta>
|
override val items: Map<NameToken, ObservableMutableMeta>
|
||||||
get() = root[nodeName]?.items?.keys?.associateWith {
|
get() = root[nodeName]?.items?.keys?.associateWith {
|
||||||
ObservableMetaWrapper(root, nodeName + it, listeners)
|
ObservableMetaWrapper(root, nodeName + it, listeners)
|
||||||
|
@ -12,7 +12,7 @@ import kotlin.reflect.KProperty
|
|||||||
import kotlin.reflect.KProperty1
|
import kotlin.reflect.KProperty1
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A base for delegate-based or descriptor-based scheme. [Scheme] has an empty constructor to simplify usage from [MetaSpec].
|
* A base for delegate-based or descriptor-based scheme. [Scheme] has an empty constructor to simplify usage from [MetaReader].
|
||||||
*
|
*
|
||||||
* @param prototype default values provided by this scheme
|
* @param prototype default values provided by this scheme
|
||||||
*/
|
*/
|
||||||
@ -80,6 +80,9 @@ public open class Scheme(
|
|||||||
override fun toString(): String = meta.toString()
|
override fun toString(): String = meta.toString()
|
||||||
|
|
||||||
private inner class SchemeMeta(val pathName: Name) : ObservableMutableMeta {
|
private inner class SchemeMeta(val pathName: Name) : ObservableMutableMeta {
|
||||||
|
|
||||||
|
override val self get() = this
|
||||||
|
|
||||||
override var value: Value?
|
override var value: Value?
|
||||||
get() = target[pathName]?.value
|
get() = target[pathName]?.value
|
||||||
?: prototype?.get(pathName)?.value
|
?: prototype?.get(pathName)?.value
|
||||||
@ -216,7 +219,7 @@ public fun <T : Scheme> Configurable.updateWith(
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A delegate that uses a [MetaSpec] to wrap a child of this provider
|
* A delegate that uses a [MetaReader] to wrap a child of this provider
|
||||||
*/
|
*/
|
||||||
public fun <T : Scheme> MutableMeta.scheme(
|
public fun <T : Scheme> MutableMeta.scheme(
|
||||||
spec: SchemeSpec<T>,
|
spec: SchemeSpec<T>,
|
||||||
@ -239,7 +242,7 @@ public fun <T : Scheme> Scheme.scheme(
|
|||||||
): ReadWriteProperty<Any?, T> = meta.scheme(spec, key)
|
): ReadWriteProperty<Any?, T> = meta.scheme(spec, key)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A delegate that uses a [MetaSpec] to wrap a child of this provider.
|
* A delegate that uses a [MetaReader] to wrap a child of this provider.
|
||||||
* Returns null if meta with given name does not exist.
|
* Returns null if meta with given name does not exist.
|
||||||
*/
|
*/
|
||||||
public fun <T : Scheme> MutableMeta.schemeOrNull(
|
public fun <T : Scheme> MutableMeta.schemeOrNull(
|
||||||
@ -264,7 +267,7 @@ public fun <T : Scheme> Scheme.schemeOrNull(
|
|||||||
): ReadWriteProperty<Any?, T?> = meta.schemeOrNull(spec, key)
|
): ReadWriteProperty<Any?, T?> = meta.schemeOrNull(spec, key)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A delegate that uses a [MetaSpec] to wrap a list of child providers.
|
* A delegate that uses a [MetaReader] to wrap a list of child providers.
|
||||||
* If children are mutable, the changes in list elements are reflected on them.
|
* If children are mutable, the changes in list elements are reflected on them.
|
||||||
* The list is a snapshot of children state, so change in structure is not reflected on its composition.
|
* The list is a snapshot of children state, so change in structure is not reflected on its composition.
|
||||||
*/
|
*/
|
||||||
|
@ -13,6 +13,9 @@ public class SealedMeta(
|
|||||||
override val value: Value?,
|
override val value: Value?,
|
||||||
override val items: Map<NameToken, SealedMeta>,
|
override val items: Map<NameToken, SealedMeta>,
|
||||||
) : TypedMeta<SealedMeta> {
|
) : TypedMeta<SealedMeta> {
|
||||||
|
|
||||||
|
override val self: SealedMeta get() = this
|
||||||
|
|
||||||
override fun toString(): String = Meta.toString(this)
|
override fun toString(): String = Meta.toString(this)
|
||||||
override fun equals(other: Any?): Boolean = Meta.equals(this, other as? Meta)
|
override fun equals(other: Any?): Boolean = Meta.equals(this, other as? Meta)
|
||||||
|
|
||||||
|
@ -58,7 +58,7 @@ public class MetaDescriptorBuilder @PublishedApi internal constructor() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun node(
|
public fun node(
|
||||||
name: Name,
|
name: Name,
|
||||||
descriptorBuilder: MetaDescriptor,
|
descriptorBuilder: MetaDescriptor,
|
||||||
): Unit {
|
): Unit {
|
||||||
|
@ -1,3 +0,0 @@
|
|||||||
package space.kscience.dataforge.misc
|
|
||||||
|
|
||||||
public expect inline fun <T> Any?.unsafeCast(): T
|
|
@ -31,6 +31,9 @@ public fun Meta.toDynamic(): dynamic {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public class DynamicMeta(internal val obj: dynamic) : TypedMeta<DynamicMeta> {
|
public class DynamicMeta(internal val obj: dynamic) : TypedMeta<DynamicMeta> {
|
||||||
|
|
||||||
|
override val self: DynamicMeta get() = this
|
||||||
|
|
||||||
private fun keys(): Array<String> = js("Object").keys(obj) as Array<String>
|
private fun keys(): Array<String> = js("Object").keys(obj) as Array<String>
|
||||||
|
|
||||||
private fun isArray(obj: dynamic): Boolean =
|
private fun isArray(obj: dynamic): Boolean =
|
||||||
|
@ -1,5 +0,0 @@
|
|||||||
package space.kscience.dataforge.misc
|
|
||||||
import kotlin.js.unsafeCast as unsafeCastJs
|
|
||||||
|
|
||||||
@Suppress("NOTHING_TO_INLINE")
|
|
||||||
public actual inline fun <T> Any?.unsafeCast(): T = unsafeCastJs<T>()
|
|
@ -1,4 +0,0 @@
|
|||||||
package space.kscience.dataforge.misc
|
|
||||||
|
|
||||||
@Suppress("UNCHECKED_CAST", "NOTHING_TO_INLINE")
|
|
||||||
public actual inline fun <T> Any?.unsafeCast(): T = this as T
|
|
@ -1,4 +0,0 @@
|
|||||||
package space.kscience.dataforge.misc
|
|
||||||
|
|
||||||
@Suppress("UNCHECKED_CAST")
|
|
||||||
public actual inline fun <T> Any?.unsafeCast(): T = this as T
|
|
@ -1,4 +0,0 @@
|
|||||||
package space.kscience.dataforge.misc
|
|
||||||
|
|
||||||
@Suppress("UNCHECKED_CAST", "NOTHING_TO_INLINE")
|
|
||||||
public actual inline fun <T> Any?.unsafeCast(): T = this as T
|
|
@ -5,8 +5,8 @@ import space.kscience.dataforge.data.DataSink
|
|||||||
import space.kscience.dataforge.data.GoalExecutionRestriction
|
import space.kscience.dataforge.data.GoalExecutionRestriction
|
||||||
import space.kscience.dataforge.data.MutableDataTree
|
import space.kscience.dataforge.data.MutableDataTree
|
||||||
import space.kscience.dataforge.meta.Meta
|
import space.kscience.dataforge.meta.Meta
|
||||||
|
import space.kscience.dataforge.meta.MetaReader
|
||||||
import space.kscience.dataforge.meta.MetaRepr
|
import space.kscience.dataforge.meta.MetaRepr
|
||||||
import space.kscience.dataforge.meta.MetaSpec
|
|
||||||
import space.kscience.dataforge.meta.descriptors.Described
|
import space.kscience.dataforge.meta.descriptors.Described
|
||||||
import space.kscience.dataforge.meta.descriptors.MetaDescriptor
|
import space.kscience.dataforge.meta.descriptors.MetaDescriptor
|
||||||
import space.kscience.dataforge.misc.DfType
|
import space.kscience.dataforge.misc.DfType
|
||||||
@ -44,10 +44,10 @@ public interface Task<T> : Described {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A [Task] with [MetaSpec] for wrapping and unwrapping task configuration
|
* A [Task] with [MetaReader] for wrapping and unwrapping task configuration
|
||||||
*/
|
*/
|
||||||
public interface TaskWithSpec<T, C : Any> : Task<T> {
|
public interface TaskWithSpec<T, C : Any> : Task<T> {
|
||||||
public val spec: MetaSpec<C>
|
public val spec: MetaReader<C>
|
||||||
override val descriptor: MetaDescriptor? get() = spec.descriptor
|
override val descriptor: MetaDescriptor? get() = spec.descriptor
|
||||||
|
|
||||||
public suspend fun execute(workspace: Workspace, taskName: Name, configuration: C): TaskResult<T>
|
public suspend fun execute(workspace: Workspace, taskName: Name, configuration: C): TaskResult<T>
|
||||||
@ -122,10 +122,10 @@ public inline fun <reified T : Any> Task(
|
|||||||
@Suppress("FunctionName")
|
@Suppress("FunctionName")
|
||||||
public fun <T : Any, C : MetaRepr> Task(
|
public fun <T : Any, C : MetaRepr> Task(
|
||||||
resultType: KType,
|
resultType: KType,
|
||||||
specification: MetaSpec<C>,
|
specification: MetaReader<C>,
|
||||||
builder: suspend TaskResultBuilder<T>.(C) -> Unit,
|
builder: suspend TaskResultBuilder<T>.(C) -> Unit,
|
||||||
): TaskWithSpec<T, C> = object : TaskWithSpec<T, C> {
|
): TaskWithSpec<T, C> = object : TaskWithSpec<T, C> {
|
||||||
override val spec: MetaSpec<C> = specification
|
override val spec: MetaReader<C> = specification
|
||||||
|
|
||||||
override suspend fun execute(
|
override suspend fun execute(
|
||||||
workspace: Workspace,
|
workspace: Workspace,
|
||||||
@ -143,6 +143,6 @@ public fun <T : Any, C : MetaRepr> Task(
|
|||||||
}
|
}
|
||||||
|
|
||||||
public inline fun <reified T : Any, C : MetaRepr> Task(
|
public inline fun <reified T : Any, C : MetaRepr> Task(
|
||||||
specification: MetaSpec<C>,
|
specification: MetaReader<C>,
|
||||||
noinline builder: suspend TaskResultBuilder<T>.(C) -> Unit,
|
noinline builder: suspend TaskResultBuilder<T>.(C) -> Unit,
|
||||||
): Task<T> = Task(typeOf<T>(), specification, builder)
|
): Task<T> = Task(typeOf<T>(), specification, builder)
|
@ -71,10 +71,10 @@ public inline fun <reified T : Any> TaskContainer.task(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a task based on [MetaSpec]
|
* Create a task based on [MetaReader]
|
||||||
*/
|
*/
|
||||||
public inline fun <reified T : Any, C : MetaRepr> TaskContainer.task(
|
public inline fun <reified T : Any, C : MetaRepr> TaskContainer.task(
|
||||||
specification: MetaSpec<C>,
|
specification: MetaReader<C>,
|
||||||
noinline builder: suspend TaskResultBuilder<T>.(C) -> Unit,
|
noinline builder: suspend TaskResultBuilder<T>.(C) -> Unit,
|
||||||
): PropertyDelegateProvider<Any?, ReadOnlyProperty<Any?, TaskReference<T>>> = PropertyDelegateProvider { _, property ->
|
): PropertyDelegateProvider<Any?, ReadOnlyProperty<Any?, TaskReference<T>>> = PropertyDelegateProvider { _, property ->
|
||||||
val taskName = Name.parse(property.name)
|
val taskName = Name.parse(property.name)
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
|
kotlin.code.style=official
|
||||||
|
|
||||||
org.gradle.parallel=true
|
org.gradle.parallel=true
|
||||||
org.gradle.jvmargs=-Xmx4096m
|
org.gradle.jvmargs=-Xmx4096m
|
||||||
|
|
||||||
kotlin.code.style=official
|
|
||||||
kotlin.mpp.stability.nowarn=true
|
kotlin.mpp.stability.nowarn=true
|
||||||
kotlin.incremental.js.ir=true
|
|
||||||
kotlin.native.ignoreDisabledTargets=true
|
kotlin.native.ignoreDisabledTargets=true
|
||||||
|
|
||||||
toolsVersion=0.15.2-kotlin-1.9.21
|
toolsVersion=0.15.4-kotlin-2.0.0
|
||||||
#kotlin.experimental.tryK2=true
|
|
Loading…
Reference in New Issue
Block a user