Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link

Copilot AI Apr 14, 2026

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.

Suggested change
> https://alchemistsimulator.github.io/reference/yaml/index.html#variable
> https://alchemistsimulator.github.io/reference/yaml/#variables

Copilot uses AI. Check for mistakes.
Likely workaround:
```yaml
$name:
formula: >
$root
```
""".trimIndent(),
)
Comment on lines +385 to +397
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

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

The suggested workaround snippet uses formula: > $root, but visitDependentVariable treats String formulas as JSR-223 code (not string literals). If $root is a YAML String, following this advice will likely evaluate as an identifier and fail unless it’s quoted as a language literal. Consider tailoring the message by type (e.g., numeric/list constants can use a non-string formula, while string constants need explicit quoting guidance).

Suggested change
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 uses AI. Check for mistakes.
val constant: Result<Constant<*>>? = when (root) {
is Number -> unsupportedConstantInstantiation()
is String -> unsupportedConstantInstantiation()
is Boolean -> unsupportedConstantInstantiation()
Comment on lines +398 to +401
Copy link

Copilot AI Apr 14, 2026

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.

Copilot uses AI. Check for mistakes.
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
}
constant?.onSuccess {
logger.debug("Variable {}, evaluated from {} as constant, returned {}", name, root, it.value)
check(!context.constants.containsKey(name) || context.constants[name] == it.value) {
Expand Down
Loading