Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
thevolatilebit committed Jul 13, 2023
1 parent 8a2f251 commit 9efb4a4
Show file tree
Hide file tree
Showing 16 changed files with 125 additions and 109 deletions.
60 changes: 30 additions & 30 deletions src/agents.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,36 +121,36 @@ function aagent(base_type, super_type, type, __module)
tname_plain = tname isa Symbol ? tname : tname.args[1]

constructor = define_agent(base_type, super_type, type, __module,
quote
function $(tname)(name::AbstractString,
args...) where {
$(param_tnames_constraints...)
}
uuid = AlgebraicAgents.uuid4()
inners = Dict{String, AbstractAlgebraicAgent}()
relpathrefs = Dict{AbstractString,
AlgebraicAgents.UUID}()
opera = AlgebraicAgents.Opera()

# if an extra field is missing, provide better error message
extra_fields = setdiff(fieldnames($tname_plain),
$common_interface_fields)
if length(args) != length(extra_fields)
error("""the agent type $tname_plain default constructor `$tname_plain(name, args...)` expects $(length(extra_fields)) arguments for custom fields $extra_fields, but $(length(args)) arguments were given.
If you intended to call a custom constructor and you passed a string as the first variable, please check that the custom constructor declares the type of the first positional argument to be `AbstractString` (so that dynamic dispatch works).
""")
end

# initialize agent
agent = new(uuid, name, nothing, inners, relpathrefs,
opera, args...)
# push ref to opera
push!(agent.opera.directory, agent.uuid => agent)

agent
end
end)
quote
function $(tname)(name::AbstractString,
args...) where {
$(param_tnames_constraints...),
}
uuid = AlgebraicAgents.uuid4()
inners = Dict{String, AbstractAlgebraicAgent}()
relpathrefs = Dict{AbstractString,
AlgebraicAgents.UUID}()
opera = AlgebraicAgents.Opera()

# if an extra field is missing, provide better error message
extra_fields = setdiff(fieldnames($tname_plain),
$common_interface_fields)
if length(args) != length(extra_fields)
error("""the agent type $tname_plain default constructor `$tname_plain(name, args...)` expects $(length(extra_fields)) arguments for custom fields $extra_fields, but $(length(args)) arguments were given.
If you intended to call a custom constructor and you passed a string as the first variable, please check that the custom constructor declares the type of the first positional argument to be `AbstractString` (so that dynamic dispatch works).
""")
end

# initialize agent
agent = new(uuid, name, nothing, inners, relpathrefs,
opera, args...)
# push ref to opera
push!(agent.opera.directory, agent.uuid => agent)

agent
end
end)

quote
# check if the base type implements the common interface fields
Expand Down
14 changes: 7 additions & 7 deletions src/integrations/AgentsIntegration/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ mutable struct ABMAgent <: AbstractAlgebraicAgent

## implement constructor
function ABMAgent(name::AbstractString, abm::Agents.AgentBasedModel;
agent_step! = Agents.dummystep, model_step! = Agents.dummystep,
when = true, when_model = when, step_size = 1.0,
tspan::NTuple{2, Float64} = (0.0, Inf), kwargs...)
agent_step! = Agents.dummystep, model_step! = Agents.dummystep,
when = true, when_model = when, step_size = 1.0,
tspan::NTuple{2, Float64} = (0.0, Inf), kwargs...)

# initialize wrap
i = new()
Expand Down Expand Up @@ -89,7 +89,7 @@ mutable struct ABMAgent <: AbstractAlgebraicAgent
end

function wrap_system(name::AbstractString, abm::Agents.AgentBasedModel, args...;
kwargs...)
kwargs...)
ABMAgent(name, abm, args...; kwargs...)
end

Expand All @@ -103,7 +103,7 @@ function _step!(a::ABMAgent)
a.when isa Bool ? a.when : a.when_model(a.abm, t)

df_agents, df_model = Agents.run!(a.abm, a.agent_step!, a.model_step!, 1;
a.kwargs...)
a.kwargs...)
# append collected data
## df_agents
if collect_agents && ("step" names(df_agents))
Expand Down Expand Up @@ -203,11 +203,11 @@ function print_custom(io::IO, mime::MIME"text/plain", a::ABMAgent)
show(IOContext(io, :indent => get(io, :indent, 0) + 4), mime, a.abm)

