Skip to content

Commit

Permalink
Merge pull request #9 from AlgebraicJulia/AddNetWorkStructure
Browse files Browse the repository at this point in the history
add schema composition method using structured cospan
  • Loading branch information
Xiaoyan-Li authored Jun 3, 2024
2 parents a0e01c1 + 33ffa6c commit 99e56b7
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/StateCharts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ using Reexport

include("StateChartsSchema.jl")
include("Visualization.jl")
include("interfacesToAMBs/NetworkSchemaInterfaces.jl")
include("interfacesToAMBs/StateChartsABMsInterfaces.jl")
include("interfacesToABMs/NetworkSchemaInterfaces.jl")
include("interfacesToABMs/StateChartsABMsInterfaces.jl")

@reexport using .StateChartsSchema
@reexport using .Visualization
Expand Down
4 changes: 4 additions & 0 deletions src/interfacesToABMs/MigrateRules.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# this module defines some commonly used migration rules
module MigrateRules

end
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module NetworkSchemaInterfaces

export SchemaTheory, Open, obname, obnames, nob, nhom, homame, homnames

using Catlab

vectorify(n::Vector) = collect(n)
Expand Down Expand Up @@ -34,6 +36,27 @@ end
@acset_type SchemaUntyped(TheorySchema, index=[:homsrc,:homtgt,:attrsrc,:attrtgt]) <: AbstractSchema
const SchemaTheory = SchemaUntyped{Symbol}

nob(s::AbstractSchema) = nparts(s, :Ob)
nhom(s::AbstractSchema) = nparts(s, :Hom)
nat(s::AbstractSchema) = nparts(s, :AttrType)

obname(s::AbstractSchema, ob) = has_subpart(s, :obname) ? subpart(s, ob, :obname) : (1:nob(s))[ob]
obnames(s::AbstractSchema) = map(ob -> obname(s, ob), 1:nob(s))

homname(s::AbstractSchema, hom) = has_subpart(s, :homname) ? subpart(s, hom, :homname) : (1:nhom(s))[hom]
homnames(s::AbstractSchema) = map(hom -> homname(s, hom), 1:nhom(s))

# define the open schema as structured cospan
const OpenSchemaObUntyped, OpenSchemaUntyped = OpenACSetTypes(SchemaUntyped, :Ob)
const OpenSchemaOb, OpenSchema = OpenSchemaObUntyped{Symbol}, OpenSchemaUntyped{Symbol}

Open(s::SchemaTheory) = OpenSchema(s, map(x -> FinFunction([x], nob(s)), 1:nob(s))...)
Open(s::SchemaTheory, legs...) = begin
s_idx = Dict(obname(s, ob) => ob for ob in 1:nob(s))
OpenSchema(s, map(l -> FinFunction(map(i -> s_idx[i], l), nob(s)), legs)...)
end
Open(n, s::SchemaTheory, m) = Open(s,n,m)

function SchemaTheory(ob,hom,attrtype,attr)
st = SchemaTheory()

Expand Down Expand Up @@ -63,23 +86,4 @@ function SchemaTheory(ob,hom,attrtype,attr)
return st
end



####### example of
ob=(:S,:I,:R,:V)
hom=(:SV=>(:S,:V),
:IV=>(:I,:V),
:RV=>(:R,:V))

ob2=(:V)
hom2=[]
attrtype=[:SIR]
attr=[:VSIR=>(:V,:SIR)]

a=SchemaTheory(ob, hom, [], [])

b=SchemaTheory(ob2, hom2, attrtype,attr)



end
42 changes: 42 additions & 0 deletions src/interfacesToABMs/Networks.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# this moduel defines the basic networks
module Networks

using Catlab

# using basic graph schemas as the schema of basic networks
SchDirectedNetwork = SchGraph
# # The Category of Graphs
#
# The Theory of Graphs is given by the following Schema:
# ```julia
# @present SchGraph(FreeSchema) begin
# V::Ob
# E::Ob
# src::Hom(E,V)
# tgt::Hom(E,V)
# end

SchUndirectedNetwork = SchSymmetricGraph

# # Symmetric graphs
#
# @present SchSymmetricGraph <: SchGraph begin
# inv::Hom(E,E)
# compose(inv,inv) == id(E)
# compose(inv,src) == tgt
# compose(inv,tgt) == src
#end

SchUndirectedReflectiveNetwork = SchSymmetricReflexiveGraph

# Symmetric reflexive graphs
#
# @present SchSymmetricReflexiveGraph <: SchSymmetricGraph begin
# refl::Hom(V,E)
# compose(refl, src) == id(V)
# compose(refl, tgt) == id(V)
# compose(refl, inv) == refl # Reflexive loop fixed by involution.
#end


end
1 change: 0 additions & 1 deletion test/core.jl

This file was deleted.

46 changes: 46 additions & 0 deletions test/interfacesToABMs/NetworkSchemaInterfaces.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module NetworkSchemaInterfaces

using Test
using StateCharts
using Catlab

# test schema of schema
ob=(:S,:I,:R,:V)
hom=(:SV=>(:S,:V),
:IV=>(:I,:V),
:RV=>(:R,:V))


schemaCSet=SchemaTheory(ob, hom, [], [])

ob2=(:V)
hom2=[]
attrtype=[:SIR]
attr=[:VSIR=>(:V,:SIR)]

schemaACSet=SchemaTheory(ob2, hom2, attrtype,attr)

# test composition of schemas - example 1: object person (which is V in this example) in state chart generated schema is identified
# with the vertice object in basic graph.

# Define basic graph schema
BasicGraphSchema=SchemaTheory([:E,:V], [:src=>(:E,:V),:tgt=>(:E,:V)], [], [])

# Compose the two schemas
composedSchema=apex(compose(Open([:S],schemaCSet,[:V]),Open([:V],BasicGraphSchema,[:E])))

@test obnames(composedSchema) == [:S,:I,:R,:V,:E]
@test homnames(composedSchema) == [:SV,:IV,:RV,:src,:tgt]

# test composition of schemas - example 2: there is a morphism from the object person in state chart generated schema to
# the vertice object in basic graph.

schemaSIRP = SchemaTheory([:S,:I,:R,:P], [:SP=>(:S,:P),:IP=>(:I,:P),:RP=>(:R,:P)], [], [])
schemaPV = SchemaTheory([:P,:V], [:PV=>(:P,:V)], [], [])
composedSchema2 = (Open([:S],schemaSIRP,[:P]) · Open([:P],schemaPV,[:V])) · Open([:V],BasicGraphSchema,[:E]) |> apex
# the syntax of compose can also using julia unicode of \cdotp

@test obnames(composedSchema2) == [:S,:I,:R,:P,:V,:E]
@test homnames(composedSchema2) == [:SP,:IP,:RP,:PV,:src,:tgt]

end
5 changes: 4 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ end
end

@testset "StateChartsABMsInterfaces" begin
include("interfacesToAMBs/StateChartsABMsInterfaces.jl")
include("interfacesToABMs/StateChartsABMsInterfaces.jl")
end

@testset "NetworkSchemaInterfaces" begin
include("interfacesToABMs/NetworkSchemaInterfaces.jl")
end

0 comments on commit 99e56b7

Please sign in to comment.