Skip to content

Commit 6d31f4b

Browse files
authored
Change namespacing implementation (#1183)
1 parent 8d5661f commit 6d31f4b

File tree

4 files changed

+13
-34
lines changed

4 files changed

+13
-34
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ModelingToolkit"
22
uuid = "961ee093-0014-501f-94e3-6117800e7a78"
33
authors = ["Chris Rackauckas <[email protected]>"]
4-
version = "6.0.0"
4+
version = "6.1.0"
55

66
[deps]
77
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
@@ -73,7 +73,7 @@ Setfield = "0.7"
7373
SpecialFunctions = "0.7, 0.8, 0.9, 0.10, 1.0"
7474
StaticArrays = "0.10, 0.11, 0.12, 1.0"
7575
SymbolicUtils = "0.12, 0.13"
76-
Symbolics = "2.0"
76+
Symbolics = "3.0"
7777
UnPack = "0.1, 1.0"
7878
Unitful = "1.1"
7979
julia = "1.2"

src/systems/abstractsystem.jl

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,6 @@ Setfield.get(obj::AbstractSystem, ::Setfield.PropertyLens{field}) where {field}
216216
end
217217

218218
rename(x::AbstractSystem, name) = @set x.name = name
219-
function rename(xx::Symbolics.ArrayOp, name)
220-
@set! xx.expr.f.arguments[1] = rename(xx.expr.f.arguments[1], name)
221-
@set! xx.term.arguments[2] = rename(xx.term.arguments[2], name)
222-
end
223219

224220
function Base.propertynames(sys::AbstractSystem; private=false)
225221
if private
@@ -324,37 +320,20 @@ GlobalScope(sym::Union{Num, Symbolic}) = setmetadata(sym, SymScope, GlobalScope(
324320

325321
renamespace(sys, eq::Equation) = namespace_equation(eq, sys)
326322

327-
function _renamespace(sys, x)
328-
v = unwrap(x)
329-
330-
if istree(v) && symtype(operation(v)) <: FnType
331-
ov = metadata(operation(v), metadata(v))
332-
return similarterm(v, renamespace(sys, ov), arguments(v), symtype(v), metadata=metadata(v))
333-
end
334-
335-
if v isa Namespace
336-
sysp, v = v.parent, v.named
337-
sysn = Symbol(getname(sys), :., getname(sysp))
338-
sys = sys isa AbstractSystem ? rename(sysp, sysn) : sysn
339-
end
340-
341-
Namespace(sys, v)
342-
end
343-
344323
function renamespace(sys, x)
345324
x = unwrap(x)
346325
if x isa Symbolic
347326
let scope = getmetadata(x, SymScope, LocalScope())
348327
if scope isa LocalScope
349-
_renamespace(sys, x)
328+
rename(x, renamespace(getname(sys), getname(x)))
350329
elseif scope isa ParentScope
351330
setmetadata(x, SymScope, scope.parent)
352331
else # GlobalScope
353332
x
354333
end
355334
end
356335
else
357-
Symbol(getname(sys), :., x)
336+
Symbol(getname(sys), :, x)
358337
end
359338
end
360339

test/serialization.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ io = IOBuffer()
2525
write(io, rc_model)
2626
str = String(take!(io))
2727
sys = include_string(@__MODULE__, str)
28-
@test_broken sys == flatten(rc_model) # this actually kind of works, but the variables would have different identities.
28+
@test sys == flatten(rc_model) # this actually kind of works, but the variables would have different identities.

test/variable_scope.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@ eqs = [
2727

2828
names = ModelingToolkit.getname.(states(sys))
2929
@test :d in names
30-
@test Symbol("sub1.c") in names
31-
@test Symbol("sub1.sub2.b") in names
32-
@test Symbol("sub1.sub2.sub3.a") in names
33-
@test Symbol("sub1.sub2.sub4.a") in names
30+
@test Symbol("sub1c") in names
31+
@test Symbol("sub1sub2b") in names
32+
@test Symbol("sub1sub2sub3a") in names
33+
@test Symbol("sub1sub2sub4a") in names
3434

3535
@named foo = NonlinearSystem(eqs, [a, b, c, d], [])
3636
@named bar = NonlinearSystem(eqs, [a, b, c, d], [])
37-
@test ModelingToolkit.getname(ModelingToolkit.namespace_expr(ModelingToolkit.namespace_expr(b, foo), bar)) == Symbol("bar.b")
37+
@test ModelingToolkit.getname(ModelingToolkit.namespace_expr(ModelingToolkit.namespace_expr(b, foo), bar)) == Symbol("barb")
3838

3939
renamed(nss, sym) = ModelingToolkit.getname(foldr(ModelingToolkit.renamespace, nss, init=sym))
4040

41-
@test renamed([:foo :bar :baz], a) == Symbol("foo.bar.baz.a")
42-
@test renamed([:foo :bar :baz], b) == Symbol("foo.bar.b")
43-
@test renamed([:foo :bar :baz], c) == Symbol("foo.c")
41+
@test renamed([:foo :bar :baz], a) == Symbol("foobarbaza")
42+
@test renamed([:foo :bar :baz], b) == Symbol("foobarb")
43+
@test renamed([:foo :bar :baz], c) == Symbol("fooc")
4444
@test renamed([:foo :bar :baz], d) == :d

0 commit comments

Comments
 (0)