-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from kleis-technology/feature/override
feature/override
- Loading branch information
Showing
9 changed files
with
146 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
core/src/main/kotlin/ch/kleis/lcaac/core/datasource/DataSourceDescription.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package ch.kleis.lcaac.core.datasource | ||
|
||
import ch.kleis.lcaac.core.lang.value.DataValue | ||
|
||
data class DataSourceDescription<Q>( | ||
val location: String, | ||
val schema: Map<String, DataValue<Q>>, | ||
) |
65 changes: 65 additions & 0 deletions
65
core/src/main/kotlin/ch/kleis/lcaac/core/datasource/DataSourceOperationsBase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package ch.kleis.lcaac.core.datasource | ||
|
||
import ch.kleis.lcaac.core.lang.evaluator.EvaluatorException | ||
import ch.kleis.lcaac.core.lang.evaluator.ToValue | ||
import ch.kleis.lcaac.core.lang.evaluator.reducer.DataExpressionReducer | ||
import ch.kleis.lcaac.core.lang.expression.DataExpression | ||
import ch.kleis.lcaac.core.lang.expression.EQuantityAdd | ||
import ch.kleis.lcaac.core.lang.expression.EQuantityMul | ||
import ch.kleis.lcaac.core.lang.expression.ERecord | ||
import ch.kleis.lcaac.core.lang.register.DataSourceRegister | ||
import ch.kleis.lcaac.core.lang.value.DataSourceValue | ||
import ch.kleis.lcaac.core.lang.value.StringValue | ||
import ch.kleis.lcaac.core.math.QuantityOperations | ||
import ch.kleis.lcaac.core.prelude.Prelude | ||
|
||
open class DataSourceOperationsBase<Q>( | ||
private val ops: QuantityOperations<Q>, | ||
private val load: (DataSourceDescription<Q>) -> Sequence<ERecord<Q>>, | ||
) : DataSourceOperations<Q> { | ||
override fun readAll(source: DataSourceValue<Q>): Sequence<ERecord<Q>> { | ||
val description = DataSourceDescription(source.location, source.schema) | ||
val records = load(description) | ||
val filter = source.filter | ||
return records | ||
.filter { record -> | ||
filter.entries.all { | ||
val expected = it.value | ||
if (expected is StringValue) { | ||
val actual = record.entries[it.key] | ||
?.let { with(ToValue(ops)) { it.toValue() } } | ||
?: throw IllegalStateException( | ||
"${source.location}: invalid schema: unknown column '${it.key}'" | ||
) | ||
actual == expected | ||
} else throw EvaluatorException("invalid matching condition $it") | ||
} | ||
} | ||
} | ||
|
||
override fun sumProduct(source: DataSourceValue<Q>, columns: List<String>): DataExpression<Q> { | ||
val reducer = DataExpressionReducer( | ||
dataRegister = Prelude.units(), | ||
dataSourceRegister = DataSourceRegister.empty(), | ||
ops = ops, | ||
sourceOps = this, | ||
) | ||
return readAll(source).map { record -> | ||
columns.map { column -> | ||
record.entries[column] | ||
?: throw IllegalStateException( | ||
"${source.location}: invalid schema: unknown column '$column'" | ||
) | ||
}.reduce { acc, expression -> | ||
reducer.reduce(EQuantityMul(acc, expression)) | ||
} | ||
}.reduce { acc, expression -> | ||
reducer.reduce(EQuantityAdd(acc, expression)) | ||
} | ||
} | ||
|
||
override fun getFirst(source: DataSourceValue<Q>): ERecord<Q> { | ||
return readAll(source).firstOrNull() | ||
?: throw EvaluatorException("no record found in '${source.location}' matching ${source.filter}") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters