Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip #13

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

wip #13

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@ version = "0.1.0"
ACSets = "227ef7b5-1206-438b-ac65-934d6da304b8"
Catlab = "134e5e36-593f-5add-ad60-77f754baafbe"
Decapodes = "679ab3ea-c928-4fe6-8d59-fd451142d391"
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"
MLStyle = "d8e11817-5142-5d16-987a-aa16d5891078"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
StructTypes = "856f2bd8-1eba-4b0a-8007-ebc267875bd4"

[compat]
Reexport = "1"
Catlab = "0.15"
JSON = "0.21"
ACSets = "0.2"
Catlab = "0.15"
Decapodes = "0.4"
StructTypes = "1"
JSON = "0.21"
JSON3 = "1"
MLStyle = "0.4"
Reexport = "1"
StructTypes = "1"
julia = "1.9"
54 changes: 52 additions & 2 deletions src/SyntacticModels.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,59 @@
module SyntacticModels

include("SyntacticModelsBase.jl")
#-----------------------------------------------------------------------------# "SyntacticModelsBase"
using InteractiveUtils: subtypes
using StructTypes

""" AbstractTerm

The super type for all SyntacticModsels types. This abstract type exists so that we can write generic methods that work on any term in any of the domain specific syntaxes.
For example, serializing to a Dictionary uses some reflection snippet that works for arbitrary types, but we only want to apply it to things that should be serialized like a Term.
"""
abstract type AbstractTerm end

function StructTypes.StructType(::Type{T}) where {T <: AbstractTerm}
isconcretetype(T) ? StructTypes.CustomStruct() : StructTypes.AbstractType()
end


# lowering/unlowering
function StructTypes.lower(x::T) where {T <: AbstractTerm}
(_type = T.name.name, NamedTuple(k => getfield(x, k) for k in fieldnames(T))...)
end

function StructTypes.lowertype(::Type{T}) where {T <: AbstractTerm}
NamedTuple{(:_type, fieldnames(T)...), Tuple{Symbol, fieldtypes(T)...}}
end

# Doesn't work with 1-field structs
# (::Type{T})(x::StructTypes.lowertype(Type{T})) where {T <: AbstractTerm} = T(x[fieldnames(T)]...)


function concrete_subtypes(T)
out = Type[]
for S in subtypes(T)
isconcretetype(S) ? push!(out, S) : append!(out, concrete_subtypes(S))
end
return out
end

StructTypes.subtypes(::Type{T}) where {T <: AbstractTerm} = Dict(T.name.name => T for T in concrete_subtypes(T))

StructTypes.subtypekey(T::Type{<: AbstractTerm}) = :_type


#-----------------------------------------------------------------------------# includes
include("amr.jl")
include("decapodes.jl")
include("uwd.jl")
include("composite_models.jl")

end
#-----------------------------------------------------------------------------# Constructors
for T in concrete_subtypes(AbstractTerm)
@eval function $(parentmodule(T)).$(T.name.name)(x::NamedTuple)
args = x[fieldnames($T)]
$(parentmodule(T)).$(T.name.name)(args...)
end
end

end
63 changes: 0 additions & 63 deletions src/SyntacticModelsBase.jl

This file was deleted.

42 changes: 22 additions & 20 deletions src/amr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ using Reexport
using ACSets.ADTs
using ACSets.ACSetInterface

using ..SyntacticModelsBase
using ..SyntacticModels: AbstractTerm

@data MathML <: AbstractTerm begin
Math(String)
Expand Down Expand Up @@ -42,6 +42,8 @@ nounit = Unit("", nomath)
PointMass(value)
end

StandardNormal(::Dict) = StandardNormal()

@as_record struct Observable{T <: AbstractTerm}
id::Symbol
name::String
Expand Down Expand Up @@ -73,7 +75,7 @@ end
end

