Skip to content

Commit 4e91b32

Browse files
committed
fix: throw clear errors when constant instantiation is performed through YAML directly
1 parent 196f300 commit 4e91b32

1 file changed

Lines changed: 26 additions & 11 deletions

File tree

alchemist-loading/src/main/kotlin/it/unibo/alchemist/boundary/loader/SimulationModel.kt

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -382,18 +382,33 @@ internal object SimulationModel {
382382
}
383383

384384
private fun visitConstant(name: String, context: Context, root: Any?): Result<Constant<*>>? {
385-
val constant: Result<Constant<*>>? =
386-
when (root) {
387-
is Map<*, *> -> {
388-
visitDependentVariable(name, context, root)
389-
?.mapCatching { it.getWith(context.constants) }
390-
?.onFailure {
391-
logger.debug("Evaluation failed at {}, context {}:\n{}", root, context, it.message)
392-
}?.onSuccess { context.registerConstant(name, root, it) }
393-
?.map { Constant(it) }
394-
}
395-
else -> null
385+
fun unsupportedConstantInstantiation(): Nothing = throw IllegalArgumentException(
386+
"""
387+
Constant creation failed: '$name' with value '$root'.
388+
Direct constant instantiation from YAML is not supported, please consult the Alchemist YAML spec at:
389+
> https://alchemistsimulator.github.io/reference/yaml/index.html#variable
390+
Likely workaround:
391+
```yaml
392+
$name:
393+
formula: >
394+
$root
395+
```
396+
""".trimIndent(),
397+
)
398+
val constant: Result<Constant<*>>? = when (root) {
399+
is Number -> unsupportedConstantInstantiation()
400+
is String -> unsupportedConstantInstantiation()
401+
is Boolean -> unsupportedConstantInstantiation()
402+
is Map<*, *> -> {
403+
visitDependentVariable(name, context, root)
404+
?.mapCatching { it.getWith(context.constants) }
405+
?.onFailure {
406+
logger.debug("Evaluation failed at {}, context {}:\n{}", root, context, it.message)
407+
}?.onSuccess { context.registerConstant(name, root, it) }
408+
?.map { Constant(it) }
396409
}
410+
else -> null
411+
}
397412
constant?.onSuccess {
398413
logger.debug("Variable {}, evaluated from {} as constant, returned {}", name, root, it.value)
399414
check(!context.constants.containsKey(name) || context.constants[name] == it.value) {

0 commit comments

Comments
 (0)