diff --git a/buildSrc/src/main/kotlin/task/SubstanceWithImpactAccumulator.kt b/buildSrc/src/main/kotlin/task/SubstanceWithImpactAccumulator.kt index 004eb5e69..58448d8b5 100644 --- a/buildSrc/src/main/kotlin/task/SubstanceWithImpactAccumulator.kt +++ b/buildSrc/src/main/kotlin/task/SubstanceWithImpactAccumulator.kt @@ -64,7 +64,7 @@ class SubstanceWithImpactAccumulator { @Suppress("SameParameterValue") private fun impactsContent(pad: Int = 4): String = factorRecords .filter { it.methodLocation().isBlank() } - .map { + .joinToString("\n") { "|".plus( padStart( "${it.characterizationFactor()} ${it.unit()} ${it.methodName()}", @@ -72,7 +72,6 @@ class SubstanceWithImpactAccumulator { ) ) } - .joinToString("\n") private val impactsSubsection: String get() = if (factorRecords.size > 0) { @@ -86,7 +85,7 @@ class SubstanceWithImpactAccumulator { private fun getSubCompartment(): String { val sub = substanceSubCompartment @Suppress("SameParameterValue") - return if (sub.isNullOrBlank()) { + return if (sub.isBlank()) { "" } else { " sub_compartment = \"$sub\"" diff --git a/src/main/kotlin/ch/kleis/lcaplugin/actions/ActionHelper.kt b/src/main/kotlin/ch/kleis/lcaplugin/actions/ActionHelper.kt index 3e4f7ece6..06d03828d 100644 --- a/src/main/kotlin/ch/kleis/lcaplugin/actions/ActionHelper.kt +++ b/src/main/kotlin/ch/kleis/lcaplugin/actions/ActionHelper.kt @@ -1,7 +1,7 @@ package ch.kleis.lcaplugin.actions -import ch.kleis.lcaplugin.core.lang.evaluator.Evaluator import ch.kleis.lcaplugin.core.lang.evaluator.EvaluationTrace +import ch.kleis.lcaplugin.core.lang.evaluator.Evaluator import ch.kleis.lcaplugin.core.lang.expression.EProcessTemplateApplication import ch.kleis.lcaplugin.language.parser.LcaFileCollector import ch.kleis.lcaplugin.language.parser.LcaLangAbstractParser @@ -29,7 +29,7 @@ fun traceSystemWithIndicator( indicator.text = "Solving system" val template = symbolTable.getTemplate(processName, matchLabels)!! // We are called from a process, so it must exist - val entryPoint = EProcessTemplateApplication(template, emptyMap()) + val entryPoint = EProcessTemplateApplication(template = template) return Evaluator(symbolTable).trace(entryPoint) } diff --git a/src/main/kotlin/ch/kleis/lcaplugin/actions/sankey/SankeyGraphBuilder.kt b/src/main/kotlin/ch/kleis/lcaplugin/actions/sankey/SankeyGraphBuilder.kt index b19d8089e..c2dd2c28d 100644 --- a/src/main/kotlin/ch/kleis/lcaplugin/actions/sankey/SankeyGraphBuilder.kt +++ b/src/main/kotlin/ch/kleis/lcaplugin/actions/sankey/SankeyGraphBuilder.kt @@ -89,10 +89,8 @@ class SankeyGraphBuilder( else -> inventory.impactFactors.valueRatio(exchange.port(), observed).amount } - val retVal = valueRatioForObservedImpact * + return valueRatioForObservedImpact * inventory.supply.quantityOf(product).amount * exchange.quantity().amount - - return retVal } } diff --git a/src/main/kotlin/ch/kleis/lcaplugin/core/allocation/Allocation.kt b/src/main/kotlin/ch/kleis/lcaplugin/core/allocation/Allocation.kt index 9ab87c7b3..b73e8cb44 100644 --- a/src/main/kotlin/ch/kleis/lcaplugin/core/allocation/Allocation.kt +++ b/src/main/kotlin/ch/kleis/lcaplugin/core/allocation/Allocation.kt @@ -5,7 +5,7 @@ import ch.kleis.lcaplugin.core.lang.evaluator.EvaluatorException import ch.kleis.lcaplugin.core.lang.value.* import kotlin.math.absoluteValue -class Allocation { +object Allocation { fun apply(system: SystemValue): SystemValue { val processes = system.processes.flatMap { processValue -> processValue.products.map { allocateProduct(it, processValue) } @@ -16,11 +16,12 @@ class Allocation { private fun allocateProduct(technoExchangeValue: TechnoExchangeValue, processValue: ProcessValue): ProcessValue { val totalAllocation = totalAmount(processValue) return ProcessValue( - processValue.name, - processValue.labels, - listOf(technoExchangeValue.copy(allocation = technoExchangeValue.allocation.copy(amount = 100.0))), - applyAllocationToInputs(processValue.inputs, technoExchangeValue.allocation, totalAllocation), - applyAllocationToBioSphere(processValue.biosphere, technoExchangeValue.allocation, totalAllocation) + name = processValue.name, + labels = processValue.labels, + products = listOf(technoExchangeValue.copy(allocation = technoExchangeValue.allocation.copy(amount = 100.0))), + inputs = processValue.inputs.map(applyAllocationToInput(technoExchangeValue.allocation, totalAllocation)), + biosphere = processValue.biosphere.map(applyAllocationToBioExchange(technoExchangeValue.allocation, totalAllocation)), + impacts = processValue.impacts.map(applyAllocationToImpact(technoExchangeValue.allocation, totalAllocation)) ) } @@ -42,48 +43,43 @@ class Allocation { return processValue.products.sumOf { it.allocation.amount } } - private fun applyAllocationToInputs( - inputs: List, - allocation: QuantityValue, - totalAllocation: Double - ): List { - return inputs.map { applyAllocationToInput(it, allocation, totalAllocation) } - } - private fun applyAllocationToInput( - technoExchangeValue: TechnoExchangeValue, allocation: QuantityValue, totalAllocation: Double - ): TechnoExchangeValue { - return TechnoExchangeValue( - QuantityValue( - technoExchangeValue.quantity.amount * allocation.referenceValue() / totalAllocation, - technoExchangeValue.quantity.unit - ), - technoExchangeValue.product, - technoExchangeValue.allocation, - ) + ): (TechnoExchangeValue) -> TechnoExchangeValue { + val ratio = allocation.referenceValue() / totalAllocation + return { technoExchangeValue: TechnoExchangeValue -> + technoExchangeValue.copy( + quantity = technoExchangeValue.quantity.copy( + amount = technoExchangeValue.quantity.amount * ratio + ) + ) + } } - private fun applyAllocationToBioSphere( - biosphere: List, + private fun applyAllocationToBioExchange( allocation: QuantityValue, totalAllocation: Double - ): List { - return biosphere.map { applyAllocationToBioExchange(it, allocation, totalAllocation) } + ): (BioExchangeValue) -> BioExchangeValue { + val ratio = allocation.referenceValue() / totalAllocation + return { bioExchange: BioExchangeValue -> + bioExchange.copy( + quantity = bioExchange.quantity.copy( + amount = bioExchange.quantity.amount * ratio + ), + ) + } } - private fun applyAllocationToBioExchange( - bioExchange: BioExchangeValue, + private fun applyAllocationToImpact( allocation: QuantityValue, - totalAllocation: Double - ): BioExchangeValue { - return BioExchangeValue( - QuantityValue( - bioExchange.quantity.amount * allocation.referenceValue() / totalAllocation, - bioExchange.quantity.unit - ), - bioExchange.substance - ) + totalAllocation: Double, + ): (ImpactValue) -> ImpactValue { + val ratio = allocation.referenceValue() / totalAllocation + return { impactValue -> + impactValue.copy(quantity = impactValue.quantity.copy( + amount = impactValue.quantity.amount * ratio + )) + } } } diff --git a/src/main/kotlin/ch/kleis/lcaplugin/core/assessment/Assessment.kt b/src/main/kotlin/ch/kleis/lcaplugin/core/assessment/Assessment.kt index a2937f340..3abf9676b 100644 --- a/src/main/kotlin/ch/kleis/lcaplugin/core/assessment/Assessment.kt +++ b/src/main/kotlin/ch/kleis/lcaplugin/core/assessment/Assessment.kt @@ -22,7 +22,7 @@ class Assessment( val allocatedSystem: SystemValue init { - allocatedSystem = Allocation().apply(system) + allocatedSystem = Allocation.apply(system) val processes = allocatedSystem.processes val substanceCharacterizations = allocatedSystem.substanceCharacterizations @@ -47,7 +47,7 @@ class Assessment( .flatMap { it.biosphere } .map { it.substance } .filter { !observableSubstances.contains(it) } - val indicators = substanceCharacterizations + val indicators = (processes + substanceCharacterizations) .flatMap { it.impacts } .map { it.indicator } controllablePorts = IndexedCollection(terminalProducts.plus(terminalSubstances).plus(indicators)) diff --git a/src/main/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/Evaluator.kt b/src/main/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/Evaluator.kt index 9c3d2e4f1..cbf1bc6c9 100644 --- a/src/main/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/Evaluator.kt +++ b/src/main/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/Evaluator.kt @@ -24,7 +24,6 @@ class Evaluator( private val reduceLabelSelectors = ReduceLabelSelectors(symbolTable) private val completeDefaultArguments = CompleteDefaultArguments(symbolTable) private val reduce = Reduce(symbolTable) - private val completeTerminals = CompleteTerminals() private val processResolver = ProcessResolver(symbolTable) private val substanceCharacterizationResolver = SubstanceCharacterizationResolver(symbolTable) @@ -73,7 +72,7 @@ class Evaluator( .let(reduceLabelSelectors::apply) .let(completeDefaultArguments::apply) .let(reduce::apply) - .let(completeTerminals::apply) + .let(CompleteTerminals::apply) val inputProductsModified = everyInputProduct.modify(reduced) { spec: EProductSpec -> resolveProcessTemplateByProductSpec(spec)?.let { template -> @@ -97,7 +96,7 @@ class Evaluator( resolveSubstanceCharacterizationBySubstanceSpec(spec)?.let { val substanceCharacterization = it .let(reduce::apply) - .let(completeTerminals::apply) + .let(CompleteTerminals::apply) trace.add(substanceCharacterization.toValue()) substanceCharacterization.referenceExchange.substance } ?: spec diff --git a/src/main/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/ToValue.kt b/src/main/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/ToValue.kt index c9e6cdff0..c47c709b6 100644 --- a/src/main/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/ToValue.kt +++ b/src/main/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/ToValue.kt @@ -38,9 +38,10 @@ fun EProcess.toValue(): ProcessValue { return ProcessValue( this.name, this.labels.mapValues { it.value.toValue() as StringValue }, - this.products.map { it.toValue() }, - this.inputs.map { it.toValue() }, - this.biosphere.map { it.toValue() }, + this.products.map(ETechnoExchange::toValue), + this.inputs.map(ETechnoExchange::toValue), + this.biosphere.map(EBioExchange::toValue), + this.impacts.map(EImpact::toValue) ) } @@ -102,13 +103,14 @@ private fun FromProcess.toValue(): FromProcessRefValue { } fun DataExpression.toValue(): DataValue { - return when(this) { + return when (this) { is EStringLiteral -> StringValue(this.value) is EUnitLiteral -> QuantityValue(1.0, this.toUnitValue()) is EQuantityScale -> when (val b = this.base) { is EUnitLiteral -> QuantityValue( this.scale, b.toUnitValue(), ) + else -> throw EvaluatorException("$b is not reduced") } diff --git a/src/main/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/reducer/LcaExpressionReducer.kt b/src/main/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/reducer/LcaExpressionReducer.kt index 4dc288cec..c3787f3fe 100644 --- a/src/main/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/reducer/LcaExpressionReducer.kt +++ b/src/main/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/reducer/LcaExpressionReducer.kt @@ -26,7 +26,7 @@ class LcaExpressionReducer( fun reduceSubstanceCharacterization(expression: ESubstanceCharacterization): ESubstanceCharacterization { return ESubstanceCharacterization( reduceBioExchange(expression.referenceExchange), - expression.impacts.map { reduceImpact(it) }, + expression.impacts.map(::reduceImpact), ) } @@ -35,9 +35,10 @@ class LcaExpressionReducer( return EProcess( expression.name, expression.labels, - expression.products.map { reduceTechnoExchange(it) }, - expression.inputs.map { reduceTechnoExchange(it) }, - expression.biosphere.map { reduceBioExchange(it) }, + expression.products.map(::reduceTechnoExchange), + expression.inputs.map(::reduceTechnoExchange), + expression.biosphere.map(::reduceBioExchange), + expression.impacts.map(::reduceImpact) ) } diff --git a/src/main/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/step/CompleteTerminals.kt b/src/main/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/step/CompleteTerminals.kt index ead8400cd..8146b9aa8 100644 --- a/src/main/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/step/CompleteTerminals.kt +++ b/src/main/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/step/CompleteTerminals.kt @@ -4,22 +4,23 @@ import arrow.optics.Every import ch.kleis.lcaplugin.core.lang.evaluator.EvaluatorException import ch.kleis.lcaplugin.core.lang.expression.* -class CompleteTerminals { +object CompleteTerminals { private val everyInputExchange = EProcessFinal.expression.inputs compose Every.list() - fun apply(expression: EProcessFinal): EProcessFinal { - return completeSubstances(completeInputs(expression)) - } + fun apply(expression: EProcessFinal): EProcessFinal = + expression + .completeInputs() + .completeSubstances() + .completeProcessIndicators() - fun apply(expression: ESubstanceCharacterization): ESubstanceCharacterization { - return completeIndicators(expression) - } + fun apply(expression: ESubstanceCharacterization): ESubstanceCharacterization = + expression.completeSubstanceIndicators() - private fun completeInputs(reduced: EProcessFinal): EProcessFinal { + private fun EProcessFinal.completeInputs(): EProcessFinal { return everyInputExchange - .modify(reduced) { exchange -> + .modify(this) { exchange -> val quantityExpression = exchange.quantity val referenceUnit = when { quantityExpression is EUnitLiteral -> @@ -38,9 +39,9 @@ class CompleteTerminals { } } - private fun completeSubstances(reduced: EProcessFinal): EProcessFinal { + private fun EProcessFinal.completeSubstances(): EProcessFinal { return (EProcessFinal.expression.biosphere compose Every.list()) - .modify(reduced) { exchange -> + .modify(this) { exchange -> val quantityExpression = exchange.quantity val referenceUnit = when { quantityExpression is EUnitLiteral -> @@ -61,24 +62,32 @@ class CompleteTerminals { } } - private fun completeIndicators(reduced: ESubstanceCharacterization): ESubstanceCharacterization { - return (ESubstanceCharacterization.impacts compose Every.list()) - .modify(reduced) { exchange -> - val quantityExpression = exchange.quantity - val referenceUnit = when { - quantityExpression is EUnitLiteral -> - EQuantityScale(1.0, quantityExpression) + private fun completeIndicators(impacts: Collection): List = + impacts.map { impactExchange -> + val quantityExpression = impactExchange.quantity + val referenceUnit = when { + quantityExpression is EUnitLiteral -> + EQuantityScale(1.0, quantityExpression) - quantityExpression is EQuantityScale && quantityExpression.base is EUnitLiteral -> - EQuantityScale(1.0, quantityExpression.base) + quantityExpression is EQuantityScale && quantityExpression.base is EUnitLiteral -> + EQuantityScale(1.0, quantityExpression.base) - else -> throw EvaluatorException("quantity $quantityExpression is not reduced") + else -> throw EvaluatorException("quantity $quantityExpression is not reduced") + } + + EImpact.indicator + .modify(impactExchange) { + EIndicatorSpec(it.name, referenceUnit) } + } - EImpact.indicator - .modify(exchange) { - EIndicatorSpec(it.name, referenceUnit) - } - } - } + private fun EProcessFinal.completeProcessIndicators(): EProcessFinal = + this.copy( + expression = this.expression.copy( + impacts = completeIndicators(this.expression.impacts) + ) + ) + + private fun ESubstanceCharacterization.completeSubstanceIndicators(): ESubstanceCharacterization = + this.copy(impacts = completeIndicators(this.impacts)) } diff --git a/src/main/kotlin/ch/kleis/lcaplugin/core/lang/expression/Constraint.kt b/src/main/kotlin/ch/kleis/lcaplugin/core/lang/expression/Constraint.kt index 9542fd3c7..a9ce5bd54 100644 --- a/src/main/kotlin/ch/kleis/lcaplugin/core/lang/expression/Constraint.kt +++ b/src/main/kotlin/ch/kleis/lcaplugin/core/lang/expression/Constraint.kt @@ -6,7 +6,7 @@ import arrow.optics.optics data class FromProcess( val name: String, val matchLabels: MatchLabels, - val arguments: Map, + val arguments: Map = emptyMap(), ) { override fun toString(): String { return "from $name$matchLabels$arguments" diff --git a/src/main/kotlin/ch/kleis/lcaplugin/core/lang/expression/LcaExpression.kt b/src/main/kotlin/ch/kleis/lcaplugin/core/lang/expression/LcaExpression.kt index 402364231..840d29ae3 100644 --- a/src/main/kotlin/ch/kleis/lcaplugin/core/lang/expression/LcaExpression.kt +++ b/src/main/kotlin/ch/kleis/lcaplugin/core/lang/expression/LcaExpression.kt @@ -95,10 +95,11 @@ data class EImpact(val quantity: DataExpression, val indicator: EIndicatorSpec) @optics data class EProcess( val name: String, - val labels: Map, - val products: List, - val inputs: List, - val biosphere: List, + val labels: Map = emptyMap(), + val products: List = emptyList(), + val inputs: List = emptyList(), + val biosphere: List = emptyList(), + val impacts: List = emptyList(), ) : LcaExpression { companion object } diff --git a/src/main/kotlin/ch/kleis/lcaplugin/core/lang/expression/ProcessTemplateExpression.kt b/src/main/kotlin/ch/kleis/lcaplugin/core/lang/expression/ProcessTemplateExpression.kt index a986ef8f1..3bb3721ca 100644 --- a/src/main/kotlin/ch/kleis/lcaplugin/core/lang/expression/ProcessTemplateExpression.kt +++ b/src/main/kotlin/ch/kleis/lcaplugin/core/lang/expression/ProcessTemplateExpression.kt @@ -9,8 +9,8 @@ sealed interface ProcessTemplateExpression : Expression { @optics data class EProcessTemplate( - val params: Map, - val locals: Map, + val params: Map = emptyMap(), + val locals: Map = emptyMap(), val body: EProcess, ) : ProcessTemplateExpression { companion object @@ -19,7 +19,7 @@ data class EProcessTemplate( @optics data class EProcessTemplateApplication( val template: EProcessTemplate, - val arguments: Map + val arguments: Map = emptyMap() ) : ProcessTemplateExpression { companion object } diff --git a/src/main/kotlin/ch/kleis/lcaplugin/core/lang/expression/optics/EveryDataRef.kt b/src/main/kotlin/ch/kleis/lcaplugin/core/lang/expression/optics/EveryDataRef.kt index 741fe2570..26852761b 100644 --- a/src/main/kotlin/ch/kleis/lcaplugin/core/lang/expression/optics/EveryDataRef.kt +++ b/src/main/kotlin/ch/kleis/lcaplugin/core/lang/expression/optics/EveryDataRef.kt @@ -140,6 +140,7 @@ val everyDataRefInProcess: PEvery EProcess.products compose Every.list() compose everyDataRefInETechnoExchange, EProcess.inputs compose Every.list() compose everyDataRefInETechnoExchange, EProcess.biosphere compose Every.list() compose everyDataRefInEBioExchange, + EProcess.impacts compose Every.list() compose everyDataRefInEImpact, ) ) diff --git a/src/main/kotlin/ch/kleis/lcaplugin/core/lang/value/ConstraintValue.kt b/src/main/kotlin/ch/kleis/lcaplugin/core/lang/value/ConstraintValue.kt index 106bc8a9e..ae38c5d3f 100644 --- a/src/main/kotlin/ch/kleis/lcaplugin/core/lang/value/ConstraintValue.kt +++ b/src/main/kotlin/ch/kleis/lcaplugin/core/lang/value/ConstraintValue.kt @@ -2,6 +2,6 @@ package ch.kleis.lcaplugin.core.lang.value data class FromProcessRefValue( val name: String, - val matchLabels: Map, - val arguments: Map, + val matchLabels: Map = emptyMap(), + val arguments: Map = emptyMap(), ) diff --git a/src/main/kotlin/ch/kleis/lcaplugin/core/lang/value/MatrixRowIndex.kt b/src/main/kotlin/ch/kleis/lcaplugin/core/lang/value/MatrixRowIndex.kt index 557db78f8..20868771c 100644 --- a/src/main/kotlin/ch/kleis/lcaplugin/core/lang/value/MatrixRowIndex.kt +++ b/src/main/kotlin/ch/kleis/lcaplugin/core/lang/value/MatrixRowIndex.kt @@ -4,15 +4,20 @@ import ch.kleis.lcaplugin.core.HasUID sealed interface MatrixRowIndex : Value, HasUID +sealed interface HasImpactList { + val impacts: List +} + data class ProcessValue( val name: String, - val labels: Map, - val products: List, - val inputs: List, - val biosphere: List, -) : Value, MatrixRowIndex + val labels: Map = emptyMap(), + val products: List = emptyList(), + val inputs: List = emptyList(), + val biosphere: List = emptyList(), + override val impacts: List = emptyList(), +) : Value, HasImpactList, MatrixRowIndex data class SubstanceCharacterizationValue( val referenceExchange: BioExchangeValue, - val impacts: List, -) : Value, MatrixRowIndex + override val impacts: List = emptyList(), +) : Value, HasImpactList, MatrixRowIndex diff --git a/src/main/kotlin/ch/kleis/lcaplugin/core/lang/value/SystemValue.kt b/src/main/kotlin/ch/kleis/lcaplugin/core/lang/value/SystemValue.kt index f41792056..0a509c4d4 100644 --- a/src/main/kotlin/ch/kleis/lcaplugin/core/lang/value/SystemValue.kt +++ b/src/main/kotlin/ch/kleis/lcaplugin/core/lang/value/SystemValue.kt @@ -4,15 +4,13 @@ import ch.kleis.lcaplugin.core.HasUID data class SystemValue( - val processes: Set, - val substanceCharacterizations: Set, + val processes: Set = emptySet(), + val substanceCharacterizations: Set = emptySet(), ) : Value, HasUID { - val productToProcessMap: Map + val productToProcessMap: Map = + processes.flatMap { process -> process.products.map { it.product to process } }.toMap() - init { - productToProcessMap = processes.flatMap { process -> process.products.map { it.product to process } }.toMap() - } } data class CharacterizationFactorValue( diff --git a/src/main/kotlin/ch/kleis/lcaplugin/core/matrix/ControllableMatrix.kt b/src/main/kotlin/ch/kleis/lcaplugin/core/matrix/ControllableMatrix.kt index 6df907365..c4d5c44b2 100644 --- a/src/main/kotlin/ch/kleis/lcaplugin/core/matrix/ControllableMatrix.kt +++ b/src/main/kotlin/ch/kleis/lcaplugin/core/matrix/ControllableMatrix.kt @@ -41,6 +41,13 @@ class ControllableMatrix( val col = ports.indexOf(it.substance) matrix.add(row, col, -it.quantity.referenceValue()) } + + process.impacts + .filter { ports.contains(it.indicator) } + .forEach { + val col = ports.indexOf(it.indicator) + matrix.add(row, col, -it.quantity.referenceValue()) + } } substanceCharacterizations.forEach { characterization -> diff --git a/src/main/kotlin/ch/kleis/lcaplugin/language/Lca.bnf b/src/main/kotlin/ch/kleis/lcaplugin/language/Lca.bnf index 21253c70d..63f619de5 100644 --- a/src/main/kotlin/ch/kleis/lcaplugin/language/Lca.bnf +++ b/src/main/kotlin/ch/kleis/lcaplugin/language/Lca.bnf @@ -180,6 +180,7 @@ LBRACE ( | block_land_use | block_resources | block_meta + | block_impacts )* RBRACE { implements=["ch.kleis.lcaplugin.language.psi.type.PsiProcess"] mixin="ch.kleis.lcaplugin.language.psi.mixin.PsiProcessMixin" diff --git a/src/main/kotlin/ch/kleis/lcaplugin/language/ide/style/LcaFormattingModelBuilder.kt b/src/main/kotlin/ch/kleis/lcaplugin/language/ide/style/LcaFormattingModelBuilder.kt index 5704d9d40..0d4e3f20a 100644 --- a/src/main/kotlin/ch/kleis/lcaplugin/language/ide/style/LcaFormattingModelBuilder.kt +++ b/src/main/kotlin/ch/kleis/lcaplugin/language/ide/style/LcaFormattingModelBuilder.kt @@ -64,6 +64,8 @@ class LcaFormattingModelBuilder : FormattingModelBuilder { .spacing(0, 0, 0, true, 1) .beforeInside(BLOCK_META, PROCESS) .spacing(0, 0, 0, true, 1) + .beforeInside(BLOCK_IMPACTS, PROCESS) + .spacing(0, 0, 0, true, 1) // Comments .before(COMMENT_CONTENT) .spaces(0) diff --git a/src/main/kotlin/ch/kleis/lcaplugin/language/parser/LcaLangAbstractParser.kt b/src/main/kotlin/ch/kleis/lcaplugin/language/parser/LcaLangAbstractParser.kt index c7ebd3954..cc5f1d737 100644 --- a/src/main/kotlin/ch/kleis/lcaplugin/language/parser/LcaLangAbstractParser.kt +++ b/src/main/kotlin/ch/kleis/lcaplugin/language/parser/LcaLangAbstractParser.kt @@ -128,12 +128,14 @@ class LcaLangAbstractParser( val landUse = psiProcess.getLandUse().map { bioExchange(it, symbolTable) } val resources = psiProcess.getResources().map { bioExchange(it, symbolTable) } val biosphere = emissions.plus(resources).plus(landUse) + val impacts = psiProcess.getImpacts().map(::impact) val body = EProcess( name = name, labels = labels, products = products, inputs = inputs, biosphere = biosphere, + impacts = impacts, ) return EProcessTemplate( params, diff --git a/src/main/kotlin/ch/kleis/lcaplugin/language/psi/type/PsiProcess.kt b/src/main/kotlin/ch/kleis/lcaplugin/language/psi/type/PsiProcess.kt index 673cbc475..28d8918c3 100644 --- a/src/main/kotlin/ch/kleis/lcaplugin/language/psi/type/PsiProcess.kt +++ b/src/main/kotlin/ch/kleis/lcaplugin/language/psi/type/PsiProcess.kt @@ -75,6 +75,12 @@ interface PsiProcess : StubBasedPsiElement, PsiNameIdentifierOwner, .flatMap { it.bioExchangeList } } + fun getImpacts(): Collection { + return node.getChildren(TokenSet.create(LcaTypes.BLOCK_IMPACTS)) + .map { it.psi as LcaBlockImpacts } + .flatMap { it.impactExchangeList } + } + fun getVariables(): Map { return PsiTreeUtil.findChildrenOfType(this, LcaVariables::class.java) .flatMap { diff --git a/src/test/kotlin/ch/kleis/lcaplugin/actions/sankey/SankeyGraphBuilderTest.kt b/src/test/kotlin/ch/kleis/lcaplugin/actions/sankey/SankeyGraphBuilderTest.kt index 4d46235f5..08846f054 100644 --- a/src/test/kotlin/ch/kleis/lcaplugin/actions/sankey/SankeyGraphBuilderTest.kt +++ b/src/test/kotlin/ch/kleis/lcaplugin/actions/sankey/SankeyGraphBuilderTest.kt @@ -2,10 +2,13 @@ package ch.kleis.lcaplugin.actions.sankey import ch.kleis.lcaplugin.core.assessment.Assessment import ch.kleis.lcaplugin.core.assessment.Inventory -import ch.kleis.lcaplugin.core.graph.* +import ch.kleis.lcaplugin.core.graph.Graph +import ch.kleis.lcaplugin.core.graph.GraphLink +import ch.kleis.lcaplugin.core.graph.GraphNode import ch.kleis.lcaplugin.core.lang.evaluator.Evaluator import ch.kleis.lcaplugin.core.lang.expression.EProcessTemplateApplication -import ch.kleis.lcaplugin.core.lang.value.* +import ch.kleis.lcaplugin.core.lang.value.MatrixColumnIndex +import ch.kleis.lcaplugin.core.lang.value.SystemValue import ch.kleis.lcaplugin.language.parser.LcaLangAbstractParser import ch.kleis.lcaplugin.language.psi.LcaFile import com.intellij.openapi.ui.naturalSorted @@ -23,17 +26,17 @@ class SankeyGraphBuilderTest : BasePlatformTestCase() { } private data class SankeyRequiredInformation( - val observedPort: MatrixColumnIndex, - val allocatedSystem: SystemValue, - val inventory: Inventory, - val comparator: Comparator, + val observedPort: MatrixColumnIndex, + val allocatedSystem: SystemValue, + val inventory: Inventory, + val comparator: Comparator, ) private fun getRequiredInformation(@Suppress("SameParameterValue") process: String, vf: VirtualFile): SankeyRequiredInformation { val file = PsiManager.getInstance(project).findFile(vf) as LcaFile val parser = LcaLangAbstractParser(sequenceOf(file)) val symbolTable = parser.load() - val entryPoint = EProcessTemplateApplication(symbolTable.getTemplate(process)!!, emptyMap()) + val entryPoint = EProcessTemplateApplication(template = symbolTable.getTemplate(process)!!) val trace = Evaluator(symbolTable).trace(entryPoint) val assessment = Assessment(trace.getSystemValue(), trace.getEntryPoint()) val inventory = assessment.inventory() @@ -47,7 +50,7 @@ class SankeyGraphBuilderTest : BasePlatformTestCase() { // given val pkgName = {}.javaClass.enclosingMethod.name val vf = myFixture.createFile( - "$pkgName.lca", """ + "$pkgName.lca", """ process p { products { 1 kg my_product @@ -65,10 +68,10 @@ class SankeyGraphBuilderTest : BasePlatformTestCase() { // then val expected = Graph.empty().addNode( - GraphNode("[Emission] my_substance(air)", "my_substance"), - GraphNode("my_product from p{}{}", "my_product") + GraphNode("[Emission] my_substance(air)", "my_substance"), + GraphNode("my_product from p{}{}", "my_product") ).addLink( - GraphLink("my_product from p{}{}", "[Emission] my_substance(air)", 1.0), + GraphLink("my_product from p{}{}", "[Emission] my_substance(air)", 1.0), ) assertEquals(expected.nodes, graph.nodes) assertEquals(expected.links, graph.links) @@ -79,7 +82,7 @@ class SankeyGraphBuilderTest : BasePlatformTestCase() { // given val pkgName = {}.javaClass.enclosingMethod.name val vf = myFixture.createFile( - "$pkgName.lca", """ + "$pkgName.lca", """ process p { products { 1 kg my_product @@ -97,10 +100,10 @@ class SankeyGraphBuilderTest : BasePlatformTestCase() { // then val expected = Graph.empty().addNode( - GraphNode("[Resource] my_substance(air)", "my_substance"), - GraphNode("my_product from p{}{}", "my_product") + GraphNode("[Resource] my_substance(air)", "my_substance"), + GraphNode("my_product from p{}{}", "my_product") ).addLink( - GraphLink("my_product from p{}{}", "[Resource] my_substance(air)", 1.0), + GraphLink("my_product from p{}{}", "[Resource] my_substance(air)", 1.0), ) assertEquals(expected.nodes, graph.nodes) assertEquals(expected.links, graph.links) @@ -111,7 +114,7 @@ class SankeyGraphBuilderTest : BasePlatformTestCase() { // given val pkgName = {}.javaClass.enclosingMethod.name val vf = myFixture.createFile( - "$pkgName.lca", """ + "$pkgName.lca", """ process p { products { 1 kg my_product @@ -129,10 +132,10 @@ class SankeyGraphBuilderTest : BasePlatformTestCase() { // then val expected = Graph.empty().addNode( - GraphNode("my_input", "my_input"), - GraphNode("my_product from p{}{}", "my_product"), + GraphNode("my_input", "my_input"), + GraphNode("my_product from p{}{}", "my_product"), ).addLink( - GraphLink("my_product from p{}{}", "my_input", 1.0), + GraphLink("my_product from p{}{}", "my_input", 1.0), ) assertEquals(expected.nodes, graph.nodes) assertEquals(expected.links, graph.links) @@ -143,7 +146,7 @@ class SankeyGraphBuilderTest : BasePlatformTestCase() { // given val pkgName = {}.javaClass.enclosingMethod.name val vf = myFixture.createFile( - "$pkgName.lca", """ + "$pkgName.lca", """ process p { products { 1 kg prod @@ -169,12 +172,12 @@ class SankeyGraphBuilderTest : BasePlatformTestCase() { // then val expected = Graph.empty().addNode( - GraphNode("prod from p{}{}", "prod"), - GraphNode("qrod from q{}{}", "qrod"), - GraphNode("my_emission", "my_emission"), + GraphNode("prod from p{}{}", "prod"), + GraphNode("qrod from q{}{}", "qrod"), + GraphNode("my_emission", "my_emission"), ).addLink( - GraphLink("prod from p{}{}", "qrod from q{}{}", 5.0), - GraphLink("qrod from q{}{}", "my_emission", 5.0), + GraphLink("prod from p{}{}", "qrod from q{}{}", 5.0), + GraphLink("qrod from q{}{}", "my_emission", 5.0), ) assertEquals(expected.nodes.naturalSorted(), graph.nodes.naturalSorted()) assertEquals(expected.links.naturalSorted(), graph.links.naturalSorted()) @@ -185,7 +188,7 @@ class SankeyGraphBuilderTest : BasePlatformTestCase() { // given val pkgName = {}.javaClass.enclosingMethod.name val vf = myFixture.createFile( - "$pkgName.lca", """ + "$pkgName.lca", """ substance my_substance { name = "my_substance" type = Emission @@ -212,12 +215,12 @@ class SankeyGraphBuilderTest : BasePlatformTestCase() { // then val expected = Graph.empty().addNode( - GraphNode("climate_change", "climate_change"), - GraphNode("my_product from p{}{}", "my_product"), - GraphNode("[Emission] my_substance(air)", "my_substance") + GraphNode("climate_change", "climate_change"), + GraphNode("my_product from p{}{}", "my_product"), + GraphNode("[Emission] my_substance(air)", "my_substance") ).addLink( - GraphLink("my_product from p{}{}", "[Emission] my_substance(air)", 1.0), - GraphLink("[Emission] my_substance(air)", "climate_change", 1.0) + GraphLink("my_product from p{}{}", "[Emission] my_substance(air)", 1.0), + GraphLink("[Emission] my_substance(air)", "climate_change", 1.0) ) assertEquals(expected.nodes, graph.nodes) assertEquals(expected.links, graph.links) @@ -229,7 +232,7 @@ class SankeyGraphBuilderTest : BasePlatformTestCase() { // given val pkgName = {}.javaClass.enclosingMethod.name val vf = myFixture.createFile( - "$pkgName.lca", """ + "$pkgName.lca", """ process p { products { 1 kg my_product @@ -255,12 +258,12 @@ class SankeyGraphBuilderTest : BasePlatformTestCase() { // then val expected = Graph.empty().addNode( - GraphNode("my_product from p{}{}", "my_product"), - GraphNode("my_input from input{}{}", "my_input"), - GraphNode("my_indicator", "my_indicator"), + GraphNode("my_product from p{}{}", "my_product"), + GraphNode("my_input from input{}{}", "my_input"), + GraphNode("my_indicator", "my_indicator"), ).addLink( - GraphLink("my_product from p{}{}", "my_input from input{}{}", 500.0), - GraphLink("my_input from input{}{}", "my_indicator", 500.0), + GraphLink("my_product from p{}{}", "my_input from input{}{}", 500.0), + GraphLink("my_input from input{}{}", "my_indicator", 500.0), ) assertEquals(expected.nodes, graph.nodes) assertEquals(expected.links, graph.links) @@ -271,7 +274,7 @@ class SankeyGraphBuilderTest : BasePlatformTestCase() { // given val pkgName = {}.javaClass.enclosingMethod.name val vf = myFixture.createFile( - "$pkgName.lca", """ + "$pkgName.lca", """ process p { products { 1 kg my_product allocate 50 percent @@ -290,12 +293,12 @@ class SankeyGraphBuilderTest : BasePlatformTestCase() { // then val expected = Graph.empty().addNode( - GraphNode("[Emission] my_substance(air)", "my_substance"), - GraphNode("my_product from p{}{}", "my_product"), - GraphNode("my_other_product from p{}{}", "my_other_product"), + GraphNode("[Emission] my_substance(air)", "my_substance"), + GraphNode("my_product from p{}{}", "my_product"), + GraphNode("my_other_product from p{}{}", "my_other_product"), ).addLink( - GraphLink("my_product from p{}{}", "[Emission] my_substance(air)", 1.0), - GraphLink("my_other_product from p{}{}", "[Emission] my_substance(air)", 1.0), + GraphLink("my_product from p{}{}", "[Emission] my_substance(air)", 1.0), + GraphLink("my_other_product from p{}{}", "[Emission] my_substance(air)", 1.0), ) assertEquals(expected.nodes, graph.nodes) assertEquals(expected.links, graph.links) @@ -306,7 +309,7 @@ class SankeyGraphBuilderTest : BasePlatformTestCase() { // given val pkgName = {}.javaClass.enclosingMethod.name val vf = myFixture.createFile( - "$pkgName.lca", """ + "$pkgName.lca", """ process p { products { 1 kg my_product @@ -349,17 +352,17 @@ class SankeyGraphBuilderTest : BasePlatformTestCase() { // then val expected = Graph.empty().addNode( - GraphNode("my_substance", "my_substance"), - GraphNode("my_product from p{}{}", "my_product"), - GraphNode("my_left_product from q{}{}", "my_left_product"), - GraphNode("my_right_product from r{}{}", "my_right_product"), - GraphNode("my_input from input{}{}", "my_input"), + GraphNode("my_substance", "my_substance"), + GraphNode("my_product from p{}{}", "my_product"), + GraphNode("my_left_product from q{}{}", "my_left_product"), + GraphNode("my_right_product from r{}{}", "my_right_product"), + GraphNode("my_input from input{}{}", "my_input"), ).addLink( - GraphLink("my_product from p{}{}", "my_left_product from q{}{}", 1.0), - GraphLink("my_product from p{}{}", "my_right_product from r{}{}", 1.0), - GraphLink("my_left_product from q{}{}", "my_input from input{}{}", 1.0), - GraphLink("my_right_product from r{}{}", "my_input from input{}{}", 1.0), - GraphLink("my_input from input{}{}", "my_substance", 2.0), + GraphLink("my_product from p{}{}", "my_left_product from q{}{}", 1.0), + GraphLink("my_product from p{}{}", "my_right_product from r{}{}", 1.0), + GraphLink("my_left_product from q{}{}", "my_input from input{}{}", 1.0), + GraphLink("my_right_product from r{}{}", "my_input from input{}{}", 1.0), + GraphLink("my_input from input{}{}", "my_substance", 2.0), ) assertEquals(expected.nodes, graph.nodes) assertEquals(expected.links, graph.links) @@ -370,7 +373,7 @@ class SankeyGraphBuilderTest : BasePlatformTestCase() { // given val pkgName = {}.javaClass.enclosingMethod.name val vf = myFixture.createFile( - "$pkgName.lca", """ + "$pkgName.lca", """ substance my_substance { name = "my_substance" type = Emission @@ -411,18 +414,18 @@ class SankeyGraphBuilderTest : BasePlatformTestCase() { // then val expected = Graph.empty().addNode( - GraphNode("climate_change", "climate_change"), - GraphNode("my_input from q{}{}", "my_input"), - GraphNode("my_product from p{}{}", "my_product"), - GraphNode("my_other_product from p{}{}", "my_other_product"), - GraphNode("[Emission] my_substance(air)", "my_substance") + GraphNode("climate_change", "climate_change"), + GraphNode("my_input from q{}{}", "my_input"), + GraphNode("my_product from p{}{}", "my_product"), + GraphNode("my_other_product from p{}{}", "my_other_product"), + GraphNode("[Emission] my_substance(air)", "my_substance") ).addLink( - GraphLink("my_product from p{}{}", "my_input from q{}{}", 1.5), - GraphLink("my_other_product from p{}{}", "my_input from q{}{}", 0.5), - GraphLink("my_input from q{}{}", "[Emission] my_substance(air)", 2.0), - GraphLink("my_product from p{}{}", "[Emission] my_substance(air)", 0.75), - GraphLink("my_other_product from p{}{}", "[Emission] my_substance(air)", 0.25), - GraphLink("[Emission] my_substance(air)", "climate_change", 3.0) + GraphLink("my_product from p{}{}", "my_input from q{}{}", 1.5), + GraphLink("my_other_product from p{}{}", "my_input from q{}{}", 0.5), + GraphLink("my_input from q{}{}", "[Emission] my_substance(air)", 2.0), + GraphLink("my_product from p{}{}", "[Emission] my_substance(air)", 0.75), + GraphLink("my_other_product from p{}{}", "[Emission] my_substance(air)", 0.25), + GraphLink("[Emission] my_substance(air)", "climate_change", 3.0) ) assertEquals(expected.nodes, graph.nodes) assertEquals(expected.links, graph.links) @@ -433,7 +436,7 @@ class SankeyGraphBuilderTest : BasePlatformTestCase() { // given val pkgName = {}.javaClass.enclosingMethod.name val vf = myFixture.createFile( - "$pkgName.lca", """ + "$pkgName.lca", """ process p1 { products { 1 kg A @@ -462,12 +465,12 @@ class SankeyGraphBuilderTest : BasePlatformTestCase() { // then val expected = Graph.empty().addNode( - GraphNode("A from p1{}{}", "A"), - GraphNode("B from p2{}{}", "B"), - GraphNode("C", "C"), + GraphNode("A from p1{}{}", "A"), + GraphNode("B from p2{}{}", "B"), + GraphNode("C", "C"), ).addLink( - GraphLink("A from p1{}{}", "B from p2{}{}", 4.0), - GraphLink("B from p2{}{}", "C", 2.0), + GraphLink("A from p1{}{}", "B from p2{}{}", 4.0), + GraphLink("B from p2{}{}", "C", 2.0), ) assertEquals(expected.nodes, graph.nodes) assertEquals(expected.links, graph.links) @@ -478,7 +481,7 @@ class SankeyGraphBuilderTest : BasePlatformTestCase() { // given val pkgName = {}.javaClass.enclosingMethod.name val vf = myFixture.createFile( - "$pkgName.lca", """ + "$pkgName.lca", """ process pA { products { 1 kg A @@ -563,28 +566,33 @@ class SankeyGraphBuilderTest : BasePlatformTestCase() { // then val expected = Graph.empty().addNode( - GraphNode("A from pA{}{}", "A"), - GraphNode("B from pB{}{}", "B"), - GraphNode("C from pC{}{}", "C"), - GraphNode("D from pD{}{}", "D"), - GraphNode("E from pE{}{}", "E"), - GraphNode("F from pF{}{}", "F"), - GraphNode("G from pG{}{}", "G"), - GraphNode("H from pH{}{}", "H"), - GraphNode("my_emission", "my_emission") + GraphNode("A from pA{}{}", "A"), + GraphNode("B from pB{}{}", "B"), + GraphNode("C from pC{}{}", "C"), + GraphNode("D from pD{}{}", "D"), + GraphNode("E from pE{}{}", "E"), + GraphNode("F from pF{}{}", "F"), + GraphNode("G from pG{}{}", "G"), + GraphNode("H from pH{}{}", "H"), + GraphNode("my_emission", "my_emission") ).addLink( - GraphLink("F from pF{}{}", "H from pH{}{}", value = 1.5), - GraphLink("C from pC{}{}", "F from pF{}{}", value = 0.5), - GraphLink("A from pA{}{}", "B from pB{}{}", value = 3.0), - GraphLink("A from pA{}{}", "C from pC{}{}", value = 0.5), - GraphLink("D from pD{}{}", "F from pF{}{}", value = 1.0), - GraphLink("H from pH{}{}", "my_emission", value = 3.5), - GraphLink("B from pB{}{}", "D from pD{}{}", value = 1.0), - GraphLink("B from pB{}{}", "E from pE{}{}", value = 2.0), - GraphLink("E from pE{}{}", "G from pG{}{}", value = 4.0), - GraphLink("G from pG{}{}", "H from pH{}{}", value = 2.0), + GraphLink("F from pF{}{}", "H from pH{}{}", value = 1.5), + GraphLink("C from pC{}{}", "F from pF{}{}", value = 0.5), + GraphLink("A from pA{}{}", "B from pB{}{}", value = 3.0), + GraphLink("A from pA{}{}", "C from pC{}{}", value = 0.5), + GraphLink("D from pD{}{}", "F from pF{}{}", value = 1.0), + GraphLink("H from pH{}{}", "my_emission", value = 3.5), + GraphLink("B from pB{}{}", "D from pD{}{}", value = 1.0), + GraphLink("B from pB{}{}", "E from pE{}{}", value = 2.0), + GraphLink("E from pE{}{}", "G from pG{}{}", value = 4.0), + GraphLink("G from pG{}{}", "H from pH{}{}", value = 2.0), ) - assertEquals(expected.nodes, graph.nodes) - assertEquals(expected.links, graph.links) + + assertEquals(expected.nodes.naturalSorted(), graph.nodes.naturalSorted()) + expected.links.naturalSorted().zip(graph.links.naturalSorted()).forEach { (expected, actual) -> + assertEquals(expected.source, actual.source) + assertEquals(expected.target, actual.target) + assertEquals(expected.value, actual.value, 0.0001) + } } } \ No newline at end of file diff --git a/src/test/kotlin/ch/kleis/lcaplugin/core/allocation/AllocationTest.kt b/src/test/kotlin/ch/kleis/lcaplugin/core/allocation/AllocationTest.kt index 8d7c7029e..a23a8f899 100644 --- a/src/test/kotlin/ch/kleis/lcaplugin/core/allocation/AllocationTest.kt +++ b/src/test/kotlin/ch/kleis/lcaplugin/core/allocation/AllocationTest.kt @@ -19,9 +19,8 @@ class AllocationTest { fun apply_when_no_allocation_should_change_nothing() { // Given val system = SystemValueFixture.carrotSystem() - val allocation = Allocation() // When - val actual = allocation.apply(SystemValueFixture.carrotSystem()) + val actual = Allocation.apply(SystemValueFixture.carrotSystem()) // Then Assert.assertEquals(system, actual) } @@ -30,11 +29,10 @@ class AllocationTest { fun apply_when_coProducts_should_duplicate_process() { // Given val system = SystemValue( - mutableSetOf( + processes = mutableSetOf( ProcessValue( - "carrot", - emptyMap(), - listOf( + name = "carrot", + products = listOf( TechnoExchangeValue( QuantityValueFixture.oneKilogram, ProductValueFixture.carrot, @@ -46,15 +44,12 @@ class AllocationTest { QuantityValueFixture.fiftyPercent ) ), - listOf(), - listOf() ) ), - mutableSetOf() ) - val allocation = Allocation() + // When - val actual = allocation.apply(system).processes.size + val actual = Allocation.apply(system).processes.size // Then Assert.assertEquals(2, actual) } @@ -63,11 +58,10 @@ class AllocationTest { fun apply_when_coProducts_should_keep_only_one_product_per_process() { // Given val system = SystemValue( - mutableSetOf( + processes = mutableSetOf( ProcessValue( - "carrot", - emptyMap(), - listOf( + name = "carrot", + products = listOf( TechnoExchangeValue( QuantityValueFixture.oneKilogram, ProductValueFixture.carrot, @@ -79,15 +73,12 @@ class AllocationTest { QuantityValueFixture.fiftyPercent ) ), - listOf(), - listOf() ) ), - mutableSetOf() ) - val allocation = Allocation() + // When - val actual = allocation.apply(system).processes.toList()[0].products.size + val actual = Allocation.apply(system).processes.toList()[0].products.size // Then Assert.assertEquals(1, actual) } @@ -96,11 +87,10 @@ class AllocationTest { fun apply_when_allocation_should_divide_inputs_quantities() { // Given val system = SystemValue( - mutableSetOf( + processes = mutableSetOf( ProcessValue( - "carrot", - emptyMap(), - listOf( + name = "carrot", + products = listOf( TechnoExchangeValue( QuantityValueFixture.oneKilogram, ProductValueFixture.carrot, @@ -112,20 +102,18 @@ class AllocationTest { QuantityValueFixture.fiftyPercent ) ), - listOf( + inputs = listOf( TechnoExchangeValue( QuantityValueFixture.twoLitres, ProductValueFixture.water ) ), - listOf() ) ), - mutableSetOf() ) - val allocation = Allocation() + // When - val actual = allocation.apply(system).processes.toList()[0].inputs[0].quantity + val actual = Allocation.apply(system).processes.toList()[0].inputs[0].quantity // Then val expected = QuantityValueFixture.oneLitre Assert.assertEquals(expected, actual) @@ -135,11 +123,10 @@ class AllocationTest { fun apply_when_allocation_should_divide_biosphere_quantities() { // given val system = SystemValue( - mutableSetOf( + processes = mutableSetOf( ProcessValue( - "carrot", - emptyMap(), - listOf( + name = "carrot", + products = listOf( TechnoExchangeValue( QuantityValueFixture.oneKilogram, ProductValueFixture.carrot, @@ -151,20 +138,18 @@ class AllocationTest { QuantityValueFixture.fiftyPercent ) ), - listOf(), - listOf( + biosphere = listOf( BioExchangeValue( QuantityValueFixture.twoKilograms, FullyQualifiedSubstanceValueFixture.propanol ) - ) + ), ) ), - mutableSetOf() ) - val allocation = Allocation() + // when - val actual = allocation.apply(system).processes.toList()[0].biosphere[0].quantity + val actual = Allocation.apply(system).processes.toList()[0].biosphere[0].quantity // then val expected = QuantityValueFixture.oneKilogram Assert.assertEquals(expected, actual) @@ -173,21 +158,18 @@ class AllocationTest { @Test fun totalAmount_whenOneProduct_shouldReturnOne() { // given - val allocation = Allocation() + val processValue = ProcessValue( - "carrot", - emptyMap(), - listOf( + name = "carrot", + products = listOf( TechnoExchangeValue( QuantityValueFixture.oneKilogram, ProductValueFixture.carrot ) ), - listOf(), - listOf() ) // when - val actual = allocation.totalAmount(processValue) + val actual = Allocation.totalAmount(processValue) // then val delta = 1E-9 Assert.assertEquals(1.0, actual, delta) @@ -196,11 +178,10 @@ class AllocationTest { @Test fun totalAmount_whenTwoProduct_shouldSumAllocation() { // given - val allocation = Allocation() + val processValue = ProcessValue( - "carrot", - emptyMap(), - listOf( + name = "carrot", + products = listOf( TechnoExchangeValue( QuantityValueFixture.oneKilogram, ProductValueFixture.carrot, @@ -212,11 +193,9 @@ class AllocationTest { QuantityValueFixture.twentyPercent ) ), - listOf(), - listOf() ) // when - val actual = allocation.totalAmount(processValue) + val actual = Allocation.totalAmount(processValue) // then val delta = 1E-9 Assert.assertEquals(1.0, actual, delta) @@ -225,11 +204,10 @@ class AllocationTest { @Test fun allocationUnitCheck_whenConsistentUnits_shouldNotThrowAnError() { // given - val allocation = Allocation() + val processValue = ProcessValue( - "carrot", - emptyMap(), - listOf( + name = "carrot", + products = listOf( TechnoExchangeValue( QuantityValueFixture.oneKilogram, ProductValueFixture.carrot, @@ -241,11 +219,9 @@ class AllocationTest { QuantityValueFixture.fiftyPercent ) ), - listOf(), - listOf() ) // when - allocation.allocationUnitCheck(processValue) + Allocation.allocationUnitCheck(processValue) // then should not throw. } @@ -253,13 +229,12 @@ class AllocationTest { @Test fun applyAllocation_whenTwoProduct_shouldWeightAllocations() { // given - val allocation = Allocation() + val system = SystemValue( - mutableSetOf( + processes = mutableSetOf( ProcessValue( - "carrot", - emptyMap(), - listOf( + name = "carrot", + products = listOf( TechnoExchangeValue( QuantityValueFixture.oneKilogram, ProductValueFixture.carrot, @@ -271,19 +246,18 @@ class AllocationTest { QuantityValueFixture.eightyPercent ) ), - listOf( + inputs = listOf( TechnoExchangeValue( QuantityValueFixture.twoLitres, ProductValueFixture.water ) ), - listOf() ), ), - mutableSetOf() ) + // when - val actual = allocation.apply(system).processes.first().inputs.first().quantity.amount + val actual = Allocation.apply(system).processes.first().inputs.first().quantity.amount // then val delta = 1E-9 @@ -297,11 +271,13 @@ class AllocationTest { fun apply_shouldKeepAllocation() { // given val system = SystemValue( - mutableSetOf(ProcessValue("", emptyMap(), listOf(), listOf(), listOf())), + mutableSetOf(ProcessValue(name = "")), mutableSetOf(propanolCharacterization) ) + // when - val actual = Allocation().apply(system).substanceCharacterizations + val actual = Allocation.apply(system).substanceCharacterizations + // then Assert.assertEquals(setOf(propanolCharacterization), actual) } @@ -310,75 +286,68 @@ class AllocationTest { fun allocationUnitCheck_whenAllocationUnitIsNotPercentage_shouldThrowAnError() { // given val processValue = ProcessValue( - "carrot", - emptyMap(), - listOf( + name = "carrot", + products = listOf( TechnoExchangeValue( QuantityValueFixture.oneKilogram, ProductValueFixture.carrot, QuantityValueFixture.hundredPiece ) ), - listOf(), - listOf() ) + // when + then assertFailsWith( EvaluatorException::class, "Only percent is allowed for allocation unit (process: ${processValue.name})" - ) { Allocation().allocationUnitCheck(processValue) } + ) { Allocation.allocationUnitCheck(processValue) } } @Test fun allocationUnitCheck_whenAllocationUnitIsPercentage_shouldNotThrowAnError() { // given val processValue = ProcessValue( - "carrot", - emptyMap(), - listOf( + name = "carrot", + products = listOf( TechnoExchangeValue( QuantityValueFixture.oneKilogram, ProductValueFixture.carrot, QuantityValueFixture.hundredPercent ) ), - listOf(), - listOf() ) + // when, then should not throw - Allocation().allocationUnitCheck(processValue) + Allocation.allocationUnitCheck(processValue) } @Test fun allocationUnitCheck_whenSumOfAllocationAreNotHundredPercent_shouldThrowAnError() { // given val processValue = ProcessValue( - "carrot", - emptyMap(), - listOf( + name = "carrot", + products = listOf( TechnoExchangeValue( QuantityValueFixture.oneKilogram, ProductValueFixture.carrot, QuantityValueFixture.fiftyPercent ) ), - listOf(), - listOf() ) + // when + then assertFailsWith( EvaluatorException::class, "The sum of the allocations should be hundred percent (process: ${processValue.name})" - ) { Allocation().allocationUnitCheck(processValue) } + ) { Allocation.allocationUnitCheck(processValue) } } @Test fun allocationUnitCheck_whenSumOfAllocationAreHundredPercent_shouldNotThrowAnError() { // given val processValue = ProcessValue( - "carrot", - emptyMap(), - listOf( + name = "carrot", + products = listOf( TechnoExchangeValue( QuantityValueFixture.oneKilogram, ProductValueFixture.carrot, @@ -390,20 +359,18 @@ class AllocationTest { QuantityValueFixture.fiftyPercent ) ), - listOf(), - listOf() ) + // when V then should not throw. - Allocation().allocationUnitCheck(processValue) + Allocation.allocationUnitCheck(processValue) } @Test fun allocationUnitCheck_whenNonConsistentUnits_shouldThrowAnError() { // given val processValue = ProcessValue( - "carrot", - emptyMap(), - listOf( + name = "carrot", + products = listOf( TechnoExchangeValue( QuantityValueFixture.oneKilogram, ProductValueFixture.carrot, @@ -415,13 +382,12 @@ class AllocationTest { QuantityValueFixture.twentyPiece ) ), - listOf(), - listOf() ) + // when + then assertFailsWith( EvaluatorException::class, "Only percent is allowed for allocation unit (process: ${processValue.name})" - ) { Allocation().allocationUnitCheck(processValue) } + ) { Allocation.allocationUnitCheck(processValue) } } } diff --git a/src/test/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/EvaluatorTest.kt b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/EvaluatorTest.kt index 5239c3739..0172a609e 100644 --- a/src/test/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/EvaluatorTest.kt +++ b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/EvaluatorTest.kt @@ -13,30 +13,53 @@ import org.junit.Test import kotlin.test.assertFailsWith class EvaluatorTest { + @Test + fun eval_processWithImpacts_shouldReduceImpacts() { + // given + val symbolTable = SymbolTable.empty() + val instance = EProcessTemplateApplication( + template = EProcessTemplate( + body = EProcess( + name = "eProcess", + impacts = listOf( + ImpactFixture.oneClimateChange + ), + ) + )) + val evaluator = Evaluator(symbolTable) + val expected = ImpactValue( + QuantityValueFixture.oneKilogram, + IndicatorValueFixture.climateChange, + ) + + // when + val actual = evaluator.eval(instance).processes.first().impacts.first() + + // then + assertEquals(expected, actual) + } + @Test fun eval_unresolvedSubstance_shouldBeTreatedAsTerminal() { // given val symbolTable = SymbolTable.empty() - val instance = EProcessTemplateApplication(EProcessTemplate( - params = mapOf(), - locals = mapOf(), - body = EProcess("eProcess", - products = listOf(), - labels = emptyMap(), - inputs = listOf(), - biosphere = listOf( - EBioExchange( - EQuantityScale(1.0, EUnitLiteral(UnitSymbol.of("kg"), 1.0, DimensionFixture.mass)), - ESubstanceSpec("doesNotExist", - "doesNotExist", - SubstanceType.EMISSION, - "water", - "sea water" + val instance = EProcessTemplateApplication( + template = EProcessTemplate( + body = EProcess( + "eProcess", + biosphere = listOf( + EBioExchange( + QuantityFixture.oneKilogram, + ESubstanceSpec("doesNotExist", + "doesNotExist", + SubstanceType.EMISSION, + "water", + "sea water" + ) ) - ) + ), ) - ) - ), emptyMap()) + )) val evaluator = Evaluator(symbolTable) val expected = FullyQualifiedSubstanceValue("doesNotExist", type = SubstanceType.EMISSION, @@ -82,29 +105,26 @@ class EvaluatorTest { val symbolTable = SymbolTable( processTemplates = processTemplates, ) - val expression = EProcessTemplateApplication(EProcessTemplate( - emptyMap(), - emptyMap(), - EProcess( - name = "salad_production", - labels = emptyMap(), - products = listOf( - ETechnoExchange( - QuantityFixture.oneKilogram, - ProductFixture.salad, - ) - ), - inputs = listOf( - ETechnoExchange( - QuantityFixture.oneKilogram, - EProductSpec( - "carrot", + val expression = EProcessTemplateApplication( + template = EProcessTemplate( + body = EProcess( + name = "salad_production", + products = listOf( + ETechnoExchange( + QuantityFixture.oneKilogram, + ProductFixture.salad, ) - ) - ), - biosphere = emptyList() - ) - ), emptyMap()) + ), + inputs = listOf( + ETechnoExchange( + QuantityFixture.oneKilogram, + EProductSpec( + "carrot", + ) + ) + ), + ) + )) val recursiveEvaluator = Evaluator(symbolTable) // when @@ -114,15 +134,13 @@ class EvaluatorTest { val expected = setOf( ProcessValue( name = "carrot_production", - labels = emptyMap(), products = listOf( TechnoExchangeValue( QuantityValueFixture.oneKilogram, ProductValueFixture.carrot.withFromProcessRef( FromProcessRefValue( - "carrot_production", - emptyMap(), - mapOf( + name = "carrot_production", + arguments = mapOf( "q_water" to QuantityValueFixture.oneLitre ), ) @@ -135,19 +153,15 @@ class EvaluatorTest { ProductValueFixture.water ) ), - biosphere = emptyList(), ), ProcessValue( name = "salad_production", - labels = emptyMap(), products = listOf( TechnoExchangeValue( QuantityValueFixture.oneKilogram, ProductValueFixture.salad.withFromProcessRef( FromProcessRefValue( - "salad_production", - emptyMap(), - emptyMap(), + name = "salad_production", ) ) ) @@ -157,16 +171,14 @@ class EvaluatorTest { QuantityValueFixture.oneKilogram, ProductValueFixture.carrot.withFromProcessRef( FromProcessRefValue( - "carrot_production", - emptyMap(), - mapOf( + name = "carrot_production", + arguments = mapOf( "q_water" to QuantityValueFixture.oneLitre, ), ) ) ) ), - biosphere = emptyList(), ), ).naturalSorted() assertEquals(expected, actual) @@ -183,35 +195,33 @@ class EvaluatorTest { val symbolTable = SymbolTable( processTemplates = processTemplates, ) - val expression = EProcessTemplateApplication(EProcessTemplate( - emptyMap(), - emptyMap(), - EProcess( - name = "salad_production", - products = listOf( - ETechnoExchange( - QuantityFixture.oneKilogram, - ProductFixture.salad, - ) - ), - inputs = listOf( - ETechnoExchange( - QuantityFixture.oneKilogram, - EProductSpec( - "carrot", - UnitFixture.kg, - FromProcess( - "carrot_production", - MatchLabels.EMPTY, - mapOf("q_water" to QuantityFixture.twoLitres), - ), + val expression = EProcessTemplateApplication( + template = EProcessTemplate( + body = EProcess( + name = "salad_production", + products = listOf( + ETechnoExchange( + QuantityFixture.oneKilogram, + ProductFixture.salad, ) - ) + ), + inputs = listOf( + ETechnoExchange( + QuantityFixture.oneKilogram, + EProductSpec( + "carrot", + UnitFixture.kg, + FromProcess( + "carrot_production", + MatchLabels.EMPTY, + mapOf("q_water" to QuantityFixture.twoLitres), + ), + ) + ) + ), ), - biosphere = emptyList(), - labels = emptyMap(), ) - ), emptyMap()) + ) val recursiveEvaluator = Evaluator(symbolTable) // when @@ -221,15 +231,12 @@ class EvaluatorTest { val expected = setOf( ProcessValue( name = "salad_production", - labels = emptyMap(), products = listOf( TechnoExchangeValue( QuantityValueFixture.oneKilogram, ProductValueFixture.salad.withFromProcessRef( FromProcessRefValue( - "salad_production", - emptyMap(), - emptyMap(), + name = "salad_production", ) ) ) @@ -239,28 +246,24 @@ class EvaluatorTest { QuantityValueFixture.oneKilogram, ProductValueFixture.carrot.withFromProcessRef( FromProcessRefValue( - "carrot_production", - emptyMap(), - mapOf( + name = "carrot_production", + arguments = mapOf( "q_water" to QuantityValueFixture.twoLitres ) ) ) ) ), - biosphere = emptyList(), ), ProcessValue( name = "carrot_production", - labels = emptyMap(), products = listOf( TechnoExchangeValue( QuantityValueFixture.oneKilogram, ProductValueFixture.carrot.withFromProcessRef( FromProcessRefValue( "carrot_production", - emptyMap(), - mapOf( + arguments = mapOf( "q_water" to QuantityValueFixture.twoLitres ), ), @@ -273,7 +276,6 @@ class EvaluatorTest { ProductValueFixture.water ) ), - biosphere = emptyList(), ), ).naturalSorted() assertEquals(expected, actual) @@ -289,35 +291,33 @@ class EvaluatorTest { ) ) ) - val expression = EProcessTemplateApplication(EProcessTemplate( - emptyMap(), - emptyMap(), - EProcess( - name = "salad_production", - products = listOf( - ETechnoExchange( - QuantityFixture.oneKilogram, - ProductFixture.salad, - ) - ), - inputs = listOf( - ETechnoExchange( - QuantityFixture.oneKilogram, - EProductSpec( - "irrelevant_product", - UnitFixture.kg, - FromProcess( - "carrot_production", - MatchLabels.EMPTY, - mapOf("q_water" to QuantityFixture.twoLitres), - ), + val expression = EProcessTemplateApplication( + template = EProcessTemplate( + body = EProcess( + name = "salad_production", + products = listOf( + ETechnoExchange( + QuantityFixture.oneKilogram, + ProductFixture.salad, ) - ) - ), - biosphere = emptyList(), - labels = emptyMap(), + ), + inputs = listOf( + ETechnoExchange( + QuantityFixture.oneKilogram, + EProductSpec( + "irrelevant_product", + UnitFixture.kg, + FromProcess( + "carrot_production", + MatchLabels.EMPTY, + mapOf("q_water" to QuantityFixture.twoLitres), + ), + ) + ) + ), + ) ) - ), emptyMap()) + ) val recursiveEvaluator = Evaluator(symbolTable) // when/then @@ -337,30 +337,27 @@ class EvaluatorTest { ) ), ) - val expression = EProcessTemplateApplication(EProcessTemplate( - emptyMap(), - emptyMap(), - EProcess( - name = "carrot_production", - products = listOf( - ETechnoExchange( - QuantityFixture.oneKilogram, - ProductFixture.salad, - ) - ), - inputs = emptyList(), - biosphere = listOf( - EBioExchange( - QuantityFixture.oneKilogram, ESubstanceSpec( + val expression = EProcessTemplateApplication( + template = EProcessTemplate( + body = EProcess( + name = "carrot_production", + products = listOf( + ETechnoExchange( + QuantityFixture.oneKilogram, + ProductFixture.salad, + ) + ), + biosphere = listOf( + EBioExchange( + QuantityFixture.oneKilogram, ESubstanceSpec( "propanol", compartment = "air", type = SubstanceType.RESOURCE, - ) - ) - ), - labels = emptyMap(), + )) + ), + ) ) - ), emptyMap()) + ) val recursiveEvaluator = Evaluator(symbolTable) // when diff --git a/src/test/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/HelperTest.kt b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/HelperTest.kt index d8c0e6d10..1c0530160 100644 --- a/src/test/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/HelperTest.kt +++ b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/HelperTest.kt @@ -15,7 +15,6 @@ class HelperTest { val ref = EDataRef("class") val body = EProcess( name = "p", - labels = emptyMap(), products = listOf( ETechnoExchange(QuantityFixture.oneKilogram, ProductFixture.carrot) ), @@ -24,14 +23,12 @@ class HelperTest { QuantityFixture.oneKilogram, ProductFixture.carrot.copy( fromProcess = FromProcess( - "another_process", - MatchLabels(mapOf("class" to ref)), - emptyMap(), + name = "another_process", + matchLabels = MatchLabels(mapOf("class" to ref)), ), ) ) ), - biosphere = emptyList(), ) val helper = Helper() @@ -41,7 +38,6 @@ class HelperTest { // then val expected = EProcess( name = "p", - labels = emptyMap(), products = listOf( ETechnoExchange(QuantityFixture.oneKilogram, ProductFixture.carrot) ), @@ -50,14 +46,12 @@ class HelperTest { QuantityFixture.oneKilogram, ProductFixture.carrot.copy( fromProcess = FromProcess( - "another_process", - MatchLabels(mapOf("class" to EStringLiteral("foo"))), - emptyMap(), + name = "another_process", + matchLabels = MatchLabels(mapOf("class" to EStringLiteral("foo"))), ), ) ) ), - biosphere = emptyList(), ) assertEquals(expected, actual) } @@ -69,7 +63,6 @@ class HelperTest { val ref = EDataRef("q") val body = EProcess( name = "p", - labels = emptyMap(), products = listOf( ETechnoExchange(ref, ProductFixture.carrot) ), @@ -88,7 +81,6 @@ class HelperTest { // then val expected = EProcess( name = "p", - labels = emptyMap(), products = listOf( ETechnoExchange(QuantityFixture.oneKilogram, ProductFixture.carrot) ), @@ -108,7 +100,6 @@ class HelperTest { val ref = EDataRef("q") val body = EProcess( name = "p", - labels = emptyMap(), products = listOf( ETechnoExchange( ref, @@ -125,8 +116,6 @@ class HelperTest { ) ) ), - inputs = emptyList(), - biosphere = emptyList(), ) val helper = Helper() @@ -136,7 +125,6 @@ class HelperTest { // then val expected = EProcess( name = "p", - labels = emptyMap(), products = listOf( ETechnoExchange( QuantityFixture.oneKilogram, @@ -153,8 +141,6 @@ class HelperTest { ) ) ), - inputs = emptyList(), - biosphere = emptyList(), ) assertEquals(expected, actual) } @@ -166,26 +152,24 @@ class HelperTest { listOf( EProcess( name = "p", - labels = emptyMap(), - listOf(ETechnoExchange(EDataRef("quantity"), ProductFixture.carrot)), - listOf( + products = listOf(ETechnoExchange(EDataRef("quantity"), ProductFixture.carrot)), + inputs = listOf( ETechnoExchange( EQuantityScale( 1.0, EQuantityMul(EDataRef("ua"), EDataRef("ub")) ), EProductSpec( - "product", - ) + "product", + ) ), ETechnoExchange( QuantityFixture.oneLitre, EProductSpec( - "water", - UnitFixture.l, - FromProcess("template", MatchLabels.EMPTY, emptyMap()) - ) + "water", + UnitFixture.l, + FromProcess(name = "template", matchLabels = MatchLabels.EMPTY), + ) ), ), - emptyList(), ) ), listOf( @@ -213,4 +197,4 @@ class HelperTest { ) assertEquals(expected, actual) } -} +} \ No newline at end of file diff --git a/src/test/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/reducer/LcaExpressionReducerTest.kt b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/reducer/LcaExpressionReducerTest.kt index 80492eacd..54447ee9e 100644 --- a/src/test/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/reducer/LcaExpressionReducerTest.kt +++ b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/reducer/LcaExpressionReducerTest.kt @@ -16,9 +16,8 @@ class LcaExpressionReducerTest { "a", UnitFixture.kg, FromProcess( - "p", - MatchLabels(mapOf("geo" to EDataRef("geo"))), - emptyMap(), + name = "p", + matchLabels = MatchLabels(mapOf("geo" to EDataRef("geo"))), ) ) ) @@ -38,9 +37,8 @@ class LcaExpressionReducerTest { "a", QuantityFixture.oneKilogram, FromProcess( - "p", - MatchLabels(mapOf("geo" to EStringLiteral("FR"))), - emptyMap(), + name = "p", + matchLabels = MatchLabels(mapOf("geo" to EStringLiteral("FR"))), ) ) ) @@ -52,7 +50,6 @@ class LcaExpressionReducerTest { // given val expression = EProcess( name = "p", - labels = emptyMap(), products = listOf( ETechnoExchange(EDataRef("q_carrot"), ProductFixture.carrot), ), @@ -79,7 +76,6 @@ class LcaExpressionReducerTest { // then val expected = EProcess( name = "p", - labels = emptyMap(), products = listOf( ETechnoExchange( QuantityFixture.oneKilogram, diff --git a/src/test/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/reducer/ProcessTemplateExpressionReducerTest.kt b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/reducer/ProcessTemplateExpressionReducerTest.kt index e08e4c695..31e5f545d 100644 --- a/src/test/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/reducer/ProcessTemplateExpressionReducerTest.kt +++ b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/reducer/ProcessTemplateExpressionReducerTest.kt @@ -1,6 +1,5 @@ package ch.kleis.lcaplugin.core.lang.evaluator.reducer -import ch.kleis.lcaplugin.core.lang.Register import ch.kleis.lcaplugin.core.lang.evaluator.EvaluatorException import ch.kleis.lcaplugin.core.lang.expression.* import ch.kleis.lcaplugin.core.lang.fixture.* @@ -23,7 +22,6 @@ class ProcessTemplateExpressionReducerTest { ), body = EProcess( name = "carrot_production", - labels = emptyMap(), products = listOf( ETechnoExchange( EQuantityAdd( @@ -35,7 +33,6 @@ class ProcessTemplateExpressionReducerTest { inputs = listOf( ETechnoExchange(EDataRef("q_water"), ProductFixture.water), ), - biosphere = emptyList(), ) ) val arguments: Map = mapOf( @@ -51,7 +48,6 @@ class ProcessTemplateExpressionReducerTest { val expected = EProcessFinal( EProcess( name = "carrot_production", - labels = emptyMap(), products = listOf( ETechnoExchange( EQuantityScale(3.0, UnitFixture.kg), @@ -74,7 +70,6 @@ class ProcessTemplateExpressionReducerTest { ProductFixture.water ), ), - biosphere = emptyList(), ) ) assertEquals(expected, actual) @@ -93,7 +88,6 @@ class ProcessTemplateExpressionReducerTest { ), body = EProcess( name = "carrot_production", - labels = emptyMap(), products = listOf( ETechnoExchange( EQuantityAdd( @@ -105,7 +99,6 @@ class ProcessTemplateExpressionReducerTest { inputs = listOf( ETechnoExchange(EDataRef("q_water"), ProductFixture.water), ), - biosphere = emptyList(), ) ) val arguments: Map = mapOf( diff --git a/src/test/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/step/CompleteDefaultArgumentsTest.kt b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/step/CompleteDefaultArgumentsTest.kt index 24321ce75..c8e995c81 100644 --- a/src/test/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/step/CompleteDefaultArgumentsTest.kt +++ b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/step/CompleteDefaultArgumentsTest.kt @@ -26,7 +26,6 @@ class CompleteDefaultArgumentsTest { mapOf( "carrot_production" to EProcessTemplate( params = params, - locals = emptyMap(), body = ProcessFixture.carrotProduction, ) ) @@ -34,13 +33,9 @@ class CompleteDefaultArgumentsTest { ) val completeDefaultArguments = CompleteDefaultArguments(symbolTable) val expression = EProcessTemplateApplication( - EProcessTemplate( - params = emptyMap(), - locals = emptyMap(), + template = EProcessTemplate( body = EProcess( "salad_production", - labels = emptyMap(), - products = emptyList(), inputs = listOf( ETechnoExchange( QuantityFixture.oneKilogram, @@ -57,9 +52,8 @@ class CompleteDefaultArgumentsTest { ) ) ), - biosphere = emptyList(), ) - ), emptyMap() + ), ) val everyInputProduct = ProcessTemplateExpression .eProcessTemplateApplication @@ -93,7 +87,6 @@ class CompleteDefaultArgumentsTest { mapOf( "carrot_production" to EProcessTemplate( params = params, - locals = emptyMap(), body = ProcessFixture.carrotProduction, ) ) @@ -101,13 +94,9 @@ class CompleteDefaultArgumentsTest { ) val completeDefaultArguments = CompleteDefaultArguments(symbolTable) val expression = EProcessTemplateApplication( - EProcessTemplate( - params = emptyMap(), - locals = emptyMap(), + template = EProcessTemplate( body = EProcess( "salad_production", - labels = emptyMap(), - products = emptyList(), inputs = listOf( ETechnoExchange( QuantityFixture.oneKilogram, @@ -124,10 +113,10 @@ class CompleteDefaultArgumentsTest { ) ) ), - biosphere = emptyList(), ) - ), emptyMap() + ), ) + val everyInputProduct = EProcessTemplateApplication .template .body @@ -159,7 +148,6 @@ class CompleteDefaultArgumentsTest { mapOf( "carrot_production" to EProcessTemplate( params = params, - locals = emptyMap(), body = ProcessFixture.carrotProduction, ) ) @@ -167,13 +155,9 @@ class CompleteDefaultArgumentsTest { ) val completeDefaultArguments = CompleteDefaultArguments(symbolTable) val expression = EProcessTemplateApplication( - EProcessTemplate( - params = emptyMap(), - locals = emptyMap(), + template = EProcessTemplate( body = EProcess( "salad_production", - labels = emptyMap(), - products = emptyList(), inputs = listOf( ETechnoExchange( QuantityFixture.oneKilogram, @@ -181,17 +165,16 @@ class CompleteDefaultArgumentsTest { "carrot", UnitFixture.kg, FromProcess( - "carrot_production", - MatchLabels.EMPTY, - emptyMap(), + name = "carrot_production", + matchLabels = MatchLabels.EMPTY, ) ) ) ), - biosphere = emptyList(), ) - ), emptyMap() + ), ) + val everyInputProduct = EProcessTemplateApplication .template .body @@ -223,7 +206,6 @@ class CompleteDefaultArgumentsTest { mapOf( "carrot_production" to EProcessTemplate( params = params, - locals = emptyMap(), body = ProcessFixture.carrotProduction, ) ) @@ -231,13 +213,9 @@ class CompleteDefaultArgumentsTest { ) val completeDefaultArguments = CompleteDefaultArguments(symbolTable) val expression = EProcessTemplateApplication( - EProcessTemplate( - params = emptyMap(), - locals = emptyMap(), + template = EProcessTemplate( body = EProcess( "salad_production", - labels = emptyMap(), - products = emptyList(), inputs = listOf( ETechnoExchange( QuantityFixture.oneKilogram, @@ -247,10 +225,10 @@ class CompleteDefaultArgumentsTest { ) ) ), - biosphere = emptyList(), ) - ), emptyMap() + ), ) + val everyInputProduct = EProcessTemplateApplication .template .body diff --git a/src/test/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/step/CompleteTerminalsTest.kt b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/step/CompleteTerminalsTest.kt index 6931904dd..e499f293e 100644 --- a/src/test/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/step/CompleteTerminalsTest.kt +++ b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/step/CompleteTerminalsTest.kt @@ -18,26 +18,19 @@ class CompleteTerminalsTest { val process = EProcessFinal( EProcess( name = "process", - labels = emptyMap(), - products = emptyList(), - inputs = emptyList(), biosphere = listOf( EBioExchange(QuantityFixture.oneKilogram, ESubstanceSpec("co2")) - ) + ), ) ) - val completeTerminals = CompleteTerminals() // when - val actual = completeTerminals.apply(process) + val actual = CompleteTerminals.apply(process) // then val expected = EProcessFinal( EProcess( name = "process", - labels = emptyMap(), - products = emptyList(), - inputs = emptyList(), biosphere = listOf( EBioExchange( QuantityFixture.oneKilogram, @@ -61,10 +54,9 @@ class CompleteTerminalsTest { EImpact(QuantityFixture.oneKilogram, EIndicatorSpec("cc")) ) ) - val completeTerminals = CompleteTerminals() // when - val actual = completeTerminals.apply(expression).toValue() + val actual = CompleteTerminals.apply(expression).toValue() // then val expected = SubstanceCharacterizationValue( @@ -78,5 +70,4 @@ class CompleteTerminalsTest { ) Assert.assertEquals(expected, actual) } - -} +} \ No newline at end of file diff --git a/src/test/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/step/ReduceLabelSelectorsTest.kt b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/step/ReduceLabelSelectorsTest.kt index fd0a9830f..05efe50c6 100644 --- a/src/test/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/step/ReduceLabelSelectorsTest.kt +++ b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/step/ReduceLabelSelectorsTest.kt @@ -13,28 +13,23 @@ class ReduceLabelSelectorsTest { fun reduce_whenPassingByArguments() { // given val instance = EProcessTemplateApplication( - EProcessTemplate( + template = EProcessTemplate( params = mapOf("geo" to EStringLiteral("GLO")), - locals = emptyMap(), - EProcess( - "salad_production", - labels = emptyMap(), - products = emptyList(), + body = EProcess( + name = "salad_production", inputs = listOf( ETechnoExchange( - QuantityFixture.oneKilogram, - EProductSpec( - "carrot", - QuantityFixture.oneKilogram, - FromProcess( - "carrot_production", - MatchLabels(mapOf("geo" to EDataRef("geo"))), - emptyMap(), + quantity = QuantityFixture.oneKilogram, + product = EProductSpec( + name = "carrot", + referenceUnit = QuantityFixture.oneKilogram, + fromProcess = FromProcess( + name = "carrot_production", + matchLabels = MatchLabels(mapOf("geo" to EDataRef("geo"))), ) ) ) ), - biosphere = emptyList(), ) ), mapOf("geo" to EStringLiteral("FR")), @@ -48,11 +43,8 @@ class ReduceLabelSelectorsTest { val expected = EProcessTemplateApplication( EProcessTemplate( params = mapOf("geo" to EStringLiteral("GLO")), - locals = emptyMap(), - EProcess( + body = EProcess( "salad_production", - labels = emptyMap(), - products = emptyList(), inputs = listOf( ETechnoExchange( QuantityFixture.oneKilogram, @@ -60,14 +52,12 @@ class ReduceLabelSelectorsTest { "carrot", QuantityFixture.oneKilogram, FromProcess( - "carrot_production", - MatchLabels(mapOf("geo" to EStringLiteral("FR"))), - emptyMap(), + name = "carrot_production", + matchLabels = MatchLabels(mapOf("geo" to EStringLiteral("FR"))), ) ) ) ), - biosphere = emptyList(), ) ), mapOf("geo" to EStringLiteral("FR")), @@ -79,13 +69,10 @@ class ReduceLabelSelectorsTest { fun reduce_whenPassingByDefaultParams() { // given val instance = EProcessTemplateApplication( - EProcessTemplate( + template = EProcessTemplate( params = mapOf("geo" to EStringLiteral("GLO")), - locals = emptyMap(), - EProcess( - "salad_production", - labels = emptyMap(), - products = emptyList(), + body = EProcess( + name = "salad_production", inputs = listOf( ETechnoExchange( QuantityFixture.oneKilogram, @@ -93,17 +80,14 @@ class ReduceLabelSelectorsTest { "carrot", QuantityFixture.oneKilogram, FromProcess( - "carrot_production", - MatchLabels(mapOf("geo" to EDataRef("geo"))), - emptyMap(), + name = "carrot_production", + matchLabels = MatchLabels(mapOf("geo" to EDataRef("geo"))), ) ) ) ), - biosphere = emptyList(), ) ), - emptyMap(), ) val reduceLabelSelectors = ReduceLabelSelectors(SymbolTable()) @@ -112,13 +96,10 @@ class ReduceLabelSelectorsTest { // then val expected = EProcessTemplateApplication( - EProcessTemplate( + template = EProcessTemplate( params = mapOf("geo" to EStringLiteral("GLO")), - locals = emptyMap(), - EProcess( - "salad_production", - labels = emptyMap(), - products = emptyList(), + body = EProcess( + name = "salad_production", inputs = listOf( ETechnoExchange( QuantityFixture.oneKilogram, @@ -126,17 +107,14 @@ class ReduceLabelSelectorsTest { "carrot", QuantityFixture.oneKilogram, FromProcess( - "carrot_production", - MatchLabels(mapOf("geo" to EStringLiteral("GLO"))), - emptyMap(), + name = "carrot_production", + matchLabels = MatchLabels(mapOf("geo" to EStringLiteral("GLO"))), ) ) ) ), - biosphere = emptyList(), ) ), - emptyMap(), ) assertEquals(expected, actual) } @@ -145,13 +123,10 @@ class ReduceLabelSelectorsTest { fun reduce_whenPassingByLocals() { // given val instance = EProcessTemplateApplication( - EProcessTemplate( - params = emptyMap(), + template = EProcessTemplate( locals = mapOf("geo" to EStringLiteral("GLO")), - EProcess( + body = EProcess( "salad_production", - labels = emptyMap(), - products = emptyList(), inputs = listOf( ETechnoExchange( QuantityFixture.oneKilogram, @@ -159,17 +134,14 @@ class ReduceLabelSelectorsTest { "carrot", QuantityFixture.oneKilogram, FromProcess( - "carrot_production", - MatchLabels(mapOf("geo" to EDataRef("geo"))), - emptyMap(), + name = "carrot_production", + matchLabels = MatchLabels(mapOf("geo" to EDataRef("geo"))), ) ) ) ), - biosphere = emptyList(), ) ), - emptyMap(), ) val reduceLabelSelectors = ReduceLabelSelectors(SymbolTable()) @@ -178,13 +150,10 @@ class ReduceLabelSelectorsTest { // then val expected = EProcessTemplateApplication( - EProcessTemplate( - params = emptyMap(), + template = EProcessTemplate( locals = mapOf("geo" to EStringLiteral("GLO")), - EProcess( - "salad_production", - labels = emptyMap(), - products = emptyList(), + body = EProcess( + name = "salad_production", inputs = listOf( ETechnoExchange( QuantityFixture.oneKilogram, @@ -192,17 +161,14 @@ class ReduceLabelSelectorsTest { "carrot", QuantityFixture.oneKilogram, FromProcess( - "carrot_production", - MatchLabels(mapOf("geo" to EStringLiteral("GLO"))), - emptyMap(), + name = "carrot_production", + matchLabels = MatchLabels(mapOf("geo" to EStringLiteral("GLO"))), ) ) ) ), - biosphere = emptyList(), ) ), - emptyMap(), ) assertEquals(expected, actual) } @@ -211,13 +177,9 @@ class ReduceLabelSelectorsTest { fun reduce_whenPassingByGlobalVariables() { // given val instance = EProcessTemplateApplication( - EProcessTemplate( - params = emptyMap(), - locals = emptyMap(), - EProcess( - "salad_production", - labels = emptyMap(), - products = emptyList(), + template = EProcessTemplate( + body = EProcess( + name = "salad_production", inputs = listOf( ETechnoExchange( QuantityFixture.oneKilogram, @@ -225,18 +187,16 @@ class ReduceLabelSelectorsTest { "carrot", QuantityFixture.oneKilogram, FromProcess( - "carrot_production", - MatchLabels(mapOf("geo" to EDataRef("geo"))), - emptyMap(), + name = "carrot_production", + matchLabels = MatchLabels(mapOf("geo" to EDataRef("geo"))), ) ) ) ), - biosphere = emptyList(), ) ), - emptyMap(), ) + val reduceLabelSelectors = ReduceLabelSelectors( SymbolTable( data = Register.from(mapOf("geo" to EStringLiteral("FR"))) @@ -248,13 +208,9 @@ class ReduceLabelSelectorsTest { // then val expected = EProcessTemplateApplication( - EProcessTemplate( - params = emptyMap(), - locals = emptyMap(), - EProcess( + template = EProcessTemplate( + body = EProcess( "salad_production", - labels = emptyMap(), - products = emptyList(), inputs = listOf( ETechnoExchange( QuantityFixture.oneKilogram, @@ -262,17 +218,14 @@ class ReduceLabelSelectorsTest { "carrot", QuantityFixture.oneKilogram, FromProcess( - "carrot_production", - MatchLabels(mapOf("geo" to EStringLiteral("FR"))), - emptyMap(), + name = "carrot_production", + matchLabels = MatchLabels(mapOf("geo" to EStringLiteral("FR"))), ) ) ) ), - biosphere = emptyList(), ) ), - emptyMap(), ) assertEquals(expected, actual) } @@ -281,13 +234,10 @@ class ReduceLabelSelectorsTest { fun reduce_whenPassingByProcessLabels() { // given val instance = EProcessTemplateApplication( - EProcessTemplate( - params = emptyMap(), - locals = emptyMap(), - EProcess( + template = EProcessTemplate( + body = EProcess( "salad_production", labels = mapOf("geo" to EStringLiteral("FR")), - products = emptyList(), inputs = listOf( ETechnoExchange( QuantityFixture.oneKilogram, @@ -295,17 +245,14 @@ class ReduceLabelSelectorsTest { "carrot", QuantityFixture.oneKilogram, FromProcess( - "carrot_production", - MatchLabels(mapOf("geo" to EDataRef("geo"))), - emptyMap(), + name = "carrot_production", + matchLabels = MatchLabels(mapOf("geo" to EDataRef("geo"))), ) ) ) ), - biosphere = emptyList(), ) ), - emptyMap(), ) val reduceLabelSelectors = ReduceLabelSelectors(SymbolTable()) @@ -314,13 +261,10 @@ class ReduceLabelSelectorsTest { // then val expected = EProcessTemplateApplication( - EProcessTemplate( - params = emptyMap(), - locals = emptyMap(), - EProcess( + template = EProcessTemplate( + body = EProcess( "salad_production", labels = mapOf("geo" to EStringLiteral("FR")), - products = emptyList(), inputs = listOf( ETechnoExchange( QuantityFixture.oneKilogram, @@ -328,18 +272,15 @@ class ReduceLabelSelectorsTest { "carrot", QuantityFixture.oneKilogram, FromProcess( - "carrot_production", - MatchLabels(mapOf("geo" to EStringLiteral("FR"))), - emptyMap(), + name = "carrot_production", + matchLabels = MatchLabels(mapOf("geo" to EStringLiteral("FR"))), ) ) ) ), - biosphere = emptyList(), ) ), - emptyMap(), ) assertEquals(expected, actual) } -} +} \ No newline at end of file diff --git a/src/test/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/step/ReduceTest.kt b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/step/ReduceTest.kt index 0978c4e9e..fbafd35ba 100644 --- a/src/test/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/step/ReduceTest.kt +++ b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/step/ReduceTest.kt @@ -25,14 +25,14 @@ class ReduceTest { val template = TemplateFixture.carrotProduction val instance = EProcessTemplateApplication( template, mapOf( - Pair( - "q_water", EQuantityAdd( - QuantityFixture.oneLitre, - QuantityFixture.oneLitre, - ) - ) + Pair( + "q_water", EQuantityAdd( + QuantityFixture.oneLitre, + QuantityFixture.oneLitre, + ) ) ) + ) val reduceAndComplete = Reduce(SymbolTable.empty()) // when @@ -40,27 +40,24 @@ class ReduceTest { // then val expected = ProcessValue( - "carrot_production", - emptyMap(), - listOf( + name = "carrot_production", + products = listOf( TechnoExchangeValue( QuantityValueFixture.oneKilogram, ProductValueFixture.carrot.withFromProcessRef( FromProcessRefValue( - "carrot_production", - emptyMap(), - mapOf("q_water" to QuantityValueFixture.twoLitres), + name = "carrot_production", + arguments = mapOf("q_water" to QuantityValueFixture.twoLitres), ) ), ) ), - listOf( + inputs = listOf( TechnoExchangeValue( QuantityValueFixture.twoLitres, ProductValueFixture.water, ) ), - emptyList(), ) assertEquals(expected, actual) } @@ -77,26 +74,23 @@ class ReduceTest { // then val expected = ProcessValue( name = "carrot_production", - emptyMap(), - listOf( + products = listOf( TechnoExchangeValue( QuantityValueFixture.oneKilogram, ProductValueFixture.carrot.withFromProcessRef( FromProcessRefValue( - "carrot_production", - emptyMap(), - mapOf("q_water" to QuantityValueFixture.oneLitre), + name = "carrot_production", + arguments = mapOf("q_water" to QuantityValueFixture.oneLitre), ) ) ) ), - listOf( + inputs = listOf( TechnoExchangeValue( QuantityValueFixture.oneLitre, ProductValueFixture.water ) ), - emptyList(), ) assertEquals(expected, actual) } diff --git a/src/test/kotlin/ch/kleis/lcaplugin/core/lang/fixture/ImpactFixture.kt b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/fixture/ImpactFixture.kt new file mode 100644 index 000000000..5c44b2bd3 --- /dev/null +++ b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/fixture/ImpactFixture.kt @@ -0,0 +1,10 @@ +package ch.kleis.lcaplugin.core.lang.fixture + +import ch.kleis.lcaplugin.core.lang.expression.EImpact + +object ImpactFixture { + val oneClimateChange: EImpact = EImpact( + quantity = QuantityFixture.oneKilogram, + indicator = IndicatorFixture.climateChange + ) +} diff --git a/src/test/kotlin/ch/kleis/lcaplugin/core/lang/fixture/IndicatorFixture.kt b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/fixture/IndicatorFixture.kt index f38d129b8..4c2ef7be0 100644 --- a/src/test/kotlin/ch/kleis/lcaplugin/core/lang/fixture/IndicatorFixture.kt +++ b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/fixture/IndicatorFixture.kt @@ -5,7 +5,7 @@ import ch.kleis.lcaplugin.core.lang.expression.EIndicatorSpec class IndicatorFixture { companion object { val climateChange = EIndicatorSpec( - "climate change", + "Climate Change", QuantityFixture.oneKilogram, ) } diff --git a/src/test/kotlin/ch/kleis/lcaplugin/core/lang/fixture/IndicatorValueFixture.kt b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/fixture/IndicatorValueFixture.kt index 20f1c5663..d9227f294 100644 --- a/src/test/kotlin/ch/kleis/lcaplugin/core/lang/fixture/IndicatorValueFixture.kt +++ b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/fixture/IndicatorValueFixture.kt @@ -4,6 +4,6 @@ import ch.kleis.lcaplugin.core.lang.value.IndicatorValue class IndicatorValueFixture { companion object { - val climateChange = IndicatorValue("climate change", UnitValueFixture.kg) + val climateChange = IndicatorValue("Climate Change", UnitValueFixture.kg) } } diff --git a/src/test/kotlin/ch/kleis/lcaplugin/core/lang/fixture/ProcessFixture.kt b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/fixture/ProcessFixture.kt index 6d37ec2fa..fbe9e1aa3 100644 --- a/src/test/kotlin/ch/kleis/lcaplugin/core/lang/fixture/ProcessFixture.kt +++ b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/fixture/ProcessFixture.kt @@ -18,6 +18,9 @@ class ProcessFixture { biosphere = listOf( EBioExchange(QuantityFixture.oneKilogram, SubstanceFixture.propanol), ), + impacts = listOf( + ImpactFixture.oneClimateChange + ) ) } } diff --git a/src/test/kotlin/ch/kleis/lcaplugin/core/lang/fixture/ProcessValueFixture.kt b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/fixture/ProcessValueFixture.kt index 4155e026d..9b355a8b4 100644 --- a/src/test/kotlin/ch/kleis/lcaplugin/core/lang/fixture/ProcessValueFixture.kt +++ b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/fixture/ProcessValueFixture.kt @@ -8,19 +8,14 @@ import ch.kleis.lcaplugin.core.lang.value.ProcessValue class ProcessValueFixture { companion object { val carrotProcessValue = ProcessValue( - "carrot", - emptyMap(), - listOf(carrotTechnoExchangeValue), - listOf(), - listOf() + name = "carrot", + products = listOf(carrotTechnoExchangeValue), ) val carrotProcessValueWithAllocation = ProcessValue( - "carrot", - emptyMap(), - listOf(carrotTechnoExchangeValueWithAllocation), - listOf(waterTechnoExchangeValueWithAllocation), - listOf() + name = "carrot", + products = listOf(carrotTechnoExchangeValueWithAllocation), + inputs = listOf(waterTechnoExchangeValueWithAllocation), ) } } diff --git a/src/test/kotlin/ch/kleis/lcaplugin/core/lang/fixture/QuantityFixture.kt b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/fixture/QuantityFixture.kt index 23f164abc..13c832341 100644 --- a/src/test/kotlin/ch/kleis/lcaplugin/core/lang/fixture/QuantityFixture.kt +++ b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/fixture/QuantityFixture.kt @@ -2,12 +2,10 @@ package ch.kleis.lcaplugin.core.lang.fixture import ch.kleis.lcaplugin.core.lang.expression.EQuantityScale -class QuantityFixture { - companion object { - val oneKilogram = EQuantityScale(1.0, UnitFixture.kg) - val twoKilograms = EQuantityScale(2.0, UnitFixture.kg) - val oneLitre = EQuantityScale(1.0, UnitFixture.l) - val twoLitres = EQuantityScale(2.0, UnitFixture.l) - val hundredPercent = EQuantityScale(100.0, UnitFixture.percent) - } -} +object QuantityFixture { + val oneUnit = EQuantityScale(1.0, UnitFixture.unit) + val oneKilogram = EQuantityScale(1.0, UnitFixture.kg) + val twoKilograms = EQuantityScale(2.0, UnitFixture.kg) + val oneLitre = EQuantityScale(1.0, UnitFixture.l) + val twoLitres = EQuantityScale(2.0, UnitFixture.l) +} \ No newline at end of file diff --git a/src/test/kotlin/ch/kleis/lcaplugin/core/lang/fixture/QuantityValueFixture.kt b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/fixture/QuantityValueFixture.kt index be5b95bac..33738b69c 100644 --- a/src/test/kotlin/ch/kleis/lcaplugin/core/lang/fixture/QuantityValueFixture.kt +++ b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/fixture/QuantityValueFixture.kt @@ -4,6 +4,7 @@ import ch.kleis.lcaplugin.core.lang.value.QuantityValue class QuantityValueFixture { companion object { + val oneUnit = QuantityValue(1.0, UnitValueFixture.unit) val oneKilogram = QuantityValue(1.0, UnitValueFixture.kg) val twoKilograms = QuantityValue(2.0, UnitValueFixture.kg) val oneLitre = QuantityValue(1.0, UnitValueFixture.l) @@ -13,7 +14,6 @@ class QuantityValueFixture { val fiftyPercent = QuantityValue(50.0, UnitValueFixture.percent) val hundredPercent = QuantityValue(100.0, UnitValueFixture.percent) val twentyPiece = QuantityValue(20.0, UnitValueFixture.piece) - val thirtyPiece = QuantityValue(30.0, UnitValueFixture.piece) val hundredPiece = QuantityValue(100.0, UnitValueFixture.piece) } } diff --git a/src/test/kotlin/ch/kleis/lcaplugin/core/lang/fixture/TemplateFixture.kt b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/fixture/TemplateFixture.kt index f40b4d8e4..e7fb452e4 100644 --- a/src/test/kotlin/ch/kleis/lcaplugin/core/lang/fixture/TemplateFixture.kt +++ b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/fixture/TemplateFixture.kt @@ -1,6 +1,9 @@ package ch.kleis.lcaplugin.core.lang.fixture -import ch.kleis.lcaplugin.core.lang.expression.* +import ch.kleis.lcaplugin.core.lang.expression.EDataRef +import ch.kleis.lcaplugin.core.lang.expression.EProcess +import ch.kleis.lcaplugin.core.lang.expression.EProcessTemplate +import ch.kleis.lcaplugin.core.lang.expression.ETechnoExchange class TemplateFixture { companion object { @@ -13,29 +16,23 @@ class TemplateFixture { ), body = EProcess( name = "carrot_production", - labels = emptyMap(), products = listOf( ETechnoExchange(EDataRef("q_carrot"), ProductFixture.carrot), ), inputs = listOf( ETechnoExchange(EDataRef("q_water"), ProductFixture.water), ), - biosphere = emptyList(), ) ) val withUnboundedRef = EProcessTemplate( - params = emptyMap(), - locals = emptyMap(), body = EProcess( name = "with_unbounded_ref", - labels = emptyMap(), products = listOf( ETechnoExchange(EDataRef("q_carrot"), ProductFixture.carrot), ), inputs = listOf( ETechnoExchange(EDataRef("q_water"), ProductFixture.water), ), - biosphere = emptyList(), ) ) } diff --git a/src/test/kotlin/ch/kleis/lcaplugin/core/lang/fixture/UnitFixture.kt b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/fixture/UnitFixture.kt index 733eb6dde..dacfa529a 100644 --- a/src/test/kotlin/ch/kleis/lcaplugin/core/lang/fixture/UnitFixture.kt +++ b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/fixture/UnitFixture.kt @@ -4,26 +4,23 @@ import ch.kleis.lcaplugin.core.lang.dimension.Dimension import ch.kleis.lcaplugin.core.lang.dimension.UnitSymbol import ch.kleis.lcaplugin.core.lang.expression.EUnitLiteral -class DimensionFixture { - companion object { - val mass = Dimension.of("mass") - val length = Dimension.of("length") - val time = Dimension.of("time") - val volume = length.pow(3.0) - } +object DimensionFixture { + val mass = Dimension.of("mass") + val length = Dimension.of("length") + val time = Dimension.of("time") + val volume = length.pow(3.0) } -class UnitFixture { - companion object { - val kg = EUnitLiteral(UnitSymbol.of("kg"), 1.0, DimensionFixture.mass) - val g = EUnitLiteral(UnitSymbol.of("g"), 1.0e-3, DimensionFixture.mass) - val m = EUnitLiteral(UnitSymbol.of("m"), 1.0, DimensionFixture.length) - val km = EUnitLiteral(UnitSymbol.of("km"), 1000.0, DimensionFixture.length) - val person = EUnitLiteral(UnitSymbol.of("person"), 1.0, Dimension.None) - val pack = EUnitLiteral(UnitSymbol.of("pack"), 1.0, Dimension.None) - val l = EUnitLiteral(UnitSymbol.of("l"), 1.0e-3, DimensionFixture.volume) - val s = EUnitLiteral(UnitSymbol.of("s"), 1.0, DimensionFixture.time) - val hour = EUnitLiteral(UnitSymbol.of("hour"), 3600.0, DimensionFixture.time) - val percent = EUnitLiteral(UnitSymbol.of("percent"), 1.0e-2, Dimension.None) - } +object UnitFixture { + val unit = EUnitLiteral(UnitSymbol.of("unit"), 1.0, Dimension.None) + val kg = EUnitLiteral(UnitSymbol.of("kg"), 1.0, DimensionFixture.mass) + val g = EUnitLiteral(UnitSymbol.of("g"), 1.0e-3, DimensionFixture.mass) + val m = EUnitLiteral(UnitSymbol.of("m"), 1.0, DimensionFixture.length) + val km = EUnitLiteral(UnitSymbol.of("km"), 1000.0, DimensionFixture.length) + val person = EUnitLiteral(UnitSymbol.of("person"), 1.0, Dimension.None) + val pack = EUnitLiteral(UnitSymbol.of("pack"), 1.0, Dimension.None) + val l = EUnitLiteral(UnitSymbol.of("l"), 1.0e-3, DimensionFixture.volume) + val s = EUnitLiteral(UnitSymbol.of("s"), 1.0, DimensionFixture.time) + val hour = EUnitLiteral(UnitSymbol.of("hour"), 3600.0, DimensionFixture.time) + val percent = EUnitLiteral(UnitSymbol.of("percent"), 1.0e-2, Dimension.None) } diff --git a/src/test/kotlin/ch/kleis/lcaplugin/core/lang/fixture/UnitValueFixture.kt b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/fixture/UnitValueFixture.kt index 4b7144fd7..fe2809944 100644 --- a/src/test/kotlin/ch/kleis/lcaplugin/core/lang/fixture/UnitValueFixture.kt +++ b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/fixture/UnitValueFixture.kt @@ -12,5 +12,6 @@ class UnitValueFixture { val l = UnitValue(UnitSymbol.of("l"), 1.0e-3, DimensionFixture.volume) val percent = UnitValue(UnitSymbol.of("percent"), 1.0e-2, Dimension.None) val piece = UnitValue(UnitSymbol.of("piece"), 1.0, Dimension.None) + val unit = UnitValue(UnitSymbol.of("unit"), 1.0, Dimension.None) } } diff --git a/src/test/kotlin/ch/kleis/lcaplugin/core/lang/resolver/ProcessResolverTest.kt b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/resolver/ProcessResolverTest.kt index a82a35ae0..943e9eea7 100644 --- a/src/test/kotlin/ch/kleis/lcaplugin/core/lang/resolver/ProcessResolverTest.kt +++ b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/resolver/ProcessResolverTest.kt @@ -24,20 +24,15 @@ class ProcessResolverTest { inputs = listOf( ETechnoExchange(EDataRef("q_water"), ProductFixture.water), ), - biosphere = emptyList(), ) val carrotProductionFR = EProcessTemplate( - emptyMap(), - emptyMap(), - carrotProductionBodyFR, + body = carrotProductionBodyFR, ) val carrotProductionBodyUK = carrotProductionBodyFR.copy( labels = mapOf("geo" to EStringLiteral("UK")), ) val carrotProductionUK = EProcessTemplate( - emptyMap(), - emptyMap(), - carrotProductionBodyUK, + body = carrotProductionBodyUK, ) val processTemplates: Register = Register.from( mapOf( @@ -76,14 +71,12 @@ class ProcessResolverTest { ), body = EProcess( name = "carrot_production", - labels = emptyMap(), products = listOf( ETechnoExchange(EDataRef("q_carrot"), ProductFixture.carrot), ), inputs = listOf( ETechnoExchange(EDataRef("q_water"), ProductFixture.water), ), - biosphere = emptyList(), ) ) val processTemplates: Register = Register.from( @@ -119,29 +112,23 @@ class ProcessResolverTest { ), body = EProcess( name = "carrot_production", - labels = emptyMap(), products = listOf( ETechnoExchange(EDataRef("q_carrot"), ProductFixture.carrot), ), inputs = listOf( ETechnoExchange(EDataRef("q_water"), ProductFixture.water), ), - biosphere = emptyList(), ) ) val saladProduction = EProcessTemplate( - params = emptyMap(), - locals = emptyMap(), body = EProcess( name = "salad_production", - labels = emptyMap(), products = listOf( ETechnoExchange(QuantityFixture.oneKilogram, ProductFixture.salad), ), inputs = listOf( ETechnoExchange(QuantityFixture.oneKilogram, ProductFixture.carrot), ), - biosphere = emptyList(), ) ) val processTemplates: Register = Register.from( @@ -177,29 +164,23 @@ class ProcessResolverTest { ), body = EProcess( name = "carrot_production", - labels = emptyMap(), products = listOf( ETechnoExchange(EDataRef("q_carrot"), ProductFixture.carrot), ), inputs = listOf( ETechnoExchange(EDataRef("q_water"), ProductFixture.water), ), - biosphere = emptyList(), ) ) val saladProduction = EProcessTemplate( - params = emptyMap(), - locals = emptyMap(), body = EProcess( name = "salad_production", - labels = emptyMap(), products = listOf( ETechnoExchange(QuantityFixture.oneKilogram, ProductFixture.salad), ), inputs = listOf( ETechnoExchange(QuantityFixture.oneKilogram, ProductFixture.carrot), ), - biosphere = emptyList(), ) ) val processTemplates: Register = Register.from( @@ -225,8 +206,6 @@ class ProcessResolverTest { fun resolve_whenNameOnly_multipleMatch_shouldReturnNull() { // given val carrotProductionFR = EProcessTemplate( - params = emptyMap(), - locals = emptyMap(), body = EProcess( name = "carrot_production", labels = mapOf("geo" to EStringLiteral("FR")), @@ -236,12 +215,9 @@ class ProcessResolverTest { inputs = listOf( ETechnoExchange(QuantityFixture.oneKilogram, ProductFixture.water), ), - biosphere = emptyList(), ) ) val carrotProductionUK = EProcessTemplate( - params = emptyMap(), - locals = emptyMap(), body = EProcess( name = "carrot_production", labels = mapOf("geo" to EStringLiteral("UK")), @@ -251,12 +227,9 @@ class ProcessResolverTest { inputs = listOf( ETechnoExchange(QuantityFixture.oneKilogram, ProductFixture.water), ), - biosphere = emptyList(), ) ) val saladProduction = EProcessTemplate( - params = emptyMap(), - locals = emptyMap(), body = EProcess( name = "salad_production", labels = emptyMap(), @@ -266,7 +239,6 @@ class ProcessResolverTest { inputs = listOf( ETechnoExchange(QuantityFixture.oneKilogram, ProductFixture.carrot), ), - biosphere = emptyList(), ) ) val processTemplates: Register = Register.from( diff --git a/src/test/kotlin/ch/kleis/lcaplugin/e2e/E2ETest.kt b/src/test/kotlin/ch/kleis/lcaplugin/e2e/E2ETest.kt index 2522ad421..0d3722114 100644 --- a/src/test/kotlin/ch/kleis/lcaplugin/e2e/E2ETest.kt +++ b/src/test/kotlin/ch/kleis/lcaplugin/e2e/E2ETest.kt @@ -14,6 +14,7 @@ import ch.kleis.lcaplugin.core.lang.expression.EQuantityScale import ch.kleis.lcaplugin.core.lang.expression.EUnitLiteral import ch.kleis.lcaplugin.core.lang.fixture.DimensionFixture import ch.kleis.lcaplugin.core.lang.fixture.UnitFixture +import ch.kleis.lcaplugin.core.lang.fixture.UnitValueFixture import ch.kleis.lcaplugin.core.lang.value.FromProcessRefValue import ch.kleis.lcaplugin.core.lang.value.ProductValue import ch.kleis.lcaplugin.core.lang.value.QuantityValue @@ -83,7 +84,7 @@ class E2ETest : BasePlatformTestCase() { val symbolTable = parser.load() // when - val entryPoint = EProcessTemplateApplication(symbolTable.getTemplate("p")!!, emptyMap()) + val entryPoint = EProcessTemplateApplication(template = symbolTable.getTemplate("p")!!) val trace = Evaluator(symbolTable).trace(entryPoint) val system = trace.getSystemValue() val assessment = Assessment(system, trace.getEntryPoint()) @@ -158,7 +159,7 @@ class E2ETest : BasePlatformTestCase() { val symbolTable = parser.load() // when - val entryPoint = EProcessTemplateApplication(symbolTable.getTemplate("p")!!, emptyMap()) + val entryPoint = EProcessTemplateApplication(template = symbolTable.getTemplate("p")!!) val trace = Evaluator(symbolTable).trace(entryPoint) val system = trace.getSystemValue() val assessment = Assessment(system, trace.getEntryPoint()) @@ -213,7 +214,7 @@ class E2ETest : BasePlatformTestCase() { // when/then does not throw symbolTable.getTemplate("p") - ?.let { Evaluator(symbolTable).eval(EProcessTemplateApplication(it, emptyMap())) }!! + ?.let { Evaluator(symbolTable).eval(EProcessTemplateApplication(template = it)) }!! } @Test @@ -249,7 +250,7 @@ class E2ETest : BasePlatformTestCase() { // when/then does not throw symbolTable.getTemplate("p") - ?.let { Evaluator(symbolTable).eval(EProcessTemplateApplication(it, emptyMap())) }!! + ?.let { Evaluator(symbolTable).eval(EProcessTemplateApplication(template = it)) }!! } @Test @@ -295,9 +296,8 @@ class E2ETest : BasePlatformTestCase() { val out = ProductValue( "out", kg, FromProcessRefValue( - "p", - emptyMap(), - mapOf( + name = "p", + arguments = mapOf( "a" to QuantityValue(1.0, kg), "b" to QuantityValue(1.0, kg), "c" to QuantityValue(1.0, kg), @@ -387,7 +387,7 @@ class E2ETest : BasePlatformTestCase() { // when val symbolTable = parser.load() - val entryPoint = EProcessTemplateApplication(symbolTable.getTemplate("p")!!, emptyMap()) + val entryPoint = EProcessTemplateApplication(template = symbolTable.getTemplate("p")!!) val trace = Evaluator(symbolTable).trace(entryPoint) val system = trace.getSystemValue() val assessment = Assessment(system, trace.getEntryPoint()) @@ -459,7 +459,7 @@ class E2ETest : BasePlatformTestCase() { // when val symbolTable = parser.load() - val entryPoint = EProcessTemplateApplication(symbolTable.getTemplate("p")!!, emptyMap()) + val entryPoint = EProcessTemplateApplication(template = symbolTable.getTemplate("p")!!) val trace = Evaluator(symbolTable).trace(entryPoint) val system = trace.getSystemValue() val assessment = Assessment(system, trace.getEntryPoint()) @@ -516,7 +516,7 @@ class E2ETest : BasePlatformTestCase() { // when val symbolTable = parser.load() - val entryPoint = EProcessTemplateApplication(symbolTable.getTemplate("office")!!, emptyMap()) + val entryPoint = EProcessTemplateApplication(template = symbolTable.getTemplate("office")!!) val trace = Evaluator(symbolTable).trace(entryPoint) val system = trace.getSystemValue() val assessment = Assessment(system, trace.getEntryPoint()) @@ -580,7 +580,7 @@ class E2ETest : BasePlatformTestCase() { // when val symbolTable = parser.load() - val entryPoint = EProcessTemplateApplication(symbolTable.getTemplate("office")!!, emptyMap()) + val entryPoint = EProcessTemplateApplication(template = symbolTable.getTemplate("office")!!) val trace = Evaluator(symbolTable).trace(entryPoint) val system = trace.getSystemValue() val assessment = Assessment(system, trace.getEntryPoint()) @@ -643,7 +643,7 @@ class E2ETest : BasePlatformTestCase() { // when val symbolTable = parser.load() - val entryPoint = EProcessTemplateApplication(symbolTable.getTemplate("office")!!, emptyMap()) + val entryPoint = EProcessTemplateApplication(template = symbolTable.getTemplate("office")!!) val trace = Evaluator(symbolTable).trace(entryPoint) val system = trace.getSystemValue() val assessment = Assessment(system, trace.getEntryPoint()) @@ -685,7 +685,7 @@ class E2ETest : BasePlatformTestCase() { // when val symbolTable = parser.load() - val entryPoint = EProcessTemplateApplication(symbolTable.getTemplate("p")!!, emptyMap()) + val entryPoint = EProcessTemplateApplication(template = symbolTable.getTemplate("p")!!) val trace = Evaluator(symbolTable).trace(entryPoint) val system = trace.getSystemValue() val assessment = Assessment(system, trace.getEntryPoint()) @@ -777,7 +777,7 @@ class E2ETest : BasePlatformTestCase() { // when val symbolTable = parser.load() - val entryPoint = EProcessTemplateApplication(symbolTable.getTemplate("p")!!, emptyMap()) + val entryPoint = EProcessTemplateApplication(template = symbolTable.getTemplate("p")!!) val trace = Evaluator(symbolTable).trace(entryPoint) val system = trace.getSystemValue() val assessment = Assessment(system, trace.getEntryPoint()) @@ -819,7 +819,7 @@ class E2ETest : BasePlatformTestCase() { val file = PsiManager.getInstance(project).findFile(vf) as LcaFile val parser = LcaLangAbstractParser(sequenceOf(file)) val symbolTable = parser.load() - val entryPoint = EProcessTemplateApplication(symbolTable.getTemplate("p")!!, emptyMap()) + val entryPoint = EProcessTemplateApplication(template = symbolTable.getTemplate("p")!!) val evaluator = Evaluator(symbolTable) // when + then @@ -855,7 +855,7 @@ class E2ETest : BasePlatformTestCase() { val file = PsiManager.getInstance(project).findFile(vf) as LcaFile val parser = LcaLangAbstractParser(sequenceOf(file)) val symbolTable = parser.load() - val entryPoint = EProcessTemplateApplication(symbolTable.getTemplate("p")!!, emptyMap()) + val entryPoint = EProcessTemplateApplication(template = symbolTable.getTemplate("p")!!) val evaluator = Evaluator(symbolTable) // when + then @@ -891,7 +891,7 @@ class E2ETest : BasePlatformTestCase() { val file = PsiManager.getInstance(project).findFile(vf) as LcaFile val parser = LcaLangAbstractParser(sequenceOf(file)) val symbolTable = parser.load() - val entryPoint = EProcessTemplateApplication(symbolTable.getTemplate("p")!!, emptyMap()) + val entryPoint = EProcessTemplateApplication(template = symbolTable.getTemplate("p")!!) val evaluator = Evaluator(symbolTable) // when, then does not throw @@ -926,9 +926,50 @@ class E2ETest : BasePlatformTestCase() { val file = PsiManager.getInstance(project).findFile(vf) as LcaFile val parser = LcaLangAbstractParser(sequenceOf(file)) val symbolTable = parser.load() - val entryPoint = EProcessTemplateApplication(symbolTable.getTemplate("p")!!, emptyMap()) + val entryPoint = EProcessTemplateApplication(template = symbolTable.getTemplate("p")!!) // when/then Evaluator(symbolTable).eval(entryPoint) } + + @Test + fun test_processImpact_whenImpactBlockInProcess_shouldEvaluate() { + val pkgName = {}.javaClass.enclosingMethod.name + val vf = myFixture.createFile( + "$pkgName.lca", """ + package $pkgName + + process p { + products { + 1 kg out + } + impacts { + 1 u climate_change + } + } + """.trimIndent() + ) + val file = PsiManager.getInstance(project).findFile(vf) as LcaFile + val parser = LcaLangAbstractParser(sequenceOf(file)) + + // when + val symbolTable = parser.load() + val entryPoint = EProcessTemplateApplication(template = symbolTable.getTemplate("p")!!) + val trace = Evaluator(symbolTable).trace(entryPoint) + val system = trace.getSystemValue() + val assessment = Assessment(system, trace.getEntryPoint()) + + // then + val result = assessment.inventory().impactFactors + val output = result.observablePorts.getElements().first() + val input = result.controllablePorts.get("climate_change") + val cf = result.value(output, input) + + val delta = 1E-9 + val expected = 1.0 + + assertEquals("climate_change", input.getDisplayName()) + assertEquals(expected, cf.input.quantity().amount, delta) + assertEquals(UnitValueFixture.unit, cf.input.quantity().unit) + } } diff --git a/src/test/kotlin/ch/kleis/lcaplugin/language/ide/style/LcaIndentBlockTest.kt b/src/test/kotlin/ch/kleis/lcaplugin/language/ide/style/LcaIndentBlockTest.kt index 0d9d86582..9c7e6a770 100644 --- a/src/test/kotlin/ch/kleis/lcaplugin/language/ide/style/LcaIndentBlockTest.kt +++ b/src/test/kotlin/ch/kleis/lcaplugin/language/ide/style/LcaIndentBlockTest.kt @@ -2,7 +2,10 @@ package ch.kleis.lcaplugin.language.ide.style import com.intellij.psi.formatter.FormatterTestCase import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 +@RunWith(JUnit4::class) class LcaIndentBlockTest : FormatterTestCase() { @Test fun test_formattingLabels() { @@ -545,6 +548,25 @@ labels { ) } + @Test + fun test_impactBlockInProcess_shouldIndent() { + doTextTest(""" + process p1 { + impacts { + 1 u climate_change + } + } + """.trimIndent(), + """ + process p1 { + impacts { + 1 u climate_change + } + } + """.trimIndent() + ) + } + override fun getTestDataPath(): String { return "" } diff --git a/src/test/kotlin/ch/kleis/lcaplugin/language/parser/LcaLangAbstractParserTest.kt b/src/test/kotlin/ch/kleis/lcaplugin/language/parser/LcaLangAbstractParserTest.kt index 81230ed85..a9c3bb353 100644 --- a/src/test/kotlin/ch/kleis/lcaplugin/language/parser/LcaLangAbstractParserTest.kt +++ b/src/test/kotlin/ch/kleis/lcaplugin/language/parser/LcaLangAbstractParserTest.kt @@ -500,11 +500,8 @@ class LcaLangAbstractParserTest : ParsingTestCase("", "lca", LcaParserDefinition data = Prelude.units ) val expected = EProcessTemplate( - params = emptyMap(), - locals = emptyMap(), - EProcess( + body = EProcess( name = "a", - labels = emptyMap(), products = listOf( ETechnoExchange( EQuantityScale(1.0, EDataRef("kg")), @@ -525,7 +522,6 @@ class LcaLangAbstractParserTest : ParsingTestCase("", "lca", LcaParserDefinition EProductSpec("water"), ), ), - biosphere = emptyList(), ) ) assertEquals(expected, actual) @@ -981,6 +977,38 @@ class LcaLangAbstractParserTest : ParsingTestCase("", "lca", LcaParserDefinition assertEquals(expected, actual) } + @Test + fun testParse_processWithImpacts_shouldParse() { + val pkgName = {}.javaClass.enclosingMethod.name + val file = parseFile( + "$pkgName.lca", """ + package $pkgName + + process a { + products { + 1 kg x + } + impacts { + 1 u cc + } + } + """.trimIndent() + ) as LcaFile + val parser = LcaLangAbstractParser( + sequenceOf(file) + ) + val symbolTable = parser.load() + + // when + val template = symbolTable.getTemplate("a") as ProcessTemplateExpression + val actual = + (ProcessTemplateExpression.eProcessTemplate.body.impacts compose + Every.list() compose EImpact.indicator).firstOrNull(template) + + // then + assertEquals("cc", actual?.name) + } + override fun getTestDataPath(): String { return "" }