From 517a7edd123f9aa78c366b519e8eefb679a91af8 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 28 Dec 2023 22:51:56 +0530 Subject: [PATCH] fix: check hasname before using getname --- src/systems/abstractsystem.jl | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 7406a5746f..a52469c587 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -191,10 +191,15 @@ function SymbolicIndexingInterface.is_variable(sys::AbstractSystem, sym) hasname(sym) && is_variable(sys, getname(sym)) end +function _syms_as_symbols(syms) + return [hasname(sym) ? getname(sym) : Symbol(sym) for sym in syms] +end + function SymbolicIndexingInterface.is_variable(sys::AbstractSystem, sym::Symbol) - return any(isequal(sym), getname.(variable_symbols(sys))) || + vars = _syms_as_symbols(variable_symbols(sys)) + return any(isequal(sym), vars) || count('₊', string(sym)) == 1 && - count(isequal(sym), Symbol.(sys.name, :₊, getname.(variable_symbols(sys)))) == 1 + count(isequal(sym), Symbol.(sys.name, :₊, vars)) == 1 end function SymbolicIndexingInterface.variable_index(sys::AbstractSystem, sym) @@ -209,11 +214,12 @@ function SymbolicIndexingInterface.variable_index(sys::AbstractSystem, sym) end function SymbolicIndexingInterface.variable_index(sys::AbstractSystem, sym::Symbol) - idx = findfirst(isequal(sym), getname.(variable_symbols(sys))) + vars = _syms_as_symbols(variable_symbols(sys)) + idx = findfirst(isequal(sym), vars) if idx !== nothing return idx elseif count('₊', string(sym)) == 1 - return findfirst(isequal(sym), Symbol.(sys.name, :₊, getname.(variable_symbols(sys)))) + return findfirst(isequal(sym), Symbol.(sys.name, :₊, vars)) end return nothing end @@ -234,9 +240,10 @@ function SymbolicIndexingInterface.is_parameter(sys::AbstractSystem, sym) end function SymbolicIndexingInterface.is_parameter(sys::AbstractSystem, sym::Symbol) - return any(isequal(sym), getname.(parameter_symbols(sys))) || + vars = _syms_as_symbols(parameter_symbols(sys)) + return any(isequal(sym), vars) || count('₊', string(sym)) == 1 && - count(isequal(sym), Symbol.(sys.name, :₊, getname.(parameter_symbols(sys)))) == 1 + count(isequal(sym), Symbol.(sys.name, :₊, vars)) == 1 end function SymbolicIndexingInterface.parameter_index(sys::AbstractSystem, sym) @@ -251,11 +258,12 @@ function SymbolicIndexingInterface.parameter_index(sys::AbstractSystem, sym) end function SymbolicIndexingInterface.parameter_index(sys::AbstractSystem, sym::Symbol) - idx = findfirst(isequal(sym), getname.(parameter_symbols(sys))) + vars = _syms_as_symbols(parameter_symbols(sys)) + idx = findfirst(isequal(sym), vars) if idx !== nothing return idx elseif count('₊', string(sym)) == 1 - return findfirst(isequal(sym), Symbol.(sys.name, :₊, getname.(parameter_symbols(sys)))) + return findfirst(isequal(sym), Symbol.(sys.name, :₊, vars)) end return nothing end