Skip to content

Commit 2814b14

Browse files
Format
1 parent 8652f3d commit 2814b14

File tree

8 files changed

+166
-71
lines changed

8 files changed

+166
-71
lines changed

src/ReactiveDynamics.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ const ReactionNetworkSchema = FoldedReactionNetworkType{
8484
Set{Symbol},
8585
FoldedObservable,
8686
Any,
87-
Bool
87+
Bool,
8888
}
8989

9090
Base.convert(::Type{Symbol}, ex::String) = Symbol(ex)

src/compilers.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ function wrap_expr(fex, species_names, prm_names, varmap)
118118

119119
return eval(quote
120120
function (state, transition)
121-
$letex
121+
return $letex
122122
end
123123
end)
124124
end

src/interface/agents.jl

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ abstract type AbstractStructuredSpecies <: AbstractAlgebraicAgent end
99
# In general, we will probably assume that each "structured agent" type implements this field.
1010
# Otherwise, it would be possible to implement getter and setter interface and use it from within ReaDyn.
1111
@aagent FreeAgent struct BaseStructuredSpecies
12-
bound_transition::Union{Nothing, ReactiveDynamics.Transition}
12+
bound_transition::Union{Nothing,ReactiveDynamics.Transition}
1313
end
1414

1515
# We use this to let the network know that the type is structured.
@@ -27,16 +27,21 @@ end
2727
# Convenience macro to define structured species.
2828
macro structured(network, type)
2929
name = Docs.namify(type.args[2])
30-
31-
quote
32-
$(AlgebraicAgents.aagent(BaseStructuredSpecies, AbstractStructuredSpecies, type, ReactiveDynamics))
30+
31+
quote
32+
$(AlgebraicAgents.aagent(
33+
BaseStructuredSpecies,
34+
AbstractStructuredSpecies,
35+
type,
36+
ReactiveDynamics,
37+
))
3338
register_structured_species!($(esc(network)), $(QuoteNode(name)))
3439
end
3540
end
3641

3742
# Add a structured agent instance to an instance of a reaction network.
3843
function add_structured_species!(problem::ReactionNetworkProblem, agent)
39-
entangle!(getagent(problem, "structured/$(nameof(typeof(agent)))"), agent)
44+
return entangle!(getagent(problem, "structured/$(nameof(typeof(agent)))"), agent)
4045
end
4146

4247
import AlgebraicAgents
@@ -49,4 +54,4 @@ AlgebraicAgents._step!(::AbstractStructuredSpecies) = nothing
4954
isblocked(a) = !isnothing(a.bound_transition)
5055

5156
# Priority with which an unbound agent will be assigned to a transition.
52-
priority(a, transition) = 0.0
57+
priority(a, transition) = 0.0

src/interface/reaction_parser.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using MacroTools: postwalk
44

55
struct FoldedReactant
6-
species::Union{Expr, Symbol}
6+
species::Union{Expr,Symbol}
77
stoich::SampleableValues
88
modality::Set{Symbol}
99
end

src/solvers.jl

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ function get_reqs_ongoing!(reqs, qs, state)
3636
(state.ongoing_transitions[i][:transCycleTime] > 0) &&
3737
(reqs[tok.index, i] += qs[i] * tok.stoich * state.dt)
3838
if in(:rate, tok.modality) && in(tok.species, state.structured_species)
39-
error("Modality `:rate` is not supported for structured species in transition $(trans[:transName]).")
39+
error(
40+
"Modality `:rate` is not supported for structured species in transition $(trans[:transName]).",
41+
)
4042
end
4143
in(:nonblock, tok.modality) && (reqs[tok.index, i] += qs[i] * tok.stoich)
4244
end
@@ -125,7 +127,6 @@ function get_init_satisfied(allocs, qs, state)
125127
return qs
126128
end
127129

128-
129130
"""
130131
Evolve transitions, spawn new transitions.
131132
"""
@@ -189,12 +190,20 @@ function evolve!(state)
189190
for (j, type) in enumerate(state.acs[:, :specName])
190191
if type state.structured_species
191192
if !isinteger(allocs[j, i])
192-
error("For structured species, stoichiometry coefficient must be integer in transition $i.")
193+
error(
194+
"For structured species, stoichiometry coefficient must be integer in transition $i.",
195+
)
193196
end
194197

