Skip to content

Commit

Permalink
Merge pull request #123 from AlgebraicJulia/cast_to_structacset
Browse files Browse the repository at this point in the history
Cast between struct and dynamic acsets
  • Loading branch information
epatters authored Mar 6, 2024
2 parents 0fed281 + d0f8bfb commit 22b89bb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/DenseACSets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -293,13 +293,22 @@ function ACSetInterface.subpart_type(x::DynamicACSet,s::Symbol)
end

"""Cast StructACSet into a DynamicACSet"""
function DynamicACSet(X::StructACSet{S}) where S
Y = DynamicACSet(string(typeof(X).name.name), Schema(S); type_assignment=datatypes(X))
copy_parts!(Y,X, NamedTuple(Dict(k=>parts(X,k) for k in types(S))))
return Y
function DynamicACSet(X::StructACSet{S, Ts, PT}) where {S, Ts, PT}
Y = DynamicACSet(string(typeof(X).name.name), Schema(S);
type_assignment=datatypes(X), index = indices(X),
unique_index = unique_indices(X), part_type=PT)
copy_parts!(Y, X)
Y
end


"""Cast DynamicACSet into a StructACSet"""
function StructACSet(X::DynamicACSet{PT}) where PT
S = acset_schema(X)
Y = AnonACSet(Schema(S); type_assignment=datatypes(X), index = indices(X),
unique_index = unique_indices(X), part_type=PT)
copy_parts!(Y, X)
Y
end

""" This works the same as something made with `@acset_type`, only the types of the
parts and subparts are stored as type parameters. Thus, this can be used with any schema.
Expand Down Expand Up @@ -332,7 +341,7 @@ end
"""
function AnonACSetType(
s::Schema;
type_assignment::Dict{Symbol, Type}=Dict{Symbol,Type}(),
type_assignment::Dict{Symbol, <:Type}=Dict{Symbol, Type}(),
index::Vector=[],
unique_index::Vector=[],
union_all::Bool=false,
Expand Down Expand Up @@ -744,11 +753,13 @@ end
ACSetInterface.copy_parts!(to::StructACSet{S}, from::StructACSet{S′}) where {S,S′} =
copy_parts!(to, from, common_objects(Val{S}, Val{S′}))

ACSetInterface.copy_parts!(to::DynamicACSet, from::DynamicACSet) =
copy_parts!(to, from, runtime(common_objects, to.schema, from.schema))

ACSetInterface.copy_parts!(to::SimpleACSet, from::SimpleACSet; kw...) =
copy_parts!(to, from, (;kw...))
function ACSetInterface.copy_parts!(to::SimpleACSet, from::SimpleACSet; kw...)
if isempty(kw)
copy_parts!(to, from, runtime(common_objects, acset_schema(to), acset_schema(from)))
else
copy_parts!(to, from, (;kw...))
end
end

ACSetInterface.copy_parts!(to::SimpleACSet, from::SimpleACSet, obs::Tuple) =
copy_parts!(to, from, NamedTuple{obs}((:) for ob in obs))
Expand Down
21 changes: 21 additions & 0 deletions test/ACSets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -834,4 +834,25 @@ SchLabeledDecGraph′ = BasicSchema([:E,:V], [(:src,:E,:V),(:tgt,:E,:V)],

@acset SymSymDecGraph begin V=1; E=1; src=1; tgt=1; dec=[:a]; label=[:b] end

# Cast Dynamic <-> StructACSet
#------------------------------
g = @acset MadDecGraph{String} begin
V = 4; E = 4; X=3
src = [1,2,3,4]
tgt = [2,3,4,1]
dec = ["a","b",AttrVar(3),AttrVar(1)]
end

@test g isa StructACSet

dyn_g = DynamicACSet(g)

@test dyn_g isa DynamicACSet
@test nparts(dyn_g, :X) == 3

g′ = StructACSet(dyn_g)

@test g′ isa AnonACSet
@test nparts(g′, :X) == 3

end # module

0 comments on commit 22b89bb

Please sign in to comment.