All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetbrains.kotlinx.ggdsl.dsl.contexts.kt Maven / Gradle / Ivy

There is a newer version: 0.4.0-dev-15
Show newest version
package org.jetbrains.kotlinx.ggdsl.dsl

import org.jetbrains.kotlinx.ggdsl.ir.Layer
import org.jetbrains.kotlinx.ggdsl.ir.Layout
import org.jetbrains.kotlinx.ggdsl.ir.Plot
import org.jetbrains.kotlinx.ggdsl.ir.aes.*
import org.jetbrains.kotlinx.ggdsl.ir.bindings.*
import org.jetbrains.kotlinx.ggdsl.ir.data.DataSource
import org.jetbrains.kotlinx.ggdsl.ir.feature.FeatureName
import org.jetbrains.kotlinx.ggdsl.ir.feature.LayerFeature
import org.jetbrains.kotlinx.ggdsl.ir.feature.PlotFeature
import org.jetbrains.kotlinx.ggdsl.ir.geom.Geom
import org.jetbrains.kotlinx.ggdsl.ir.scale.NonPositionalUnspecifiedScale
import org.jetbrains.kotlinx.ggdsl.ir.scale.NonPositionalScale
import org.jetbrains.kotlinx.ggdsl.ir.scale.PositionalUnspecifiedScale
import org.jetbrains.kotlinx.ggdsl.ir.scale.PositionalScale

/**
 * Internal collector of mappings and settings.
 */
class BindingCollector internal constructor() {
    val mappings: MutableMap = mutableMapOf()
    val settings: MutableMap = mutableMapOf()

    fun copyFrom(other: BindingCollector) {
        mappings.putAll(other.mappings)
        settings.putAll(other.settings)
    }
}

/**
 * Base class for binding context.
 *
 * In this context, the mechanism of bindings, that is, mappings and settings, is defined.
 * It is implemented with aesthetic attribute properties invocation with raw or scaled source as an argument.
 *
 * @property data the mutual dataset context.
 */
@PlotDslMarker
abstract class BindingContext {
    abstract var data: MutableNamedData

    //todo
    private var counter = 0

    @PublishedApi
    internal fun generateID(): String = "*gen${counter++}"

    // todo add for arrays/others???
    @PublishedApi
    internal inline fun  Iterable.toDataSource(): DataSource {
        val list = toList()
        val id = generateID()
        data[id] = list
        return source(id)
    }

    // todo how to hide?
    val bindingCollector = BindingCollector()

    // todo how to hide?
    fun copyFrom(other: BindingContext, copyData: Boolean = true) {
        if (copyData) {
            data = other.data
        }
        this.bindingCollector.copyFrom(other.bindingCollector)
    }


    // todo move???
    inline fun  Iterable.scaled() =
        SourceScaledUnspecifiedDefault(this.toDataSource())

    inline fun  Iterable.scaled(scale: PositionalUnspecifiedScale) =
        SourceScaledPositionalUnspecified(this.toDataSource(), scale)


    inline fun  Iterable.scaled(scale: NonPositionalUnspecifiedScale) =
        SourceScaledNonPositionalUnspecified(this.toDataSource(), scale)


    inline fun  Iterable.scaled(
        scale: PositionalScale
    ) = SourceScaledPositional(this.toDataSource(), scale)


    inline fun  Iterable.scaled(
        scale: NonPositionalScale
    ) = SourceScaledNonPositional(this.toDataSource(), scale)


}

// todo
abstract class BaseBindingContext : BindingContext() {
    val x = XAes(this)
    val y = YAes(this)
}

/**
 * Layer context interface.
 *
 * todo
 */
@PlotDslMarker
abstract class LayerContext : BaseBindingContext() {
    // todo hide?
    val features: MutableMap = mutableMapOf()
}

/**
 * Creates a new [Layer] from this [LayerContext]
 *
 * @return new [Plot]
 */
fun LayerContext.toLayer(geom: Geom): Layer {
    return Layer(
        data,
        geom,
        this.bindingCollector.mappings,
        this.bindingCollector.settings,
        features
    )
}

// todo
@PlotDslMarker
class PlotContext : BaseBindingContext() {
    override var data: MutableNamedData = mutableMapOf()

    var layout: Layout? = null

    // todo how to hide?
    val layers: MutableList = mutableListOf()
    // todo how to hide?
    val features: MutableMap = mutableMapOf()
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy