From a722e614263cbeb8b47935769b19ed0a98e9d096 Mon Sep 17 00:00:00 2001 From: Joachim Desroches Date: Fri, 27 Oct 2023 09:39:42 +0200 Subject: [PATCH] Remove only exchange comments in imports. --- .../imports/ecospold/EcoSpoldImporter.kt | 1 + .../ecospold/EcoSpoldProcessRenderer.kt | 2 + .../plugin/imports/model/ImportedProcess.kt | 1 + .../plugin/imports/model/ImportedUnit.kt | 1 + .../shared/serializer/FormulaConverter.kt | 5 ++- .../shared/serializer/SubstanceSerializer.kt | 10 ++++- .../shared/serializer/UnitSerializer.kt | 6 ++- .../imports/simapro/SimaproProcessMapper.kt | 39 +++++++++++++++---- .../ecospold/EcospoldProcessRendererTest.kt | 3 +- .../shared/serializer/FormulaConverterTest.kt | 14 +++++-- .../serializer/SubstanceSerializerTest.kt | 1 + .../simapro/SimaproProcessMapperTest.kt | 12 ++++++ 12 files changed, 79 insertions(+), 16 deletions(-) diff --git a/plugin/src/main/kotlin/ch/kleis/lcaac/plugin/imports/ecospold/EcoSpoldImporter.kt b/plugin/src/main/kotlin/ch/kleis/lcaac/plugin/imports/ecospold/EcoSpoldImporter.kt index 9a0abd468..65af4e159 100644 --- a/plugin/src/main/kotlin/ch/kleis/lcaac/plugin/imports/ecospold/EcoSpoldImporter.kt +++ b/plugin/src/main/kotlin/ch/kleis/lcaac/plugin/imports/ecospold/EcoSpoldImporter.kt @@ -192,6 +192,7 @@ class EcoSpoldImporter( LOG.info("Read dataset from ${it.first}") processRenderer.render( activityDataset = it.second, + processComment = "from ${it.first}", ) } } diff --git a/plugin/src/main/kotlin/ch/kleis/lcaac/plugin/imports/ecospold/EcoSpoldProcessRenderer.kt b/plugin/src/main/kotlin/ch/kleis/lcaac/plugin/imports/ecospold/EcoSpoldProcessRenderer.kt index ea6faa27d..606aa6b00 100644 --- a/plugin/src/main/kotlin/ch/kleis/lcaac/plugin/imports/ecospold/EcoSpoldProcessRenderer.kt +++ b/plugin/src/main/kotlin/ch/kleis/lcaac/plugin/imports/ecospold/EcoSpoldProcessRenderer.kt @@ -21,12 +21,14 @@ class EcoSpoldProcessRenderer( fun render( activityDataset: ActivityDataset, + processComment: String, ) { nbProcesses++ val category = category(activityDataset) val subFolder = if (category == null) "" else "${category}${File.separatorChar}" val process = processMapper.map(activityDataset) + process.comments.add(processComment) val strProcess = ProcessSerializer.serialize(process) writer.writeRotateFile("processes${File.separatorChar}$subFolder${process.uid}", strProcess) diff --git a/plugin/src/main/kotlin/ch/kleis/lcaac/plugin/imports/model/ImportedProcess.kt b/plugin/src/main/kotlin/ch/kleis/lcaac/plugin/imports/model/ImportedProcess.kt index 83c1acd82..bd5704bf3 100644 --- a/plugin/src/main/kotlin/ch/kleis/lcaac/plugin/imports/model/ImportedProcess.kt +++ b/plugin/src/main/kotlin/ch/kleis/lcaac/plugin/imports/model/ImportedProcess.kt @@ -12,6 +12,7 @@ data class ImportedProcess( val impactBlocks: List> = emptyList(), ) { var params: MutableList = mutableListOf() + var comments: MutableList = mutableListOf() } data class ImportedParam(val symbol: String, val value: String) diff --git a/plugin/src/main/kotlin/ch/kleis/lcaac/plugin/imports/model/ImportedUnit.kt b/plugin/src/main/kotlin/ch/kleis/lcaac/plugin/imports/model/ImportedUnit.kt index ff332ab82..e2717f8df 100644 --- a/plugin/src/main/kotlin/ch/kleis/lcaac/plugin/imports/model/ImportedUnit.kt +++ b/plugin/src/main/kotlin/ch/kleis/lcaac/plugin/imports/model/ImportedUnit.kt @@ -8,6 +8,7 @@ class ImportedUnit( val dimension: String, val symbol: String, aliasFor: ImportedUnitAliasFor? = null, + val comment: String? = null ) { val aliasFor = if (aliasFor?.baseUnitExpressionStr == symbol) null else aliasFor diff --git a/plugin/src/main/kotlin/ch/kleis/lcaac/plugin/imports/shared/serializer/FormulaConverter.kt b/plugin/src/main/kotlin/ch/kleis/lcaac/plugin/imports/shared/serializer/FormulaConverter.kt index 5d49c870d..ae1bacfff 100644 --- a/plugin/src/main/kotlin/ch/kleis/lcaac/plugin/imports/shared/serializer/FormulaConverter.kt +++ b/plugin/src/main/kotlin/ch/kleis/lcaac/plugin/imports/shared/serializer/FormulaConverter.kt @@ -37,7 +37,7 @@ class FormulaConverter { } - fun compute(amountFormula: String): String { + fun compute(amountFormula: String, comments: MutableList): String { val computed = try { if (onlyScientific.matches(amountFormula)) { @@ -53,6 +53,9 @@ class FormulaConverter { val withoutSpacesAroundPowerTen = withoutUnitInExponent.replace(power10WithSpaceCapture, "$1E$2") "( $withoutSpacesAroundPowerTen ) * 1" } + if (computed != amountFormula) { + comments.add("Formula=[$amountFormula]") + } return computed } } diff --git a/plugin/src/main/kotlin/ch/kleis/lcaac/plugin/imports/shared/serializer/SubstanceSerializer.kt b/plugin/src/main/kotlin/ch/kleis/lcaac/plugin/imports/shared/serializer/SubstanceSerializer.kt index 6b8540c9c..1efe4e955 100644 --- a/plugin/src/main/kotlin/ch/kleis/lcaac/plugin/imports/shared/serializer/SubstanceSerializer.kt +++ b/plugin/src/main/kotlin/ch/kleis/lcaac/plugin/imports/shared/serializer/SubstanceSerializer.kt @@ -3,6 +3,7 @@ package ch.kleis.lcaac.plugin.imports.shared.serializer import ch.kleis.lcaac.core.prelude.Prelude import ch.kleis.lcaac.plugin.imports.model.ImportedImpact import ch.kleis.lcaac.plugin.imports.model.ImportedSubstance +import ch.kleis.lcaac.plugin.imports.util.StringUtils.asComment import ch.kleis.lcaac.plugin.imports.util.StringUtils.formatMetaValues import ch.kleis.lcaac.plugin.imports.util.sanitizeSymbol @@ -55,8 +56,15 @@ class SubstanceSerializer { return builder } - private fun serializeImportedImpact(ii: ImportedImpact, builder: StringBuilder = StringBuilder()): CharSequence { + private fun serializeImportedImpact( + ii: ImportedImpact, + builder: StringBuilder = StringBuilder() + ): CharSequence { val name = sanitizeSymbol(Prelude.sanitize(ii.name)) + ii.comment?.apply { + builder.append(asComment(ii.comment)) + builder.appendLine() + } builder.append("${ii.value} ${ii.unitSymbol} $name") return builder } diff --git a/plugin/src/main/kotlin/ch/kleis/lcaac/plugin/imports/shared/serializer/UnitSerializer.kt b/plugin/src/main/kotlin/ch/kleis/lcaac/plugin/imports/shared/serializer/UnitSerializer.kt index 4c9ec30cc..80f438edf 100644 --- a/plugin/src/main/kotlin/ch/kleis/lcaac/plugin/imports/shared/serializer/UnitSerializer.kt +++ b/plugin/src/main/kotlin/ch/kleis/lcaac/plugin/imports/shared/serializer/UnitSerializer.kt @@ -9,9 +9,10 @@ class UnitSerializer { } private fun serializeAliasFor(unit: ImportedUnit): CharSequence { + val sanitizedComment = unit.comment?.let { " // $it" } ?: "" val aliasFor = unit.aliasFor ?: throw ImportException("$unit cannot be serialized with an alias") return """ - unit ${unit.ref()} { + unit ${unit.ref()} {$sanitizedComment symbol = "${unit.symbol}" alias_for = ${aliasFor.scale} ${aliasFor.baseUnitExpressionStr} } @@ -20,8 +21,9 @@ class UnitSerializer { } private fun serializeLiteral(unit: ImportedUnit): CharSequence { + val sanitizedComment = unit.comment?.let { " // $it" } ?: "" return """ - unit ${unit.ref()} { + unit ${unit.ref()} {$sanitizedComment symbol = "${unit.symbol}" dimension = "${unit.dimension}" } diff --git a/plugin/src/main/kotlin/ch/kleis/lcaac/plugin/imports/simapro/SimaproProcessMapper.kt b/plugin/src/main/kotlin/ch/kleis/lcaac/plugin/imports/simapro/SimaproProcessMapper.kt index 1e1c8fd06..2e138cb3f 100644 --- a/plugin/src/main/kotlin/ch/kleis/lcaac/plugin/imports/simapro/SimaproProcessMapper.kt +++ b/plugin/src/main/kotlin/ch/kleis/lcaac/plugin/imports/simapro/SimaproProcessMapper.kt @@ -76,12 +76,12 @@ class SimaproProcessMapper(mode: SubstanceImportMode) { ) - // Resources & Landuse + // Resources & LandUse val allRes = process.resources().groupBy { if (it.subCompartment() == "land") "land_use" else "resources" } - val resources = mapResourcesOrLanduse(allRes["resources"]?.asSequence(), "Resource") - val landUse = mapResourcesOrLanduse(allRes["land_use"]?.asSequence(), "Land_use") + val resources = mapResourcesOrLandUse(allRes["resources"]?.asSequence(), "Resource") + val landUse = mapResourcesOrLandUse(allRes["land_use"]?.asSequence(), "Land_use") - return ImportedProcess( + val result = ImportedProcess( uid = process.uid(), meta = mapMetas(process), productBlocks = listOfNotNull(baseProducts, wasteTreatment, wasteScenario, avoidedProducts), @@ -102,6 +102,12 @@ class SimaproProcessMapper(mode: SubstanceImportMode) { resourceBlocks = listOf(resources), landUseBlocks = listOf(landUse), ) + + // Unsupported + result.comments.addAll(process.remainingWaste().map { "// QQQ Unsupported ${it.wasteTreatment()}" }) + result.comments.addAll(process.separatedWaste().map { "// QQQ Unsupported ${it.wasteTreatment()}" }) + + return result } private fun mapMetas(process: ProcessBlock) = mapOf( @@ -140,7 +146,7 @@ class SimaproProcessMapper(mode: SubstanceImportMode) { elementaryExchangeRows?.map { renderElementary(it, "Emission", comp) } ?: emptySequence() ) - private fun mapResourcesOrLanduse( + private fun mapResourcesOrLandUse( elementaryExchangeRows: Sequence?, type: String ): ExchangeBlock = @@ -161,9 +167,10 @@ class SimaproProcessMapper(mode: SubstanceImportMode) { val initComments = ArrayList() product.name()?.let { initComments.add("name: $it") } product.category()?.let { initComments.add("category: $it") } + val comments = createComments(product.comment(), initComments) val unit = sanitizeSymbol(product.unit()) val allocation = product.allocation().value() - val amount = FormulaConverter.compute(product.amount().toString()) + val amount = FormulaConverter.compute(product.amount().toString(), comments) return ImportedProductExchange( name = product.uid(), qty = amount, @@ -184,10 +191,12 @@ class SimaproProcessMapper(mode: SubstanceImportMode) { private fun render( exchange: ExchangeRow, suffix: String = "", + additionalComments: List = listOf() ): ImportedProductExchange { + val comments = createComments(exchange.comment(), additionalComments) val unit = sanitizeSymbol(exchange.unit()) val name = Prelude.sanitize(exchange.name()) + suffix - val amount = FormulaConverter.compute(exchange.amount().toString()) + val amount = FormulaConverter.compute(exchange.amount().toString(), comments) return ImportedProductExchange( name = name, qty = amount, @@ -195,16 +204,30 @@ class SimaproProcessMapper(mode: SubstanceImportMode) { ) } + private fun createComments(text: String, existingComments: List = listOf()): MutableList { + val comments = existingComments.toMutableList() + val additionalComments = text + .split("\n") + .filter { it.isNotBlank() } + comments.addAll(additionalComments) + return comments + } + private fun renderElementary( exchange: ElementaryExchangeRow, type: String, compartment: String ): ImportedBioExchange { - val amount = FormulaConverter.compute(exchange.amount().toString()) + val comments = createComments(exchange.comment()) + val amount = FormulaConverter.compute(exchange.amount().toString(), comments) val unit = sanitizeSymbol(exchange.unit()) val sub = exchange.subCompartment().ifBlank { null } val name = exchange.name() val realKey = substanceDict.realKeyForSubstance(name, type, unit, compartment, sub) + if (realKey.hasChanged) { + comments.add("Fallback for [$name, $type, $compartment, ${sub}]") + } + val uid = realKey.uid() return ImportedBioExchange(amount, unit, uid, compartment, sub) } diff --git a/plugin/src/test/kotlin/ch/kleis/lcaac/plugin/imports/ecospold/EcospoldProcessRendererTest.kt b/plugin/src/test/kotlin/ch/kleis/lcaac/plugin/imports/ecospold/EcospoldProcessRendererTest.kt index 7df81a83a..f232f9305 100644 --- a/plugin/src/test/kotlin/ch/kleis/lcaac/plugin/imports/ecospold/EcospoldProcessRendererTest.kt +++ b/plugin/src/test/kotlin/ch/kleis/lcaac/plugin/imports/ecospold/EcospoldProcessRendererTest.kt @@ -21,6 +21,7 @@ class EcospoldProcessRendererTest { val blockSlot = slot() every { writer.writeRotateFile(any(), capture(blockSlot)) } returns Unit + val processComment = "" val methodName = "EF v3.1" val unitManager = UnitManager() @@ -29,7 +30,7 @@ class EcospoldProcessRendererTest { val renderer = EcoSpoldProcessRenderer(unitManager, dict, writer, methodName) // when - renderer.render(data) + renderer.render(data, processComment) // then verify { diff --git a/plugin/src/test/kotlin/ch/kleis/lcaac/plugin/imports/shared/serializer/FormulaConverterTest.kt b/plugin/src/test/kotlin/ch/kleis/lcaac/plugin/imports/shared/serializer/FormulaConverterTest.kt index a36d012c2..b194145e6 100644 --- a/plugin/src/test/kotlin/ch/kleis/lcaac/plugin/imports/shared/serializer/FormulaConverterTest.kt +++ b/plugin/src/test/kotlin/ch/kleis/lcaac/plugin/imports/shared/serializer/FormulaConverterTest.kt @@ -21,9 +21,11 @@ class FormulaConverterTest { data.forEach { (param, expected) -> // When - val result = FormulaConverter.compute(param) + val comments = mutableListOf() + val result = FormulaConverter.compute(param, comments) // Then assertEquals(expected, result) + assertEquals(0, comments.size) } } @@ -38,9 +40,12 @@ class FormulaConverterTest { ) data.forEach { (param, expected) -> // When - val result = FormulaConverter.compute(param) + val comments = mutableListOf() + val result = FormulaConverter.compute(param, comments) // Then assertEquals(expected, result) + assertEquals(1, comments.size) + assertEquals("Formula=[$param]", comments[0]) } } @@ -56,9 +61,12 @@ class FormulaConverterTest { ) data.forEach { (param, expected) -> // When - val result = FormulaConverter.compute(param) + val comments = mutableListOf() + val result = FormulaConverter.compute(param, comments) // Then assertEquals(expected, result) + assertEquals(1, comments.size) + assertEquals("Formula=[$param]", comments[0]) } } diff --git a/plugin/src/test/kotlin/ch/kleis/lcaac/plugin/imports/shared/serializer/SubstanceSerializerTest.kt b/plugin/src/test/kotlin/ch/kleis/lcaac/plugin/imports/shared/serializer/SubstanceSerializerTest.kt index c2ff4261e..09f48a876 100644 --- a/plugin/src/test/kotlin/ch/kleis/lcaac/plugin/imports/shared/serializer/SubstanceSerializerTest.kt +++ b/plugin/src/test/kotlin/ch/kleis/lcaac/plugin/imports/shared/serializer/SubstanceSerializerTest.kt @@ -84,6 +84,7 @@ class SubstanceSerializerTest { // Then val expected = """ impacts { + | // Comment | 1000.0 P_Eq alu_tox | } """.trimMargin() diff --git a/plugin/src/test/kotlin/ch/kleis/lcaac/plugin/imports/simapro/SimaproProcessMapperTest.kt b/plugin/src/test/kotlin/ch/kleis/lcaac/plugin/imports/simapro/SimaproProcessMapperTest.kt index 17c72c210..3476efa78 100644 --- a/plugin/src/test/kotlin/ch/kleis/lcaac/plugin/imports/simapro/SimaproProcessMapperTest.kt +++ b/plugin/src/test/kotlin/ch/kleis/lcaac/plugin/imports/simapro/SimaproProcessMapperTest.kt @@ -96,7 +96,9 @@ class SimaproProcessMapperTest { // Then assertEquals(3, actual.inputBlocks.count()) assertEquals(0, actual.inputBlocks[0].exchanges.count()) + assertEquals(0, actual.inputBlocks[1].exchanges.count()) + assertEquals(0, actual.inputBlocks[2].exchanges.count()) } @@ -206,9 +208,11 @@ System boundaries: Cradle-to-gate .allocationRules("allocationRules") sample.literatures().add( LiteratureRow().name("Methodological Guidelines for the Life Cycle Inventory of Agricultural Products.") + .comment("comment") ) sample.literatures().add( LiteratureRow().name("Another Methodological.") + .comment("comment 2") ) sample.resources().add( ElementaryExchangeRow() @@ -217,6 +221,7 @@ System boundaries: Cradle-to-gate .unit("kg") .amount(Numeric.of("145.56 * 67E6 / (1 -4E-6)")) .uncertainty(UncertaintyRecord.logNormal(1.0744)) + .comment("(2,2,1,1,1,na)\n") ) sample.resources().add( ElementaryExchangeRow() @@ -225,6 +230,7 @@ System boundaries: Cradle-to-gate .unit("MJ") .amount(Numeric.of(20295.524449877732)) .uncertainty(UncertaintyRecord.logNormal(1.0744)) + .comment("(2,2,1,1,1,na)\n") ) sample.resources().add( ElementaryExchangeRow() @@ -233,6 +239,7 @@ System boundaries: Cradle-to-gate .unit("m3") .amount(Numeric.of(501.95555914578216)) .uncertainty(UncertaintyRecord.logNormal(1.0744)) + .comment("(2,1,1,1,1,na)\n") ) sample.resources().add( ElementaryExchangeRow() @@ -241,6 +248,7 @@ System boundaries: Cradle-to-gate .unit("m2a") .amount(Numeric.of(10000.0)) .uncertainty(UncertaintyRecord.logNormal(1.1130)) + .comment("(2,1,1,1,1,na)\u007F") ) sample.products().add( @@ -250,6 +258,10 @@ System boundaries: Cradle-to-gate .allocation(Numeric.of("100")) .wasteType("not defined") .category("_WFLDB 3.7 (Phase 2)\\Plant products\\Perennials\\Acai berry") + .comment( + """The yield when productive is 9750 kg/ha-y . +The final yield corresponds to the average yield over the entire lifetime of the tree.""" + ) ) sample.platformId("platformId") return sample