Skip to content

Commit

Permalink
Merge pull request #90 from SciML/as/symcontainer-recursive
Browse files Browse the repository at this point in the history
fix: fix stack overflow for methods using `symbolic_container` to define a fallback
  • Loading branch information
AayushSabharwal authored Jul 31, 2024
2 parents a5be447 + 463e750 commit 759f5a1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
5 changes: 4 additions & 1 deletion docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
### Mandatory methods

```@docs
symbolic_container
is_variable
variable_index
variable_symbols
Expand All @@ -26,6 +25,10 @@ allvariables

### Optional Methods

```@docs
symbolic_container
```

#### Observed equation handling

```@docs
Expand Down
22 changes: 16 additions & 6 deletions src/index_provider_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ Using `indp`, return an object that implements the index provider interface. In
itself implements the interface, `indp` can be returned as-is. All index provider interface
methods fall back to calling the same method on `symbolic_container(indp)`, so this may be
used for trivial implementations of the interface that forward all calls to another object.
Note that this method is optional. Thus the correct method to check for a fallback is:
```julia
hasmethod(symbolic_container, Tuple{typeof(indp)}) && symbolic_container(indp) != indp
```
"""
function symbolic_container end

Expand Down Expand Up @@ -59,8 +64,9 @@ parameter_index(indp, sym) = parameter_index(symbolic_container(indp), sym)
Check whether the given `sym` is a timeseries parameter in `indp`.
"""
function is_timeseries_parameter(indp, sym)
if hasmethod(symbolic_container, Tuple{typeof(indp)})
is_timeseries_parameter(symbolic_container(indp), sym)
if hasmethod(symbolic_container, Tuple{typeof(indp)}) &&
(sc = symbolic_container(indp)) != indp
is_timeseries_parameter(sc, sym)
else
return false
end
Expand Down Expand Up @@ -91,7 +97,8 @@ parameter in `indp`. Defaults to returning `nothing`. Respects the
[`symbolic_container`](@ref) fallback for `indp` if present.
"""
function timeseries_parameter_index(indp, sym)
if hasmethod(symbolic_container, Tuple{typeof(indp)})
if hasmethod(symbolic_container, Tuple{typeof(indp)}) &&
(sc = symbolic_container(indp)) != indp
timeseries_parameter_index(symbolic_container(indp), sym)
else
return nothing
Expand All @@ -111,7 +118,8 @@ By default, this function returns `nothing`, indicating that the index provider
support generating parameter observed functions.
"""
function parameter_observed(indp, sym)
if hasmethod(symbolic_container, Tuple{typeof(indp)})
if hasmethod(symbolic_container, Tuple{typeof(indp)}) &&
(sc = symbolic_container(indp)) != indp
return parameter_observed(symbolic_container(indp), sym)
else
return nothing
Expand Down Expand Up @@ -143,7 +151,8 @@ variable.
By default, this function returns `Set([ContinuousTimeseries()])`.
"""
function get_all_timeseries_indexes(indp, sym)
if hasmethod(symbolic_container, Tuple{typeof(indp)})
if hasmethod(symbolic_container, Tuple{typeof(indp)}) &&
(sc = symbolic_container(indp)) != indp
return get_all_timeseries_indexes(symbolic_container(indp), sym)
else
return Set([ContinuousTimeseries()])
Expand Down Expand Up @@ -240,7 +249,8 @@ Return a dictionary mapping symbols in the index provider to their default value
This includes parameter symbols. The dictionary must be mutable.
"""
function default_values(indp)
if hasmethod(symbolic_container, Tuple{typeof(indp)})
if hasmethod(symbolic_container, Tuple{typeof(indp)}) &&
(sc = symbolic_container(indp)) != indp
default_values(symbolic_container(indp))
else
Dict()
Expand Down

0 comments on commit 759f5a1

Please sign in to comment.