Skip to content

Commit

Permalink
Update To New Petri API (#251)
Browse files Browse the repository at this point in the history
* Remove ModelingToolkit dependency from semanticmodels

* Updated to new Petri.jl interface

* Updated to new Petri.jl interface for petrimodels
  • Loading branch information
mehalter authored Jun 17, 2020
1 parent f85f5fe commit f772bef
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 88 deletions.
1 change: 0 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ version = "0.3.0"
Catlab = "134e5e36-593f-5add-ad60-77f754baafbe"
LightGraphs = "093fc24a-ae57-5d10-9952-331d41423f4d"
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
Petri = "4259d249-1051-49fa-8328-3f8ab9391c33"

[compat]
Expand Down
6 changes: 0 additions & 6 deletions src/CategoryTheory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,10 @@ module CategoryTheory

using SemanticModels
import Catlab.Doctrines: dom, codom
using MacroTools: prewalk, postwalk
using ModelingToolkit
import Base: append!, push!, deleteat!, delete!


export , AbstractMorph, FinSetMorph, dom, codom, verify, func, Decorated, decorations, undecorate, AbstractSpan, leftob, rightob, apexob, Span, left, right, DoublePushout, AbstractCospan, Cospan, pushout

import MacroTools.walk
walk(x::Operation, inner, outer) = outer(Operation(x.op, map(inner, x.args)))


(a::UnitRange, b::UnitRange) = 1:(length(a)+length(b))
(a::AbstractVector{Int}, b::AbstractVector{Int}) = vcat(a,b)
Expand Down
6 changes: 3 additions & 3 deletions src/PetriCospans.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ end

# TODO: The Model Toolkit interface was dumb, let's just do everything with integer variable numbers
# SEIR is Petri.Mode([1,2,3], [([1], [2]), ([1,2], [3]), ([3],[2])])
IntPetriModel(S, Δ) = Petri.Model(S, Δ, missing, missing)
IntPetriModel(S, Δ) = Petri.Model(S, Δ)
IntPetriModel(S, Δ, λ, ϕ) = Petri.Model(S, Δ, λ, ϕ)
NullModel(n::Int) = PetriModel(IntPetriModel(collect(1:n), Vector{Tuple{Vector{Int}, Vector{Int}}}(), missing, missing))
NullModel(n::Int) = PetriModel(IntPetriModel(collect(1:n), Vector{Tuple{Vector{Int}, Vector{Int}}}()))

function tcat(v::Vector{Tuple{Vector{Int},Vector{Int}}},
w::Vector{Tuple{Vector{Int},Vector{Int}}}, shift::Int)
Expand All @@ -116,7 +116,7 @@ function otimes_ipm(f::PetriModel, g::PetriModel)
domcat(v, w) = vcat(v, w.+length(M.S))
newS = domcat(M.S, N.S)
newΔ = tcat(M.Δ, N.Δ, length(M.S))
return PetriModel(Petri.Model(newS, newΔ, missing, missing))
return PetriModel(Petri.Model(newS, newΔ))
end

compose(f::FinSetMorph, g::FinSetMorph) = FinSetMorph(g.codom, func(g).(f.fun))
Expand Down
84 changes: 7 additions & 77 deletions src/PetriModels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
module PetriModels
import Base: show
using Petri
using ModelingToolkit
import ModelingToolkit: Constant, Variable
using MacroTools
import MacroTools: prewalk, postwalk
import MacroTools: prewalk
using Catlab.WiringDiagrams
import Catlab.Graphics.Graphviz: Graph

Expand All @@ -21,70 +19,10 @@ struct PetriModel <: AbstractModel
model::Petri.Model
end

OpVar(s::Symbol) = Operation(Variable(s), [])

function model(::Type{PetriModel}, m::Petri.Model)
return PetriModel(m)
end

function model(::Type{PetriModel}, d::WiringDiagram)::PetriModel
# TODO design Multiple Dispatch Lens API
vars = SemanticModels.WiringDiagrams.wirenames(d)
symvars = OpVar.(vars)
byvar = Dict{Symbol, Operation}()
homnames = Vector{Symbol}()
transitions = map(enumerate(boxes(d))) do (i, box)
invars = input_ports(box)
outvars = output_ports(box)
homname = box.value
push!(homnames, homname)
δ_in = length(invars) > 1 ? +(OpVar.( invars)...) : OpVar.(invars[1])
δ_out = length(outvars) > 1 ? +(OpVar.(outvars)...) : OpVar.(outvars[1])
return (δ_in, δ_out)
end
return PetriModel(Petri.Model(symvars, unique(transitions)))
end

function rewrite!(pm::PetriModel, pm2::PetriModel)
rewrite!(pm, pm2, Dict())
end

function rewrite!(pModel::PetriModel, pModel2::PetriModel, f::Dict)
pm = pModel.model
pm2 = pModel2.model
vars = map(pm.S) do s
s.op
end
@show
for i in 1:length(pm2.S)
s = pm2.S[i]
found = findfirst(vars .== (haskey(f, s) ? f[s].op : s.op))
if typeof(found) == Nothing
push!(pm.S, s)
push!(pm.Δ, pm2.Δ[i])
push!(pm.Λ, pm2.Λ[i])
push!(pm.Φ, pm2.Φ[i])
else
pm.Δ[found] = pm2.Δ[i] == Nothing ? m.Δ[found] : m2.Δ[i]
pm.Λ[found] = pm2.Λ[i] == Nothing ? m.Λ[found] : m2.Λ[i]
pm.Φ[found] = pm2.Φ[i] == Nothing ? m.Φ[found] : m2.Φ[i]
end
end
end

struct PetriSpan{L,C,R}
l::L
c::C
r::R
end

struct DPOProblem
rule::PetriSpan
c′::PetriModel
end

solve(p::DPOProblem) = pushout(p.rule.r, p.c′)

function equnion(a::Vector, b::Vector)
x = copy(a)
for item in b
Expand All @@ -101,15 +39,11 @@ dictReplace(item, dict) = prewalk(i -> i in keys(dict) ? dict[i] : i, item)
function (gModel::PetriModel, hModel::PetriModel)
g = gModel.model
h = hModel.model
dict = Dict(i => any(isequal.(i, g.S)) ? i : Operation(i.op, Expression[ModelingToolkit.Constant(2)]) for i in h.S)
dict = Dict(i => any(isequal.(i, g.S)) ? i : Symbol(i, "") for i in h.S)
newS = [dictReplace(n, dict) for n in h.S]
newΔ = [(dictReplace(n[1], dict), dictReplace(n[2], dict)) for n in h.Δ]
newΛ = [dictReplace(n, dict) for n in h.Λ]
newΦ = [dictReplace(n, dict) for n in h.Φ]
newΔ = [(dictReplace(first(n), dict), dictReplace(last(n), dict)) for n in h.Δ]
PetriModel(Petri.Model(union(g.S, newS),
equnion(g.Δ, newΔ),
equnion(g.Λ, newΛ),
equnion(g.Φ, newΦ)))
equnion(g.Δ, newΔ)))
end