print(io, "\n" * " "^(indent + 3), crayon"italics", "df_agents", ": ", crayon"reset",
"\n")
"\n")
show(IOContext(io, :indent => get(io, :indent, 0) + 4), mime, a.df_model)

print(io, "\n" * " "^(indent + 3), crayon"italics", "df_model", ": ", crayon"reset",
"\n")
"\n")
show(IOContext(io, :indent => get(io, :indent, 0) + 4), mime, a.df_agents)
end

Expand Down
8 changes: 4 additions & 4 deletions src/integrations/AlgebraicDynamicsIntegration/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ GraphicalAgent("rabbit", ContinuousMachine{Float64}(1,1,1, dotr, (u, p, t) -> u)
end

function wrap_system(name::AbstractString, sharer::GraphicalModelType, args...;
kwargs...)
kwargs...)
GraphicalAgent(name, sharer, args...; kwargs...)
end

Expand Down Expand Up @@ -59,7 +59,7 @@ Apply `oapply(diagram, systems...)` and wrap the result as a `GraphicalAgent`.

@doc sum_algebraicdynamics_docstring
function (x::Vector{M}; diagram, pushout = nothing,
name = "diagram") where {M <: GraphicalAgent}
name = "diagram") where {M <: GraphicalAgent}
x_ = map(x -> x.system, x)
m = isnothing(pushout) ? AlgebraicDynamics.oapply(diagram, x_) :
AlgebraicDynamics.oapply(diagram, x, pushout)
Expand All @@ -73,7 +73,7 @@ end

@doc sum_algebraicdynamics_docstring
function (x::Vararg{M}; diagram, pushout = nothing,
name = "diagram") where {M <: GraphicalAgent}
name = "diagram") where {M <: GraphicalAgent}
x_ = map(x -> x.system, collect(x))
m = isnothing(pushout) ? AlgebraicDynamics.oapply(diagram, x_) :
AlgebraicDynamics.oapply(diagram, x, pushout)
Expand All @@ -98,7 +98,7 @@ end

@doc sum_algebraicdynamics_docstring
function (x::AbstractDict{S, M}; diagram, pushout = nothing,
name = "diagram") where {S, M <: GraphicalAgent}
name = "diagram") where {S, M <: GraphicalAgent}
x_ = Dict(x -> x[1] => x[2].system, x)
m = isnothing(pushout) ? AlgebraicDynamics.oapply(diagram, x_) :
AlgebraicDynamics.oapply(diagram, x, pushout)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import .DifferentialEquations: OrdinaryDiffEq

