diff --git a/core/src/main/kotlin/ch/kleis/lcaac/core/lang/evaluator/protocol/Oracle.kt b/core/src/main/kotlin/ch/kleis/lcaac/core/lang/evaluator/protocol/Oracle.kt index 4147bfb3..3edc2a49 100644 --- a/core/src/main/kotlin/ch/kleis/lcaac/core/lang/evaluator/protocol/Oracle.kt +++ b/core/src/main/kotlin/ch/kleis/lcaac/core/lang/evaluator/protocol/Oracle.kt @@ -4,7 +4,6 @@ import ch.kleis.lcaac.core.datasource.DataSourceOperations import ch.kleis.lcaac.core.lang.SymbolTable import ch.kleis.lcaac.core.lang.evaluator.step.CompleteTerminals import ch.kleis.lcaac.core.lang.evaluator.step.Reduce -import ch.kleis.lcaac.core.lang.evaluator.step.ReduceLabelSelectors import ch.kleis.lcaac.core.lang.expression.EProcess import ch.kleis.lcaac.core.lang.expression.EProcessTemplateApplication import ch.kleis.lcaac.core.lang.resolver.ProcessResolver @@ -14,9 +13,8 @@ import ch.kleis.lcaac.core.math.QuantityOperations class Oracle( val symbolTable: SymbolTable, val ops: QuantityOperations, - private val sourceOps: DataSourceOperations, + sourceOps: DataSourceOperations, ) { - private val reduceLabelSelectors = ReduceLabelSelectors(symbolTable, ops, sourceOps) private val reduceDataExpressions = Reduce(symbolTable, ops, sourceOps) private val completeTerminals = CompleteTerminals(ops) private val processResolver = ProcessResolver(symbolTable) @@ -40,7 +38,6 @@ class Oracle( .plus(spec.fromProcess?.arguments ?: emptyMap()) val expression = EProcessTemplateApplication(template, arguments) val process = expression - .let(reduceLabelSelectors::apply) .let(reduceDataExpressions::apply) .let(completeTerminals::apply) val selectedPortIndex = indexOf(request.value.name, process) diff --git a/core/src/main/kotlin/ch/kleis/lcaac/core/lang/evaluator/reducer/TemplateExpressionReducer.kt b/core/src/main/kotlin/ch/kleis/lcaac/core/lang/evaluator/reducer/TemplateExpressionReducer.kt index fe88a089..5613ac94 100644 --- a/core/src/main/kotlin/ch/kleis/lcaac/core/lang/evaluator/reducer/TemplateExpressionReducer.kt +++ b/core/src/main/kotlin/ch/kleis/lcaac/core/lang/evaluator/reducer/TemplateExpressionReducer.kt @@ -36,6 +36,7 @@ class TemplateExpressionReducer( val localRegister = DataRegister(dataRegister) .plus(actualArguments.mapKeys { DataKey(it.key) }) .plus(template.locals.mapKeys { DataKey(it.key) }) + .plus(template.body.labels.mapKeys { DataKey(it.key) }) val reducer = LcaExpressionReducer(localRegister, dataSourceRegister, ops, sourceOps) val dataReducer = DataExpressionReducer(localRegister, dataSourceRegister, ops, sourceOps) diff --git a/core/src/main/kotlin/ch/kleis/lcaac/core/lang/evaluator/step/ReduceLabelSelectors.kt b/core/src/main/kotlin/ch/kleis/lcaac/core/lang/evaluator/step/ReduceLabelSelectors.kt deleted file mode 100644 index 62405eec..00000000 --- a/core/src/main/kotlin/ch/kleis/lcaac/core/lang/evaluator/step/ReduceLabelSelectors.kt +++ /dev/null @@ -1,45 +0,0 @@ -package ch.kleis.lcaac.core.lang.evaluator.step - -import arrow.optics.Every -import ch.kleis.lcaac.core.datasource.DataSourceOperations -import ch.kleis.lcaac.core.lang.SymbolTable -import ch.kleis.lcaac.core.lang.evaluator.reducer.DataExpressionReducer -import ch.kleis.lcaac.core.lang.expression.* -import ch.kleis.lcaac.core.lang.expression.optics.everyDataRefInDataExpression -import ch.kleis.lcaac.core.lang.expression.optics.everyEntry -import ch.kleis.lcaac.core.lang.register.DataKey -import ch.kleis.lcaac.core.lang.register.Register -import ch.kleis.lcaac.core.math.QuantityOperations - -class ReduceLabelSelectors( - private val symbolTable: SymbolTable, - private val ops: QuantityOperations, - private val sourceOps: DataSourceOperations, -) { - private val everyInputProduct = - EProcessTemplateApplication.template().body().inputs() compose - Every.list() compose - BlockExpression.everyEntry() compose - ETechnoExchange.product() - private val everyLabelSelector = everyInputProduct compose - EProductSpec.fromProcess().matchLabels().elements() compose - Every.map() compose - everyDataRefInDataExpression() - - fun apply(expression: EProcessTemplateApplication): EProcessTemplateApplication { - val template = expression.template - val labels = template.body.labels - val actualArguments = template.params.plus(expression.arguments) - val locals = template.locals - val reducer = DataExpressionReducer( - Register(symbolTable.data) - .plus(actualArguments.mapKeys { DataKey(it.key) }) - .plus(labels.mapKeys { DataKey(it.key) }) - .plus(locals.mapKeys { DataKey(it.key) }), - symbolTable.dataSources, - ops, - sourceOps, - ) - return everyLabelSelector.modify(expression) { ref -> reducer.reduce(ref) } - } -} diff --git a/core/src/test/kotlin/ch/kleis/lcaac/core/lang/evaluator/step/ReduceLabelSelectorsTest.kt b/core/src/test/kotlin/ch/kleis/lcaac/core/lang/evaluator/step/ReduceLabelSelectorsTest.kt deleted file mode 100644 index 22a4123f..00000000 --- a/core/src/test/kotlin/ch/kleis/lcaac/core/lang/evaluator/step/ReduceLabelSelectorsTest.kt +++ /dev/null @@ -1,316 +0,0 @@ -package ch.kleis.lcaac.core.lang.evaluator.step - -import ch.kleis.lcaac.core.datasource.DataSourceOperations -import ch.kleis.lcaac.core.lang.SymbolTable -import ch.kleis.lcaac.core.lang.expression.* -import ch.kleis.lcaac.core.lang.fixture.QuantityFixture -import ch.kleis.lcaac.core.lang.register.DataKey -import ch.kleis.lcaac.core.lang.register.DataRegister -import ch.kleis.lcaac.core.math.basic.BasicNumber -import ch.kleis.lcaac.core.math.basic.BasicOperations -import io.mockk.mockk -import org.junit.jupiter.api.Test -import kotlin.test.assertEquals - - -class ReduceLabelSelectorsTest { - private val ops = BasicOperations - private val sourceOps = mockk>() - - @Test - fun reduce_whenPassingByArguments() { - // given - val instance = EProcessTemplateApplication( - template = EProcessTemplate( - params = mapOf("geo" to EStringLiteral("GLO")), - body = EProcess( - name = "salad_production", - inputs = listOf( - ETechnoBlockEntry( - ETechnoExchange( - quantity = QuantityFixture.oneKilogram, - product = EProductSpec( - name = "carrot", - referenceUnit = QuantityFixture.oneKilogram, - fromProcess = FromProcess( - name = "carrot_production", - matchLabels = MatchLabels(mapOf("geo" to EDataRef("geo"))), - ) - ) - ) - ) - ), - ) - ), - mapOf("geo" to EStringLiteral("FR")), - ) - val reduceLabelSelectors = ReduceLabelSelectors(SymbolTable(), ops, sourceOps) - - // when - val actual = reduceLabelSelectors.apply(instance) - - // then - val expected = EProcessTemplateApplication( - EProcessTemplate( - params = mapOf("geo" to EStringLiteral("GLO")), - body = EProcess( - "salad_production", - inputs = listOf( - ETechnoBlockEntry( - ETechnoExchange( - QuantityFixture.oneKilogram, - EProductSpec( - "carrot", - QuantityFixture.oneKilogram, - FromProcess( - name = "carrot_production", - matchLabels = MatchLabels(mapOf("geo" to EStringLiteral("FR"))), - ) - ) - ) - ) - ), - ) - ), - mapOf("geo" to EStringLiteral("FR")), - ) - assertEquals(expected, actual) - } - - @Test - fun reduce_whenPassingByDefaultParams() { - // given - val instance = EProcessTemplateApplication( - template = EProcessTemplate( - params = mapOf("geo" to EStringLiteral("GLO")), - body = EProcess( - name = "salad_production", - inputs = listOf( - ETechnoBlockEntry( - ETechnoExchange( - QuantityFixture.oneKilogram, - EProductSpec( - "carrot", - QuantityFixture.oneKilogram, - FromProcess( - name = "carrot_production", - matchLabels = MatchLabels(mapOf("geo" to EDataRef("geo"))), - ) - ) - ) - ) - ), - ) - ), - ) - val reduceLabelSelectors = ReduceLabelSelectors(SymbolTable(), ops, sourceOps) - - // when - val actual = reduceLabelSelectors.apply(instance) - - // then - val expected = EProcessTemplateApplication( - template = EProcessTemplate( - params = mapOf("geo" to EStringLiteral("GLO")), - body = EProcess( - name = "salad_production", - inputs = listOf( - ETechnoBlockEntry( - ETechnoExchange( - QuantityFixture.oneKilogram, - EProductSpec( - "carrot", - QuantityFixture.oneKilogram, - FromProcess( - name = "carrot_production", - matchLabels = MatchLabels(mapOf("geo" to EStringLiteral("GLO"))), - ) - ) - ) - ) - ), - ) - ), - ) - assertEquals(expected, actual) - } - - @Test - fun reduce_whenPassingByLocals() { - // given - val instance = EProcessTemplateApplication( - template = EProcessTemplate( - locals = mapOf("geo" to EStringLiteral("GLO")), - body = EProcess( - "salad_production", - inputs = listOf( - ETechnoBlockEntry( - ETechnoExchange( - QuantityFixture.oneKilogram, - EProductSpec( - "carrot", - QuantityFixture.oneKilogram, - FromProcess( - name = "carrot_production", - matchLabels = MatchLabels(mapOf("geo" to EDataRef("geo"))), - ) - ) - ) - ) - ), - ) - ), - ) - val reduceLabelSelectors = ReduceLabelSelectors(SymbolTable(), ops, sourceOps) - - // when - val actual = reduceLabelSelectors.apply(instance) - - // then - val expected = EProcessTemplateApplication( - template = EProcessTemplate( - locals = mapOf("geo" to EStringLiteral("GLO")), - body = EProcess( - name = "salad_production", - inputs = listOf( - ETechnoBlockEntry( - ETechnoExchange( - QuantityFixture.oneKilogram, - EProductSpec( - "carrot", - QuantityFixture.oneKilogram, - FromProcess( - name = "carrot_production", - matchLabels = MatchLabels(mapOf("geo" to EStringLiteral("GLO"))), - ) - ) - ) - ) - ), - ) - ), - ) - assertEquals(expected, actual) - } - - @Test - fun reduce_whenPassingByGlobalVariables() { - // given - val instance = EProcessTemplateApplication( - template = EProcessTemplate( - body = EProcess( - name = "salad_production", - inputs = listOf( - ETechnoBlockEntry( - ETechnoExchange( - QuantityFixture.oneKilogram, - EProductSpec( - "carrot", - QuantityFixture.oneKilogram, - FromProcess( - name = "carrot_production", - matchLabels = MatchLabels(mapOf("geo" to EDataRef("geo"))), - ) - ) - ) - ) - ), - ) - ), - ) - - val reduceLabelSelectors = ReduceLabelSelectors( - SymbolTable( - data = DataRegister(mapOf(DataKey("geo") to EStringLiteral("FR"))) - ), - ops, - sourceOps, - ) - - // when - val actual = reduceLabelSelectors.apply(instance) - - // then - val expected = EProcessTemplateApplication( - template = EProcessTemplate( - body = EProcess( - "salad_production", - inputs = listOf( - ETechnoBlockEntry( - ETechnoExchange( - QuantityFixture.oneKilogram, - EProductSpec( - "carrot", - QuantityFixture.oneKilogram, - FromProcess( - name = "carrot_production", - matchLabels = MatchLabels(mapOf("geo" to EStringLiteral("FR"))), - ) - ) - ) - ) - ), - ) - ), - ) - assertEquals(expected, actual) - } - - @Test - fun reduce_whenPassingByProcessLabels() { - // given - val instance = EProcessTemplateApplication( - template = EProcessTemplate( - body = EProcess( - "salad_production", - labels = mapOf("geo" to EStringLiteral("FR")), - inputs = listOf( - ETechnoBlockEntry( - ETechnoExchange( - QuantityFixture.oneKilogram, - EProductSpec( - "carrot", - QuantityFixture.oneKilogram, - FromProcess( - name = "carrot_production", - matchLabels = MatchLabels(mapOf("geo" to EDataRef("geo"))), - ) - ) - ) - ) - ), - ) - ), - ) - val reduceLabelSelectors = ReduceLabelSelectors(SymbolTable(), ops, sourceOps) - - // when - val actual = reduceLabelSelectors.apply(instance) - - // then - val expected = EProcessTemplateApplication( - template = EProcessTemplate( - body = EProcess( - "salad_production", - labels = mapOf("geo" to EStringLiteral("FR")), - inputs = listOf( - ETechnoBlockEntry( - ETechnoExchange( - QuantityFixture.oneKilogram, - EProductSpec( - "carrot", - QuantityFixture.oneKilogram, - FromProcess( - name = "carrot_production", - matchLabels = MatchLabels(mapOf("geo" to EStringLiteral("FR"))), - ) - ) - ) - ) - ), - ) - ), - ) - assertEquals(expected, actual) - } -}