Skip to content

Commit

Permalink
feat: include all of variable metadata in the Model.structure
Browse files Browse the repository at this point in the history
  • Loading branch information
ven-k committed Jul 5, 2023
1 parent a1a02a2 commit a661f56
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
35 changes: 30 additions & 5 deletions src/systems/model_parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ function connector_macro(mod, name, body)
end

function parse_variable_def!(dict, mod, arg, varclass, kwargs, def = nothing)
metatypes = [(:connection_type, VariableConnectType),

Check warning on line 63 in src/systems/model_parsing.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L63

Added line #L63 was not covered by tests
(:description, VariableDescription),
(:unit, VariableUnit),
(:bounds, VariableBounds),
(:noise, VariableNoiseType),
(:input, VariableInput),
(:output, VariableOutput),
(:irreducible, VariableIrreducible),
(:state_priority, VariableStatePriority),
(:misc, VariableMisc),
(:disturbance, VariableDisturbance),
(:tunable, VariableTunable),
(:dist, VariableDistribution),
(:binary, VariableBinary),
(:integer, VariableInteger)]

arg isa LineNumberNode && return
MLStyle.@match arg begin
a::Symbol => begin
Expand All @@ -79,9 +95,12 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, def = nothing)
def, meta = parse_default(mod, b)
var, _ = parse_variable_def!(dict, mod, a, varclass, kwargs, def)
dict[varclass][getname(var)][:default] = def
if !isnothing(meta)
if (ct = get(meta, VariableConnectType, nothing)) !== nothing
dict[varclass][getname(var)][:connection_type] = nameof(ct)
if meta !== nothing
for (type, key) in metatypes
if (mt = get(meta, key, nothing)) !== nothing
key == VariableConnectType && (mt = nameof(mt))
dict[varclass][getname(var)][type] = mt

Check warning on line 102 in src/systems/model_parsing.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L98-L102

Added lines #L98 - L102 were not covered by tests
end
end
var = set_var_metadata(var, meta)
end
Expand All @@ -90,8 +109,14 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, def = nothing)
Expr(:tuple, a, b) => begin
var, def = parse_variable_def!(dict, mod, a, varclass, kwargs)
meta = parse_metadata(mod, b)
if (ct = get(meta, VariableConnectType, nothing)) !== nothing
dict[varclass][getname(var)][:connection_type] = nameof(ct)
if meta !== nothing
for (type, key) in metatypes
if (mt = get(meta, key, nothing)) !== nothing
key == VariableConnectType && (mt = nameof(mt))
dict[varclass][getname(var)][type] = mt

Check warning on line 116 in src/systems/model_parsing.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L112-L116

Added lines #L112 - L116 were not covered by tests
end
end
var = set_var_metadata(var, meta)

Check warning on line 119 in src/systems/model_parsing.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L118-L119

Added lines #L118 - L119 were not covered by tests
end
(set_var_metadata(var, meta), def)
end
Expand Down
14 changes: 14 additions & 0 deletions test/model_parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,17 @@ getdefault(a.b.k) == 1
getdefault(a.b.i) == 20
getdefault(a.b.j) == 30
getdefault(a.b.k) == 40

metadata = Dict(:description => "Variable to test metadata in the Model.structure",
:input => true, :bounds => :((-1, 1)), :connection_type => :Flow, :integer => true,
:binary => false, :tunable => false, :disturbance => true, :dist => :(Normal(1, 1)))

@connector MockMeta begin
m(t), [description = "Variable to test metadata in the Model.structure",
input = true, bounds = (-1, 1), connect = Flow, integer = true,
binary = false, tunable = false, disturbance = true, dist = Normal(1, 1)]
end

for (k, v) in metadata
@test MockMeta.structure[:variables][:m][k] == v
end

0 comments on commit a661f56

Please sign in to comment.