Skip to content

Commit

Permalink
fix(loading): ensure columnName is initialized before use with lazy d…
Browse files Browse the repository at this point in the history
…elegation (#4178)
  • Loading branch information
angelacorte authored Feb 6, 2025
1 parent 65cd0f7 commit b62fd75
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,24 @@ abstract class AbstractAggregatingDoubleExporter
precision = null,
)

/**
* The name of the column in the output file.
*/
abstract val columnName: String

private val aggregators: Map<String, UnivariateStatistic> =
aggregatorNames
.associateWith { StatUtil.makeUnivariateStatistic(it) }
.filter { it.value.isPresent }
.map { it.key to it.value.get() }
.toMap()

override val columnNames: List<String> =
override val columnNames: List<String> by lazy {
aggregators.keys
.takeIf { it.isNotEmpty() }
?.map { "$colunmName[$it]" }
?: listOf("$colunmName@node-id")
?.map { "$columnName[$it]" }
?: listOf("$columnName@node-id")
}

final override fun <T> extractData(
environment: Environment<T, *>,
Expand All @@ -63,7 +69,7 @@ abstract class AbstractAggregatingDoubleExporter
when {
aggregators.isEmpty() ->
getData(environment, reaction, time, step)
.mapKeys { (key, _) -> "$colunmName@${key.id}" }
.mapKeys { (key, _) -> "$columnName@${key.id}" }
else -> {
val data =
getData(environment, reaction, time, step)
Expand All @@ -72,16 +78,11 @@ abstract class AbstractAggregatingDoubleExporter
.toDoubleArray()
aggregators
.map { (aggregator, statistics) ->
"$colunmName[$aggregator]" to statistics.evaluate(data)
"$columnName[$aggregator]" to statistics.evaluate(data)
}.toMap()
}
}

/**
* The name of the column in the output file.
*/
abstract val colunmName: String

/**
* Delegated to the concrete implementation to extract data from the environment.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,24 @@ class MoleculeReader
private const val SHORT_NAME_MAX_LENGTH = 5
}

override val colunmName: String
get() = "$shortProp$moleculeName"
override val columnName: String

private val molecule: Molecule = incarnation.createMolecule(moleculeName)
init {
val propertyText =
if (property.isNullOrEmpty()) {
""
} else {
property.replace("\\W*".toRegex(), "")
}

val shortProp: String =
propertyText.takeIf(String::isEmpty)
?: "${propertyText.substring(0..<min(propertyText.length, SHORT_NAME_MAX_LENGTH))}@"

private val propertyText =
if (property.isNullOrEmpty()) {
""
} else {
property.replace("[^\\d\\w]*".toRegex(), "")
}
columnName = "$shortProp$moleculeName"
}

private val shortProp =
propertyText.takeIf(String::isEmpty)
?: "${propertyText.substring(0..<min(propertyText.length, SHORT_NAME_MAX_LENGTH))}@"
private val molecule: Molecule = incarnation.createMolecule(moleculeName)

override fun <T> getData(
environment: Environment<T, *>,
Expand Down

0 comments on commit b62fd75

Please sign in to comment.