Skip to content

Commit

Permalink
Reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
thevolatilebit committed Feb 26, 2024
1 parent cda743c commit 1a7a677
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/opera.jl
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ function execute_controls!(opera::Opera, time)
end

# if `expr` is a string, parse it as an expression
function get_expr(expr; eval_scope=@__MODULE__)
function get_expr(expr; eval_scope = @__MODULE__)
if expr isa AbstractString
Base.eval(eval_scope, Meta.parseall(expr))
else
Expand Down
5 changes: 2 additions & 3 deletions src/queries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,8 @@ Accepts both anonymous queries (`_.name`) and named queries (`name=_.name`). By
"""
macro transform(exs...)
n_noname::Int = 0
queries = map(
ex -> Meta.isexpr(ex, :(=)) ? (ex.args[1], ex.args[2]) :
(n_noname += 1; ("query_$n_noname", ex)),
queries = map(ex -> Meta.isexpr(ex, :(=)) ? (ex.args[1], ex.args[2]) :
(n_noname += 1; ("query_$n_noname", ex)),
exs)
names, queries = map(x -> x[1], queries), map(x -> x[2], queries)
quote
Expand Down
44 changes: 30 additions & 14 deletions src/wires.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,16 @@ Add a wire connecting two agents.
add_wire!(joint_system; from=alice, to=bob, from_var_name="alice_x", to_var_name="bob_x")
```
"""
function add_wire!(a::AbstractAlgebraicAgent; from::T, to::T, from_var_name=nothing, to_var_name=nothing) where T<:Union{AbstractAlgebraicAgent, AbstractString, UUID}
function add_wire!(a::AbstractAlgebraicAgent;
from::T,
to::T,
from_var_name = nothing,
to_var_name = nothing) where {T <: Union{AbstractAlgebraicAgent, AbstractString, UUID}}
from, to = to_agent(a, from), to_agent(a, to)
from_var_name = something(from_var_name, to_var_name)
to_var_name = something(to_var_name, from_var_name)

wire = (; from, from_var_name=from_var_name, to, to_var_name=to_var_name)
wire = (; from, from_var_name = from_var_name, to, to_var_name = to_var_name)

