Skip to content

Commit be40fb0

Browse files
authored
Fix dispatch for remove_all_from_space! (#1105)
* Add type restriction * Add test * Add fallback * Update docs
1 parent 24bd774 commit be40fb0

File tree

5 files changed

+47
-2
lines changed

5 files changed

+47
-2
lines changed

docs/src/devdocs.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ In principle, the following should be done:
6161
6. Extend `nearby_ids(pos, model, r)`.
6262
7. Create a new "minimal" agent type to be used with [`@agent`](@ref) (see the source code of [`GraphAgent`](@ref) for an example).
6363

64-
And that's it! Every function of the main API will now work. In some situations you might want to explicitly extend other functions such as `move_agent!` for performance reasons.
64+
And that's it! Every function of the main API will now work. In some situations you might want to explicitly extend other functions such as `move_agent!` or `remove_all_from_space!` for performance reasons.
6565

6666
### Visualization of a custom space
6767

src/core/model_standard.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,12 @@ function remove_all_from_model!(model::StandardABM)
175175
empty!(agent_container(model))
176176
end
177177

178+
function remove_all_from_space!(model)
179+
for a in allagents(model)
180+
remove_agent_from_space!(a, model)
181+
end
182+
end
183+
178184

179185
"""
180186
dummystep(model)

src/spaces/discrete.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ function swap_agents!(agent1, agent2, model::ABM{<:DiscreteSpace})
301301
return nothing
302302
end
303303

304-
function remove_all_from_space!(model)
304+
function remove_all_from_space!(model::ABM{<:DiscreteSpace})
305305
for p in positions(model)
306306
empty!(ids_in_position(p, model))
307307
end

test/new_space_tests.jl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using Test, Agents
2+
3+
@testset "create new space" begin
4+
struct DummySpace <: Agents.AbstractSpace
5+
ids::Set{Int}
6+
end
7+
8+
DummySpace() = DummySpace(Set{Int}())
9+
10+
Agents.random_position(::ABM{<:DummySpace}) = nothing
11+
12+
function Agents.add_agent_to_space!(agent::Agents.AbstractAgent, model::ABM{<:DummySpace})
13+
space = Agents.abmspace(model)
14+
push!(space.ids, agent.id)
15+
return agent
16+
end
17+
18+
function Agents.remove_agent_from_space!(agent::Agents.AbstractAgent, model::ABM{<:DummySpace})
19+
space = Agents.abmspace(model)
20+
delete!(space.ids, agent.id)
21+
return agent
22+
end
23+
24+
25+
@agent struct DummyAgent(NoSpaceAgent)
26+
pos::Nothing
27+
end
28+
29+
model = StandardABM(DummyAgent, DummySpace(); agent_step! = (a, m) -> a)
30+
31+
for i in 1:3
32+
add_agent!(DummyAgent, model)
33+
@test nagents(model) == i
34+
end
35+
36+
remove_all!(model)
37+
@test iszero(nagents(model))
38+
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,5 @@ end
169169
include("csv_tests.jl")
170170
include("jld2_tests.jl")
171171
include("visualization_tests.jl")
172+
include("new_space_tests.jl")
172173
end

0 commit comments

Comments
 (0)