Skip to content

Commit

Permalink
Merge pull request #58 from mipt-npm/dev
Browse files Browse the repository at this point in the history
0.2.0 release
  • Loading branch information
altavir authored Nov 28, 2020
2 parents d951668 + 2315fb9 commit 6912f26
Show file tree
Hide file tree
Showing 169 changed files with 5,537 additions and 3,076 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ out/
build/


!gradle-wrapper.jar
gradle.properties
!gradle-wrapper.jar
39 changes: 39 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Changelog

## [Unreleased]
### Added

### Changed

### Deprecated

### Removed

### Fixed

### Security
## [0.2.0]
### Added

### Changed
- Context content resolution refactor
- Kotlin 1.4.10 (build tools 0.6.0)
- Empty query in Name is null instead of ""
- Provider provides an empty map instead of error by default
- Hidden delegates hierarchy in favor of stdlib properties
- Removed io depdendency from `dataforge-output`. Replaced Output by Appendable.
- Configurable is no longer MutableItemProvider. All functionality moved to Scheme.

### Deprecated
- Context activation API
- TextRenderer

### Removed
- Functional server prototype
- `dataforge-output` module

### Fixed
- Global context CoroutineScope resolution
- Library mode compliance

### Security
15 changes: 6 additions & 9 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@

plugins {
val toolsVersion = "0.5.0"
id("scientifik.mpp") version toolsVersion apply false
id("scientifik.jvm") version toolsVersion apply false
id("scientifik.publish") version toolsVersion apply false
id("org.jetbrains.dokka") version "0.10.1"
id("ru.mipt.npm.project")
}

val dataforgeVersion by extra("0.1.8")
val dataforgeVersion by extra("0.2.0")

val bintrayRepo by extra("dataforge")
val githubProject by extra("dataforge-core")
val spaceRepo by extra("https://maven.jetbrains.space/mipt-npm/p/df/maven")

allprojects {
group = "hep.dataforge"
version = dataforgeVersion

apply<org.jetbrains.dokka.gradle.DokkaPlugin>()

repositories {
mavenLocal()
}
}

subprojects {
apply(plugin = "scientifik.publish")
apply(plugin = "org.jetbrains.dokka")
apply(plugin = "ru.mipt.npm.publish")
}
334 changes: 334 additions & 0 deletions dataforge-context/api/dataforge-context.api

Large diffs are not rendered by default.

14 changes: 6 additions & 8 deletions dataforge-context/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
import scientifik.useCoroutines

plugins {
id("scientifik.mpp")
id("ru.mipt.npm.mpp")
id("ru.mipt.npm.native")
}

description = "Context and provider definitions"


useCoroutines()
kscience {
useCoroutines()
}

