Skip to content

Commit 59f7970

Browse files
Merge pull request #6 from SciML/symbol-cast
Add default methods for interface functions
2 parents 0a313d5 + 21344da commit 59f7970

File tree

7 files changed

+108
-20
lines changed

7 files changed

+108
-20
lines changed

.github/workflows/Downstream.yml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: IntegrationTest
2+
on:
3+
push:
4+
branches: [master]
5+
tags: [v*]
6+
pull_request:
7+
8+
jobs:
9+
test:
10+
name: ${{ matrix.package.repo }}/${{ matrix.package.group }}/${{ matrix.julia-version }}
11+
runs-on: ${{ matrix.os }}
12+
env:
13+
GROUP: ${{ matrix.package.group }}
14+
strategy:
15+
fail-fast: false
16+
matrix:
17+
julia-version: [1,1.6]
18+
os: [ubuntu-latest]
19+
package:
20+
- {user: SciML, repo: RecursiveArrayTools.jl, group: All}
21+
steps:
22+
- uses: actions/checkout@v2
23+
- uses: julia-actions/setup-julia@v1
24+
with:
25+
version: ${{ matrix.julia-version }}
26+
arch: x64
27+
- uses: julia-actions/julia-buildpkg@latest
28+
- name: Clone Downstream
29+
uses: actions/checkout@v2
30+
with:
31+
repository: ${{ matrix.package.user }}/${{ matrix.package.repo }}
32+
path: downstream
33+
- name: Load this and run the downstream tests
34+
shell: julia --color=yes --project=downstream {0}
35+
run: |
36+
using Pkg
37+
try
38+
# force it to use this PR's version of the package
39+
Pkg.develop(PackageSpec(path=".")) # resolver may fail with main deps
40+
Pkg.update()
41+
Pkg.test(coverage=true) # resolver may fail with test time deps
42+
catch err
43+
err isa Pkg.Resolve.ResolverError || rethrow()
44+
# If we can't resolve that means this is incompatible by SemVer and this is fine
45+
# It means we marked this as a breaking change, so we don't need to worry about
46+
# Mistakenly introducing a breaking change, as we have intentionally made one
47+
@info "Not compatible with this release. No problem." exception=err
48+
exit(0) # Exit immediately, as a success
49+
end
50+
- uses: julia-actions/julia-processcoverage@v1
51+
- uses: codecov/codecov-action@v1
52+
with:
53+
file: lcov.info

docs/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
44

55
[compat]
66
Documenter = "0.27"
7-
SymbolicIndexingInterface = "0.1"
7+
SymbolicIndexingInterface = "0.2"

docs/src/api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Interface Functions
22

