Skip to content
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

Error handling for repeated variable names #164

Merged
merged 7 commits into from
Feb 21, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
34 changes: 29 additions & 5 deletions src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,12 @@ 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_state_var = :x
default_input_var = :u
default_noise_var = :w
state_var = nothing
input_var = nothing
noise_var = nothing
dimension = nothing

# main loop to parse the subexpressions in exprs
Expand Down Expand Up @@ -313,8 +316,29 @@ function parse_system(exprs)
nsets = length(constraints)
nsets > 3 && throw(ArgumentError("cannot parse $nsets set constraints"))

return dynamic_equation, AT, constraints,
state_var, input_var, noise_var, dimension
# error handling for variable names
got_state_var = state_var != nothing
got_input_var = input_var != nothing
got_noise_var = noise_var != nothing
if (got_state_var && got_input_var) && (state_var == input_var)
mforets marked this conversation as resolved.
Show resolved Hide resolved
throw(ArgumentError("state and input variables have the same name `$(state_var)`"))
elseif (got_state_var && got_noise_var) && (state_var == noise_var)
mforets marked this conversation as resolved.
Show resolved Hide resolved
throw(ArgumentError("state and noise variables have the same name `$(state_var)`"))
elseif (got_input_var && got_noise_var) && (input_var == noise_var)
mforets marked this conversation as resolved.
Show resolved Hide resolved
throw(ArgumentError("input and noise variables have the same name `$(input_var)`"))
end

# assign default values
if !got_state_var
state_var = default_state_var
end
if !got_input_var
input_var = default_input_var
end
if !got_noise_var
noise_var = default_noise_var
end
schillic marked this conversation as resolved.
Show resolved Hide resolved
return dynamic_equation, AT, constraints, state_var, input_var, noise_var, dimension
end

# extract the field and value parameter from the dynamic equation `equation`
Expand Down
3 changes: 3 additions & 0 deletions test/@system.jl
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,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
Expand Down