Skip to content

Commit 7ec7383

Browse files
committed
better handling of forbidden symbols and more tests
1 parent 99e54d2 commit 7ec7383

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

src/dsl.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ function extract_sps_and_ps(reactions, excluded_syms; requiredec = false)
472472
add_syms_from_expr!(species, reactant.reactant, excluded_syms)
473473
end
474474
(!isempty(species) && requiredec) &&
475-
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."))
475+
throw(UndeclaredSymbolicError("Unrecognized reactant $(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 option."))
476476
end
477477
union!(excluded_syms, species)
478478

@@ -481,11 +481,11 @@ function extract_sps_and_ps(reactions, excluded_syms; requiredec = false)
481481
for reaction in reactions
482482
add_syms_from_expr!(parameters, reaction.rate, excluded_syms)
483483
(!isempty(parameters) && requiredec) &&
484-
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."))
484+
throw(UndeclaredSymbolicError("Unrecognized symbol $(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 option."))
485485
for reactant in Iterators.flatten((reaction.substrates, reaction.products))
486486
add_syms_from_expr!(parameters, reactant.stoichiometry, excluded_syms)
487487
(!isempty(parameters) && requiredec) &&
488-
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."))
488+
throw(UndeclaredSymbolicError("Unrecognized symbol $(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 option."))
489489
end
490490
end
491491

@@ -728,7 +728,7 @@ function read_equations_options!(diffsexpr, options, syms_unavailable, tiv; requ
728728
# Any undeclared symbolic variables encountered should be extracted as variables.
729729
add_syms_from_expr!(vs_inferred, eq, syms_unavailable)
730730
(!isempty(vs_inferred) && requiredec) && throw(UndeclaredSymbolicError(
731-
"Unrecognized symbolic variables $(join(vs_inferred, ", ")) detected in equation expression: \"$(string(eq))\". Since the flag @require_declaration is declared, all symbolic variables must be explicitly declared with the @species, @variables, and @parameters options."))
731+
"Unrecognized symbol $(join(vs_inferred, ", ")) detected in equation expression: \"$(string(eq))\". Since the flag @require_declaration is declared, all symbolic variables must be explicitly declared with the @species, @variables, and @parameters options."))
732732
end
733733

734734
# If `D` differential is used, add it to differential expression and inferred differentials list.
@@ -804,7 +804,7 @@ function read_observed_options(options, all_ivs, us_declared, all_syms; required
804804

805805
# Error checks.
806806
(requiredec && !in(obs_name, us_declared)) &&
807-
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."))
807+
throw(UndeclaredSymbolicError("An undeclared symbol ($obs_name) was used as an observable in the following observable equation: \"$obs_eq\". Since the flag @require_declaration is set, all observables must be declared with either the @species or @variables options."))
808808
isempty(ivs) ||
809809
error("An observable ($obs_name) was given independent variable(s). These should not be given, as they are inferred automatically.")
810810
isnothing(defaults) ||
@@ -937,6 +937,9 @@ function make_reaction(ex::Expr)
937937
reaction = get_reaction(ex)
938938
species, parameters = extract_sps_and_ps([reaction], [])
939939

940+
# Checks for input errors. Needed here but not in `@reaction_network` as `ReactionSystem` perform this check but `Reaction` don't.
941+
forbidden_symbol_check(union(species, parameters))
942+
940943
# Creates expressions corresponding to code for declaring the parameters, species, and reaction.
941944
spexprs = get_usexpr(species, Dict{Symbol, Expr}())
942945
pexprs = get_psexpr(parameters, Dict{Symbol, Expr}())

src/expression_utils.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ function is_escaped_expr(expr)
2222
return (expr isa Expr) && (expr.head == :escape) && (length(expr.args) == 1)
2323
end
2424

25+
### Parameters/Species/Variables Symbols Correctness Checking ###
26+
27+
# Throws an error when a forbidden symbol is used.
28+
function forbidden_symbol_check(syms)
29+
used_forbidden_syms = intersect(forbidden_symbols_error, syms)
30+
isempty(used_forbidden_syms) ||
31+
error("The following symbol(s) are used as species or parameters: $used_forbidden_syms, this is not permitted.")
32+
end
33+
34+
2535
### Catalyst-specific Expressions Manipulation ###
2636

2737
# Some options takes input on form that is either `@option ...` or `@option begin ... end`.

src/spatial_reaction_systems/spatial_reactions.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,8 @@ function make_transport_reaction(rateex, species)
5555
find_parameters_in_rate!(parameters, rateex)
5656

5757
# Checks for input errors.
58-
if isempty(intersect(forbidden_symbols_error, union([species], parameters)))
59-
error("The following symbol(s) are used as species or parameters: $(intersect(forbidden_symbols_error, union([species], parameters)))), this is not permitted.")
60-
end
58+
forbidden_symbol_check(union([species], parameters))
59+
6160

6261
# Creates expressions corresponding to actual code from the internal DSL representation.
6362
sexprs = get_usexpr([species], Dict{Symbol, Expr}())

0 commit comments

Comments
 (0)