Skip to content

Commit 32c0c47

Browse files
committed
add direct unit def extraction to local param
1 parent 429fb93 commit 32c0c47

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

src/localparameter.rs

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ use std::{cell::RefCell, pin::Pin, rc::Rc};
2020
use cxx::let_cxx_string;
2121

2222
use crate::{
23-
clone, inner, into_id, pin_ptr,
23+
clone, get_unit_definition, inner, into_id, pin_ptr,
2424
prelude::KineticLaw,
2525
sbase,
2626
sbmlcxx::{self},
2727
sbo_term,
28-
traits::fromptr::FromPtr,
28+
traits::{fromptr::FromPtr, sbase::SBase},
2929
upcast, upcast_annotation, upcast_optional_property, upcast_pin, upcast_required_property,
3030
};
3131

@@ -85,6 +85,9 @@ impl<'a> LocalParameter<'a> {
8585
}
8686
}
8787

88+
// Gets the unit definition for the local parameter
89+
get_unit_definition!(units);
90+
8891
// Getter and setter for id
8992
upcast_required_property!(
9093
LocalParameter<'a>,
@@ -281,7 +284,7 @@ mod tests {
281284
use serde::{Deserialize, Serialize};
282285

283286
use super::*;
284-
use crate::{model::Model, prelude::Reaction, sbmldoc::SBMLDocument};
287+
use crate::{model::Model, prelude::Reaction, sbmldoc::SBMLDocument, unit::UnitKind};
285288

286289
#[test]
287290
fn test_parameter_creation() {
@@ -392,4 +395,65 @@ mod tests {
392395
.expect("Failed to get annotation");
393396
assert_eq!(extracted.test, "test");
394397
}
398+
399+
#[test]
400+
fn test_local_parameter_unit_definition() {
401+
let doc = SBMLDocument::default();
402+
let model = doc.create_model("test");
403+
404+
// Create the unit definition
405+
model
406+
.build_unit_definition("ml", "milliliter")
407+
.unit(UnitKind::Litre, Some(-1), Some(-3), None, None)
408+
.build();
409+
410+
model
411+
.build_unit_definition("M", "Molar")
412+
.unit(UnitKind::Mole, Some(1), Some(1), None, None)
413+
.unit(UnitKind::Litre, Some(-1), Some(1), None, None)
414+
.build();
415+
416+
model
417+
.build_compartment("compartment")
418+
.unit("ml")
419+
.constant(true)
420+
.build();
421+
422+
let substrate = model.build_species("substrate").build();
423+
let product = model.build_species("product").build();
424+
425+
let reaction = model
426+
.build_reaction("reaction")
427+
.reactant(&substrate, 1.0)
428+
.product(&product, 1.0)
429+
.build();
430+
431+
let kinetic_law = reaction.create_kinetic_law("k1 * substrate");
432+
let local_parameter = kinetic_law.build_local_parameter("k1").units("M").build();
433+
434+
let valid = doc.check_consistency();
435+
436+
if !valid.valid {
437+
println!("{:#?}", valid.errors);
438+
panic!("Invalid SBML document");
439+
}
440+
441+
let unit_definition = local_parameter.unit_definition().unwrap();
442+
assert_eq!(unit_definition.id(), "M");
443+
assert_eq!(unit_definition.units().len(), 2);
444+
445+
// Mole
446+
assert_eq!(unit_definition.units()[0].kind(), UnitKind::Mole);
447+
assert_eq!(unit_definition.units()[0].exponent(), 1);
448+
assert_eq!(unit_definition.units()[0].scale(), 1);
449+
assert_eq!(unit_definition.units()[0].multiplier(), 1.0);
450+
assert_eq!(unit_definition.units()[0].offset(), 0.0);
451+
452+
// Litre
453+
assert_eq!(unit_definition.units()[1].kind(), UnitKind::Litre);
454+
assert_eq!(unit_definition.units()[1].exponent(), -1);
455+
assert_eq!(unit_definition.units()[1].scale(), 1);
456+
assert_eq!(unit_definition.units()[1].multiplier(), 1.0);
457+
assert_eq!(unit_definition.units()[1].offset(), 0.0);
458+
}
395459
}

src/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ macro_rules! get_unit_definition {
443443
($property:ident) => {
444444
pub fn unit_definition(&self) -> Option<Rc<$crate::unitdef::UnitDefinition<'a>>> {
445445
let model_ptr = self.base().getModel();
446-
let model = Model::from_ptr(model_ptr as *mut $crate::sbmlcxx::Model);
446+
let model = $crate::model::Model::from_ptr(model_ptr as *mut $crate::sbmlcxx::Model);
447447

448448
if let Some(unit) = self.$property() {
449449
model.get_unit_definition(&unit)

0 commit comments

Comments
 (0)