Skip to content

Commit 4e720ee

Browse files
committed
save progress
1 parent 50f8f82 commit 4e720ee

File tree

1 file changed

+38
-43
lines changed

1 file changed

+38
-43
lines changed

src/dsl.jl

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ struct DSLReaction
217217
rxexpr::Expr
218218

219219
function DSLReaction(sub_line::ExprValues, prod_line::ExprValues,
220-
rate::ExprValues, metadata_line::ExprValues)
220+
rate::ExprValues, metadata_line::ExprValues, rx_line::Expr)
221221
subs = recursive_find_reactants!(sub_line, 1, Vector{DSLReactant}(undef, 0))
222222
prods = recursive_find_reactants!(prod_line, 1, Vector{DSLReactant}(undef, 0))
223223
metadata = extract_metadata(metadata_line)
@@ -482,9 +482,14 @@ function push_reactions!(reactions::Vector{DSLReaction}, subs::ExprValues,
482482
push!(metadata_i.args, :(only_use_rate = $(in(arrow, pure_rate_arrows))))
483483
end
484484

485+
# Checks that metadata fields are unique.
486+
if !allunique(arg.args[1] for arg in metadata_i.args)
487+
error("Some reaction metadata fields where repeated: $(metadata_entries)")
488+
end
489+
485490
# Extracts substrates, products, and rates for the i'th reaction.
486491
subs_i, prods_i, rate_i = get_tup_arg.((subs, prods, rate), i)
487-
push!(reactions, DSLReaction(subs_i, prods_i, rate_i, metadata_i))
492+
push!(reactions, DSLReaction(subs_i, prods_i, rate_i, metadata_i, line))
488493
end
489494
end
490495

@@ -505,29 +510,29 @@ function extract_syms(opts, vartype::Symbol)
505510
end
506511

507512
# Function looping through all reactions, to find undeclared symbols (species or
508-
# parameters), and assign them to the right category.
513+
# parameters) and assign them to the right category.
509514
function extract_species_and_parameters(reactions, excluded_syms)
510515
# Loops through all reactant, extract undeclared ones as species.
511516
species = OrderedSet{Union{Symbol, Expr}}()
512517
for reaction in reactions
513518
for reactant in Iterators.flatten((reaction.substrates, reaction.products))
514519
add_syms_from_expr!(species, reactant.reactant, excluded_syms)
515-
(!isempty(species) && requiredec) && throw(UndeclaredSymbolicError(
516-
"Unrecognized variables $(join(species, ", ")) detected in reaction expression: \"$(string(reaction.rxexpr))\". Since the flag @require_declaration is declared, all species must be explicitly declared with the @species macro."))
517520
end
521+
(!isempty(species) && requiredec) &&
522+
throw(UndeclaredSymbolicError("Unrecognized variables $(join(species, ", ")) detected in reaction expression: \"$(string(reaction.rxexpr))\". Since the flag @require_declaration is declared, all species must be explicitly declared with the @species macro."))
518523
end
519-
foreach(s -> push!(excluded_syms, s), species)
524+
union!(excluded_syms, species)
520525

521526
# Loops through all rates and stoichiometries, extracting used symbols as parameters.
522527
parameters = OrderedSet{Union{Symbol, Expr}}()
523528
for reaction in reactions
524529
add_syms_from_expr!(parameters, reaction.rate, excluded_syms)
525-
(!isempty(parameters) && requiredec) && throw(UndeclaredSymbolicError(
526-
"Unrecognized parameter $(join(parameters, ", ")) detected in rate expression: $(reaction.rate) for the following reaction expression: \"$(string(reaction.rxexpr))\". Since the flag @require_declaration is declared, all parameters must be explicitly declared with the @parameters macro."))
530+
(!isempty(parameters) && requiredec) &&
531+
throw(UndeclaredSymbolicError("Unrecognized parameter $(join(parameters, ", ")) detected in rate expression: $(reaction.rate) for the following reaction expression: \"$(string(reaction.rxexpr))\". Since the flag @require_declaration is declared, all parameters must be explicitly declared with the @parameters macro."))
527532
for reactant in Iterators.flatten((reaction.substrates, reaction.products))
528533
add_syms_from_expr!(parameters, reactant.stoichiometry, excluded_syms)
529-
(!isempty(parameters) && requiredec) && throw(UndeclaredSymbolicError(
530-
"Unrecognized parameters $(join(parameters, ", ")) detected in the stoichiometry for reactant $(reactant.reactant) in the following reaction expression: \"$(string(reaction.rxexpr))\". Since the flag @require_declaration is declared, all parameters must be explicitly declared with the @parameters macro."))
534+
(!isempty(parameters) && requiredec) &&
535+
throw(UndeclaredSymbolicError("Unrecognized parameters $(join(parameters, ", ")) detected in the stoichiometry for reactant $(reactant.reactant) in the following reaction expression: \"$(string(reaction.rxexpr))\". Since the flag @require_declaration is declared, all parameters must be explicitly declared with the @parameters macro."))
531536
end
532537
end
533538

@@ -556,12 +561,12 @@ end
556561
# `@species ...` (or `@variables ..`) expression which would declare these.
557562
# If `key = :variables`, does this for variables (and not species).
558563
function get_usexpr(us_extracted, options, key = :species; ivs = (DEFAULT_IV_SYM,))
559-
if haskey(options, key)
560-
usexpr = options[key]
564+
usexpr = if haskey(options, key)
565+
options[key]
561566
elseif isempty(us_extracted)
562-
usexpr = :()
567+
:()
563568
else
564-
usexpr = Expr(:macrocall, Symbol("@", key), LineNumberNode(0))
569+
Expr(:macrocall, Symbol("@", key), LineNumberNode(0))
565570
end
566571
for u in us_extracted
567572
u isa Symbol && push!(usexpr.args, Expr(:call, u, ivs...))
@@ -572,12 +577,12 @@ end
572577
# Given the parameters that were extracted from the reactions, and the options dictionary,
573578
# creates the `@parameters ...` expression for the macro output.
574579
function get_pexpr(parameters_extracted, options)
575-
if haskey(options, :parameters)
576-
pexprs = options[:parameters]
580+
pexprs = if haskey(options, :parameters)
581+
options[:parameters]
577582
elseif isempty(parameters_extracted)
578-
pexprs = :()
583+
:()
579584
else
580-
pexprs = :(@parameters)
585+
:(@parameters)
581586
end
582587
foreach(p -> push!(pexprs.args, p), parameters_extracted)
583588
return pexprs
@@ -586,7 +591,7 @@ end
586591
# Takes a ModelingToolkit declaration macro (like @parameters ...) and return and expression:
587592
# That calls the macro and then scalarizes all the symbols created into a vector of Nums.
588593
# stores the created symbolic variables in a variable (which name is generated from `name`).
589-
# It will also return the name used for the variable that stores the symbolci variables.
594+
# It will also return the name used for the variable that stores the symbolic variables.
590595
function scalarize_macro(expr_init, name)
591596
# Generates a random variable name which (in generated code) will store the produced
592597
# symbolic variables (e.g. `var"##ps#384"`).
@@ -638,8 +643,8 @@ end
638643

639644
### DSL Option Handling ###
640645

641-
# Finds the time idenepdnet variable, and any potential spatial indepndent variables.
642-
# Returns these (individually and combined), as well as an expression for declaring them
646+
# Finds the time independent variable, and any potential spatial indepndent variables.
647+
# Returns these (individually and combined), as well as an expression for declaring them.
643648
function read_ivs_option(options)
644649
# Creates the independent variables expressions (depends on whether the `ivs` option was used).
645650
if haskey(options, :ivs)
@@ -651,7 +656,7 @@ function read_ivs_option(options)
651656
ivexpr = :($(DEFAULT_IV_SYM) = default_t())
652657
end
653658

654-
# Extracts the independet variables symbols, and returns the output.
659+
# Extracts the independet variables symbols (time and spatial), and returns the output.
655660
tiv = ivs[1]
656661
sivs = (length(ivs) > 1) ? Expr(:vect, ivs[2:end]...) : nothing
657662
return tiv, sivs, ivs, ivexpr
@@ -802,11 +807,8 @@ end
802807
# Reads the combinatiorial ratelaw options, which determines if a combinatorial rate law should
803808
# be used or not. If not provides, uses the default (true).
804809
function read_combinatoric_ratelaws_option(options)
805-
if haskey(options, :combinatoric_ratelaws)
806-
return options[:combinatoric_ratelaws].args[end]
807-
else
808-
return true
809-
end
810+
return haskey(options, :combinatoric_ratelaws) ?
811+
options[:combinatoric_ratelaws].args[end] : true
810812
end
811813

812814
# Reads the observables options. Outputs an expression ofr creating the observable variables, and a vector of observable equations.
@@ -821,26 +823,19 @@ function read_observed_options(options, species_n_vars_declared, ivs_sorted; req
821823
for (idx, obs_eq) in enumerate(observed_eqs.args)
822824
# Extract the observable, checks for errors.
823825
obs_name, ivs, defaults, metadata = find_varinfo_in_declaration(obs_eq.args[2])
824-
if (requiredec && !in(obs_name, species_n_vars_declared))
825-
throw(UndeclaredSymbolicError(
826-
"An undeclared variable ($obs_name) was declared as an observable in the following observable equation: \"$obs_eq\". Since the flag @require_declaration is set, all variables must be declared with the @species, @parameters, or @variables macros."))
827-
end
828-
if !isempty(ivs)
826+
827+
(requiredec && !in(obs_name, species_n_vars_declared)) &&
828+
throw(UndeclaredSymbolicError("An undeclared variable ($obs_name) was declared as an observable in the following observable equation: \"$obs_eq\". Since the flag @require_declaration is set, all variables must be declared with the @species, @parameters, or @variables macros."))
829+
isempty(ivs) ||
829830
error("An observable ($obs_name) was given independent variable(s). These should not be given, as they are inferred automatically.")
830-
end
831-
if !isnothing(defaults)
831+
isnothing(defaults) ||
832832
error("An observable ($obs_name) was given a default value. This is forbidden.")
833-
end
834-
if in(obs_name, forbidden_symbols_error)
833+
in(obs_name, forbidden_symbols_error) &&
835834
error("A forbidden symbol ($(obs_eq.args[2])) was used as an observable name.")
836-
end
837-
if (obs_name in species_n_vars_declared) && is_escaped_expr(obs_eq.args[2])
835+
(obs_name in species_n_vars_declared) && is_escaped_expr(obs_eq.args[2]) &&
838836
error("An interpolated observable have been used, which has also been ereqxplicitly declared within the system using either @species or @variables. This is not permitted.")
839-
end
840-
if ((obs_name in species_n_vars_declared) || is_escaped_expr(obs_eq.args[2])) &&
841-
!isnothing(metadata)
842-
error("Metadata was provided to observable $obs_name in the `@observables` macro. However, the observable was also declared separately (using either @species or @variables). When this is done, metadata should instead be provided within the original @species or @variable declaration.")
843-
end
837+
((obs_name in species_n_vars_declared) || is_escaped_expr(obs_eq.args[2])) &&
838+
!isnothing(metadata) && error("Metadata was provided to observable $obs_name in the `@observables` macro. However, the observable was also declared separately (using either @species or @variables). When this is done, metadata should instead be provided within the original @species or @variable declaration.")
844839

845840
# This bits adds the observables to the @variables vector which is given as output.
846841
# For Observables that have already been declared using @species/@variables,

0 commit comments

Comments
 (0)