3+
Default methods cast all symbols to `Symbol` before comparing.
4+
35
```@docs
46
independent_variables
57
is_indep_sym

src/interface.jl

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,83 @@
11
"""
22
$(TYPEDSIGNATURES)
33
4-
Get the set of independent variables for the given system.
4+
Get an iterable over the independent variables for the given system. Default to an empty
5+
vector.
56
"""
67
function independent_variables end
8+
independent_variables(::Any) = []
79

810
"""
911
$(TYPEDSIGNATURES)
1012
11-
Check if the given sym is an independent variable in the given system. Defaults
12-
to `false` if not implemented for the given system/container type.
13+
Check if the given sym is an independent variable in the given system. Default to checking
14+
if the given `sym` exists in the iterable returned by `independent_variables`.
1315
"""
1416
function is_indep_sym end
1517

18+
function is_indep_sym(store, sym)
19+
any(isequal(Symbol(sym)), Symbol.(independent_variables(store)))
20+
end
21+
1622
"""
1723
$(TYPEDSIGNATURES)
1824
19-
Get the set of states for the given system.
25+
Get an iterable over the states for the given system. Default to an empty vector.
2026
"""
2127
function states end
2228

29+
states(::Any) = []
30+
2331
"""
2432
$(TYPEDSIGNATURES)
2533
26-
Find the index of the given sym in the given system.
34+
Find the index of the given sym in the given system. Default to the index of the first
35+
symbol in the iterable returned by `states` which matches the given `sym`. Return
36+
`nothing` if the given `sym` does not match.
2737
"""
2838
function state_sym_to_index end
2939

40+
function state_sym_to_index(store, sym)
41+
findfirst(isequal(Symbol(sym)), Symbol.(states(store)))
42+
end
43+
3044
"""
3145
$(TYPEDSIGNATURES)
3246
33-
Check if the given sym is a state variable in the given system. Defaults
34-
to `false` if not implemented for the given system/container type.
47+
Check if the given sym is a state variable in the given system. Default to checking if
48+
the value returned by `state_sym_to_index` is not `nothing`.
3549
"""
3650
function is_state_sym end
3751

52+
is_state_sym(store, sym) = !isnothing(state_sym_to_index(store, sym))
53+
3854
"""
3955
$(TYPEDSIGNATURES)
4056
41-
Get the set of parameters variables for the given system.
57+
Get an iterable over the parameters variables for the given system. Default to an empty
58+
vector.
4259
"""
4360
function parameters end
4461

62+
parameters(::Any) = []
63+
4564
"""
4665
$(TYPEDSIGNATURES)
4766
48-
Find the index of the given sym in the given system.
67+
Find the index of the given sym in the given system. Default to the index of the first
68+
symbol in the iterable retruned by `parameters` which matches the given `sym`. Return
69+
`nothing` if the given `sym` does not match.
4970
"""
5071
function param_sym_to_index end
5172

73+
param_sym_to_index(store, sym) = findfirst(isequal(Symbol(sym)), Symbol.(parameters(store)))
74+
5275
"""
5376
$(TYPEDSIGNATURES)
5477
55-
Check if the given sym is a parameter variable in the given system. Defaults
56-
to `false` if not implemented for the given system/container type.
78+
Check if the given sym is a parameter variable in the given system. Default
79+
to checking if the value returned by `param_sym_to_index` is not `nothing`.
5780
"""
5881
function is_param_sym end
82+
83+
is_param_sym(store, sym) = !isnothing(param_sym_to_index(store, sym))

src/symbolcache.jl

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
"""
2+
SymbolCache(syms, indepsym, paramsyms)
3+
4+
A container that simply stores a vector of all syms, indepsym and paramsyms.
5+
"""
16
struct SymbolCache{S, T, U}
27
syms::S
38
indepsym::T
@@ -6,21 +11,13 @@ end
611

712
independent_variables(sc::SymbolCache) = sc.indepsym
813
independent_variables(::SymbolCache{S, Nothing}) where {S} = []
9-
is_indep_sum(::Any, _) = false
10-
is_indep_sym(sc::SymbolCache, sym) = any(isequal(sym), sc.indepsym)
1114
is_indep_sym(::SymbolCache{S, Nothing}, _) where {S} = false
1215
states(sc::SymbolCache) = sc.syms
1316
states(::SymbolCache{Nothing}) = []
14-
state_sym_to_index(sc::SymbolCache, sym) = findfirst(isequal(sym), sc.syms)
1517
state_sym_to_index(::SymbolCache{Nothing}, _) = nothing
16-
is_state_sym(::Any, _) = false
17-
is_state_sym(sc::SymbolCache, sym) = !isnothing(state_sym_to_index(sc, sym))
1818
parameters(sc::SymbolCache) = sc.paramsyms
1919
parameters(::SymbolCache{S, T, Nothing}) where {S, T} = []
20-
param_sym_to_index(sc::SymbolCache, sym) = findfirst(isequal(sym), sc.paramsyms)
2120
param_sym_to_index(::SymbolCache{S, T, Nothing}, _) where {S, T} = nothing
22-
is_param_sym(::Any, _) = false
23-
is_param_sym(sc::SymbolCache, sym) = !isnothing(param_sym_to_index(sc, sym))
2421

2522
function Base.copy(VA::SymbolCache)
2623
typeof(VA)((VA.syms === nothing) ? nothing : copy(VA.syms),

test/default_function_test.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using SymbolicIndexingInterface, Test
2+
3+
@test independent_variables(nothing) == []
4+
@test states(nothing) == []
5+
@test parameters(nothing) == []
6+
@test !is_indep_sym(nothing, :a)
7+
@test !is_state_sym(nothing, :a)
8+
@test !is_param_sym(nothing, :a)
9+
@test isnothing(state_sym_to_index(nothing, :a))
10+
@test isnothing(param_sym_to_index(nothing, :a))

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ using SymbolicIndexingInterface
22
using Test
33

44
@time begin @time @testset begin include("symbolcache.jl") end end
5+
@time begin @time @testset begin include("default_function_test.jl") end end

0 commit comments

Comments
 (0)