@as_record struct ASKEModel <: AbstractTerm
header::Header
header::Header
model::ACSetSpec
semantics::Vector{Semantic}
end
Expand Down Expand Up @@ -114,7 +116,7 @@ function amr_to_string(amr)
m::ACSetSpec => "Model = begin\n$(padlines(sprint(show, m),2))\nend"
ODEList(l) => "ODE_Equations = begin\n" * padlines(join(map(!, l), "\n")) * "\nend"
ODERecord(rts, init, para, time) => join(vcat(["ODE_Record = begin\n"], !rts , !init, !para, [!time, "end"]), "\n")
vs::Vector{Pair} => map(vs) do v; "$(v[1]) => $(v[2])," end |> x-> join(x, "\n")
vs::Vector{Pair} => map(vs) do v; "$(v[1]) => $(v[2])," end |> x-> join(x, "\n")
vs::Vector{Semantic} => join(map(!, vs), "\n\n")
xs::Vector => map(!, xs)
Typing(system, map) => "Typing = begin\n$(padlines(!system, 2))\nTypeMap = [\n$(padlines(!map, 2))]\nend"
Expand All @@ -125,7 +127,7 @@ end

block(exprs) = begin
q = :(begin

end)
append!(q.args, exprs)
return q
Expand Down Expand Up @@ -252,14 +254,14 @@ end

function load(::Type{Header}, d::AbstractDict)
@match d begin
Dict("name"=>n, "schema"=>s, "description"=>d, "schema_name"=>sn, "model_version"=>mv) => Header(n,s,d,sn,mv)
Dict("name"=>n, "schema"=>s, "description"=>d, "schema_name"=>sn, "model_version"=>mv) => Header(n,s,d,sn,mv)
_ => error("Information for Header was not found in $d")
end
end

function load(::Type{Typing}, d::AbstractDict)
@match d begin
Dict("type_system"=>s, "type_map"=>m) => begin @show m; Typing(petrispec(s), [x[1]=> x[2] for x in m]) end
Dict("type_system"=>s, "type_map"=>m) => begin @show m; Typing(petrispec(s), [x[1]=> x[2] for x in m]) end
_ => error("Typing judgement was not properly encoded in $d")
end
end
Expand Down Expand Up @@ -323,11 +325,11 @@ end

function load(d::Type{Distribution}, ex::Expr)
@matchast ex quote
U(0,1) => StandardUniform
U($min,$max) => Uniform(min, max)
N(0,1) => StandardNormal
N($mu,$var) => Normal(mu, var)
δ($value) => PointMass(value)
U(0,1) => StandardUniform
U($min,$max) => Uniform(min, max)
N(0,1) => StandardNormal
N($mu,$var) => Normal(mu, var)
δ($value) => PointMass(value)
_ => error("Failed to find distribution in $ex")
end
end
Expand All @@ -346,16 +348,16 @@ end

function load(::Type{ODEList}, ex::Expr)
map(ex.args[2].args) do arg
try
try
return load(Rate, arg)
catch ErrorException
try
catch ErrorException
try
return load(Initial, arg)
catch ErrorException
try
catch ErrorException
try
return load(Parameter, arg)
catch ErrorException
try
catch ErrorException
try
return load(Time, arg)
catch
return nothing
Expand Down Expand Up @@ -410,9 +412,9 @@ function load(::Type{ASKEModel}, ex::Expr)
Expr(:(=), :ODE_Record, body) => load(ODEList, arg)
Expr(:(=), :ODE_Equations, body) => load(ODEList, arg)
Expr(:(=), :Typing, body) => load(Typing, arg)
_ => arg
_ => arg
end
end
ASKEModel(elts[2][1], elts[2][2], [elts[4], elts[6]])
end
end # module end
end # module end
17 changes: 6 additions & 11 deletions src/composite_models.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ export CompositeModelExpr, OpenModel, OpenDecapode, CompositeModel, interface, o
using MLStyle
using Catlab
using Decapodes
using StructTypes

