Skip to content

Commit

Permalink
fix: check hasname before using getname
Browse files Browse the repository at this point in the history
  • Loading branch information
AayushSabharwal committed Dec 28, 2023
1 parent 7e0917f commit 517a7ed
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/systems/abstractsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Check warning on line 218 in src/systems/abstractsystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/abstractsystem.jl#L217-L218

Added lines #L217 - L218 were not covered by tests
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))

Check warning on line 222 in src/systems/abstractsystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/abstractsystem.jl#L222

Added line #L222 was not covered by tests
end
return nothing
end
Expand All @@ -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)
Expand All @@ -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)

Check warning on line 262 in src/systems/abstractsystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/abstractsystem.jl#L261-L262

Added lines #L261 - L262 were not covered by tests
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))

Check warning on line 266 in src/systems/abstractsystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/abstractsystem.jl#L266

Added line #L266 was not covered by tests
end
return nothing
end
Expand Down

0 comments on commit 517a7ed

Please sign in to comment.