Skip to content

Commit 4d95c29

Browse files
NathekipDanySK
authored andcommitted
feat: allow to round values in CSVExporter
1 parent 5a92f6e commit 4d95c29

3 files changed

Lines changed: 11 additions & 2 deletions

File tree

alchemist-incarnation-protelis/src/test/resources/testbase.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ export:
33
- type: CSVExporter
44
parameters:
55
fileNameRoot: "test_base"
6+
interval: 1.0
7+
decimalPlacesCount: 3
68
data:
79
- time
810

alchemist-loading/src/main/kotlin/it/unibo/alchemist/boundary/exporters/CSVExporter.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@ import org.slf4j.LoggerFactory
3737
* @property exportPath the directory to write exported files (temporary folder is used when omitted)
3838
* @property appendTime if true a timestamp is appended to the file name to avoid overwriting
3939
* @property fileExtension the extension for the exported files (default: 'csv')
40+
* @property decimalPlacesCount the count of places after decimal
4041
*/
4142
class CSVExporter<T, P : Position<P>>
4243
@JvmOverloads
4344
constructor(
4445
private val fileNameRoot: String = "",
4546
val interval: Double = DEFAULT_INTERVAL,
47+
val decimalPlacesCount: String = "2",
4648
val exportPath: String =
4749
createTempDirectory("alchemist-export")
4850
.absolutePathString()
@@ -100,7 +102,8 @@ constructor(
100102
val data = extractor.extractDataAsText(environment, reaction, time, step)
101103
val names = extractor.columnNames
102104
when {
103-
data.size <= 1 -> data.values.joinToString(" ")
105+
data.size <= 1 ->
106+
roundValues(data.values, decimalPlacesCount.toInt()).joinToString(" ")
104107
// Labels and keys match
105108
data.size == names.size && data.keys.containsAll(names) ->
106109
names.joinToString(" ") {
@@ -140,6 +143,10 @@ constructor(
140143
}
141144
}
142145

146+
private fun roundValues(values: Collection<String>, decimalsNumber: Int): Collection<String> = values.map {
147+
if (it.toDouble() % 0 == 0.0) it else String.format(Locale.US, "%.${decimalsNumber}f", it.toDouble())
148+
}
149+
143150
private companion object {
144151
/**
145152
* Character used to separate comments from data on export files.

alchemist-loading/src/test/kotlin/it/unibo/alchemist/test/TestCSVExporter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class TestCSVExporter<T, P : Position<P>> :
5757
}
5858
"should have limited-length decimals" {
5959
val limitedDecimalsFile = simulation.csvExporters()[1].dataFile("fixed-decimals_")
60-
val precision2 = """(0\.0*\d\d|\d\.0*\d|\d\.\d|\d\d)(e(-|\+)\d+)?"""
60+
val precision2 = """(\d\d.\d\d|0\.0*\d\d|\d\.0*\d|\d\.\d|\d\d)(e(-|\+)\d+)?"""
6161
val lineRegex = Regex("""^$precision2(\s($precision2))+$""")
6262
limitedDecimalsFile.useLines { lines ->
6363
lines

0 commit comments

Comments
 (0)