Skip to content

Commit

Permalink
Merge pull request #394 from kleis-technology/393-fix-stdlib
Browse files Browse the repository at this point in the history
Fix empty cell parsing in stdlib generation for EF v3.1
  • Loading branch information
jedesroches authored Oct 30, 2023
2 parents 26bc6e0 + f3473e2 commit 3b16b06
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 10 deletions.
15 changes: 12 additions & 3 deletions buildSrc/src/main/kotlin/task/EFRecord.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ sealed class EFRecord(val record: CSVRecord) {

fun isSubstance(): Boolean = record.isMapped("FLOW_propertyUnit")

abstract fun characterizationFactor(): String
abstract fun characterizationFactor(): Double
fun unit(): String {
return when (val raw = if (this.isSubstance()) sanitizeString(record["FLOW_propertyUnit"].trim()) else "u") {
"Item(s)" -> "piece"
Expand Down Expand Up @@ -50,12 +50,21 @@ sealed class EFRecord(val record: CSVRecord) {
// FIXME - is this correct ?
fun subCompartment(): String =
record["FLOW_class2"]

protected fun getCF(cell: String): Double {
val csvValue = record[cell]
return when {
csvValue == null || csvValue.isEmpty() -> 0.0
csvValue.toDoubleOrNull() == null -> throw Exception("Invalid CF value: $csvValue")
else -> csvValue.toDouble()
}
}
}

class EF31Record(record: CSVRecord) : EFRecord(record) {
override fun characterizationFactor(): String = record["CF EF3.1"]
override fun characterizationFactor(): Double = getCF("CF EF3.1")
}

class EF30Record(record: CSVRecord) : EFRecord(record) {
override fun characterizationFactor(): String = record["LCIAMethod_meanvalue"]
override fun characterizationFactor(): Double = getCF("LCIAMethod_meanvalue")
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ abstract class GenerateEmissionFactorsTask30 : DefaultTask() {
@Suppress("UNUSED_PARAMETER")
fun execute(inputChanges: InputChanges) {
GenerateEmissionFactorsTask<EF30Record>(inputDir, outputDir)
.createLibArchive("30", "3.0", { i -> EF30Record(i) })
.createLibArchive("30", "3.0") { i -> EF30Record(i) }
}


Expand Down
78 changes: 75 additions & 3 deletions buildSrc/src/test/kotlin/task/GenerateEmissionFactorsTaskTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package task

import io.mockk.every
import io.mockk.mockk
import org.apache.commons.csv.CSVRecord
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.RegularFile
import org.gradle.api.internal.provider.DefaultProvider
Expand All @@ -12,6 +13,77 @@ import java.util.concurrent.Callable

class GenerateEmissionFactorsTaskTest {

@Test
fun substance_31_parser_should_succeed_on_double() {
// given
val record = mockk<CSVRecord>()
val expected = 3.7e-5
every { record["CF EF3.1"] } returns expected.toString()
val sut = EF31Record(record)

// {w,t}hen
Assertions.assertEquals(expected, sut.characterizationFactor())
}

@Test
fun substance_30_parser_should_succeed_on_double() {
// given
val record = mockk<CSVRecord>()
val expected = 13.7e-5
every { record["LCIAMethod_meanvalue"] } returns expected.toString()
val sut = EF30Record(record)

// {w,t}hen
Assertions.assertEquals(expected, sut.characterizationFactor())
}

@Test
fun substance_31_parser_should_zero_for_empty() {
// given
val record = mockk<CSVRecord>()
val expected = 0.0
every { record["CF EF3.1"] } returns ""
val sut = EF31Record(record)

// {w,t}hen
Assertions.assertEquals(expected, sut.characterizationFactor())
}

@Test
fun substance_30_parser_should_zero_for_empty() {
// given
val record = mockk<CSVRecord>()
val expected = 0.0
every { record["LCIAMethod_meanvalue"] } returns expected.toString()
val sut = EF30Record(record)

// {w,t}hen
Assertions.assertEquals(expected, sut.characterizationFactor())
}

@Test
fun substance_31_parser_should_fail_when_not_double() {
// given
val record = mockk<CSVRecord>()
every { record["CF EF3.1"] } returns "hello world"
val sut = EF31Record(record)

// {w,t}hen
val e = Assertions.assertThrows(Exception::class.java) { sut.characterizationFactor() }
Assertions.assertEquals("Invalid CF value: hello world", e.message)
}

@Test
fun substance_30_parser_should_fail_when_not_double() {
// given
val record = mockk<CSVRecord>()
every { record["LCIAMethod_meanvalue"] } returns "hello world"
val sut = EF30Record(record)

// {w,t}hen
val e = Assertions.assertThrows(Exception::class.java) { sut.characterizationFactor() }
Assertions.assertEquals("Invalid CF value: hello world", e.message)
}
@Test
fun substance31() {
// given
Expand All @@ -38,8 +110,8 @@ class GenerateEmissionFactorsTaskTest {
reference_unit = kg
impacts {
2.0522E-08 CTUh human_toxicity_non_carcinogenic
2.0522E-08 CTUh human_toxicity_non_carcinogenic_organics
2.0522E-8 CTUh human_toxicity_non_carcinogenic
2.0522E-8 CTUh human_toxicity_non_carcinogenic_organics
}
meta {
Expand All @@ -59,7 +131,7 @@ class GenerateEmissionFactorsTaskTest {
reference_unit = kg
impacts {
4.02E-08 CTUh human_toxicity_non_carcinogenic_organics
4.02E-8 CTUh human_toxicity_non_carcinogenic_organics
}
meta {
Expand Down
4 changes: 2 additions & 2 deletions plugin/.run/Build Plugin.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</option>
<option name="taskNames">
<list>
<option value="plugin:plugin:buildPlugin" />
<option value="plugin:buildPlugin" />
</list>
</option>
<option name="vmOptions" value="" />
Expand All @@ -22,4 +22,4 @@
<RunAsTest>false</RunAsTest>
<method v="2" />
</configuration>
</component>
</component>
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class SimaproImportSettings : PersistentStateComponent<SimaproImportSettings.Sta

var importUnits: Boolean
get() = true
set(value) {
set(_) {
// do nothing
}

Expand Down

0 comments on commit 3b16b06

Please sign in to comment.