Skip to content

Commit d88131c

Browse files
github-actions[bot]TortarDatserisCompatHelper Julia
authored
CompatHelper: bump compat for LightSumTypes to 5, (keep existing compat) (#1103)
Co-authored-by: Adriano Meligrana <[email protected]> Co-authored-by: George Datseris <[email protected]> Co-authored-by: CompatHelper Julia <[email protected]>
1 parent a31f778 commit d88131c

File tree

5 files changed

+59
-205
lines changed

5 files changed

+59
-205
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Graphs = "1.4"
5353
JLD2 = "0.4, 0.5"
5454
LazyArtifacts = "1.3"
5555
LightOSM = "0.2, 0.3"
56-
LightSumTypes = "4"
56+
LightSumTypes = "5"
5757
LinearAlgebra = "1"
5858
MacroTools = "0.5"
5959
Makie = "0.20, 0.21"

src/deprecations.jl

Lines changed: 0 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -391,97 +391,3 @@ function nearby_agents_exact(a, model, r=1)
391391
@warn "`nearby_agents_exact` is deprecated in favor of `nearby_agents(...; search=:exact)`." maxlog=1
392392
return (model[id] for id in nearby_ids(a, model, r; search=:exact))
393393
end
394-
395-
export @dispatch, @finalize_dispatch, kindof, allkinds
396-
export LightSumTypes
397-
using LightSumTypes: allkinds
398-
399-
macro multiagent(version, struct_repr)
400-
expr = _multiagent(version, struct_repr)
401-
return esc(expr)
402-
end
403-
404-
function _multiagent(version, struct_repr)
405-
new_type, base_type_spec, abstract_type, agent_specs = decompose_struct_base(struct_repr)
406-
base_fields = compute_base_fields(base_type_spec)
407-
agent_specs_with_base = []
408-
for a_spec in agent_specs
409-
@capture(a_spec, @subagent astruct_spec_)
410-
int_type, new_fields = decompose_struct(astruct_spec)
411-
push!(agent_specs_with_base,
412-
:(@kwdef mutable struct $int_type
413-
$(base_fields...)
414-
$(new_fields...)
415-
end))
416-
end
417-
t = :($new_type <: $abstract_type)
418-
c = @capture(new_type, new_type_n_{new_params__})
419-
if c == false
420-
new_type_n = new_type
421-
new_params = []
422-
end
423-
new_params_no_constr = [p isa Expr && p.head == :(<:) ? p.args[1] : p for p in new_params]
424-
new_type_no_constr = :($new_type_n{$(new_params_no_constr...)})
425-
a_specs = :(begin $(agent_specs_with_base...) end)
426-
if version == QuoteNode(:opt_speed)
427-
expr = quote
428-
LightSumTypes.@sum_structs :on_fields $t $a_specs
429-
Agents.ismultiagentcompacttype(::Type{$(namify(new_type))}) = true
430-
LightSumTypes.export_variants($(namify(t)))
431-
end
432-
elseif version == QuoteNode(:opt_memory)
433-
expr = quote
434-
LightSumTypes.@sum_structs :on_types $t $a_specs
435-
Agents.ismultiagentsumtype(::Type{$(namify(new_type))}) = true
436-
LightSumTypes.export_variants($(namify(t)))
437-
end
438-
else
439-
error("The version of @multiagent chosen was not recognized, use either `:opt_speed` or `:opt_memory` instead.")
440-
end
441-
442-
expr_multiagent = :(Agents.ismultiagenttype(::Type{$(namify(new_type))}) = true)
443-
if new_params != []
444-
if version == QuoteNode(:opt_speed)
445-
expr_multiagent_p = quote
446-
Agents.ismultiagenttype(::Type{$(new_type_no_constr)}) where {$(new_params...)} = true
447-
Agents.ismultiagentcompacttype(::Type{$(new_type_no_constr)}) where {$(new_params...)} = true
448-
end
449-
else
450-
expr_multiagent_p = quote
451-
Agents.ismultiagenttype(::Type{$(new_type_no_constr)}) where {$(new_params...)} = true
452-
Agents.ismultiagentsumtype(::Type{$(new_type_no_constr)}) where {$(new_params...)} = true
453-
end
454-
end
455-
else
456-
expr_multiagent_p = :()
457-
end
458-
459-
expr = quote
460-
$expr
461-
$expr_multiagent
462-
$expr_multiagent_p
463-
nothing
464-
end
465-
466-
return expr
467-
end
468-
469-
ismultiagenttype(::Type) = false
470-
ismultiagentsumtype(::Type) = false
471-
ismultiagentcompacttype(::Type) = false
472-
473-
function LightSumTypes.kindof(a::AbstractAgent)
474-
throw(ArgumentError("Agent of type $(typeof(a)) has not been created via `@multiagent`."))
475-
end
476-
477-
function LightSumTypes.allkinds(a::Type{<:AbstractAgent})
478-
(nameof(a), ) # this function is extended automatically in the macro
479-
end
480-
481-
macro dispatch(f_def)
482-
return esc(:(LightSumTypes.@pattern $f_def))
483-
end
484-
485-
macro finalize_dispatch()
486-
return esc(:(LightSumTypes.@finalize_patterns))
487-
end

src/precompile.jl

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,5 @@ using PrecompileTools
2727
c::Int16
2828
end)
2929
_agent(expr)
30-
expr = :(struct A(NoSpaceAgent) <: AbstractA
31-
@subagent struct B
32-
x::Int
33-
end
34-
@subagent struct C
35-
y::Int
36-
end
37-
end)
38-
_multiagent(QuoteNode(:opt_memory), expr)
39-
_multiagent(QuoteNode(:opt_speed), expr)
40-
expr = :(struct A{X}(GridAgent{2})
41-
@subagent struct B
42-
x::Bool = false
43-
y::Int
44-
end
45-
@subagent struct C{X<:Real}
46-
y::Int
47-
z::X
48-
end
49-
end)
50-
_multiagent(QuoteNode(:opt_memory), expr)
51-
_multiagent(QuoteNode(:opt_speed), expr)
5230
end
5331
end

