Skip to content

Commit

Permalink
Do not add links that would create a cycle.
Browse files Browse the repository at this point in the history
  • Loading branch information
jedesroches committed Jul 7, 2023
1 parent 58d2ab6 commit 0be6470
Showing 1 changed file with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class SankeyGraphBuilder(
when (port) {
is SubstanceValue -> {
graph.addNode(GraphNode(port.getUID(), port.getShortName()))
.addLink(
.addLinkIfNoCycle(
GraphLink(
port.getUID(),
sankeyIndicator.getUID(),
Expand All @@ -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(),
Expand All @@ -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
Expand Down

0 comments on commit 0be6470

Please sign in to comment.