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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//! Types and operations for creating [`RawComponent`]s that are easier to make manually than [`Component`].
//! They can then be converted to real [`Component`]s later.
//!
//! [`Component`]: crate::model::Component

use itertools::Itertools;

use super::raw_constraint::RawConstraint;
use crate::model::Component;
use std::{collections::HashMap, fmt::Debug};

/// An intermediate struct for constructing [`Component`]s.
#[derive(PartialEq, Debug)]
pub struct RawComponent<T> {
    name: String,
    variables: Vec<String>,
    values: Vec<T>,
    constraints: Vec<RawConstraint<T>>,
}

impl<T> RawComponent<T> {
    /// Constructs a new [`RawComponent`].
    pub fn new<S: Into<String>>(
        name: S,
        variables: Vec<S>,
        values: Vec<T>,
        constraints: Vec<RawConstraint<T>>,
    ) -> Self {
        Self {
            name: name.into(),
            variables: variables.into_iter().map_into().collect(),
            values,
            constraints,
        }
    }

    /// Get a map from variable name to its index
    pub fn indices(&self) -> HashMap<String, usize> {
        let mut name_to_index = HashMap::new();
        for (i, v) in self.variables.iter().enumerate() {
            name_to_index.insert(v.to_string(), i);
        }
        name_to_index
    }

    /// Add a new constraint to the component.
    pub fn add_constraint(&mut self, c: RawConstraint<T>) {
        self.constraints.push(c);
    }

    /// Converts this [`RawComponent`] into a [`Component`].
    pub fn into_component(self) -> Component<T>
    where
        T: Clone,
    {
        let var_to_idx = self.indices();
        // Create constraints
        let constraints = self
            .constraints
            .into_iter()
            .map(|c| c.into_constraint(&var_to_idx))
            .collect();

        // Combine into component
        Component::new_with_map(
            self.name.to_string(),
            var_to_idx.into_iter().map(|(k, v)| (k, v)).collect(),
            self.values,
            constraints,
        )
    }
}