Skip to content

Commit

Permalink
Merge pull request #2801 from SciML/myb/ns
Browse files Browse the repository at this point in the history
Use NAMESPACE_SEPARATOR
  • Loading branch information
YingboMa authored Jun 12, 2024
2 parents 634c54b + c38aafa commit a6949b6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/ModelingToolkit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ using Symbolics: _parse_vars, value, @derivatives, get_variables,
NAMESPACE_SEPARATOR, set_scalar_metadata, setdefaultval,
initial_state, transition, activeState, entry, hasnode,
ticksInState, timeInState, fixpoint_sub, fast_substitute
const NAMESPACE_SEPARATOR_SYMBOL = Symbol(NAMESPACE_SEPARATOR)
import Symbolics: rename, get_variables!, _solve, hessian_sparsity,
jacobian_sparsity, isaffine, islinear, _iszero, _isone,
tosymbol, lower_varname, diff2term, var_from_nested_derivative,
Expand Down
12 changes: 6 additions & 6 deletions src/inputoutput.jl
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,17 @@ function same_or_inner_namespace(u, var)
nv = get_namespace(var)
nu == nv || # namespaces are the same
startswith(nv, nu) || # or nv starts with nu, i.e., nv is an inner namespace to nu
occursin('', string(getname(var))) &&
!occursin('', string(getname(u))) # or u is top level but var is internal
occursin(NAMESPACE_SEPARATOR, string(getname(var))) &&
!occursin(NAMESPACE_SEPARATOR, string(getname(u))) # or u is top level but var is internal
end

function inner_namespace(u, var)
nu = get_namespace(u)
nv = get_namespace(var)
nu == nv && return false
startswith(nv, nu) || # or nv starts with nu, i.e., nv is an inner namespace to nu
occursin('', string(getname(var))) &&
!occursin('', string(getname(u))) # or u is top level but var is internal
occursin(NAMESPACE_SEPARATOR, string(getname(var))) &&
!occursin(NAMESPACE_SEPARATOR, string(getname(u))) # or u is top level but var is internal
end

"""
Expand All @@ -139,11 +139,11 @@ Return the namespace of a variable as a string. If the variable is not namespace
"""
function get_namespace(x)
sname = string(getname(x))
parts = split(sname, '')
parts = split(sname, NAMESPACE_SEPARATOR)
if length(parts) == 1
return ""
end
join(parts[1:(end - 1)], '')
join(parts[1:(end - 1)], NAMESPACE_SEPARATOR)
end

"""
Expand Down
24 changes: 14 additions & 10 deletions src/systems/abstractsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,9 @@ function SymbolicIndexingInterface.is_variable(sys::AbstractSystem, sym::Symbol)
return is_variable(ic, sym)
end
return any(isequal(sym), getname.(variable_symbols(sys))) ||
count('', string(sym)) == 1 &&
count(isequal(sym), Symbol.(nameof(sys), :₊, getname.(variable_symbols(sys)))) ==
count(NAMESPACE_SEPARATOR, string(sym)) == 1 &&
count(isequal(sym),
Symbol.(nameof(sys), NAMESPACE_SEPARATOR_SYMBOL, getname.(variable_symbols(sys)))) ==
1
end

Expand Down Expand Up @@ -399,9 +400,10 @@ function SymbolicIndexingInterface.variable_index(sys::AbstractSystem, sym::Symb
idx = findfirst(isequal(sym), getname.(variable_symbols(sys)))
if idx !== nothing
return idx
elseif count('', string(sym)) == 1
elseif count(NAMESPACE_SEPARATOR, string(sym)) == 1
return findfirst(isequal(sym),
Symbol.(nameof(sys), :₊, getname.(variable_symbols(sys))))
Symbol.(
nameof(sys), NAMESPACE_SEPARATOR_SYMBOL, getname.(variable_symbols(sys))))
end
return nothing
end
Expand Down Expand Up @@ -431,9 +433,10 @@ function SymbolicIndexingInterface.is_parameter(sys::AbstractSystem, sym::Symbol
return is_parameter(ic, sym)
end
return any(isequal(sym), getname.(parameter_symbols(sys))) ||
count('', string(sym)) == 1 &&
count(NAMESPACE_SEPARATOR, string(sym)) == 1 &&
count(isequal(sym),
Symbol.(nameof(sys), :₊, getname.(parameter_symbols(sys)))) == 1
Symbol.(nameof(sys), NAMESPACE_SEPARATOR_SYMBOL, getname.(parameter_symbols(sys)))) ==
1
end

function SymbolicIndexingInterface.parameter_index(sys::AbstractSystem, sym)
Expand Down Expand Up @@ -466,9 +469,10 @@ function SymbolicIndexingInterface.parameter_index(sys::AbstractSystem, sym::Sym
idx = findfirst(isequal(sym), getname.(parameter_symbols(sys)))
if idx !== nothing
return idx
elseif count('', string(sym)) == 1
elseif count(NAMESPACE_SEPARATOR, string(sym)) == 1
return findfirst(isequal(sym),
Symbol.(nameof(sys), :₊, getname.(parameter_symbols(sys))))
Symbol.(
nameof(sys), NAMESPACE_SEPARATOR_SYMBOL, getname.(parameter_symbols(sys))))
end
return nothing
end
Expand Down Expand Up @@ -889,7 +893,7 @@ function renamespace(sys, x)
elseif x isa AbstractSystem
rename(x, renamespace(sys, nameof(x)))
else
Symbol(getname(sys), :₊, x)
Symbol(getname(sys), NAMESPACE_SEPARATOR_SYMBOL, x)
end
end

Expand Down Expand Up @@ -1248,7 +1252,7 @@ function round_trip_eq(eq::Equation, var2name)
syss = get_systems(eq.rhs)
call = Expr(:call, connect)
for sys in syss
strs = split(string(nameof(sys)), "")
strs = split(string(nameof(sys)), NAMESPACE_SEPARATOR)
s = Symbol(strs[1])
for st in strs[2:end]
s = Expr(:., s, Meta.quot(Symbol(st)))
Expand Down
2 changes: 1 addition & 1 deletion src/systems/connectors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ function generate_isouter(sys::AbstractSystem)
function isouter(sys)::Bool
s = string(nameof(sys))
isconnector(sys) || error("$s is not a connector!")
idx = findfirst(isequal(''), s)
idx = findfirst(isequal(NAMESPACE_SEPARATOR), s)
parent_name = Symbol(idx === nothing ? s : s[1:prevind(s, idx)])
parent_name in outer_connectors
end
Expand Down

0 comments on commit a6949b6

Please sign in to comment.