1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use mathml_rs::{evaluate_lambda, MathNode};

use super::math::MathTag;
use super::model::Model;
use super::tag::{Tag, TagIndex};
use std::collections::HashMap;

#[derive(Clone, Debug, Default)]
pub struct ListOfFunctionDefinitions {
    pub function_definitions: Vec<TagIndex>,
    pub parent: Option<TagIndex>,
}

#[derive(Clone, Debug, Default)]
pub struct FunctionDefinition {
    pub id: Option<String>,
    pub name: Option<String>,
    pub sbo_term: Option<String>,
    pub math: Option<TagIndex>,
    pub parent: Option<TagIndex>,
}

impl FunctionDefinition {
    pub fn math_tag(&self, model: &Model) -> Option<MathTag> {
        let mut result = None;
        if let Some(math_tag_idx) = self.math {
            if let Tag::MathTag(math_tag) = &model.nodes[math_tag_idx] {
                result = Some(math_tag.clone());
            }
        }
        result
    }

    pub fn evaluate(
        &self,
        model: &Model,
        argument_values: &[f64],
        functions: &HashMap<String, Vec<MathNode>>,
    ) -> Result<f64, String> {
        let math_tag = self.math_tag(model).unwrap();
        evaluate_lambda(
            &math_tag.nodes,
            0,
            &argument_values,
            &HashMap::new(),
            functions,
        )
    }
}