195-
all_of_type = collect(values(inners(getagent(state, "structured/$(string(type))"))))
198+
all_of_type = collect(
199+
values(inners(getagent(state, "structured/$(string(type))"))),
200+
)
196201
filter!(!isblocked, all_of_type)
197-
sort!(all_of_type, by=a->priority(a, state.acs[i, :transName]), rev=true)
202+
sort!(
203+
all_of_type;
204+
by = a -> priority(a, state.acs[i, :transName]),
205+
rev = true,
206+
)
198207

199208
ix = 1
200209
while allocs[j, i] > 0 && ix <= length(all_of_type)
@@ -246,12 +255,20 @@ function evolve!(state)
246255
for (j, type) in enumerate(state.acs[:, :specName])
247256
if type state.structured_species
248257
if !isinteger(allocs[j, i])
249-
error("For structured species, stoichiometry coefficient must be integer in transition $i.")
258+
error(
259+
"For structured species, stoichiometry coefficient must be integer in transition $i.",
260+
)
250261
end
251262

252-
all_of_type = collect(values(inners(getagent(state, "structured/$(string(type))"))))
263+
all_of_type = collect(
264+
values(inners(getagent(state, "structured/$(string(type))"))),
265+
)
253266
filter!(!isblocked, all_of_type)
254-
sort!(all_of_type, by=a -> priority(a, state.acs[i, :transName]), rev=true)
267+
sort!(
268+
all_of_type;
269+
by = a -> priority(a, state.acs[i, :transName]),
270+
rev = true,
271+
)
255272

256273
ix = 1
257274
while allocs[j, i] > 0 && ix <= length(all_of_type)
@@ -301,7 +318,7 @@ function finish!(state)
301318
((state.t - trans_.t) < trans_.trans[:transMaxLifeTime]) &&
302319
(trans_.state < trans_[:transCycleTime]) &&
303320
(ix += 1; continue)
304-
321+
305322
q = if trans_.state >= trans_[:transCycleTime]
306323
rand(Distributions.Binomial(Int(trans_.q), trans_[:transProbOfSuccess]))
307324
else
@@ -320,7 +337,7 @@ function finish!(state)
320337
state.u[i] += q * stoich
321338
val_reward += state[i, :specReward] * q * stoich
322339

323-
for _ in 1:q
340+
for _ = 1:q
324341
a = context_eval(state, trans_, state.wrap_fun(r.species))
325342
entangle!(getagent(state, "structured/$(r.species.args[1])"), a)
326343
end
@@ -340,7 +357,7 @@ function finish!(state)
340357
tok.stoich *
341358
(in(:rate, tok.modality) ? trans_[:transCycleTime] : 1)
342359
if tok.species state.structured_species
343-
for _ in 1:(trans_.q * tok.stoich)
360+
for _ = 1:(trans_.q*tok.stoich)
344361
trans_.bound_structured_agents[begin].bound_transition = nothing
345362
deleteat!(trans_.bound_structured_agents, 1)
346363
end
@@ -349,12 +366,14 @@ function finish!(state)
349366

350367
if in(:nonblock, tok.modality)
351368
if in(:conserved, tok.modality)
352-
error("Modalities `:conserved` and `:nonblock` cannot be specified at the same time.")
369+
error(
370+
"Modalities `:conserved` and `:nonblock` cannot be specified at the same time.",
371+
)
353372
end
354373

355-
state.u[tok.index] += trans_.q * tok.stoich
374+
state.u[tok.index] += trans_.q * tok.stoich
356375
if tok.species state.structured_species
357-
for _ in 1:(trans_.q * tok.stoich)
376+
for _ = 1:(trans_.q*tok.stoich)
358377
trans_.nonblock_structured_agents[begin].bound_transition = nothing
359378
deleteat!(trans_.nonblock_structured_agents, 1)
360379
end
@@ -366,7 +385,7 @@ function finish!(state)
366385

367386
terminated_all[Symbol(trans_[:transHash])] =
368387
get(terminated_all, Symbol(trans_[:transHash]), 0) + trans_.q
369-
388+
370389
terminated_success[Symbol(trans_[:transHash])] =
371390
get(terminated_success, Symbol(trans_[:transHash]), 0) + q
372391

