Skip to content

Commit

Permalink
Remove FloatingPointRepresentation.kt.
Browse files Browse the repository at this point in the history
  • Loading branch information
jedesroches committed Nov 15, 2023
1 parent 44ade00 commit e39f3a5
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 272 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import ch.kleis.lcaac.core.lang.value.SubstanceValue
import ch.kleis.lcaac.core.math.basic.BasicMatrix
import ch.kleis.lcaac.core.math.basic.BasicNumber
import ch.kleis.lcaac.core.math.basic.BasicOperations
import ch.kleis.lcaac.plugin.ui.toolwindow.shared.FloatingPointRepresentation
import ch.kleis.lcaac.plugin.ui.toolwindow.shared.QuantityRenderer

class SankeyGraphBuilder(
private val analysis: ContributionAnalysis<BasicNumber, BasicMatrix>,
Expand Down Expand Up @@ -96,7 +96,7 @@ class SankeyGraphBuilder(
return if (source == target || 0 < compareResult) {
this
} else {
val name = FloatingPointRepresentation.of(value)
val name = QuantityRenderer.formatDouble(value)
this.addLink(GraphLink(source.getUID(), target.getUID(), value, """$name $unit"""))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package ch.kleis.lcaac.plugin.ui.toolwindow.sensitivity_analysis
import ch.kleis.lcaac.core.assessment.SensitivityAnalysis
import ch.kleis.lcaac.core.lang.value.ProductValue
import ch.kleis.lcaac.core.math.dual.DualNumber
import ch.kleis.lcaac.plugin.ui.toolwindow.shared.FloatingPointRepresentation
import ch.kleis.lcaac.plugin.ui.toolwindow.LcaToolWindowContent
import ch.kleis.lcaac.plugin.ui.toolwindow.shared.QuantityRenderer
import ch.kleis.lcaac.plugin.ui.toolwindow.shared.WithHeaderTransferableHandler
import com.intellij.icons.AllIcons
import com.intellij.openapi.project.Project
Expand Down Expand Up @@ -42,7 +42,7 @@ class SensitivityAnalysisWindow(

val cellRenderer = DefaultTableCellRenderer()
cellRenderer.horizontalAlignment = JLabel.RIGHT
table.setDefaultRenderer(FloatingPointRepresentation::class.java, cellRenderer)
table.setDefaultRenderer(Double::class.java, QuantityRenderer)

val tablePane = JBScrollPane(table)
tablePane.border = JBEmptyBorder(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ch.kleis.lcaac.plugin.ui.toolwindow.sensitivity_analysis
import ch.kleis.lcaac.core.assessment.SensitivityAnalysis
import ch.kleis.lcaac.core.lang.value.MatrixColumnIndex
import ch.kleis.lcaac.core.math.dual.DualNumber
import ch.kleis.lcaac.plugin.ui.toolwindow.shared.FloatingPointRepresentation
import javax.swing.event.TableModelListener
import javax.swing.table.TableModel

Expand Down Expand Up @@ -36,24 +35,20 @@ class SensitivityTableModel(
override fun getColumnClass(columnIndex: Int): Class<*> {
return when (columnIndex) {
0, 2 -> String::class.java
else -> FloatingPointRepresentation::class.java
else -> Double::class.java
}
}

override fun isCellEditable(rowIndex: Int, columnIndex: Int): Boolean {
return false
}

private fun repr(d: Double, suffix: String? = null): FloatingPointRepresentation {
return FloatingPointRepresentation.of(d, 3, suffix)
}

override fun getValueAt(rowIndex: Int, columnIndex: Int): Any {
return when (columnIndex) {
0 -> analysis.getParameters().getName(rowIndex).uid
1 -> {
val value = analysis.getParameters().getValue(rowIndex)
repr(value.amount.zeroth)
value.amount.zeroth
}

2 -> {
Expand All @@ -62,12 +57,11 @@ class SensitivityTableModel(
}

else -> {
val relativeSensibility = analysis.getRelativeSensibility(
analysis.getRelativeSensibility(
target,
sortedControllablePorts[columnIndex - 3],
analysis.getParameters().getName(rowIndex),
)
return repr(relativeSensibility)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ch.kleis.lcaac.plugin.ui.toolwindow.sensitivity_analysis
import ch.kleis.lcaac.core.assessment.SensitivityAnalysis
import ch.kleis.lcaac.core.lang.value.MatrixColumnIndex
import ch.kleis.lcaac.core.math.dual.DualNumber
import ch.kleis.lcaac.plugin.ui.toolwindow.shared.FloatingPointRepresentation
import javax.swing.event.TableModelListener
import javax.swing.table.TableModel

Expand Down Expand Up @@ -37,25 +36,21 @@ class TransposedSensitivityTableModel(
override fun getColumnClass(columnIndex: Int): Class<*> {
return when (columnIndex) {
0, 2 -> String::class.java
else -> FloatingPointRepresentation::class.java
else -> Double::class.java
}
}

override fun isCellEditable(rowIndex: Int, columnIndex: Int): Boolean {
return false
}

private fun repr(d: Double, suffix: String? = null): FloatingPointRepresentation {
return FloatingPointRepresentation.of(d, 3, suffix)
}

override fun getValueAt(rowIndex: Int, columnIndex: Int): Any {
return when (columnIndex) {
0 -> sortedControllablePorts[rowIndex].getDisplayName()
1 -> {
val indicator = sortedControllablePorts[rowIndex]
val contribution = analysis.getPortContribution(target, indicator)
repr(contribution.amount.zeroth)
contribution.amount.zeroth
}

2 -> {
Expand All @@ -65,12 +60,11 @@ class TransposedSensitivityTableModel(
}

else -> {
val relativeSensibility = analysis.getRelativeSensibility(
analysis.getRelativeSensibility(
target,
sortedControllablePorts[rowIndex],
analysis.getParameters().getName(columnIndex - 3)
)
return repr(relativeSensibility)
}
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import javax.swing.table.DefaultTableCellRenderer
object QuantityRenderer : DefaultTableCellRenderer() {
private val formatter = DecimalFormat("0.##E0")

// `+ 0.0` converts the special double value -0.0 to the expected 0.0, with no other side effects.
fun formatDouble(value: Double): String =
formatter.format(value + 0.0).removeSuffix("E0")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ class SankeyGraphWindowBuilderTest : BasePlatformTestCase() {
}

@Test
fun test_units() {
fun `When processes define different units of the same dimension, normalization is applied when building graph links`() {
// given
val pkgName = {}.javaClass.enclosingMethod.name
val vf = myFixture.createFile(
Expand Down Expand Up @@ -327,8 +327,8 @@ class SankeyGraphWindowBuilderTest : BasePlatformTestCase() {
GraphNode("my_input from input{}{}", "my_input"),
GraphNode("my_indicator", "my_indicator"),
).addLink(
GraphLink("my_product from p{}{}", "my_input from input{}{}", 0.5, "0.5 g"),
GraphLink("my_input from input{}{}", "my_indicator", 0.5, "0.5 g"),
GraphLink("my_product from p{}{}", "my_input from input{}{}", 0.5, "5E-1 g"),
GraphLink("my_input from input{}{}", "my_indicator", 0.5, "5E-1 g"),
)
assertEquals(expected.nodes.naturalSorted(), graph.nodes.naturalSorted())
assertEquals(expected.links.naturalSorted(), graph.links.naturalSorted())
Expand Down Expand Up @@ -436,7 +436,7 @@ class SankeyGraphWindowBuilderTest : BasePlatformTestCase() {
}

@Test
fun test_whenComplexGraph_thenSankey() {
fun `When provided with a semi-complex graph using allocation, substances and several processes, the graph is as expected`() {
// given
val pkgName = {}.javaClass.enclosingMethod.name
val vf = myFixture.createFile(
Expand Down Expand Up @@ -489,10 +489,10 @@ class SankeyGraphWindowBuilderTest : BasePlatformTestCase() {
GraphNode("[Emission] my_substance(air)", "my_substance")
).addLink(
GraphLink("my_product from p{}{}", "my_input from q{}{}", 1.5, "1.5 u"),
GraphLink("my_other_product from p{}{}", "my_input from q{}{}", 0.5, "0.5 u"),
GraphLink("my_other_product from p{}{}", "my_input from q{}{}", 0.5, "5E-1 u"),
GraphLink("my_input from q{}{}", "[Emission] my_substance(air)", 2.0, "2 u"),
GraphLink("my_product from p{}{}", "[Emission] my_substance(air)", 0.75, "0.75 u"),
GraphLink("my_other_product from p{}{}", "[Emission] my_substance(air)", 0.25, "0.25 u"),
GraphLink("my_product from p{}{}", "[Emission] my_substance(air)", 0.75, "7.5E-1 u"),
GraphLink("my_other_product from p{}{}", "[Emission] my_substance(air)", 0.25, "2.5E-1 u"),
GraphLink("[Emission] my_substance(air)", "climate_change", 3.0, "3 u")
)
assertEquals(expected.nodes.naturalSorted(), graph.nodes.naturalSorted())
Expand Down Expand Up @@ -546,7 +546,7 @@ class SankeyGraphWindowBuilderTest : BasePlatformTestCase() {
}

@Test
fun test_whenComplexCycles_thenAcyclicSankey() {
fun `When provided with a complex cyclic graph of processes, the generated sankey is acyclic`() {
// given
val pkgName = {}.javaClass.enclosingMethod.name
val vf = myFixture.createFile(
Expand Down Expand Up @@ -647,9 +647,9 @@ class SankeyGraphWindowBuilderTest : BasePlatformTestCase() {
GraphNode("my_emission", "my_emission")
).addLink(
GraphLink("F from pF{}{}", "H from pH{}{}", value = 1.5, "1.5 kg"),
GraphLink("C from pC{}{}", "F from pF{}{}", value = 0.5, "0.5 kg"),
GraphLink("C from pC{}{}", "F from pF{}{}", value = 0.5, "5E-1 kg"),
GraphLink("A from pA{}{}", "B from pB{}{}", value = 3.0, "3 kg"),
GraphLink("A from pA{}{}", "C from pC{}{}", value = 0.5, "0.5 kg"),
GraphLink("A from pA{}{}", "C from pC{}{}", value = 0.5, "5E-1 kg"),
GraphLink("D from pD{}{}", "F from pF{}{}", value = 1.0, "1 kg"),
GraphLink("H from pH{}{}", "my_emission", value = 3.5, "3.5 kg"),
GraphLink("B from pB{}{}", "D from pD{}{}", value = 1.0, "1 kg"),
Expand Down
Loading

0 comments on commit e39f3a5

Please sign in to comment.