Skip to content

Commit

Permalink
Report duplicate names and find terminal names in helper
Browse files Browse the repository at this point in the history
  • Loading branch information
lukem12345 committed Jun 25, 2024
1 parent 81649a0 commit 38a1576
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/DiagrammaticEquations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Collage, collate,
oapply, unique_by, unique_by!, OpenSummationDecapodeOb, OpenSummationDecapode, Open, default_composition_diagram,
## acset
SchDecapode, SchNamedDecapode, AbstractDecapode, AbstractNamedDecapode, NamedDecapode, SummationDecapode,
contract_operators!, contract_operators, add_constant!, add_parameter, fill_names!, dot_rename!, expand_operators, infer_state_names, recognize_types,
contract_operators!, contract_operators, add_constant!, add_parameter, fill_names!, dot_rename!, expand_operators, infer_state_names, infer_terminal_names, recognize_types,
resolve_overloads!, replace_names!,
apply_inference_rule_op1!, apply_inference_rule_op2!,
transfer_parents!, transfer_children!,
Expand Down
11 changes: 9 additions & 2 deletions src/acset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ end
""" function infer_states(d::SummationDecapode)
Find variables which have a time derivative or are not the source of a computation.
See also: [`infer_state_names`](@ref).
See also: [`infer_terminals`](@ref).
"""
function infer_states(d::SummationDecapode)
parentless = filter(parts(d, :Var)) do v
Expand All @@ -226,7 +226,7 @@ infer_state_names(d) = d[infer_states(d), :name]

""" function infer_terminals(d::SummationDecapode)
Find variables which have no children.
See also: [`infer_state_names`](@ref).
See also: [`infer_states`](@ref).
"""
function infer_terminals(d::SummationDecapode)
filter(parts(d, :Var)) do v
Expand All @@ -237,6 +237,13 @@ function infer_terminals(d::SummationDecapode)
end
end

""" function infer_terminal_names(d)
Find names of variables which have no children.
See also: [`infer_terminals`](@ref).
"""
infer_terminal_names(d) = d[infer_terminals(d), :name]

""" function expand_operators(d::SummationDecapode)
Find operations that are compositions, and expand them with intermediate variables.
Expand Down
11 changes: 9 additions & 2 deletions src/composition.jl
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,13 @@ oapply(r::RelationDiagram, pode::OpenSummationDecapode) = oapply(r, [pode])
# Default composition
# -------------------

# This helper function finds elements which appear in an array more than once.
function find_duplicates(vs::Vector{T}) where T
once, twice = Set{T}(), Set{T}()
foreach(v -> v once ? push!(twice,v) : push!(once,v), vs)
twice

Check warning on line 227 in src/composition.jl

View check run for this annotation

Codecov / codecov/patch

src/composition.jl#L224-L227

Added lines #L224 - L227 were not covered by tests
end

# TODO: Upstream this to Catlab?
function construct_relation_diagram(boxes::Vector{Symbol}, junctions::Vector{Vector{Symbol}})
tables = map(boxes, junctions) do b, j
Expand All @@ -246,11 +253,11 @@ function default_composition_diagram(podes::Vector{D}, names::Vector{Symbol}, on
pode[findall(!=(:Literal), pode[:type]), :name]
end
for (nln, name) in zip(non_lit_names, names)
allunique(nln) || error("Decapode $name contains repeated variable names: $nln.")
allunique(nln) || error("Decapode $name contains repeated variable names: $(find_duplicates(nln)).")
end
if only_states_terminals
foreach(non_lit_names, podes) do nln, pode
outers = infer_state_names(pode) pode[infer_terminals(pode), :name]
outers = infer_state_names(pode) infer_terminal_names(pode)
filter!(x -> x outers, nln)
end
end
Expand Down

0 comments on commit 38a1576

Please sign in to comment.