src/spaces/nothing.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ otherwise they will try to add `nothing` to the agent position.
77

88
# We need to extend this one, because otherwise there is a `pos` that
99
# is attempted to be given to the agent creation...
10-
function add_agent!(A::Type, model::ABM{Nothing}, args::Vararg{Any, N}; kwargs...) where {N}
10+
function add_agent!(A::Union{Function, Type}, model::ABM{Nothing}, args::Vararg{Any, N}; kwargs...) where {N}
1111
id = nextid(model)
1212
if !isempty(args)
1313
newagent = A(id, args...)

test/api_tests.jl

Lines changed: 57 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -350,75 +350,53 @@ end
350350
@test agent2.pos == (2, 4)
351351
end
352352

353-
@multiagent :opt_memory struct Animal{T,N,J}(GridAgent{2})
354-
@subagent struct Wolf{T,N}
355-
energy::T = 0.5
356-
ground_speed::N
357-
const fur_color::Symbol
358-
end
359-
@subagent struct Hawk{T,N,J}
360-
energy::T = 0.1
361-
ground_speed::N
362-
flight_speed::J
363-
end
353+
@agent struct Wolf{T,N}(GridAgent{2})
354+
energy::T = 0.5
355+
ground_speed::N
356+
const fur_color::Symbol
364357
end
365358

366-
@multiagent :opt_speed struct A{T}(NoSpaceAgent)
367-
@subagent struct B{T}
368-
a::T = 1
369-
b::Int
370-
c::Symbol
371-
end
372-
@subagent struct C
373-
b::Int = 2
374-
c::Symbol
375-
d::Vector{Int}
376-
end
377-
@subagent struct D{T}
378-
c::Symbol = :k
379-
d::Vector{Int}
380-
a::T
381-
end
359+
@agent struct Hawk{T,N,J}(GridAgent{2})
360+
energy::T = 0.1
361+
ground_speed::N
362+
flight_speed::J
382363
end
383364

384-
abstract type AbstractE <: AbstractAgent end
385-
@multiagent struct E(NoSpaceAgent) <: AbstractE
386-
@subagent struct F
387-
x::Int
388-
end
389-
@subagent struct G
390-
y::Int
391-
end
392-
end
365+
@multiagent Animal(Wolf, Hawk)
393366

394-
@multiagent :opt_speed struct MultiSchelling1{X}(GridAgent{2})
395-
@subagent struct Civilian1 # can't re-define existing `Schelling` name
396-
mood::Bool = false
397-
group::Int
398-
end
399-
@subagent struct Governor1{X<:Real} # can't redefine existing `Politician` name
400-
group::Int
401-
influence::X
402-
end
367+
@agent struct B{T}(NoSpaceAgent)
368+
a::T = 1
369+
b::Int
370+
c::Symbol
403371
end
372+
@agent struct C(NoSpaceAgent)
373+
b::Int = 2
374+
c::Symbol
375+
d::Vector{Int}
376+
end
377+
@agent struct D{T}(NoSpaceAgent)
378+
c::Symbol = :k
379+
d::Vector{Int}
380+
a::T
381+
end
382+
@multiagent A(B,C,D)
404383

405-
@multiagent :opt_memory struct MultiSchelling2{X}(GridAgent{2})
406-
@subagent struct Civilian2 # can't re-define existing `Schelling` name
407-
mood::Bool = false
408-
group::Int
409-
end
410-
@subagent struct Governor2{X<:Real} # can't redefine existing `Politician` name
411-
group::Int
412-
influence::X
413-
end
384+
abstract type AbstractE <: AbstractAgent end
385+
386+
@agent struct F(NoSpaceAgent)
387+
x::Int
414388
end
389+
@agent struct G(NoSpaceAgent)
390+
y::Int
391+
end
392+
@multiagent E(F,G) <: AbstractE
415393

416394
@testset "@multiagent macro" begin
417395

