From 0be647003fd47ba58b12e1ed0c954e23d3146fdc Mon Sep 17 00:00:00 2001 From: Joachim Desroches Date: Fri, 7 Jul 2023 14:12:18 +0200 Subject: [PATCH] Do not add links that would create a cycle. --- .../lcaplugin/actions/sankey/SankeyGraphBuilder.kt | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) 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 2b8846562..9483cbfc5 100644 --- a/src/main/kotlin/ch/kleis/lcaplugin/actions/sankey/SankeyGraphBuilder.kt +++ b/src/main/kotlin/ch/kleis/lcaplugin/actions/sankey/SankeyGraphBuilder.kt @@ -19,7 +19,7 @@ class SankeyGraphBuilder( when (port) { is SubstanceValue -> { graph.addNode(GraphNode(port.getUID(), port.getShortName())) - .addLink( + .addLinkIfNoCycle( GraphLink( port.getUID(), sankeyIndicator.getUID(), @@ -36,7 +36,7 @@ class SankeyGraphBuilder( } linksWithObservedImpact.fold(graph.addNode(GraphNode(port.getUID(), port.getShortName()))) { accumulatorGraph, exchange -> - accumulatorGraph.addLink( + accumulatorGraph.addLinkIfNoCycle( GraphLink( port.getUID(), exchange.port().getUID(), @@ -51,6 +51,13 @@ class SankeyGraphBuilder( return completeGraph } + private fun Graph.addLinkIfNoCycle(link: GraphLink): Graph = + if (this.links.any { it.source == link.target && it.target == link.source }) { + this + } else { + this.addLink(link) + } + private fun impactAmountForSubstance(observed: MatrixColumnIndex, inventory: Inventory, substance: SubstanceValue): Double { return inventory.impactFactors.valueRatio(substance, observed).amount * inventory.supply.quantityOf(substance).amount