@@ -89,17 +89,31 @@ impl BooleanNetwork {
89
89
create_regulations ( & mut regulatory_graph, & transitions, & specie_to_name) ?;
90
90
91
91
let mut boolean_network = BooleanNetwork :: new ( regulatory_graph) ;
92
+
93
+ // First, collect and create parameters (in alphabetic order)
94
+ let mut parameters = HashMap :: new ( ) ;
92
95
for transition in & transitions {
93
96
if transition. default_term . is_some ( ) {
94
- // Only continue if this transition has a function
95
- // First, create parameters used by this transition.
97
+ // First, register parameters used by this transition.
96
98
for term in & transition. function_terms {
97
99
if let Some ( math) = & term. math {
98
- create_explicit_parameters ( math, & mut boolean_network ) ?;
100
+ create_explicit_parameters ( math, & mut parameters ) ?;
99
101
}
100
102
}
103
+ }
104
+ }
105
+
106
+ let mut parameters = Vec :: from_iter ( parameters) ;
107
+ parameters. sort_by_cached_key ( |( it, _) | it. clone ( ) ) ;
108
+ for ( name, arity) in parameters {
109
+ boolean_network. add_parameter ( name. as_str ( ) , arity) ?;
110
+ }
101
111
102
- // At this point we know this won't fail because we already created the regulations.
112
+ // Then actually go through the functions and create them.
113
+ for transition in & transitions {
114
+ if transition. default_term . is_some ( ) {
115
+ // At this point we know this won't fail because we already
116
+ // created the regulations and parameters.
103
117
let out_var = & specie_to_name[ & transition. outputs [ 0 ] . qual_species ] ;
104
118
let out_var = boolean_network. graph . find_variable ( out_var) . unwrap ( ) ;
105
119
let update_function = sbml_transition_to_update_function (
@@ -339,20 +353,23 @@ fn create_regulations(
339
353
}
340
354
341
355
/// **(internal)** Create any explicit parameters used in the given MathML tree.
342
- fn create_explicit_parameters ( math : & MathMl , network : & mut BooleanNetwork ) -> Result < ( ) , String > {
356
+ fn create_explicit_parameters (
357
+ math : & MathMl ,
358
+ params : & mut HashMap < String , u32 > ,
359
+ ) -> Result < ( ) , String > {
343
360
match math {
344
361
MathMl :: Boolean ( _) => Ok ( ( ) ) ,
345
362
MathMl :: Integer ( _) => Ok ( ( ) ) ,
346
363
MathMl :: Identifier ( _) => Ok ( ( ) ) ,
347
364
MathMl :: Apply ( _, args) => {
348
365
for a in args {
349
- create_explicit_parameters ( a, network ) ?;
366
+ create_explicit_parameters ( a, params ) ?;
350
367
}
351
368
Ok ( ( ) )
352
369
}
353
370
MathMl :: SymbolApply ( name, args) => {
354
- if let Some ( p) = network . find_parameter ( name) {
355
- let current = network . get_parameter ( p ) . get_arity ( ) ;
371
+ if let Some ( p) = params . get ( name) {
372
+ let current = * p ;
356
373
if current != u32:: try_from ( args. len ( ) ) . unwrap ( ) {
357
374
return Err ( format ! (
358
375
"Parameter `{}` is used with cardinality {} as well as {}" ,
@@ -363,11 +380,11 @@ fn create_explicit_parameters(math: &MathMl, network: &mut BooleanNetwork) -> Re
363
380
}
364
381
} else {
365
382
// Seems that at the moment there are no restrictions on parameter names (weird).
366
- network . add_parameter ( name, u32:: try_from ( args. len ( ) ) . unwrap ( ) ) ? ;
383
+ params . insert ( name. clone ( ) , u32:: try_from ( args. len ( ) ) . unwrap ( ) ) ;
367
384
}
368
385
// Also run this, in case we allow complex parameter applications in the future.
369
386
for a in args {
370
- create_explicit_parameters ( a, network ) ?;
387
+ create_explicit_parameters ( a, params ) ?;
371
388
}
372
389
Ok ( ( ) )
373
390
}
0 commit comments