@@ -427,7 +446,8 @@ function ReactionNetworkProblem(
427446

428447
acs = remove_choose(acs)
429448

430-
structured_species_names = acs[filter(i -> acs[i, :specStructured], 1:nparts(acs, :S)), :specName]
449+
structured_species_names =
450+
acs[filter(i -> acs[i, :specStructured], 1:nparts(acs, :S)), :specName]
431451

432452
attrs, transitions, wrap_fun = compile_attrs(acs, structured_species_names)
433453
transition_recipes = transitions

src/state.jl

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ end
6565
# evaluate compiled numeric expression in context of (u, p, t)
6666
function context_eval(state::ReactionNetworkProblem, transition, o)
6767
o = o isa Function ? Base.invokelatest(o, state, transition) : o
68-
68+
6969
return o isa Sampleable ? rand(o) : o
7070
end
7171

@@ -74,7 +74,8 @@ function Base.getindex(state::ReactionNetworkProblem, keys...)
7474
return state.acs[keys[1], keys[2]]
7575
else
7676
return context_eval(
77-
state, nothing,
77+
state,
78+
nothing,
7879
(contains(string(keys[2]), "trans") ? state.transitions : state.attrs)[keys[2]][keys[1]],
7980
)
8081
end
@@ -176,7 +177,10 @@ function sample_transitions!(state::ReactionNetworkProblem)
176177
l_line, r_line = prune_r_line(state.transition_recipes[:trans][i])
177178

178179
for attr in keys(state.transition_recipes)
179-
(attr [:trans, :transPreAction, :transPostAction, :transActivated, :transHash]) && continue
180+
(
181+
attr
182+
[:trans, :transPreAction, :transPostAction, :transActivated, :transHash]
183+
) && continue
180184
push!(
181185
state.transitions[attr],
182186
context_eval(state, nothing, state.transition_recipes[attr][i]),
@@ -196,10 +200,10 @@ function sample_transitions!(state::ReactionNetworkProblem)
196200
),
197201
)
198202
end
199-
203+
200204
push!(state.transitions[:transLHS], reactants)
201205
push!(state.transitions[:transRHS], r_line)
202-
206+
203207
foreach(
204208
k -> push!(state.transitions[k], state.transition_recipes[k][i]),
205209
[:transPreAction, :transPostAction, :transToSpawn, :transHash],

tutorial/agents-integration/agents.jl

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,19 @@ network = @ReactionNetworkSchema
2222
# With specified intensities, generate experimental resources.
2323
ρ1, ∅ --> R1
2424
ρ2, ∅ --> R2
25-
25+
2626
# Generate "Molecule 1" (where the integer corresponds to a "state" of, e.g., experimental triage).
2727
ρ3, ∅ --> M1(@t(), rand(4))
28-
28+
2929
# Based on properties of particular "structured agent" assigned to the transition,
3030
# we can update the attributes of the instance of a transition (such as probability of success).
31-
31+
3232
# Transition "Molecule 1" into "Molecule 2."
3333
# Update transition probability based on properties of "M1,"
3434
# which was assigned as a "resource" to the transition.
35-
ρ4, R1 + M1 --> M2(@t(), rand(4)), preAction => update_prob_transition(state, transition)
35+
ρ4,
36+
R1 + M1 --> M2(@t(), rand(4)),
37+
preAction => update_prob_transition(state, transition)
3638
end
3739

3840
@prob_init network R1 = 10 R2 = 15
@@ -48,8 +50,8 @@ end
4850
# We use `@structured` macro, which is a convenience wrapper around `@aagent`),
4951
# defined in ReactiveDynamics.jl
5052
@structured network struct M1
51-
descriptor
52-
time_created
53+
descriptor::Any
54+
time_created::Any
5355
end
5456

5557
using Random
@@ -66,7 +68,7 @@ ReactiveDynamics.M1(time, descriptor) = M1("M1" * randstring(4), nothing, descri
6668
update_prob_transition = function (state, transition)
6769
if !isnothing(transition) && !isempty(transition.bound_structured_agents)
6870
bound_agent = first(transition.bound_structured_agents)
69-
71+
7072
transition[:transProbOfSuccess] = min(1.0, sum(bound_agent.descriptor))
7173
end
7274
end
@@ -77,8 +79,8 @@ end
7779
# of ReactiveDynamics.
7880
@register begin
7981
@aagent BaseStructuredSpecies AbstractStructuredSpecies struct M2
80-
descriptor
81-
time_created
82+
descriptor::Any
83+
time_created::Any
8284
end
8385

8486
using Random
@@ -92,10 +94,10 @@ ReactiveDynamics.register_structured_species!(network, :M2)
9294
# Instantiate the network.
9395
network_instance = ReactionNetworkProblem(network)
9496

95-
for i in 1:2
97+
for i = 1:2
9698
add_structured_species!(network_instance, ReactiveDynamics.M1(0.0, rand(4)))
9799
end
98100

99101
# --------------------------------------------------------------------------------
100102
# Simulate the network.
101-
simulate(network_instance, 10)
103+
simulate(network_instance, 10)

0 commit comments

Comments
 (0)