generated from AlgebraicJulia/AlgebraicTemplate.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'AddNetWorkStructure' into main
- Loading branch information
Showing
7 changed files
with
130 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,53 @@ | ||
# this module defines some commonly used migration rules | ||
module MigrateRules | ||
|
||
export schemaACSet, schemaPresent | ||
|
||
using Catlab | ||
using ..NetworkSchemaInterfaces | ||
|
||
# create an acset schema from a present schema | ||
function schemaACSet(s::Presentation) | ||
obs = nameof.(generators(s,:Ob)) | ||
h = generators(s,:Hom) | ||
homs = map((name,dom,codom)->name=>(dom,codom),nameof.(h),nameof.(dom.(h)),nameof.(codom.(h))) | ||
attrtypes = nameof.(generators(s,:AttrType)) | ||
a = generators(s,:Attr) | ||
attrs = map((name,dom,codom)->name=>(dom,codom),nameof.(a),nameof.(dom.(a)),nameof.(codom.(a))) | ||
schema = SchemaTheory(obs,homs,attrtypes,attrs) | ||
schema | ||
end | ||
|
||
# create an acset schema from a basic schema | ||
function schemaACSet(s::BasicSchema) | ||
obs = objects(s) | ||
hs = [a=>(b,c) for (a,b,c) in homs(s)] | ||
ats = attrtypes(s) | ||
as = [a=>(b,c) for (a,b,c) in attrs(s)] | ||
schema = SchemaTheory(obs,hs,ats,as) | ||
schema | ||
end | ||
|
||
# create a @present schema from ACSet schema | ||
function schemaPresent(s) | ||
pres = Presentation(FreeSchema) | ||
for ob in parts(s, :Ob) | ||
add_generator!(pres, Ob(FreeSchema.Ob, s[ob, :obname])) | ||
end | ||
obs = generators(pres, :Ob) | ||
for hom in parts(s, :Hom) | ||
f = Hom(s[hom, :homname], obs[s[hom, :homsrc]], obs[s[hom, :homtgt]]) | ||
add_generator!(pres, f) | ||
end | ||
for attrtype in parts(s, :AttrType) | ||
add_generator!(pres, AttrType(FreeSchema.AttrType, s[attrtype, :attrtypename])) | ||
end | ||
attrtypes = generators(pres, :AttrType) | ||
for attr in parts(s, :Attr) | ||
f = Attr(s[attr, :attrname], obs[s[attr, :attrsrc]], attrtypes[s[attr, :attrtgt]]) | ||
add_generator!(pres, f) | ||
end | ||
pres | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
module TestMigrateRules | ||
|
||
using Test | ||
using StateCharts | ||
using Catlab | ||
|
||
# Create a schema with S, I and R as objects | ||
@present SchSIR(FreeSchema) begin | ||
Person::Ob | ||
(S,I,R)::Ob | ||
SPerson::Hom(S,Person) | ||
IPerson::Hom(I,Person) | ||
RPerson::Hom(R,Person) | ||
end | ||
|
||
SchSIRACSet=schemaACSet(SchSIR) | ||
|
||
@test obnames(SchSIRACSet) == [:Person, :S,:I,:R] | ||
@test homnames(SchSIRACSet) == [:SPerson,:IPerson,:RPerson] | ||
|
||
# Create a schema with a state chart as an attribute | ||
@present SchSIR2(FreeSchema) begin | ||
Person::Ob | ||
|
||
Inf::AttrType | ||
|
||
inf::Attr(Person,Inf) | ||
end | ||
|
||
SchSIRACSet2=schemaACSet(SchSIR2) | ||
|
||
@test obnames(SchSIRACSet2) == [:Person] | ||
@test attrtypenames(SchSIRACSet2) == [:Inf] | ||
@test attrnames(SchSIRACSet2) == [:inf] | ||
|
||
|
||
# test basic schema convert to acset | ||
basicschema = BasicSchema([:P,:V],[(:PV,:P,:V)],[:Inf,:Coord],[(:inf,:P,:Inf),(:coord,:V,:Coord)]) | ||
SchACSetBS = schemaACSet(basicschema) | ||
|
||
@test obnames(SchACSetBS) == [:P,:V] | ||
@test homnames(SchACSetBS) == [:PV] | ||
@test attrtypenames(SchACSetBS) == [:Inf,:Coord] | ||
@test attrnames(SchACSetBS) == [:inf,:coord] | ||
|
||
# test convert back to present schema | ||
@test SchSIR == schemaPresent(schemaACSet(SchSIR)) | ||
@test SchSIR2 == schemaPresent(schemaACSet(SchSIR2)) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters