Skip to content

Commit

Permalink
Merge branch 'main' into jpf/formulas-examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jpfairbanks authored Feb 8, 2024
2 parents c840496 + cb10c4d commit 01c0994
Show file tree
Hide file tree
Showing 16 changed files with 212 additions and 77 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/TagBot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ jobs:
steps:
- uses: JuliaRegistries/TagBot@v1
with:
token: ${{ secrets.RELEASE_TOKEN }}
token: ${{ secrets.SERVICE_TOKEN }}
16 changes: 8 additions & 8 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ name = "SyntacticModels"
uuid = "22bb929c-8bcf-4852-b455-eb3e1675e09c"
license = "MIT"
authors = ["James Fairbanks <[email protected]>"]
version = "0.1.0"
version = "0.2.0"

[deps]
ACSets = "227ef7b5-1206-438b-ac65-934d6da304b8"
Catlab = "134e5e36-593f-5add-ad60-77f754baafbe"
Decapodes = "679ab3ea-c928-4fe6-8d59-fd451142d391"
DiagrammaticEquations = "6f00c28b-6bed-4403-80fa-30e0dc12f317"
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"
Decapodes = "0.4"
StructTypes = "1"
Catlab = "0.16"
DiagrammaticEquations = "0.1.1"
JSON = "0.21"
JSON3 = "1"
MLStyle = "0.4"
julia = "1.9"
Reexport = "1"
StructTypes = "1"
julia = "1.10"
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

[![Stable Documentation](https://img.shields.io/badge/docs-stable-blue.svg)](https://AlgebraicJulia.github.io/SyntacticModels.jl/stable)
[![Development Documentation](https://img.shields.io/badge/docs-dev-blue.svg)](https://AlgebraicJulia.github.io/SyntacticModels.jl/dev)
[![Code Coverage](https://codecov.io/gh/AlgebraicJulia/SyntacticModels.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/AlgebraicJulia/SyntacticModelse.jl)
[![Code Coverage](https://codecov.io/gh/AlgebraicJulia/SyntacticModels.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/AlgebraicJulia/SyntacticModels.jl)
[![CI/CD](https://github.com/AlgebraicJulia/SyntacticModels.jl/actions/workflows/julia_ci.yml/badge.svg)](https://github.com/AlgebraicJulia/SyntacticModels.jl/actions/workflows/julia_ci.yml)

A package for building languages for model representations.

## 🛠️ Usage

See the literate code examples in the [documentation](https://AlgebraicJulia.github.io/SyntacticModels.jl/dev).
See the literate code examples in the [documentation](https://AlgebraicJulia.github.io/SyntacticModels.jl/dev).
2 changes: 1 addition & 1 deletion docs/Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[deps]
ACSets = "227ef7b5-1206-438b-ac65-934d6da304b8"
Catlab = "134e5e36-593f-5add-ad60-77f754baafbe"
Decapodes = "679ab3ea-c928-4fe6-8d59-fd451142d391"
DiagrammaticEquations = "6f00c28b-6bed-4403-80fa-30e0dc12f317"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
Expand Down
11 changes: 6 additions & 5 deletions docs/literate/composite_models_examples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import SyntacticModels.ASKEMDecapodes.Decapodes as Decapodes
using Catlab
using Catlab.RelationalPrograms
using Catlab.WiringDiagrams
using DiagrammaticEquations
using Test
using JSON3

Expand Down Expand Up @@ -58,7 +59,7 @@ h = AMR.Header("harmonic_oscillator",

# The easiest way to write down a DecaExpr is in our DSL and calling the parser.
# This formula is $$∂ₜ(∂ₜ(x)) = -kx$$.
dexpr = Decapodes.parse_decapode(quote
dexpr = DiagrammaticEquations.parse_decapode(quote
X::Form0{Point}
V::Form0{Point}

Expand All @@ -81,7 +82,7 @@ d2 = ASKEMDecaExpr(
"modelreps.io/SummationDecapode",
"Velocity makes it get hot, but you dissipate heat away from Q₀",
"SummationDecapode", "v1.0"),
Decapodes.parse_decapode(quote
DiagrammaticEquations.parse_decapode(quote
V::Form0{Point}
Q::Form0{Point}
κ::Constant{Point}
Expand Down Expand Up @@ -134,7 +135,7 @@ uwdʰ = UWDExpr([v, Q], [Statement(:drag, [v, Q₊]), Statement(:cooling, [Q₋,
#
drag = ASKEMDecaExpr(
AMR.Header("DragHeat", "modelreps.io/SummationDecapode", "velocity makes it get hot", "SummationDecapode", "v1.0"),
Decapodes.parse_decapode(quote
DiagrammaticEquations.parse_decapode(quote
V::Form0{Point}
Q₊::Form0{Point}
κ::Constant{Point}
Expand All @@ -148,7 +149,7 @@ drag = ASKEMDecaExpr(

cooling = ASKEMDecaExpr(
AMR.Header("NetwonCooling", "modelreps.io/SummationDecapode", "heat dissipates to the enviornment", "SummationDecapode", "v1.0"),
Decapodes.parse_decapode(quote
DiagrammaticEquations.parse_decapode(quote
Q₋::Form0{Point}
Q₀::Parameter{Point}
Q::Form0{Point}
Expand All @@ -162,7 +163,7 @@ cooling = ASKEMDecaExpr(

superposition = ASKEMDecaExpr(
AMR.Header("LinearSuperpositon", "modelreps.io/SummationDecapode", "variables be addin", "SummationDecapode", "v1.0"),
Decapodes.parse_decapode(quote
DiagrammaticEquations.parse_decapode(quote
X::Form0{Point}
Y::Form0{Point}
T::Form0{Point}
Expand Down
6 changes: 3 additions & 3 deletions docs/literate/decapodes_examples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ using JSON3
using Catlab
using ACSets
using ACSets.JSONACSets
using Decapodes
using DiagrammaticEquations
using Test

# Build the header object describing the model.
Expand All @@ -29,7 +29,7 @@ h = AMR.Header("harmonic_oscillator",
# The easiest way to write down a DecaExpr is in our DSL and calling the parser.
# This example is $$x'' = -kx$$. We have manual transformed this into first order ODE
# by introducing a velocity variable for $$x'$$.
dexpr = Decapodes.parse_decapode(quote
dexpr = DiagrammaticEquations.parse_decapode(quote
X::Form0{Point}
V::Form0{Point}

Expand All @@ -46,7 +46,7 @@ mexpr = ASKEMDecaExpr(h, dexpr)
# Convert a the DecaExpr to a SummationDecapode which is the
# combinatorial representation. The converter lives in Decapodes/src/language.jl.

d = Decapodes.SummationDecapode(mexpr.model)
d = DiagrammaticEquations.SummationDecapode(mexpr.model)

# To visualize the Decapode as a compute graph, you can use Graphviz

Expand Down
4 changes: 3 additions & 1 deletion src/SyntacticModelsBase.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module SyntacticModelsBase

using StructTypes
using DiagrammaticEquations
using JSON3
import Base: Dict

Expand All @@ -23,6 +24,7 @@ _dict(x::Symbol) = x
_dict(x::String) = x
_dict(x::Number) = x
_dict(x::AbstractVector) = map(_dict, x)
_dict(x::DiagrammaticEquations.decapodes.Judgement) = x

""" _dict(x::T) where T <: AbstractTerm
Expand Down Expand Up @@ -60,4 +62,4 @@ StructTypes.StructType(::Type{AbstractTerm}) = StructTypes.AbstractType()
This is how you tell StructTypes where to look for the name of a type when reading the type back in from JSON.
"""
StructTypes.subtypekey(::Type{AbstractTerm}) = :_type
end
end
49 changes: 47 additions & 2 deletions src/amr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ export Math, MathML, ExpressionFormula, Unit, Distribution, Observable, Expressi
Rate, Initial, Parameter, Time,
StandardUniform, Uniform, StandardNormal, Normal, PointMass,
Semantic, Header, ODERecord, ODEList, Typing, ASKEModel,
distro_string, amr_to_string
distro_string, amr_to_string,
Annotation, Note, Name, Description, Grounding, Units

using Reexport
@reexport using MLStyle
@reexport using ACSets
using ACSets.ADTs
using ACSets.ACSetInterface
using StructTypes

using ..SyntacticModelsBase

Expand Down Expand Up @@ -64,6 +66,24 @@ end
# Stratification
end

@data Note <: AbstractTerm begin
Name(str::String)
Description(str::String)
Grounding(ontology::String, identifier::String)
Units(expression::String)
end

StructTypes.StructType(::Type{Note}) = StructTypes.AbstractType()
StructTypes.subtypekey(::Type{Note}) = :_type
StructTypes.subtypes(::Type{Note}) =
(Name=Name,Description=Description,Grounding=Grounding,Units=Units,)

@as_record struct Annotation{E,T} <: AbstractTerm
entity::E
type::T
note::Note
end

@as_record struct Header <: AbstractTerm
name::String
schema::String
Expand Down Expand Up @@ -92,6 +112,19 @@ function distro_expr(d::Distribution)
return Base.Meta.parse(distro_string(d))
end

function note_string(n::Note)
@match n begin
Name(n) => "Name($n)"
Description(d) => "Description($d)"
Grounding(ont, ident) => "Grounding($ont,$ident)"
Units(e) => "Units($e)"
end
end

function note_expr(n::Note)
return Base.Meta.parse(note_string(n))
end

padlines(ss::Vector, n) = map(ss) do s
" "^n * s
end
Expand Down Expand Up @@ -119,6 +152,7 @@ function amr_to_string(amr)
xs::Vector => map(!, xs)
Typing(system, map) => "Typing = begin\n$(padlines(!system, 2))\nTypeMap = [\n$(padlines(!map, 2))]\nend"
ASKEModel(h, m, s) => "$(!h)\n$(!m)\n\n$(!s)"
Annotation(e,t,n) => "Annotation = $(String(e)),$(String(t)): $(note_string(n))"
end
end
end
Expand Down Expand Up @@ -155,6 +189,7 @@ function amr_to_expr(amr)
xs::Vector => begin ys = map(!, xs); block(ys) end
Typing(system, map) => :(Typing = $(!system); TypeMap = $(block(map)))
ASKEModel(h, m, s) => :($(!h);$(!m);$(!s))
Annotation(e,t,n) => "Annotation = $(String(e)),$(String(t)): $(note_expr(n))"
end
end
end
Expand Down Expand Up @@ -225,6 +260,17 @@ end

load(::Type{Distribution}, ::Nothing) = PointMass(missing)

function load(::Type{Note}, d::AbstractDict)
@match d begin
Dict("type"=>"Name", "parameters"=>p) => Name(p["str"])
Dict("type"=>"Description", "parameters"=>p) => Description(p["str"])
Dict("type"=>"Grounding", "parameters"=>p) => Grounding(p["ontology"], p["identifier"])
Dict("type"=>"Units", "parameters"=>p) => Units(p["expression"])
end
end
function load(::Type{Annotation}, d::AbstractDict)
Annotation(d["entity"], d["type"], load(Note,d["note"]))
end

function load(::Type{Parameter}, d::AbstractDict)
u = load(Unit, d)
Expand Down Expand Up @@ -332,7 +378,6 @@ function load(d::Type{Distribution}, ex::Expr)
end
end


function load(::Type{Parameter}, ex::Expr)
name, desc, ex = docval(ex)
id, u, val, dist = @matchast ex quote
Expand Down
4 changes: 2 additions & 2 deletions src/composite_models.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export CompositeModelExpr, OpenModel, OpenDecapode, CompositeModel, interface, o

using MLStyle
using Catlab
using Decapodes
using DiagrammaticEquations
using StructTypes

using ..SyntacticModelsBase
Expand Down Expand Up @@ -83,4 +83,4 @@ function OpenDecapode(m::CompositeModel)
OpenDecapode(ASKEMDecapode(m.header,apex(composite)), feet)
end

end
end
47 changes: 24 additions & 23 deletions src/decapodes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ using ..SyntacticModelsBase
using ..AMR

using StructTypes
using Decapodes
using DiagrammaticEquations
using DiagrammaticEquations.Deca
using MLStyle

@data ASKEMDeca <: AbstractTerm begin
ASKEMDecaExpr(header::AMR.Header, model::Decapodes.DecaExpr)
ASKEMDecapode(header::AMR.Header, model::Decapodes.SummationDecapode)
ASKEMDecaExpr(header::AMR.Header, model::DiagrammaticEquations.DecaExpr, annotations::Vector{AMR.Annotation{Symbol,Symbol}})
ASKEMDecapode(header::AMR.Header, model::DiagrammaticEquations.SummationDecapode, annotations::Vector{AMR.Annotation{Symbol,Symbol}})
end

@doc """ ASKEMDeca
Expand All @@ -25,37 +26,37 @@ ASKEMDeca
Stores the syntactic expression of a Decapode Expression with the
model metadata for ASKEM AMR conformance.
"""
ASKEMDecaExpr
ASKEMDecaExpr(header::AMR.Header, model::DiagrammaticEquations.DecaExpr) = ASKEMDecaExpr(header,model,Vector{AMR.Annotation{Symbol,Symbol}}())

@doc """ ASKEMDecapode
Stores the combinatorial representation of a Decapode with the
model metadata for ASKEM AMR conformance.
"""
ASKEMDecapode
ASKEMDecapode(header::AMR.Header, model::DiagrammaticEquations.SummationDecapode) = ASKEMDecapode(header,model,Vector{AMR.Annotation{Symbol,Symbol}}())

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
SyntacticModelsBase._dict(x::T) where {T<:Union{DiagrammaticEquations.DecaExpr, DiagrammaticEquations.Equation, DiagrammaticEquations.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
StructTypes.StructType(::Type{DiagrammaticEquations.Equation}) = StructTypes.AbstractType()
StructTypes.subtypekey(::Type{DiagrammaticEquations.Equation}) = :_type
StructTypes.subtypes(::Type{DiagrammaticEquations.Equation}) = (Eq=Eq,)

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

end
15 changes: 9 additions & 6 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
[deps]
SyntacticModels = "22bb929c-8bcf-4852-b455-eb3e1675e09c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
ACSets = "227ef7b5-1206-438b-ac65-934d6da304b8"
Catlab = "134e5e36-593f-5add-ad60-77f754baafbe"
DiagrammaticEquations = "6f00c28b-6bed-4403-80fa-30e0dc12f317"
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"
ACSets = "227ef7b5-1206-438b-ac65-934d6da304b8"
Catlab = "134e5e36-593f-5add-ad60-77f754baafbe"
Decapodes = "679ab3ea-c928-4fe6-8d59-fd451142d391"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
SyntacticModels = "22bb929c-8bcf-4852-b455-eb3e1675e09c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[compat]
Catlab = "0.16"
Loading

0 comments on commit 01c0994

Please sign in to comment.