418-
hawk_1 = Hawk(1, (1, 1), 1.0, 2.0, 3)
419-
hawk_2 = Hawk(; id = 2, pos = (1, 2), ground_speed = 2.3, flight_speed = 2)
420-
wolf_1 = Wolf(3, (2, 2), 2.0, 3.0, :black)
421-
wolf_2 = Wolf(; id = 4, pos = (2, 1), ground_speed = 2.0, fur_color = :white)
396+
hawk_1 = (AnimalHawk)(1, (1, 1), 1.0, 2.0, 3)
397+
hawk_2 = (AnimalHawk)(; id = 2, pos = (1, 2), ground_speed = 2.3, flight_speed = 2)
398+
wolf_1 = (AnimalWolf)(3, (2, 2), 2.0, 3.0, :black)
399+
wolf_2 = (AnimalWolf)(; id = 4, pos = (2, 1), ground_speed = 2.0, fur_color = :white)
422400

423401
@test hawk_1.energy == 1.0
424402
@test hawk_2.energy == 0.1
@@ -430,31 +408,31 @@ end
430408
@test wolf_2.fur_color == :white
431409
@test_throws "" hawk_1.fur_color
432410
@test_throws "" wolf_1.flight_speed
433-
@test kindof(hawk_1) == kindof(hawk_2) == :Hawk
434-
@test kindof(wolf_1) == kindof(wolf_2) == :Wolf
411+
@test variant(hawk_1) isa Hawk && variant(hawk_2) isa Hawk
412+
@test variant(wolf_1) isa Wolf && variant(wolf_2) isa Wolf
435413

436414
fake_step!(a) = nothing
437415
model = StandardABM(Animal, GridSpace((5, 5)); agent_step! = fake_step!)
438416

439-
add_agent!(Hawk, model, 1.0, 2.0, 3)
440-
add_agent!(Wolf, model, 2.0, 3.0, :black)
417+
add_agent!(AnimalHawk, model, 1.0, 2.0, 3)
418+
add_agent!(AnimalWolf, model, 2.0, 3.0, :black)
441419
@test nagents(model) == 2
442420
sample!(model, 2)
443421
@test nagents(model) == 2
444422

445-
b1 = B(1, 2, 1, :s)
446-
c1 = C(1, 1, :s, Int[])
447-
d1 = D(1, :s, [1], 1.0)
448-
b2 = B(; id = 1, b = 1, c = :s)
449-
c2 = C(; id = 1, c = :s, d = [1,2])
450-
d2 = D(; id = 1, d = [1], a = true)
423+
b1 = (AB)(1, 2, 1, :s)
424+
c1 = (AC)(1, 1, :s, Int[])
425+
d1 = (AD)(1, :s, [1], 1.0)
426+
b2 = (AB)(; id = 1, b = 1, c = :s)
427+
c2 = (AC)(; id = 1, c = :s, d = [1,2])
428+
d2 = (AD)(; id = 1, d = [1], a = true)
451429

452430
@test b2.a == 1
453431
@test c2.b == 2
454432
@test d2.c == :k
455-
@test kindof(b1) == kindof(b2) == :B
456-
@test kindof(c1) == kindof(c2) == :C
457-
@test kindof(d1) == kindof(d2) == :D
433+
@test variant(b1) isa B && variant(b2) isa B
434+
@test variant(c1) isa C && variant(c2) isa C
435+
@test variant(d1) isa D && variant(d2) isa D
458436
@test_throws "" b2.d
459437
@test_throws "" c1.a
460438
@test_throws "" d1.b
@@ -466,33 +444,25 @@ end
466444

467445
model = StandardABM(A; agent_step! = fake_step!)
468446

469-
add_agent!(B, model, 2, 1, :s)
470-
add_agent!(C, model, 1, :s, Int[])
471-
add_agent!(D, model, :s, [1], 1.0)
447+
add_agent!(AB, model, 2, 1, :s)
448+
add_agent!(AC, model, 1, :s, Int[])
449+
add_agent!(AD, model, :s, [1], 1.0)
472450
@test nagents(model) == 3
473451
sample!(model, 2)
474452
@test nagents(model) == 2
475453

476-
f = F(1, 1)
477-
g = G(2, 2)
454+
f = (EF)(1, 1)
455+
g = (EG)(2, 2)
478456

479457
@test f.id == 1
480458
@test g.id == 2
481459
@test f.x == 1
482460
@test g.y == 2
483461
@test_throws "" f.y
484462
@test_throws "" g.x
485-
@test kindof(f) == :F
486-
@test kindof(g) == :G
463+
@test variant(f) isa F
464+
@test variant(g) isa G
487465
@test E <: AbstractE && E <: AbstractE
488466
@test f isa E && g isa E
489-
490-
491-
civ = Civilian1(; id = 2, pos = (2, 2), group = 2)
492-
gov = Governor1(; id = 3 , pos = (2, 2), group = 2, influence = 0.5)
493-
civ = Civilian2(; id = 2, pos = (2, 2), group = 2)
494-
gov = Governor2(; id = 3 , pos = (2, 2), group = 2, influence = 0.5)
495-
496-
@test_throws "" Governor1(; id = 3 , pos = (2, 2), group = 2, influence = im)
497-
@test_throws "" Governor2(; id = 3 , pos = (2, 2), group = 2, influence = im)
498467
end
468+

0 commit comments

Comments
 (0)