Skip to content

allow to round values in CSVexporter - #5497

Open
Nathekip wants to merge 2 commits into
AlchemistSimulator:masterfrom
Nathekip:master
Open

allow to round values in CSVexporter#5497
Nathekip wants to merge 2 commits into
AlchemistSimulator:masterfrom
Nathekip:master

Conversation

@Nathekip

Copy link
Copy Markdown

This PR partially tackles the issue #131, specifically the ability for user to specify the amount of numbers they want after the decimal

@Nathekip
Nathekip force-pushed the master branch 3 times, most recently from 25a9222 to 92ba4b8 Compare July 21, 2026 10:46
@codecov

codecov Bot commented Jul 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 61.53%. Comparing base (922a84e) to head (d31fcd8).

Additional details and impacted files
@@            Coverage Diff            @@
##             master    #5497   +/-   ##
=========================================
  Coverage     61.53%   61.53%           
  Complexity       14       14           
=========================================
  Files             2        2           
  Lines            78       78           
  Branches          4        4           
=========================================
  Hits             48       48           
  Misses           24       24           
  Partials          6        6           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mergify

mergify Bot commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR begins implementing issue #131’s request to let users control numeric rounding in CSV exports by introducing a new decimalPlacesCount parameter on CSVExporter and adjusting tests/configuration accordingly.

Changes:

  • Added a decimalPlacesCount constructor parameter to CSVExporter and used it to round exported values in one output branch.
  • Updated TestCSVExporter’s numeric-matching regex for the “fixed decimals” export.
  • Updated a Protelis test YAML to pass interval and decimalPlacesCount to CSVExporter.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

File Description
alchemist-loading/src/main/kotlin/it/unibo/alchemist/boundary/exporters/CSVExporter.kt Adds exporter-level decimal rounding support via a new parameter and a rounding helper.
alchemist-loading/src/test/kotlin/it/unibo/alchemist/test/TestCSVExporter.kt Updates the regex used to validate decimal formatting in exported data.
alchemist-incarnation-protelis/src/test/resources/testbase.yml Exercises the new exporter parameters in a test configuration.

Comment on lines 104 to 108
when {
data.size <= 1 -> data.values.joinToString(" ")
data.size <= 1 ->
roundValues(data.values, decimalPlacesCount.toInt()).joinToString(" ")
// Labels and keys match
data.size == names.size && data.keys.containsAll(names) ->

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Nathekip, add a test with multiple columns, please, and if that it goes red, apply a fix!
Thank you, let me know

Comment thread alchemist-loading/src/test/kotlin/it/unibo/alchemist/test/TestCSVExporter.kt Outdated
@DanySK
DanySK force-pushed the master branch 20 times, most recently from 212bdd4 to 7b19587 Compare July 27, 2026 19:52
@DanySK
DanySK force-pushed the master branch 7 times, most recently from 131dcac to dc16b4f Compare July 30, 2026 11:19
@DanySK
DanySK requested a review from Copilot July 31, 2026 10:50

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Suppressed comments (3)

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

  • This test change updates the output-matching regex, but it still doesn't exercise the newly added decimalPlacesCount CSVExporter parameter (the YAML uses extractor-level precision instead). Consider adding/adjusting a test config so one exporter sets decimalPlacesCount and asserts the resulting CSV formatting, otherwise regressions in the new rounding logic may go unnoticed.
            "should have limited-length decimals" {
                val limitedDecimalsFile = simulation.csvExporters()[1].dataFile("fixed-decimals_")
                val precision2 = """(\d\d\.\d\d|0\.0*\d\d|\d\.0*\d|\d\.\d|\d\d)(e(-|\+)\d+)?"""
                val lineRegex = Regex("""^$precision2(\s($precision2))+$""")
                limitedDecimalsFile.useLines { lines ->
                    lines
                        .filterNot { it.startsWith("#") }
                        .forEach {
                            lineRegex shouldMatch it
                        }

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

  • decimalPlacesCount/decimalsNumber is not validated: passing a negative value will make String.format("%.<n>f") throw at runtime (e.g., UnknownFormatPrecisionException). Add a non-negative requirement once per call to avoid exporter crashes from misconfiguration.
    private fun roundValues(values: Collection<String>, decimalsNumber: Int): Collection<String> = values.map { raw ->
        val number = raw.toDoubleOrNull()
        if (number == null || !number.isFinite() || number % 1.0 == 0.0) {
            raw
        } else {
            String.format(Locale.US, "%.${decimalsNumber}f", number)
        }
    }

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

  • decimalPlacesCount defaults to 2, which changes CSVExporter output for existing YAML configs that don't specify this parameter (values will now be rounded by default). To avoid an implicit behavior change, consider making rounding opt-in (e.g., decimalPlacesCount: Int? = null / a separate roundValues flag / a sentinel like -1 meaning "disabled").
constructor(
    private val fileNameRoot: String = "",
    val interval: Double = DEFAULT_INTERVAL,
    val decimalPlacesCount: Int = 2,
    val exportPath: String =

@DanySK
DanySK force-pushed the master branch 5 times, most recently from 66f7274 to 87f9890 Compare July 31, 2026 21:56
@sonarqubecloud

sonarqubecloud Bot commented Aug 1, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants