Skip to content

Commit

Permalink
Remove only exchange comments in imports.
Browse files Browse the repository at this point in the history
  • Loading branch information
jedesroches committed Oct 27, 2023
1 parent 1f0b230 commit a722e61
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ class EcoSpoldImporter(
LOG.info("Read dataset from ${it.first}")
processRenderer.render(
activityDataset = it.second,
processComment = "from ${it.first}",
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ data class ImportedProcess(
val impactBlocks: List<ExchangeBlock<ImportedImpactExchange>> = emptyList(),
) {
var params: MutableList<ImportedParam> = mutableListOf()
var comments: MutableList<String> = mutableListOf()
}

data class ImportedParam(val symbol: String, val value: String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class FormulaConverter {
}


fun compute(amountFormula: String): String {
fun compute(amountFormula: String, comments: MutableList<String>): String {

val computed = try {
if (onlyScientific.matches(amountFormula)) {
Expand All @@ -53,6 +53,9 @@ class FormulaConverter {
val withoutSpacesAroundPowerTen = withoutUnitInExponent.replace(power10WithSpaceCapture, "$1E$2")
"( $withoutSpacesAroundPowerTen ) * 1"
}
if (computed != amountFormula) {
comments.add("Formula=[$amountFormula]")
}
return computed
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
Expand All @@ -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}"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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(
Expand Down Expand Up @@ -140,7 +146,7 @@ class SimaproProcessMapper(mode: SubstanceImportMode) {
elementaryExchangeRows?.map { renderElementary(it, "Emission", comp) } ?: emptySequence()
)

private fun mapResourcesOrLanduse(
private fun mapResourcesOrLandUse(
elementaryExchangeRows: Sequence<ElementaryExchangeRow>?,
type: String
): ExchangeBlock<ImportedBioExchange> =
Expand All @@ -161,9 +167,10 @@ class SimaproProcessMapper(mode: SubstanceImportMode) {
val initComments = ArrayList<String>()
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,
Expand All @@ -184,27 +191,43 @@ class SimaproProcessMapper(mode: SubstanceImportMode) {
private fun render(
exchange: ExchangeRow,
suffix: String = "",
additionalComments: List<String> = 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,
unit = unit,
)
}

private fun createComments(text: String, existingComments: List<String> = listOf()): MutableList<String> {
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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class EcospoldProcessRendererTest {
val blockSlot = slot<CharSequence>()
every { writer.writeRotateFile(any(), capture(blockSlot)) } returns Unit

val processComment = ""
val methodName = "EF v3.1"

val unitManager = UnitManager()
Expand All @@ -29,7 +30,7 @@ class EcospoldProcessRendererTest {
val renderer = EcoSpoldProcessRenderer(unitManager, dict, writer, methodName)

// when
renderer.render(data)
renderer.render(data, processComment)

// then
verify {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ class FormulaConverterTest {

data.forEach { (param, expected) ->
// When
val result = FormulaConverter.compute(param)
val comments = mutableListOf<String>()
val result = FormulaConverter.compute(param, comments)
// Then
assertEquals(expected, result)
assertEquals(0, comments.size)
}
}

Expand All @@ -38,9 +40,12 @@ class FormulaConverterTest {
)
data.forEach { (param, expected) ->
// When
val result = FormulaConverter.compute(param)
val comments = mutableListOf<String>()
val result = FormulaConverter.compute(param, comments)
// Then
assertEquals(expected, result)
assertEquals(1, comments.size)
assertEquals("Formula=[$param]", comments[0])
}
}

Expand All @@ -56,9 +61,12 @@ class FormulaConverterTest {
)
data.forEach { (param, expected) ->
// When
val result = FormulaConverter.compute(param)
val comments = mutableListOf<String>()
val result = FormulaConverter.compute(param, comments)
// Then
assertEquals(expected, result)
assertEquals(1, comments.size)
assertEquals("Formula=[$param]", comments[0])
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class SubstanceSerializerTest {

// Then
val expected = """ impacts {
| // Comment
| 1000.0 P_Eq alu_tox
| }
""".trimMargin()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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(
Expand All @@ -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
Expand Down

0 comments on commit a722e61

Please sign in to comment.