function _get_problem_type(system::GraphicalModelType)
if typeof(system) <: Union{AlgebraicDynamics.DWDDynam.ContinuousMachine,
AlgebraicDynamics.UWDDynam.ContinuousResourceSharer}
AlgebraicDynamics.UWDDynam.ContinuousResourceSharer}
OrdinaryDiffEq.ODEProblem
elseif typeof(system) <: Union{AlgebraicDynamics.DWDDynam.DiscreteMachine,
AlgebraicDynamics.UWDDynam.DiscreteResourceSharer}
AlgebraicDynamics.UWDDynam.DiscreteResourceSharer}
OrdinaryDiffEq.DiscreteProblem
else
OrdinaryDiffEq.DDEProblem
Expand All @@ -26,7 +26,7 @@ DiffEqAgent(system, u0, tspan, params; alg=Tsit5())
```
"""
function DiffEqAgent(agent::GraphicalAgent, args...;
alg = nothing, kwargs...)
alg = nothing, kwargs...)
# get DEProblem
prob = _get_problem_type(agent.system)(agent.system, args...)
# get alg
Expand Down
10 changes: 5 additions & 5 deletions src/integrations/SciMLIntegration/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ mutable struct DiffEqAgent <: AbstractAlgebraicAgent
observables::Dict{Any, Int}

function DiffEqAgent(name, problem::DiffEqBase.DEProblem,
alg = DifferentialEquations.default_algorithm(problem)[1], args...;
observables = Dict{Any, Int}(), kwargs...)
alg = DifferentialEquations.default_algorithm(problem)[1], args...;
observables = Dict{Any, Int}(), kwargs...)
problem = DifferentialEquations.remake(problem;
p = Params(Val(DummyType), problem.p))
p = Params(Val(DummyType), problem.p))

# initialize wrap
i = new()
Expand Down Expand Up @@ -67,8 +67,8 @@ end
Base.setindex!(p::Params, v, i::Int) = getfield(p, :params)[i] = v

function wrap_system(name::AbstractString, problem::DiffEqBase.DEProblem, args...;
alg = DifferentialEquations.default_algorithm(problem)[1],
kwargs...)
alg = DifferentialEquations.default_algorithm(problem)[1],
kwargs...)
DiffEqAgent(name, problem, alg, args...; kwargs...)
end

Expand Down
18 changes: 9 additions & 9 deletions src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ mutable struct FreeAgent <: AbstractAlgebraicAgent
```
"""
function FreeAgent(name::AbstractString,
agents::Vector{<:AbstractAlgebraicAgent} = AbstractAlgebraicAgent[])
agents::Vector{<:AbstractAlgebraicAgent} = AbstractAlgebraicAgent[])
m = new()
m.uuid = uuid4()
m.name = name
Expand Down Expand Up @@ -342,18 +342,18 @@ end
function print_header(io::IO, a::AbstractAlgebraicAgent)
indent = get(io, :indent, 0)
print(io,
" "^indent *
"$(typeof(a)){name=$(getname(a)), uuid=$(string(getuuid(a))[1:8]), parent=$(getparent(a))}")
" "^indent *
"$(typeof(a)){name=$(getname(a)), uuid=$(string(getuuid(a))[1:8]), parent=$(getparent(a))}")
end

"Pretty-print agent's parent and inners. Optionally specify indent."
function print_neighbors(io::IO, m::MIME"text/plain", a::AbstractAlgebraicAgent,
expand_inners = true)
expand_inners = true)
indent = get(io, :indent, 0)

expand_inners && !isnothing(getparent(a)) &&
print(io, "\n", " "^(indent + 3), crayon"bold", "parent: ", crayon"reset",
crayon"green", getname(getparent(a)), crayon"reset")
crayon"green", getname(getparent(a)), crayon"reset")
!isempty(values(inners(a))) &&
print(io, "\n", " "^(indent + 3), crayon"bold", "inner agents: ", crayon"reset")

Expand All @@ -365,8 +365,8 @@ function print_neighbors(io::IO, m::MIME"text/plain", a::AbstractAlgebraicAgent,
show(iio, m, a, false)
end
(length(inners(a)) > max_inners) && print(io,
"\n" * " "^(indent + 4) *
"$(length(inners(a))-max_inners) more agent(s) not shown ...")
"\n" * " "^(indent + 4) *
"$(length(inners(a))-max_inners) more agent(s) not shown ...")
else
print(io, join([getname(a) for a in values(inners(a))], ", "))
end
Expand All @@ -382,13 +382,13 @@ function print_custom(io::IO, mime::MIME"text/plain", a::AbstractAlgebraicAgent)
print(io, "\n", " "^(indent + 3), "custom properties:")
for field in extra_fields
print(io, "\n", " "^(indent + 3), AlgebraicAgents.crayon"italics", field, ": ",
AlgebraicAgents.crayon"reset", getproperty(a, field))
AlgebraicAgents.crayon"reset", getproperty(a, field))
end
end

# specialize show method
function Base.show(io::IO, m::MIME"text/plain", a::AbstractAlgebraicAgent,
expand_inners = true)
expand_inners = true)
print_header(io, m, a)
print_custom(io, m, a)
print_neighbors(io, m, a, expand_inners)
Expand Down
28 changes: 14 additions & 14 deletions src/opera.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

## action types
const InstantiousInteraction = NamedTuple{(:id, :call, :priority),
<:Tuple{AbstractString, Function, Number}}
<:Tuple{AbstractString, Function, Number}}
const InstantiousInteractionLog = NamedTuple{(:id, :time, :retval),
<:Tuple{AbstractString, Number, Any}}
<:Tuple{AbstractString, Number, Any}}
const Future = NamedTuple{(:id, :call, :time),
<:Tuple{AbstractString, Function, Any}}
<:Tuple{AbstractString, Function, Any}}
const FutureLog = NamedTuple{(:id, :time, :retval),
<:Tuple{AbstractString, Any, Any}}
<:Tuple{AbstractString, Any, Any}}
const Control = NamedTuple{(:id, :call), <:Tuple{AbstractString, Function}}
const ControlLog = NamedTuple{(:id, :time, :retval), <:Tuple{AbstractString, Any, Any}}

Expand Down Expand Up @@ -150,8 +150,8 @@ add_instantious!(agent, () -> wake_up(agent))
```
"""
function add_instantious!(opera::Opera, call, priority::Number = 0.0,
id = "instantious_" *
get_count(opera, :n_instantious_interactions))
id = "instantious_" *
get_count(opera, :n_instantious_interactions))
add_instantious!(opera, (; id, call, priority))
end