function (f::FinSetMorph)(gModel::G) where G <: PetriModel
Expand All @@ -124,7 +58,7 @@ function (f::FinSetMorph)(gModel::G) where G <: PetriModel
push!(outΔ, (ins,outs))
end
out = deepcopy(g)
PetriModel(Petri.Model(outS, outΔ, out.Λ, out.Φ))
PetriModel(Petri.Model(outS, outΔ))
end

# TODO: this version uses model toolkit variables, we want to delete it
Expand All @@ -151,9 +85,7 @@ function pushout(pModel::PetriModel, pModel2::PetriModel)
pm2 = pModel2.model
states = equnion(pm.S, pm2.S)
Δ = equnion(pm.Δ, pm2.Δ)
Λ = equnion(pm.Λ, pm2.Λ)
Φ = equnion(pm.Φ, pm2.Φ)
return model(PetriModel, Petri.Model(states, Δ, Λ, Φ))
return model(PetriModel, Petri.Model(states, Δ))
end


Expand All @@ -170,9 +102,7 @@ function dropdown(pL::PetriModel, pC::PetriModel, pL′::PetriModel)
pl′ = pL′.model
states = union(pl′.S, pc.S)
Δ = union(setdiff(pl′.Δ, pl.Δ), pc.Δ)
Λ = union(setdiff(pl′.Λ, pl.Λ), pc.Λ)
Φ = union(setdiff(pl′.Φ, pl.Φ), pc.Φ)
return model(PetriModel, Petri.Model(states, Δ, Λ, Φ))
return model(PetriModel, Petri.Model(states, Δ))
end

end
2 changes: 1 addition & 1 deletion src/SemanticModels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ include("PetriModels.jl")
include("RelOlogModels.jl")
include("GraphModels.jl")
include("OpenModels.jl")
include("OpenPetris.jl")
#include("OpenPetris.jl")
include("PetriCospans.jl")
include("WiringDiagrams.jl")

Expand Down

0 comments on commit f772bef

Please sign in to comment.