return add_wire!(a, wire)
end
Expand All @@ -73,11 +77,15 @@ delete_wires!(joint_system; from=alice, to=bob)
delete_wires!(joint_system; from=alice, to=bob, from_var_name="alice_x", to_var_name="bob_x")
```
"""
function delete_wires!(a::AbstractAlgebraicAgent; from::T, to::T, from_var_name=nothing, to_var_name=nothing) where T<:Union{AbstractAlgebraicAgent, AbstractString, UUID}
function delete_wires!(a::AbstractAlgebraicAgent;
from::T,
to::T,
from_var_name = nothing,
to_var_name = nothing) where {T <: Union{AbstractAlgebraicAgent, AbstractString, UUID}}
from, to = to_agent(a, from), to_agent(a, to)

ixs = findall(x -> x.from == from && x.to == to, getopera(a).wires)

if !isnothing(from_var_name)
ixs = ixs findall(x -> x.from_var_name == from_var_name, getopera(a).wires)
end
Expand Down Expand Up @@ -129,11 +137,16 @@ function wiring_diagram(agent::AbstractAlgebraicAgent; kwargs...)
return wiring_diagram(all_agents; kwargs...)
end

function wiring_diagram(agents::Vector{T}; parentship_edges=true, wires=true) where T<:AbstractAlgebraicAgent
function wiring_diagram(agents::Vector{T};
parentship_edges = true,
wires = true) where {T <: AbstractAlgebraicAgent}
wiring_diagram([agents]; parentship_edges, wires)
end

function wiring_diagram(groups; group_labels=nothing, parentship_edges=true, wires=true)
function wiring_diagram(groups;
group_labels = nothing,
parentship_edges = true,
wires = true)
nodes = build_nodes(groups; group_labels)
edges_parentship, edges_wires = build_edges(vcat(groups...))

Expand Down Expand Up @@ -162,18 +175,19 @@ function build_nodes(groups; group_labels = nothing)

return join(nodes, "\n")
else
subgraphs = []; j = 0
subgraphs = []
j = 0
for (i_group, group) in enumerate(groups)
nodes = ["""$(j+i) [label="$(getname(a))"]""" for (i, a) in enumerate(group)]

push!(subgraphs,
push!(subgraphs,
"""
subgraph cluster_$i_group {\n
""" *
(!isnothing(group_labels) ? """label="$(group_labels[i_group])" \n""" : "") *
(!isnothing(group_labels) ? """label="$(group_labels[i_group])" \n""" :
"") *
join(nodes, "\n") *
"\n}"
)
"\n}")
j += length(group)
end

Expand All @@ -197,7 +211,8 @@ function build_edges(all_agents)
if !isnothing(parents[i])
p = parents[i]
ix1, ix2 = findfirst(==(p), all_agents), findfirst(==(a), all_agents)
push!(edges_parentship, "$ix1 -> $ix2 [len=1, penwidth=0.5, arrowsize=0.4, arrowtype=normal, style=dashed, fontsize=5.0, color=grey]")
push!(edges_parentship,
"$ix1 -> $ix2 [len=1, penwidth=0.5, arrowsize=0.4, arrowtype=normal, style=dashed, fontsize=5.0, color=grey]")
end
end

Expand All @@ -207,9 +222,10 @@ function build_edges(all_agents)

oriented_wires_between = get_wires_from(a) get_wires_to(b)
for wire in oriented_wires_between
push!(edges_wires, "$ix1 -> $ix2 [len=1, headlabel=$(wire.from_var_name), taillabel=$(wire.to_var_name), arrowsize=0.3, arrow=normal, fontsize=7.0]")
push!(edges_wires,
"$ix1 -> $ix2 [len=1, headlabel=$(wire.from_var_name), taillabel=$(wire.to_var_name), arrowsize=0.3, arrow=normal, fontsize=7.0]")
end
end

return join(edges_parentship, "\n"), join(edges_wires, "\n")
end
end
8 changes: 3 additions & 5 deletions test/agents.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ end
system_dump = AlgebraicAgents.save(system)

@test system_dump == Dict{String, Any}("name" => "diagram",
"inners" => Dict{String, Any}[
Dict("name" => "agent1",
"inners" => Dict{String, Any}[Dict("name" => "agent1",
"arguments" => [1],
"type" => MyAgentLoadsave),
Dict("name" => "agent2", "arguments" => [2], "type" => MyAgentLoadsave)],
Expand All @@ -124,9 +123,8 @@ end
agent1 = inners(system_reloaded)["agent1"]
@test agent1 isa MyAgentLoadsave

opera_dump = Dict(
"instantious" => [
Dict("call" => () -> println("instantious interaction"))
opera_dump = Dict("instantious" => [
Dict("call" => () -> println("instantious interaction")),
],
"futures" => [Dict("time" => 2.0, "call" => () -> println("future"))],
"controls" => [Dict("call" => () -> println("control"))])
Expand Down
30 changes: 23 additions & 7 deletions test/opera.jl
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,27 @@ end
joint_system = (alice, bob, name = "joint system")

# Add wires.
add_wire!(joint_system; from=alice, to=bob, from_var_name="alice_x", to_var_name="bob_x")
add_wire!(joint_system; from=bob, to=alice, from_var_name="bob_y", to_var_name="alice_y")

add_wire!(joint_system; from=alice, to=alice1, from_var_name="alice_x", to_var_name="alice1_x")
add_wire!(joint_system; from=bob, to=bob1, from_var_name="bob_x", to_var_name="bob1_x")
add_wire!(joint_system;
from = alice,
to = bob,
from_var_name = "alice_x",
to_var_name = "bob_x")
add_wire!(joint_system;
from = bob,
to = alice,
from_var_name = "bob_y",
to_var_name = "alice_y")

add_wire!(joint_system;
from = alice,
to = alice1,
from_var_name = "alice_x",
to_var_name = "alice1_x")
add_wire!(joint_system;
from = bob,
to = bob1,
from_var_name = "bob_x",
to_var_name = "bob1_x")

# Show wires.
@test length(get_wires_from(alice)) == 2
Expand All @@ -219,5 +235,5 @@ end
@test retrieve_input_vars(alice1) == Dict("alice1_x" => "alice")

# Delete wires.
@test length(delete_wires!(joint_system; from=alice, to=alice1)) == 3
end
@test length(delete_wires!(joint_system; from = alice, to = alice1)) == 3
end

0 comments on commit 1a7a677

Please sign in to comment.