Expand All @@ -162,7 +162,7 @@ end
function add_instantious!(opera::Opera, action::InstantiousInteraction)
# sorted insert
insert_at = searchsortedfirst(opera.instantious_interactions, action;
by = x -> x.priority)
by = x -> x.priority)
insert!(opera.instantious_interactions, insert_at, action)
end

Expand Down Expand Up @@ -191,10 +191,10 @@ poke(agent, 1.) # with priority equal to 1
```
"""
function poke(agent, priority::Number = 0.0,
id = "instantious_" * get_count(getopera(agent), :n_instantious_interactions))
id = "instantious_" * get_count(getopera(agent), :n_instantious_interactions))
add_instantious!(getopera(agent),
(; id, call = () -> _interact!(agent),
priority = Float64(priority)))
(; id, call = () -> _interact!(agent),
priority = Float64(priority)))
end

"""
Expand Down Expand Up @@ -223,8 +223,8 @@ macro call(opera, call, priority::Number = 0.0, id = nothing)
end

add_instantious!(opera,
(; id, call = () -> $(esc(call)),
priority = Float64($(esc(priority)))))
(; id, call = () -> $(esc(call)),
priority = Float64($(esc(priority)))))
end
end

Expand All @@ -251,7 +251,7 @@ add_future!(alice, 5.0, () -> interact(alice), "alice_schedule")
function add_future! end

function add_future!(opera::Opera, time, call,
id = "future_" * get_count(opera, :n_futures))
id = "future_" * get_count(opera, :n_futures))
new_action = (; id, call, time)

# sorted insert
Expand Down Expand Up @@ -290,7 +290,7 @@ macro future(opera, time, call, id = nothing)
end

add_future!(opera, $(esc(time)), () -> $(esc(call)),
id)
id)
end
end

Expand Down
2 changes: 1 addition & 1 deletion src/paths.jl
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ Return agents in the hierarchy whose names match the given wildcard.
If `inners_only==true`, consider descendants of `agent` only.
"""
function by_name(agent::AbstractAlgebraicAgent, name::Union{Glob.FilenameMatch, Regex};
inners_only = false)
inners_only = false)
agent = inners_only ? agent : topmost(agent)
agents = []
prewalk(a -> (occursin(name, getname(a))) && push!(agents, a), agent)
Expand Down
10 changes: 5 additions & 5 deletions src/queries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function Base.filter(a::AbstractAlgebraicAgent, queries::Vararg{<:FilterQuery})
end

function Base.filter(a::Vector{<:AbstractAlgebraicAgent},
queries::Vararg{<:FilterQuery})
queries::Vararg{<:FilterQuery})
filtered = AbstractAlgebraicAgent[]
for a in a
all(q -> _filter(a, q), queries) && push!(filtered, a)
Expand Down Expand Up @@ -125,8 +125,8 @@ struct TransformQuery{F <: Function} <: AbstractQuery
query::F

function TransformQuery(name::T,
query::F) where {T <: Union{Symbol, AbstractString},
F <: Function}
query::F) where {T <: Union{Symbol, AbstractString},
F <: Function}
new{F}(Symbol(name), query)
end
end
Expand All @@ -144,7 +144,7 @@ macro transform(exs...)
names, queries = map(x -> x[1], queries), map(x -> x[2], queries)
quote
queries = TransformQuery.($(names),
[$(interpolate_underscores.(queries)...)])
[$(interpolate_underscores.(queries)...)])

a -> transform(a, queries...)
end
Expand All @@ -170,7 +170,7 @@ function transform(a::AbstractAlgebraicAgent, queries::Vararg{<:TransformQuery})
end

function transform(a::Vector{<:AbstractAlgebraicAgent},
queries::Vararg{<:TransformQuery})
queries::Vararg{<:TransformQuery})
results = []
for a in a
try
Expand Down
Loading

0 comments on commit 9efb4a4

Please sign in to comment.