using ..SyntacticModelsBase
using ..SyntacticModels: AbstractTerm
using ..AMR
using ..ASKEMDecapodes
using ..ASKEMUWDs
Expand All @@ -30,10 +29,6 @@ end
"""
CompositeModel

StructTypes.StructType(::Type{CompositeModel}) = StructTypes.AbstractType()
StructTypes.subtypekey(::Type{CompositeModel}) = :_type
StructTypes.subtypes(::Type{CompositeModel}) = (OpenModel=OpenModel, OpenDecapode=OpenDecapode, CompositeModelExpr)


""" interface(m::CompositeModel)

Expand All @@ -53,9 +48,9 @@ open_decapode(d::ASKEMDecapode, interface) = Open(d.model, interface)

CompositeModels can be flattened into a single level of model with the oapply function.

!!! warning
Because the oapply algorithm operates on the compute graph representation of the equations, it does not produce syntactic equations.
Calls to oapply produce instances of OpenDecapode and not DecaExpr.
!!! warning
Because the oapply algorithm operates on the compute graph representation of the equations, it does not produce syntactic equations.
Calls to oapply produce instances of OpenDecapode and not DecaExpr.
Software that expects to consume decapodes should plan to interact with both forms.
"""
function Catlab.oapply(m::CompositeModel)
Expand All @@ -67,7 +62,7 @@ function Catlab.oapply(m::CompositeModel)
# For a composite model, we have to recurse
CompositeModelExpr(h, pattern, components) => begin
uwd = ASKEMUWDs.construct(RelationDiagram, pattern)
Ms = map(m.components) do mᵢ;
Ms = map(m.components) do mᵢ;
!(mᵢ) # oapply all the component models recursively
end
# OpenDecapode(ASKEMDecapode(h, apex(!(uwd, Ms))), interface(m)) # Then we call the oapply from Decapodes.
Expand All @@ -83,4 +78,4 @@ function OpenDecapode(m::CompositeModel)
OpenDecapode(ASKEMDecapode(m.header,apex(composite)), feet)
end

end
end
27 changes: 1 addition & 26 deletions src/decapodes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ module ASKEMDecapodes

export ASKEMDecaExpr, ASKEMDecapode

using ..SyntacticModelsBase
using ..SyntacticModels: AbstractTerm
using ..AMR

using StructTypes
using Decapodes
using MLStyle

Expand Down Expand Up @@ -34,28 +33,4 @@ model metadata for ASKEM AMR conformance.
"""
ASKEMDecapode

StructTypes.StructType(::Type{ASKEMDeca}) = StructTypes.AbstractType()
StructTypes.subtypekey(::Type{ASKEMDeca}) = :_type
StructTypes.subtypes(::Type{ASKEMDeca}) = (ASKEMDecaExpr=ASKEMDecaExpr, ASKEMDecapode=ASKEMDecapode)

SyntacticModelsBase._dict(x::T) where {T<:Union{Decapodes.DecaExpr, Decapodes.Equation, Decapodes.Term}} = begin
Dict(:_type => typename_last(T), [k=>_dict(getfield(x, k)) for k in fieldnames(T)]...)
end

StructTypes.StructType(::Type{Decapodes.Equation}) = StructTypes.AbstractType()
StructTypes.subtypekey(::Type{Decapodes.Equation}) = :_type
StructTypes.subtypes(::Type{Decapodes.Equation}) = (Eq=Eq,)

StructTypes.StructType(::Type{Decapodes.Term}) = StructTypes.AbstractType()
StructTypes.subtypekey(::Type{Decapodes.Term}) = :_type
StructTypes.subtypes(::Type{Decapodes.Term}) = (Var=Decapodes.Var,
Lit=Decapodes.Lit,
Judgement=Decapodes.Judgement,
AppCirc1=Decapodes.AppCirc1,
App1=Decapodes.App1,
App2=Decapodes.App2,
Plus=Decapodes.Plus,
Mult=Decapodes.Mult,
Tan=Decapodes.Tan)

end
Loading