diff --git a/src/main/kotlin/ch/kleis/lcaplugin/actions/AssessProcessAction.kt b/src/main/kotlin/ch/kleis/lcaplugin/actions/AssessProcessAction.kt index 84686ffb9..dd6e9141a 100644 --- a/src/main/kotlin/ch/kleis/lcaplugin/actions/AssessProcessAction.kt +++ b/src/main/kotlin/ch/kleis/lcaplugin/actions/AssessProcessAction.kt @@ -42,7 +42,7 @@ class AssessProcessAction( override fun run(indicator: ProgressIndicator) { val trace = traceSystemWithIndicator(indicator, file, processName, matchLabels) - val order = trace.getProductOrder() + val order = trace.getObservableOrder() val inventory = Assessment(trace.getSystemValue(), trace.getEntryPoint()).inventory() this.data = Pair(inventory, order) } diff --git a/src/main/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/EvaluationTrace.kt b/src/main/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/EvaluationTrace.kt index 6c8ee16cd..abc554132 100644 --- a/src/main/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/EvaluationTrace.kt +++ b/src/main/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/EvaluationTrace.kt @@ -7,7 +7,7 @@ class EvaluationTrace { private var currentStage = HashSet() private val processes = HashSet() private val substanceCharacterizations = HashSet() - private val productDepthMap = HashMap() + private val observableDepthMap = HashMap() companion object { fun empty(): EvaluationTrace { @@ -15,11 +15,11 @@ class EvaluationTrace { } } - fun getProductOrder(): Comparator { + fun getObservableOrder(): Comparator { return object : Comparator { override fun compare(o1: MatrixColumnIndex, o2: MatrixColumnIndex): Int { - val d1 = productDepthMap[o1] ?: throw EvaluatorException("unknown ${o1}") - val d2 = productDepthMap[o2] ?: throw EvaluatorException("unknown ${o2}") + val d1 = observableDepthMap[o1] ?: throw EvaluatorException("unknown ${o1}") + val d2 = observableDepthMap[o2] ?: throw EvaluatorException("unknown ${o2}") if (d1 < d2) { return -1 } @@ -94,13 +94,13 @@ class EvaluationTrace { .filterIsInstance() .forEach { process -> process.products.forEach { exchange -> - productDepthMap[exchange.product] = currentDepth + observableDepthMap[exchange.product] = currentDepth } } currentStage .filterIsInstance() .forEach { sc -> - productDepthMap[sc.referenceExchange.substance] = currentDepth + observableDepthMap[sc.referenceExchange.substance] = currentDepth } stages.add(currentStage) diff --git a/src/main/kotlin/ch/kleis/lcaplugin/imports/ecospold/EcoSpold2ProcessMapper.kt b/src/main/kotlin/ch/kleis/lcaplugin/imports/ecospold/EcoSpold2ProcessMapper.kt index 5bb684ccd..ea354117a 100644 --- a/src/main/kotlin/ch/kleis/lcaplugin/imports/ecospold/EcoSpold2ProcessMapper.kt +++ b/src/main/kotlin/ch/kleis/lcaplugin/imports/ecospold/EcoSpold2ProcessMapper.kt @@ -1,11 +1,14 @@ package ch.kleis.lcaplugin.imports.ecospold +import ch.kleis.lcaplugin.core.lang.expression.SubstanceType import ch.kleis.lcaplugin.imports.ModelWriter import ch.kleis.lcaplugin.imports.ecospold.lcia.EcospoldImporter.Companion.unitToStr import ch.kleis.lcaplugin.imports.ecospold.model.ActivityDataset +import ch.kleis.lcaplugin.imports.ecospold.model.ElementaryExchange import ch.kleis.lcaplugin.imports.ecospold.model.IntermediateExchange import ch.kleis.lcaplugin.imports.ecospold.model.Uncertainty import ch.kleis.lcaplugin.imports.model.ExchangeBlock +import ch.kleis.lcaplugin.imports.model.ImportedBioExchange import ch.kleis.lcaplugin.imports.model.ImportedProcess import ch.kleis.lcaplugin.imports.model.ImportedProductExchange import ch.kleis.lcaplugin.imports.simapro.sanitizeSymbol @@ -36,7 +39,7 @@ open class EcoSpold2ProcessMapper(val process: ActivityDataset) { mapInputs() mapLandUse() mapResources() - mapEmission() + mapEmissions() return result } @@ -48,11 +51,46 @@ open class EcoSpold2ProcessMapper(val process: ActivityDataset) { result.productBlocks = mutableListOf(ExchangeBlock("Products", products)) } - open fun mapEmission() {} - open fun mapInputs() {} - open fun mapLandUse() {} - open fun mapResources() {} + private fun mapEmissions() { + result.emissionBlocks += ExchangeBlock("FIXME", + process.flowData.elementaryExchanges + .filter { it.substanceType == SubstanceType.EMISSION } + .map(::elementaryExchangeToImportedBioExchange).toMutableList() + ) + + // TODO: Remove when closing #261 + val bio = ImportedBioExchange(listOf(), "1.0", "u", pUid, "") + result.emissionBlocks += ExchangeBlock("Virtual Substance for Impact Factors", mutableListOf(bio)) + + } + + private fun mapLandUse() { + result.landUseBlocks = mutableListOf(ExchangeBlock("FIXME", + process.flowData.elementaryExchanges + .filter { it.substanceType == SubstanceType.LAND_USE } + .map(::elementaryExchangeToImportedBioExchange).toMutableList() + )) + } + + private fun mapResources() { + result.resourceBlocks = mutableListOf(ExchangeBlock("FIXME", + process.flowData.elementaryExchanges + .filter { it.substanceType == SubstanceType.RESOURCE } + .map(::elementaryExchangeToImportedBioExchange).toMutableList() + )) + } + private fun elementaryExchangeToImportedBioExchange(elementaryExchange: ElementaryExchange): ImportedBioExchange = + ImportedBioExchange( + comments = elementaryExchange.comment?.let { listOf(it) } ?: listOf(), + qty = elementaryExchange.amount.toString(), + unit = elementaryExchange.unit, + uid = ModelWriter.sanitizeAndCompact(elementaryExchange.name), + compartment = elementaryExchange.compartment, + subCompartment = elementaryExchange.subCompartment, + ) + + open fun mapInputs() {} open fun mapMetas() { val metas = result.meta diff --git a/src/main/kotlin/ch/kleis/lcaplugin/imports/ecospold/lcia/Ecospold2ProcessRenderer.kt b/src/main/kotlin/ch/kleis/lcaplugin/imports/ecospold/lcia/Ecospold2ProcessRenderer.kt index 2dce79de1..cf2459514 100644 --- a/src/main/kotlin/ch/kleis/lcaplugin/imports/ecospold/lcia/Ecospold2ProcessRenderer.kt +++ b/src/main/kotlin/ch/kleis/lcaplugin/imports/ecospold/lcia/Ecospold2ProcessRenderer.kt @@ -1,6 +1,7 @@ package ch.kleis.lcaplugin.imports.ecospold.lcia import ch.kleis.lcaplugin.imports.ModelWriter +import ch.kleis.lcaplugin.imports.ecospold.EcoSpold2ProcessMapper import ch.kleis.lcaplugin.imports.ecospold.lcia.EcospoldImporter.ProcessDictRecord import ch.kleis.lcaplugin.imports.ecospold.model.ActivityDataset import ch.kleis.lcaplugin.imports.shared.serializer.ProcessSerializer @@ -19,19 +20,19 @@ class Ecospold2ProcessRenderer { val category = category(data) val subFolder = if (category == null) "" else "${category}${File.separatorChar}" - val process = LciaProcessMapper(data).map() + val process = EcoSpold2ProcessMapper(data).map() process.comments.add(processComment) val strProcess = ProcessSerializer.serialize(process) w.write( - "processes${File.separatorChar}$subFolder${process.uid}.lca", - strProcess, index = false, closeAfterWrite = false + "processes${File.separatorChar}$subFolder${process.uid}.lca", + strProcess, index = false, closeAfterWrite = false ) val substance = EcoSpold2SubstanceMapper.map(data, methodName) val strSubstance = SubstanceSerializer.serialize(substance) w.write( - "processes${File.separatorChar}$subFolder${process.uid}.lca", - strSubstance, index = false, closeAfterWrite = true + "processes${File.separatorChar}$subFolder${process.uid}.lca", + strSubstance, index = false, closeAfterWrite = true ) @@ -39,8 +40,8 @@ class Ecospold2ProcessRenderer { private fun category(data: ActivityDataset): String? { val desc = data.description.classifications - .firstOrNull { it.system == "EcoSpold01Categories" } - ?.value + .firstOrNull { it.system == "EcoSpold01Categories" } + ?.value return desc?.let { ModelWriter.sanitizeAndCompact(it) } } diff --git a/src/main/kotlin/ch/kleis/lcaplugin/imports/ecospold/lcia/LciaProcessMapper.kt b/src/main/kotlin/ch/kleis/lcaplugin/imports/ecospold/lcia/LciaProcessMapper.kt deleted file mode 100644 index 987c7f23a..000000000 --- a/src/main/kotlin/ch/kleis/lcaplugin/imports/ecospold/lcia/LciaProcessMapper.kt +++ /dev/null @@ -1,14 +0,0 @@ -package ch.kleis.lcaplugin.imports.ecospold.lcia - -import ch.kleis.lcaplugin.imports.ecospold.EcoSpold2ProcessMapper -import ch.kleis.lcaplugin.imports.ecospold.model.ActivityDataset -import ch.kleis.lcaplugin.imports.model.ExchangeBlock -import ch.kleis.lcaplugin.imports.model.ImportedBioExchange - -class LciaProcessMapper(process: ActivityDataset) : EcoSpold2ProcessMapper(process) { - override fun mapEmission() { - val bio = ImportedBioExchange(listOf(), "1.0", "u", pUid, "") - result.emissionBlocks = mutableListOf(ExchangeBlock("Virtual Substance for Impact Factors", mutableListOf(bio))) - } - -} \ No newline at end of file diff --git a/src/main/kotlin/ch/kleis/lcaplugin/imports/ecospold/model/FlowData.kt b/src/main/kotlin/ch/kleis/lcaplugin/imports/ecospold/model/FlowData.kt index a6b5fcc01..6944d83c2 100644 --- a/src/main/kotlin/ch/kleis/lcaplugin/imports/ecospold/model/FlowData.kt +++ b/src/main/kotlin/ch/kleis/lcaplugin/imports/ecospold/model/FlowData.kt @@ -2,6 +2,7 @@ package ch.kleis.lcaplugin.imports.ecospold.model data class FlowData( - val intermediateExchanges: List = ArrayList(), - val impactIndicators: List + val intermediateExchanges: List = ArrayList(), + val impactIndicators: List, + val elementaryExchanges: Sequence, ) \ No newline at end of file diff --git a/src/main/kotlin/ch/kleis/lcaplugin/imports/ecospold/model/Parser.kt b/src/main/kotlin/ch/kleis/lcaplugin/imports/ecospold/model/Parser.kt index c17791843..79096f874 100644 --- a/src/main/kotlin/ch/kleis/lcaplugin/imports/ecospold/model/Parser.kt +++ b/src/main/kotlin/ch/kleis/lcaplugin/imports/ecospold/model/Parser.kt @@ -1,5 +1,6 @@ package ch.kleis.lcaplugin.imports.ecospold.model +import ch.kleis.lcaplugin.core.lang.expression.SubstanceType import org.jdom2.Element import org.jdom2.JDOMFactory import org.jdom2.input.SAXBuilder @@ -20,14 +21,14 @@ class Parser { } return root.getChildren("unitConversion") - .map { - UnitConversion( - it.getAttribute("factor").doubleValue, - it.getChildText("unitFromName"), - it.getChildText("unitToName"), - dimension(it.getChildText("unitType")) - ) - } + .map { + UnitConversion( + it.getAttribute("factor").doubleValue, + it.getChildText("unitFromName"), + it.getChildText("unitToName"), + dimension(it.getChildText("unitType")) + ) + } } fun readMethodUnits(stream: InputStream, methodName: String): List { @@ -36,20 +37,20 @@ class Parser { fun realName(unitName: String) = if (unitName == "dimensionless") "dimensionless_impact" else unitName return root.getChildren("impactMethod").asSequence() - .filter { it.getChildText("name") == methodName } - .flatMap { m -> m.getChildren("category") } - .map { c -> c.getChild("indicator") } - .map { - UnitConversion( - 1.0, - realName(it.getChildText("unitName")), - "No Ref", - realName(it.getChildText("unitName")), - it.getChildText("name"), - ) - } - .distinctBy { it.fromUnit } - .toList() + .filter { it.getChildText("name") == methodName } + .flatMap { m -> m.getChildren("category") } + .map { c -> c.getChild("indicator") } + .map { + UnitConversion( + 1.0, + realName(it.getChildText("unitName")), + "No Ref", + realName(it.getChildText("unitName")), + it.getChildText("name"), + ) + } + .distinctBy { it.fromUnit } + .toList() } @@ -58,9 +59,9 @@ class Parser { val root = rootElt(builder, stream) val xmlDataset = root.getChild("activityDataset") ?: root.getChild("childActivityDataset") val dataset = ActivityDataset.Builder() - .description(readDescription(xmlDataset.getChild("activityDescription"))) - .flowData(readFlowData(xmlDataset.getChild("flowData"))) - .build() + .description(readDescription(xmlDataset.getChild("activityDescription"))) + .flowData(readFlowData(xmlDataset.getChild("flowData"))) + .build() return EcospoldRoot(dataset) } @@ -70,52 +71,81 @@ class Parser { val root = rootElt(builder, stream) return root.getChildren("impactMethod") - .map { it.getChildText("name") } - .sorted() + .map { it.getChildText("name") } + .sorted() } private fun readIndicators(indicators: List): List { return indicators.map { ImpactIndicator.Builder() - .amount(it.getAttributeValue("amount").toDouble()) - .name(it.getChildText("name")) - .unitName(it.getChildText("unitName")) - .categoryName(it.getChildText("impactCategoryName")) - .methodName(it.getChildText("impactMethodName")) - .build() + .amount(it.getAttributeValue("amount").toDouble()) + .name(it.getChildText("name")) + .unitName(it.getChildText("unitName")) + .categoryName(it.getChildText("impactCategoryName")) + .methodName(it.getChildText("impactMethodName")) + .build() + } + } + + // TODO: setting to use, or not, the mapping towards ef3x + // FIXME: unspecified compartment + private fun readElementaryExchanges(elementaryExchangeList: List): Sequence = + elementaryExchangeList.asSequence().map { + ElementaryExchange( + elementaryExchangeId = it.getAttributeValue("elementaryExchangeId")!!, + amount = it.getAttributeValue("amount")!!.toDouble(), + name = it.getChildText("name")!!, + unit = it.getChildText("unitName")!!, + compartment = it.getChild("compartment")!!.getChildText("compartment")!!, + subCompartment = it.getChild("compartment")!!.getChildText("subcompartment"), + substanceType = readSubstanceType(it), + comment = it.getChildText("comment") + ) + } + + private fun readSubstanceType(elementaryExchange: Element): SubstanceType { + val outputGroup = elementaryExchange.getChildText("outputGroup") + val inputGroup = elementaryExchange.getChildText("inputGroup") + val subCompartment: String? = elementaryExchange.getChild("compartment")!!.getChildText("subcompartment") + return when { + subCompartment?.equals("land") ?: false -> SubstanceType.LAND_USE + outputGroup?.equals("4") ?: false -> SubstanceType.EMISSION + inputGroup?.equals("4") ?: false -> SubstanceType.RESOURCE + else -> throw Error("Invalid input and output group for exchange ${elementaryExchange.getChildText("name")}") } } private fun readFlowData(xmlDesc: Element): FlowData { - val exchanges = xmlDesc.getChildren("intermediateExchange") - .map { - val builder = IntermediateExchange.Builder() - .amount(it.getAttributeValue("amount").toDouble()) - .name(it.getChildText("name")) - .unit(it.getChildText("unitName")) - .synonyms(it.getChildren("synonym").map { n -> n.value }) - .uncertainty(readUncertainty(it.getChild("uncertainty"))) - .outputGroup(it.getChildText("outputGroup").toInt()) - .classifications(readClassifications(it.getChildren("classification"))) - .properties(readProperties(it.getChildren("property"))) - builder.build() - } + val intermediateExchangeList = xmlDesc.getChildren("intermediateExchange") + .map { + val builder = IntermediateExchange.Builder() + .amount(it.getAttributeValue("amount").toDouble()) + .name(it.getChildText("name")) + .unit(it.getChildText("unitName")) + .synonyms(it.getChildren("synonym").map { n -> n.value }) + .uncertainty(readUncertainty(it.getChild("uncertainty"))) + .outputGroup(it.getChildText("outputGroup").toInt()) + .classifications(readClassifications(it.getChildren("classification"))) + .properties(readProperties(it.getChildren("property"))) + builder.build() + } val indicators = readIndicators(xmlDesc.getChildren("impactIndicator")) + val elementaryExchangeList = readElementaryExchanges(xmlDesc.getChildren("elementaryExchange")) - return FlowData(exchanges, indicators) + return FlowData(intermediateExchangeList, indicators, elementaryExchangeList) } private fun readProperties(children: List): List { return children.map { Property( - it.getChildText("name"), - it.getAttributeValue("amount").toDouble(), - it.getChildText("unitName"), - it.getAttributeValue("isDefiningValue"), - it.getAttributeValue("isCalculatedAmount"), + it.getChildText("name"), + it.getAttributeValue("amount").toDouble(), + it.getChildText("unitName"), + it.getAttributeValue("isDefiningValue"), + it.getAttributeValue("isCalculatedAmount"), ) } @@ -123,46 +153,46 @@ class Parser { private fun readDescription(xmlDesc: Element): ActivityDescription { return ActivityDescription.Builder() - .activity(readActivity(xmlDesc.getChild("activity"))) - .classifications(readClassifications(xmlDesc.getChildren("classification"))) - .geography(readGeography(xmlDesc.getChild("geography"))) - .build() + .activity(readActivity(xmlDesc.getChild("activity"))) + .classifications(readClassifications(xmlDesc.getChildren("classification"))) + .geography(readGeography(xmlDesc.getChild("geography"))) + .build() } private fun readClassifications(children: List): List { return children.map { Classification( - it.getChildText("classificationSystem"), - it.getChildText("classificationValue") + it.getChildText("classificationSystem"), + it.getChildText("classificationValue") ) } } private fun readGeography(xml: Element): Geography { return Geography( - xml.getChildText("shortname"), - readMultiline(xml.getChild("comment")) + xml.getChildText("shortname"), + readMultiline(xml.getChild("comment")) ) } private fun readActivity(xml: Element): Activity { return Activity.Builder() - .id(xml.getAttributeValue("id")) - .type(xml.getAttributeValue("type")) - .energyValues(xml.getAttributeValue("energyValues")) - .name(xml.getChildText("activityName")) - .includedActivitiesStart(xml.getChildText("includedActivitiesStart")) - .includedActivitiesEnd(xml.getChildText("includedActivitiesEnd")) - .generalComment(readMultiline(xml.getChild("generalComment"))) - .build() + .id(xml.getAttributeValue("id")) + .type(xml.getAttributeValue("type")) + .energyValues(xml.getAttributeValue("energyValues")) + .name(xml.getChildText("activityName")) + .includedActivitiesStart(xml.getChildText("includedActivitiesStart")) + .includedActivitiesEnd(xml.getChildText("includedActivitiesEnd")) + .generalComment(readMultiline(xml.getChild("generalComment"))) + .build() } private fun readMultiline(xml: Element?): List { return xml?.getChildren("text") - ?.sortedBy { it.getAttribute("index").intValue } - ?.map { it.text } - ?: listOf() + ?.sortedBy { it.getAttribute("index").intValue } + ?.map { it.text } + ?: listOf() } private fun rootElt(builder: SAXBuilder, stream: InputStream): Element { @@ -184,7 +214,7 @@ class Parser { override fun createSAXHandler(factory: JDOMFactory?): SAXHandler { return object : SAXHandler() { override fun startElement( - namespaceURI: String?, localName: String?, qName: String?, atts: Attributes? + namespaceURI: String?, localName: String?, qName: String?, atts: Attributes? ) { super.startElement("", localName, qName, atts) } @@ -201,46 +231,46 @@ class Parser { val logNormal: LogNormal? = xml.getChild("lognormal")?.let { LogNormal( - it.getAttributeValue("meanValue").toDouble(), - it.getAttributeValue("mu").toDouble(), - it.getAttributeValue("variance").toDouble(), - it.getAttributeValue("varianceWithPedigreeUncertainty").toDouble() + it.getAttributeValue("meanValue").toDouble(), + it.getAttributeValue("mu").toDouble(), + it.getAttributeValue("variance").toDouble(), + it.getAttributeValue("varianceWithPedigreeUncertainty").toDouble() ) } val normal: Normal? = xml.getChild("normal")?.let { Normal( - it.getAttributeValue("meanValue").toDouble(), - it.getAttributeValue("variance").toDouble(), - it.getAttributeValue("varianceWithPedigreeUncertainty").toDouble(), + it.getAttributeValue("meanValue").toDouble(), + it.getAttributeValue("variance").toDouble(), + it.getAttributeValue("varianceWithPedigreeUncertainty").toDouble(), ) } val triangular: Triangular? = xml.getChild("triangular")?.let { Triangular( - it.getAttributeValue("minValue").toDouble(), - it.getAttributeValue("mostLikelyValue").toDouble(), - it.getAttributeValue("maxValue").toDouble(), + it.getAttributeValue("minValue").toDouble(), + it.getAttributeValue("mostLikelyValue").toDouble(), + it.getAttributeValue("maxValue").toDouble(), ) } val uniform: Uniform? = xml.getChild("uniform")?.let { Uniform( - it.getAttributeValue("minValue").toDouble(), - it.getAttributeValue("maxValue").toDouble(), + it.getAttributeValue("minValue").toDouble(), + it.getAttributeValue("maxValue").toDouble(), ) } val undefined: UndefinedUncertainty? = xml.getChild("undefined")?.let { UndefinedUncertainty( - it.getAttributeValue("minValue").toDouble(), - it.getAttributeValue("maxValue").toDouble(), - it.getAttributeValue("standardDeviation95").toDouble(), + it.getAttributeValue("minValue").toDouble(), + it.getAttributeValue("maxValue").toDouble(), + it.getAttributeValue("standardDeviation95").toDouble(), ) } val pedigreeMatrix: PedigreeMatrix? = xml.getChild("pedigreeMatrix")?.let { PedigreeMatrix( - it.getAttributeValue("reliability").toInt(), - it.getAttributeValue("completeness").toInt(), - it.getAttributeValue("temporalCorrelation").toInt(), - it.getAttributeValue("geographicalCorrelation").toInt(), - it.getAttributeValue("furtherTechnologyCorrelation").toInt() + it.getAttributeValue("reliability").toInt(), + it.getAttributeValue("completeness").toInt(), + it.getAttributeValue("temporalCorrelation").toInt(), + it.getAttributeValue("geographicalCorrelation").toInt(), + it.getAttributeValue("furtherTechnologyCorrelation").toInt() ) } val comment: String? = xml.getChildText("comment") diff --git a/src/test/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/EvaluationTraceTest.kt b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/EvaluationTraceTest.kt index 00c61a8bc..200c7051b 100644 --- a/src/test/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/EvaluationTraceTest.kt +++ b/src/test/kotlin/ch/kleis/lcaplugin/core/lang/evaluator/EvaluationTraceTest.kt @@ -59,7 +59,7 @@ class EvaluationTraceTest { trace.commit() // when/then - val e = assertFailsWith(EvaluatorException::class) { trace.getEntryPoint() } + val e = assertFailsWith(EvaluatorException::class) { trace.getEntryPoint() } assertEquals("execution trace contains multiple entrypoint", e.message) } @@ -69,7 +69,7 @@ class EvaluationTraceTest { val trace = EvaluationTrace.empty() // when/then - val e = assertFailsWith(EvaluatorException::class) { trace.getEntryPoint() } + val e = assertFailsWith(EvaluatorException::class) { trace.getEntryPoint() } assertEquals("execution trace is empty", e.message) } @@ -103,7 +103,7 @@ class EvaluationTraceTest { trace.commit() // when - val actual = trace.getProductOrder() + val actual = trace.getObservableOrder() // then assert(actual.compare(product1, product2) < 0) @@ -145,7 +145,7 @@ class EvaluationTraceTest { trace.commit() // when - val actual = trace.getProductOrder() + val actual = trace.getObservableOrder() // then assert(actual.compare(product1, product2) == -actual.compare(product2, product1)) diff --git a/src/test/kotlin/ch/kleis/lcaplugin/imports/ecospold/lcia/EcoSpold2Fixture.kt b/src/test/kotlin/ch/kleis/lcaplugin/imports/ecospold/lcia/EcoSpold2Fixture.kt index d5aebd956..d47a72a94 100644 --- a/src/test/kotlin/ch/kleis/lcaplugin/imports/ecospold/lcia/EcoSpold2Fixture.kt +++ b/src/test/kotlin/ch/kleis/lcaplugin/imports/ecospold/lcia/EcoSpold2Fixture.kt @@ -1,48 +1,81 @@ package ch.kleis.lcaplugin.imports.ecospold.lcia +import ch.kleis.lcaplugin.core.lang.expression.SubstanceType import ch.kleis.lcaplugin.imports.ecospold.model.* class EcoSpold2Fixture { companion object { fun buildData(outputGroup: Int = 0): ActivityDataset { val activity = Activity.Builder() - .id("aId") - .name("aName") - .type("1") - .generalComment(listOf("ageneralComment")) - .energyValues("123") - .includedActivitiesStart("includedActivitiesStart") - .includedActivitiesEnd("includedActivitiesEnd") - .build() + .id("aId") + .name("aName") + .type("1") + .generalComment(listOf("ageneralComment")) + .energyValues("123") + .includedActivitiesStart("includedActivitiesStart") + .includedActivitiesEnd("includedActivitiesEnd") + .build() val c = Classification("System", "Value") val geo = Geography("ch", listOf("comment")) val description = ActivityDescription.Builder() - .activity(activity) - .geography(geo) - .classifications(listOf(c)) - .build() + .activity(activity) + .geography(geo) + .classifications(listOf(c)) + .build() val prod = IntermediateExchange.Builder() - .name("pName") - .outputGroup(outputGroup) - .classifications(listOf(Classification("PSystem", "PValue"))) - .uncertainty( - Uncertainty( - logNormal = LogNormal(1.2, 3.4, 2.3, 4.5) + .name("pName") + .outputGroup(outputGroup) + .classifications(listOf(Classification("PSystem", "PValue"))) + .uncertainty( + Uncertainty( + logNormal = LogNormal(1.2, 3.4, 2.3, 4.5) + ) ) - ) - .amount(1.0) - .unit("km") - .synonyms(listOf("p1")) - .build() + .amount(1.0) + .unit("km") + .synonyms(listOf("p1")) + .build() val impacts = listOf( - ImpactIndicator("EF v3.0 no LT", "water use", "deprivation", 0.1188, "m3 world eq. deprived"), - ImpactIndicator("EF v3.1", "acidification", "accumulated exceedance (AE)", 0.0013, "mol H+-Eq"), - ImpactIndicator("EF v3.1", "climate change", "global warming potential (GWP100)", 0.6, "kg CO2-Eq"), + ImpactIndicator("EF v3.0 no LT", "water use", "deprivation", 0.1188, "m3 world eq. deprived"), + ImpactIndicator("EF v3.1", "acidification", "accumulated exceedance (AE)", 0.0013, "mol H+-Eq"), + ImpactIndicator("EF v3.1", "climate change", "global warming potential (GWP100)", 0.6, "kg CO2-Eq"), + ) + val emissions = sequenceOf( + ElementaryExchange( + "9645e02f-855a-4b9f-8baf-f34a08fa80c4", + "1.8326477008541038E-08".toDouble(), + "1,2-Dichlorobenzene", + "kg", + "air", + "urban air close to ground", + SubstanceType.EMISSION, + null + ), + ElementaryExchange( + "e3f5fd63-7dcb-41f1-9b8a-a48a8d68bc65", + "0.004413253823373581".toDouble(), + "Nitrogen", + "kg", + "natural resource", + "land", + SubstanceType.RESOURCE, + null + ), + ElementaryExchange( + "c4a82f46-381f-474c-a362-3363064b9c33", + "0.04997982922431679".toDouble(), + "Occupation, annual crop, irrigated", + "m2*year", + "natural resource", + "land", + SubstanceType.LAND_USE, + null + ), ) return ActivityDataset.Builder() - .description(description) - .flowData(FlowData(listOf(prod), impacts)) - .build() + .description(description) + .flowData(FlowData(listOf(prod), impacts, emissions)) + .build() } } diff --git a/src/test/kotlin/ch/kleis/lcaplugin/imports/ecospold/lcia/EcoSpold2ProcessMapperTest.kt b/src/test/kotlin/ch/kleis/lcaplugin/imports/ecospold/lcia/EcoSpold2ProcessMapperTest.kt index 00ef26d9e..6b8504d2b 100644 --- a/src/test/kotlin/ch/kleis/lcaplugin/imports/ecospold/lcia/EcoSpold2ProcessMapperTest.kt +++ b/src/test/kotlin/ch/kleis/lcaplugin/imports/ecospold/lcia/EcoSpold2ProcessMapperTest.kt @@ -17,7 +17,7 @@ class EcoSpold2ProcessMapperTest { // Given // When - val result = EcoSpold2ProcessMapper.map(sub) + val result = EcoSpold2ProcessMapper(sub).map() // Then assertEquals("aname_ch", result.uid) @@ -36,7 +36,7 @@ class EcoSpold2ProcessMapperTest { // Given // When - val result = EcoSpold2ProcessMapper.map(sub) + val result = EcoSpold2ProcessMapper(sub).map() // Then assertEquals(1, result.productBlocks.size) @@ -48,12 +48,12 @@ class EcoSpold2ProcessMapperTest { assertEquals("km", p.unit) assertEquals(100.0, p.allocation) assertEquals( - listOf( - "pName", - "PSystem = PValue", - "// uncertainty: logNormal mean=1.2, variance=2.3, mu=3.4", - "synonym_0 = p1" - ), p.comments + listOf( + "pName", + "PSystem = PValue", + "// uncertainty: logNormal mean=1.2, variance=2.3, mu=3.4", + "synonym_0 = p1" + ), p.comments ) } @@ -65,9 +65,9 @@ class EcoSpold2ProcessMapperTest { // When assertThrows( - ImportException::class.java, - "Invalid outputGroup for product, expected 0, found 1" - ) { EcoSpold2ProcessMapper.map(falseSub) } + ImportException::class.java, + "Invalid outputGroup for product, expected 0, found 1" + ) { EcoSpold2ProcessMapper(falseSub).map() } } @Test @@ -75,13 +75,13 @@ class EcoSpold2ProcessMapperTest { // Given // When - val result = EcoSpold2ProcessMapper.map(sub) + val result = EcoSpold2ProcessMapper(sub).map() // Then - assertEquals(1, result.emissionBlocks.size) - assertEquals("Virtual Substance for Impact Factors", result.emissionBlocks[0].comment) - assertEquals(1, result.emissionBlocks[0].exchanges.size) - val substance = result.emissionBlocks[0].exchanges[0] + assertEquals(2, result.emissionBlocks.size) + assertEquals("Virtual Substance for Impact Factors", result.emissionBlocks[1].comment) + assertEquals(1, result.emissionBlocks[1].exchanges.size) + val substance = result.emissionBlocks[1].exchanges[0] assertEquals("aname_ch", substance.uid) assertEquals("1.0", substance.qty) assertEquals("u", substance.unit) diff --git a/src/test/kotlin/ch/kleis/lcaplugin/imports/ecospold/lcia/Ecospold2ProcessRendererTest.kt b/src/test/kotlin/ch/kleis/lcaplugin/imports/ecospold/lcia/Ecospold2ProcessRendererTest.kt index a63896da6..78d979181 100644 --- a/src/test/kotlin/ch/kleis/lcaplugin/imports/ecospold/lcia/Ecospold2ProcessRendererTest.kt +++ b/src/test/kotlin/ch/kleis/lcaplugin/imports/ecospold/lcia/Ecospold2ProcessRendererTest.kt @@ -37,7 +37,7 @@ class Ecospold2ProcessRendererTest { every { activity.description.classifications } returns listOf(Classification("EcoSpold01Categories", "cat")) mockkObject(EcoSpold2ProcessMapper) val importedProcess = mockk() - every { EcoSpold2ProcessMapper.map(activity) } returns importedProcess + every { EcoSpold2ProcessMapper(activity).map() } returns importedProcess val comments = mutableListOf() every { importedProcess.comments } returns comments every { importedProcess.uid } returns "uid"