-
Notifications
You must be signed in to change notification settings - Fork 48
fix: throw clear errors when constant instantiation is performed through YAML directly #5271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -382,18 +382,33 @@ internal object SimulationModel { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private fun visitConstant(name: String, context: Context, root: Any?): Result<Constant<*>>? { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| val constant: Result<Constant<*>>? = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| when (root) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| is Map<*, *> -> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| visitDependentVariable(name, context, root) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ?.mapCatching { it.getWith(context.constants) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ?.onFailure { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger.debug("Evaluation failed at {}, context {}:\n{}", root, context, it.message) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }?.onSuccess { context.registerConstant(name, root, it) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ?.map { Constant(it) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else -> null | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fun unsupportedConstantInstantiation(): Nothing = throw IllegalArgumentException( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Constant creation failed: '$name' with value '$root'. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Direct constant instantiation from YAML is not supported, please consult the Alchemist YAML spec at: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| > https://alchemistsimulator.github.io/reference/yaml/index.html#variable | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Likely workaround: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ```yaml | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $name: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| formula: > | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $root | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """.trimIndent(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+385
to
+397
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fun unsupportedConstantInstantiation(): Nothing = throw IllegalArgumentException( | |
| """ | |
| Constant creation failed: '$name' with value '$root'. | |
| Direct constant instantiation from YAML is not supported, please consult the Alchemist YAML spec at: | |
| > https://alchemistsimulator.github.io/reference/yaml/index.html#variable | |
| Likely workaround: | |
| ```yaml | |
| $name: | |
| formula: > | |
| $root | |
| ``` | |
| """.trimIndent(), | |
| ) | |
| fun formulaLiteral(value: Any?): String = when (value) { | |
| is String -> "\"" + value.replace("\\", "\\\\").replace("\"", "\\\"") + "\"" | |
| else -> value.toString() | |
| } | |
| fun unsupportedConstantInstantiation(): Nothing { | |
| val suggestedFormula = formulaLiteral(root) | |
| throw IllegalArgumentException( | |
| """ | |
| Constant creation failed: '$name' with value '$root'. | |
| Direct constant instantiation from YAML is not supported, please consult the Alchemist YAML spec at: | |
| > https://alchemistsimulator.github.io/reference/yaml/index.html#variable | |
| Likely workaround: | |
| ```yaml | |
| $name: | |
| formula: > | |
| $suggestedFormula | |
| ``` | |
| """.trimIndent(), | |
| ) | |
| } |
Copilot
AI
Apr 14, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change introduces a new hard failure for scalar entries under variables (Number/String/Boolean). There are existing YAML-loading regression tests for variables/constants (e.g., alchemist-loading/src/test/kotlin/it/unibo/alchemist/test/TestKtVariable.kt); please add a regression test that loads a YAML with variables: { someConst: 3 } (and/or a scalar string/boolean) and asserts that the thrown exception is the new, user-friendly one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The exception message links to
.../reference/yaml/index.html#variable, but the docs in this repo reference the variables section as/reference/yaml/#variables(e.g.,site/content/howtos/simulation/variables/_index.md:10). Please update the URL/anchor so the guidance doesn’t send users to a dead/incorrect page.