From 10b79ec111925f1236b15ce06fcad9fdc4419d0b Mon Sep 17 00:00:00 2001 From: slwu89 Date: Mon, 28 Aug 2023 17:42:34 -0700 Subject: [PATCH 1/4] rebase onto main --- src/AlgebraicPetri.jl | 22 ++++++++++++++- test/algebraicpetri/types.jl | 53 ++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/src/AlgebraicPetri.jl b/src/AlgebraicPetri.jl index 743b769e..28ecba78 100644 --- a/src/AlgebraicPetri.jl +++ b/src/AlgebraicPetri.jl @@ -5,7 +5,7 @@ module AlgebraicPetri export SchPetriNet, PetriNet, OpenPetriNetOb, AbstractPetriNet, ns, nt, ni, no, os, ot, is, it, add_species!, add_transition!, add_transitions!, - add_input!, add_inputs!, add_output!, add_outputs!, inputs, outputs, + add_input!, add_inputs!, add_output!, add_outputs!, inputs, outputs, induced_subnet, TransitionMatrices, vectorfield, vectorfield_expr, flatten_labels, SchLabelledPetriNet, LabelledPetriNet, AbstractLabelledPetriNet, sname, tname, snames, tnames, SchReactionNet, ReactionNet, AbstractReactionNet, concentration, concentrations, rate, rates, @@ -272,6 +272,26 @@ inputs(p::AbstractPetriNet, t) = subpart(p, incident(p, t, :it), :is) """ outputs(p::AbstractPetriNet, t) = subpart(p, incident(p, t, :ot), :os) +""" induced_subnet(p::T, t) + +Return the induced subnet consisting of the transition `t`, all input +places and input arcs, and all output places and output arcs. The returned +object will be of the same type as `p`. +""" +function induced_subnet(p::T, t) where {T <: AbstractPetriNet} + @assert t ∈ parts(p, :T) + + input_arcs = incident(p, t, :it) + output_arcs = incident(p, t, :ot) + + input_places = subpart(p, input_arcs, :is) + output_places = subpart(p, output_arcs, :os) + + subnet = T() + copy_parts!(subnet, p, T=t, I=input_arcs, O=output_arcs, S=union(input_places, output_places)) + return subnet +end + valueat(x::Number, u, t) = x valueat(f::Function, u, t) = try diff --git a/test/algebraicpetri/types.jl b/test/algebraicpetri/types.jl index 441a8f63..568c0451 100644 --- a/test/algebraicpetri/types.jl +++ b/test/algebraicpetri/types.jl @@ -176,4 +176,57 @@ for p in [sir_proplpetri, sir_proplrxn] @test Open(p, [:S], [:I], [:R]) == Open(p) end +# test subnet for each type of net +for net in [sir_petri, sir_rxn] + si_net = induced_subnet(net, 1) + + @test ns(si_net) == 2 + @test nt(si_net) == 1 + @test is(si_net) == [1,2] + @test os(si_net) == [2,2] + + r_net = induced_subnet(net, 2) + + @test ns(r_net) == 2 + @test nt(r_net) == 1 + @test is(r_net) == [1] + @test os(r_net) == [2] +end + +for net in [sir_lpetri, sir_lrxn] + si_net = induced_subnet(sir_lpetri, 1) + + @test ns(si_net) == 2 + @test nt(si_net) == 1 + @test is(si_net) == [1,2] + @test os(si_net) == [2,2] + @test only(si_net[:tname]) == :inf + @test si_net[:sname] == [:S,:I] + + r_net = induced_subnet(sir_lpetri, 2) + + @test ns(r_net) == 2 + @test nt(r_net) == 1 + @test is(r_net) == [1] + @test os(r_net) == [2] + @test only(r_net[:tname]) == :rec + @test r_net[:sname] == [:I,:R] +end + +sir_net = induced_subnet(sir_petri, [1,2]) +sir_net == sir_petri + +test_petri = PetriNet( + 6, + 1 => (3,4), + 1 => 2, + 5 => 6 +) + +test_net = induced_subnet(test_petri, [1,2]) +@test ns(test_net) == 4 +@test nt(test_net) == 2 +@test is(test_net) == [1,1] +@test os(test_net) == [2,3,4] + end From 93faedf0114ab8644ce2c62c34da758ec35be941 Mon Sep 17 00:00:00 2001 From: slwu89 <10673535+slwu89@users.noreply.github.com> Date: Mon, 27 Mar 2023 18:48:05 -0700 Subject: [PATCH 2/4] generalize subnet to any number of transitions; add tests --- src/AlgebraicPetri.jl | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/AlgebraicPetri.jl b/src/AlgebraicPetri.jl index 28ecba78..67dd9e1b 100644 --- a/src/AlgebraicPetri.jl +++ b/src/AlgebraicPetri.jl @@ -279,16 +279,20 @@ places and input arcs, and all output places and output arcs. The returned object will be of the same type as `p`. """ function induced_subnet(p::T, t) where {T <: AbstractPetriNet} - @assert t ∈ parts(p, :T) + induced_subnet(p, [t]) +end + +function induced_subnet(p::T, t::AbstractVector{Int}) where {T <: AbstractPetriNet} + @assert all([tt ∈ parts(p,:T) for tt in t]) input_arcs = incident(p, t, :it) output_arcs = incident(p, t, :ot) - input_places = subpart(p, input_arcs, :is) - output_places = subpart(p, output_arcs, :os) + input_places = [subpart(p, input_i, :is) for input_i in input_arcs] + output_places = [subpart(p, output_i, :os) for output_i in output_arcs] subnet = T() - copy_parts!(subnet, p, T=t, I=input_arcs, O=output_arcs, S=union(input_places, output_places)) + copy_parts!(subnet, p, T=t, I=vcat(input_arcs...), O=vcat(output_arcs...), S=union(input_places..., output_places...)) return subnet end From ea215165dd5abdc8b39298097cb391be17fdf41f Mon Sep 17 00:00:00 2001 From: slwu89 <10673535+slwu89@users.noreply.github.com> Date: Mon, 27 Mar 2023 18:49:07 -0700 Subject: [PATCH 3/4] clarify docstring --- src/AlgebraicPetri.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AlgebraicPetri.jl b/src/AlgebraicPetri.jl index 67dd9e1b..6642af7c 100644 --- a/src/AlgebraicPetri.jl +++ b/src/AlgebraicPetri.jl @@ -274,7 +274,7 @@ outputs(p::AbstractPetriNet, t) = subpart(p, incident(p, t, :ot), :os) """ induced_subnet(p::T, t) -Return the induced subnet consisting of the transition `t`, all input +Return the induced subnet consisting of the transition(s) in `t`, all input places and input arcs, and all output places and output arcs. The returned object will be of the same type as `p`. """ From 13f861832f82eba46016c5eeae0be92f307bde0f Mon Sep 17 00:00:00 2001 From: slwu89 Date: Sat, 4 May 2024 16:16:08 -0700 Subject: [PATCH 4/4] use subobj interface --- src/AlgebraicPetri.jl | 13 ++----------- test/algebraicpetri/types.jl | 4 ++-- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/src/AlgebraicPetri.jl b/src/AlgebraicPetri.jl index 6642af7c..e050e6f2 100644 --- a/src/AlgebraicPetri.jl +++ b/src/AlgebraicPetri.jl @@ -283,17 +283,8 @@ function induced_subnet(p::T, t) where {T <: AbstractPetriNet} end function induced_subnet(p::T, t::AbstractVector{Int}) where {T <: AbstractPetriNet} - @assert all([tt ∈ parts(p,:T) for tt in t]) - - input_arcs = incident(p, t, :it) - output_arcs = incident(p, t, :ot) - - input_places = [subpart(p, input_i, :is) for input_i in input_arcs] - output_places = [subpart(p, output_i, :os) for output_i in output_arcs] - - subnet = T() - copy_parts!(subnet, p, T=t, I=vcat(input_arcs...), O=vcat(output_arcs...), S=union(input_places..., output_places...)) - return subnet + subnet = Subobject(p, T=t) + return dom(hom(~(¬subnet))) end valueat(x::Number, u, t) = x diff --git a/test/algebraicpetri/types.jl b/test/algebraicpetri/types.jl index 568c0451..f477097b 100644 --- a/test/algebraicpetri/types.jl +++ b/test/algebraicpetri/types.jl @@ -226,7 +226,7 @@ test_petri = PetriNet( test_net = induced_subnet(test_petri, [1,2]) @test ns(test_net) == 4 @test nt(test_net) == 2 -@test is(test_net) == [1,1] -@test os(test_net) == [2,3,4] +@test ni(test_net) == 2 +@test no(test_net) == 3 end