diff --git a/src/macros.jl b/src/macros.jl index 6ad5149f..2bff4cdd 100644 --- a/src/macros.jl +++ b/src/macros.jl @@ -251,9 +251,11 @@ function parse_system(exprs) constraints = Vector{Expr}() # define defaults for state, noise and input symbol and default dimension - state_var = :x - input_var = :u - noise_var = :w + default_input_var = :u + default_noise_var = :w + state_var = nothing + input_var = nothing + noise_var = nothing dimension = nothing initial_state = nothing # for initial-value problems @@ -323,6 +325,26 @@ function parse_system(exprs) nsets = length(constraints) nsets > 3 && throw(ArgumentError("cannot parse $nsets set constraints")) + # error handling for variable names + state_var == nothing && throw(ArgumentError("the state variable was not found")) + got_input_var = input_var != nothing + got_noise_var = noise_var != nothing + if got_input_var && (state_var == input_var) + throw(ArgumentError("state and input variables have the same name `$(state_var)`")) + elseif got_noise_var && (state_var == noise_var) + throw(ArgumentError("state and noise variables have the same name `$(state_var)`")) + elseif got_input_var && got_noise_var && (input_var == noise_var) + throw(ArgumentError("input and noise variables have the same name `$(input_var)`")) + end + + # assign default values + if !got_input_var + input_var = default_input_var + end + if !got_noise_var + noise_var = default_noise_var + end + return dynamic_equation, AT, constraints, state_var, input_var, noise_var, dimension, initial_state end diff --git a/test/@system.jl b/test/@system.jl index 6f85cd3a..7b133a64 100644 --- a/test/@system.jl +++ b/test/@system.jl @@ -234,6 +234,9 @@ end sys = @system(x⁺ = Ax + Bu + c + Dw, x ∈ X, u ∈ U1, w ∈ W1, w:noise, u:input) @test sys == NoisyConstrainedAffineControlDiscreteSystem(A, B, c, D, X, U1, W1) + + # check error handling for repeated variable names + @test_throws ArgumentError @system(x⁺ = Ax + Bw + Dw, x ∈ X, w ∈ U1, w ∈ W1, noise=w, input=w) end @testset "@system for black-box discrete systems" begin