kotlin {
sourceSets {
val commonMain by getting {
dependencies {
api(project(":dataforge-meta"))
api("io.github.microutils:kotlin-logging-common:1.7.9")
api("io.github.microutils:kotlin-logging:1.9.0-dev-npm-2")
}
}
val jvmMain by getting {
dependencies {
api(kotlin("reflect"))
api("io.github.microutils:kotlin-logging:1.7.9")
api("ch.qos.logback:logback-classic:1.2.3")
}
}
val jsMain by getting {
dependencies {
api("io.github.microutils:kotlin-logging-js:1.7.9")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KClass
import kotlin.reflect.KProperty

abstract class AbstractPlugin(override val meta: Meta = Meta.EMPTY) : Plugin {
public abstract class AbstractPlugin(override val meta: Meta = Meta.EMPTY) : Plugin {
private var _context: Context? = null
private val dependencies = ArrayList<PluginFactory<*>>()

Expand All @@ -30,11 +30,9 @@ abstract class AbstractPlugin(override val meta: Meta = Meta.EMPTY) : Plugin {
dependencies.add(factory)
return PluginDependencyDelegate(factory.type)
}

override fun provideTop(target: String): Map<Name, Any> = emptyMap()
}

fun <T : Named> Collection<T>.toMap(): Map<Name, T> = associate { it.name to it }
public fun <T : Named> Collection<T>.toMap(): Map<Name, T> = associate { it.name to it }

private class PluginDependencyDelegate<P : Plugin>(val type: KClass<out P>) : ReadOnlyProperty<AbstractPlugin, P> {
override fun getValue(thisRef: AbstractPlugin, property: KProperty<*>): P {
Expand Down
138 changes: 35 additions & 103 deletions dataforge-context/src/commonMain/kotlin/hep/dataforge/context/Context.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package hep.dataforge.context

import hep.dataforge.meta.*
import hep.dataforge.meta.Laminate
import hep.dataforge.meta.Meta
import hep.dataforge.meta.MetaRepr
import hep.dataforge.meta.sequence
import hep.dataforge.names.Name
import hep.dataforge.names.asName
import hep.dataforge.names.plus
import hep.dataforge.provider.Provider
import hep.dataforge.provider.top
import hep.dataforge.values.Value
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import mu.KLogger
import mu.KotlinLogging
import kotlin.coroutines.CoroutineContext
import kotlin.jvm.JvmName

/**
* The local environment for anything being done in DataForge framework. Contexts are organized into tree structure with [Global] at the top.
Expand All @@ -24,73 +23,52 @@ import kotlin.jvm.JvmName
* different plugins with the same interface in different contexts in the hierarchy. The usual behaviour is to use nearest one, but it could
* be overridden by plugin implementation.
*
* Since plugins could contain mutable state, context has two states: active and inactive. No changes are allowed to active context.
* @author Alexander Nozik
*/
open class Context(
public open class Context(
final override val name: Name,
val parent: Context? = Global
public val parent: Context?,
meta: Meta,
plugins: Set<Plugin> = emptySet(),
) : Named, MetaRepr, Provider, CoroutineScope {

private val config = Config()

/**
* Context properties. Working as substitute for environment variables
*/
val properties: Meta = if (parent == null) {
config
private val properties: Laminate = if (parent == null) {
Laminate(meta)
} else {
Laminate(config, parent.properties)
Laminate(meta, parent.properties)
}

/**
* Context logger
*/
val logger: KLogger = KotlinLogging.logger(name.toString())
public val logger: KLogger = KotlinLogging.logger(name.toString())

/**
* A [PluginManager] for current context
*/
val plugins: PluginManager by lazy { PluginManager(this) }

private val activators = HashSet<Any>()

/**
* Defines if context is used in any kind of active computations. Active context properties and plugins could not be changed
*/
val isActive: Boolean = activators.isNotEmpty()
public val plugins: PluginManager by lazy { PluginManager(this, plugins)}

override val defaultTarget: String get() = Plugin.PLUGIN_TARGET
override val defaultTarget: String get() = Plugin.TARGET

override fun provideTop(target: String): Map<Name, Any> {
return when (target) {
Value.TYPE -> properties.sequence().toMap()
Plugin.PLUGIN_TARGET -> plugins.sequence(true).associateBy { it.name }
else -> emptyMap()
public fun content(target: String, inherit: Boolean): Map<Name, Any> {
return if (inherit) {
when (target) {
PROPERTY_TARGET -> properties.sequence().toMap()
Plugin.TARGET -> plugins.list(true).associateBy { it.name }
else -> emptyMap()
}
} else {
when (target) {
PROPERTY_TARGET -> properties.layers.firstOrNull()?.sequence()?.toMap() ?: emptyMap()
Plugin.TARGET -> plugins.list(false).associateBy { it.name }
else -> emptyMap()
}
}
}

/**
* Mark context as active and used by [activator]
*/
fun activate(activator: Any) {
activators.add(activator)
}

/**
* Mark context unused by [activator]
*/
fun deactivate(activator: Any) {
activators.remove(activator)
}

/**
* Change the properties of the context. If active, throw an exception
*/
fun configure(action: Config.() -> Unit) {
if (isActive) error("Can't configure active context")
config.action()
}
override fun content(target: String): Map<Name, Any> = content(target, true)

override val coroutineContext: CoroutineContext by lazy {
(parent ?: Global).coroutineContext.let { parenContext ->
Expand All @@ -101,81 +79,35 @@ open class Context(
/**
* Detach all plugins and terminate context
*/
open fun close() {
if (isActive) error("Can't close active context")
public open fun close() {
//detach all plugins
plugins.forEach { it.detach() }
}

override fun toMeta(): Meta = Meta {
"parent" to parent?.name
"properties" put properties.seal()
"properties" put properties.layers.firstOrNull()
"plugins" put plugins.map { it.toMeta() }
}
}

fun Context.content(target: String): Map<Name, Any> = content<Any>(target)

/**
* A map of all objects provided by plugins with given target and type
*/
@JvmName("typedContent")
inline fun <reified T : Any> Context.content(target: String): Map<Name, T> =
plugins.flatMap { plugin ->
plugin.top<T>(target).entries.map { (plugin.name + it.key) to it.value }
}.associate { it }


/**
* A global root context. Closing [Global] terminates the framework.
*/
object Global : Context("GLOBAL".asName(), null) {
/**
* Closing all contexts
*
* @throws Exception
*/
override fun close() {
logger.info { "Shutting down GLOBAL" }
for (ctx in contextRegistry.values) {
ctx.close()
}
super.close()
public companion object {
public const val PROPERTY_TARGET: String = "context.property"
}

private val contextRegistry = HashMap<String, Context>()

/**
* Get previously built context
*
* @param name
* @return
*/
fun getContext(name: String): Context? {
return contextRegistry[name]
}

fun context(name: String, parent: Context = this, block: ContextBuilder.() -> Unit = {}): Context =
ContextBuilder(name, parent).apply(block).build()

}


/**
* The interface for something that encapsulated in context
*
* @author Alexander Nozik
* @version $Id: $Id
*/
interface ContextAware {
public interface ContextAware {
/**
* Get context for this object
*
* @return
*/
val context: Context
public val context: Context

val logger: KLogger
public val logger: KLogger
get() = if (this is Named) {
KotlinLogging.logger((context.name + this.name).toString())
} else {
Expand Down
Loading

0 comments on commit 6912f26

Please sign in to comment.