Skip to content

Commit b4f95c7

Browse files
committed
improve handling of same nested namespaces
previously, have a system :load with a subsystem :load could lead to problems
1 parent b6b7533 commit b4f95c7

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

ext/MTKExt.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ For a given system and name, extract all the relevant meta we want to keep for t
189189
function _get_metadata(sys, name)
190190
nt = (;)
191191
sym = try
192-
getproperty_symbolic(sys, name)
192+
getproperty_symbolic(sys, name; might_contain_toplevel_ns=false)
193193
catch e
194194
if !endswith(string(name), "ˍt") # known for "internal" derivatives
195195
@warn "Could not extract metadata for $name $(e.msg)"

ext/MTKUtils.jl

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,24 @@ function _collect_differentials!(found, ex)
9393
end
9494

9595
"""
96-
getproperty_symbolic(sys, var)
96+
getproperty_symbolic(sys, var; might_contain_toplevel_ns=true)
9797
9898
Like `getproperty` but works on a greater varaity of "var"
9999
- var can be Num or Symbolic (resolved using genname)
100-
- strip namespace of sys if present
100+
- strip namespace of sys if present (don't strip if `might_contain_top_level_ns=false`)
101101
- for nested variables (foo₊bar₊baz) resolve them one by one
102102
"""
103-
function getproperty_symbolic(sys, var)
103+
function getproperty_symbolic(sys, var; might_contain_toplevel_ns=true)
104104
ns = string(getname(sys))
105105
varname = string(getname(var))
106-
varname_nons = replace(varname, r"^"*ns*"" => "")
107-
parts = split(varname_nons, "")
106+
# split of the toplevel namespace if necessary
107+
if might_contain_toplevel_ns && startswith(varname, ns*"")
108+
if getname(sys) getname.(ModelingToolkit.get_systems(sys))
109+
@warn "Namespace :$ns appears multiple times, this might lead to unexpected, since it is not clear whether the namespace should be stripped or not."
110+
end
111+
varname = replace(varname, r"^"*ns*"" => "")
112+
end
113+
parts = split(varname, "")
108114
r = getproperty(sys, Symbol(parts[1]); namespace=false)
109115
for part in parts[2:end]
110116
r = getproperty(r, Symbol(part); namespace=true)

0 commit comments

Comments
 (0)