Skip to content

Commit

Permalink
Merge pull request #17 from kleis-technology/fix/params
Browse files Browse the repository at this point in the history
fix/params
  • Loading branch information
pevab authored Feb 2, 2024
2 parents 60f3a91 + 18c73c9 commit 8ff20d4
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package ch.kleis.lcaac.core.datasource

import ch.kleis.lcaac.core.lang.expression.DataExpression
import ch.kleis.lcaac.core.lang.expression.EQuantityMul
import ch.kleis.lcaac.core.lang.expression.ERecord
import ch.kleis.lcaac.core.lang.expression.EStringLiteral
import ch.kleis.lcaac.core.lang.value.*

/*
The dummy source ops does not read from any file.
It simply returns the default record defined by the source's schema.
It is used when reducing a EUnitOf, as there is no need to fetch
an actual value from the data source, but only learn about the relevant dimension.
*/

class DummySourceOperations<Q> : DataSourceOperations<Q> {
override fun readAll(source: DataSourceValue<Q>): Sequence<ERecord<Q>> {
return sequenceOf(ERecord(source.schema.mapValues { it.value.toDataExpression() }))
}

override fun sumProduct(source: DataSourceValue<Q>, columns: List<String>): DataExpression<Q> {
return source.schema.filterKeys { columns.contains(it) }
.map { it.value.toDataExpression() }
.reduce { acc, dataExpression -> EQuantityMul(acc, dataExpression) }
}

override fun getFirst(source: DataSourceValue<Q>): ERecord<Q> {
return ERecord(source.schema.mapValues { it.value.toDataExpression() })
}

private fun DataValue<Q>.toDataExpression(): DataExpression<Q> {
return when (this) {
is QuantityValue -> this.toEQuantityScale()
is RecordValue -> ERecord(this.entries.mapValues { it.value.toDataExpression() })
is StringValue -> EStringLiteral(this.s)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ch.kleis.lcaac.core.lang.evaluator.reducer

import ch.kleis.lcaac.core.datasource.DataSourceOperations
import ch.kleis.lcaac.core.datasource.DummySourceOperations
import ch.kleis.lcaac.core.lang.dimension.UnitSymbol
import ch.kleis.lcaac.core.lang.evaluator.EvaluatorException
import ch.kleis.lcaac.core.lang.evaluator.ToValue
Expand Down Expand Up @@ -110,14 +111,12 @@ class DataExpressionReducer<Q>(


private fun reduceUnitOf(unitOf: EUnitOf<Q>): DataExpression<Q> {
with(ops) {
val reducedExpression = reduce(unitOf.expression)
return when {
reducedExpression is EQuantityScale && reducedExpression.base is EUnitLiteral -> EQuantityScale(pure(1.0), reducedExpression.base)
val reducedExpression = dummyReducer().reduce(unitOf.expression)
return when {
reducedExpression is EQuantityScale && reducedExpression.base is EUnitLiteral -> EQuantityScale(ops.pure(1.0), reducedExpression.base)

reducedExpression is EUnitOf -> reducedExpression
else -> EUnitOf(reducedExpression)
}
reducedExpression is EUnitOf -> reducedExpression
else -> EUnitOf(reducedExpression)
}
}

Expand Down Expand Up @@ -251,4 +250,9 @@ class DataExpressionReducer<Q>(

return if (left.scale > right.scale) left else right
}


private fun dummyReducer(): DataExpressionReducer<Q> {
return DataExpressionReducer(dataRegister, dataSourceRegister, ops, DummySourceOperations())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,41 @@ class DataExpressionReducerTest {
assertEquals(expected, actual)
}

@Test
fun reduce_whenUnitOf_withDataSourceExpression() {
// given
val dataSource = EDataSource(
location = "source.csv",
schema = mapOf(
"n_items" to EQuantityScale(BasicNumber(0.0), UnitFixture.unit),
"mass" to EQuantityScale(BasicNumber(0.0), UnitFixture.kg),
)
)
val expr = EUnitOf<BasicNumber>(
ESumProduct(EDataSourceRef("source"), listOf("n_items", "mass"))
)
val reducer = DataExpressionReducer(
Register.empty(),
DataSourceRegister.from(mapOf(
DataSourceKey("source") to dataSource,
)), ops, sourceOps)


// when
val actual = reducer.reduce(expr)

// then
val expected = EQuantityScale(
BasicNumber(1.0),
EUnitLiteral(
UnitSymbol.of("unit").multiply(UnitSymbol.of("kg")),
1.0,
Dimension.of("none").multiply(Dimension.of("mass"))
)
)
assertEquals(expected, actual)
}

@Test
fun reduce_whenUnitOfUnitLiteral_shouldReturnNormalForm() {
// given
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ javaVersion=17
gradleVersion=7.6
org.gradle.jvmargs=-Xmx4096m
lcaacGroup=ch.kleis.lcaac
lcaacVersion=1.6.1
lcaacVersion=1.6.2
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ datasource inventory {
the default value will be the one chosen for the entire column.
*/
schema {
id = "small"
quantity = 1 p
ram_size = 16 GB
storage_size = 1 TB
Expand Down

0 comments on commit 8ff20d4

Please sign in to comment.