Skip to main content

domainforge_core/primitives/
flow.rs

1use crate::ConceptId;
2use rust_decimal::Decimal;
3use serde::{Deserialize, Serialize};
4use serde_json::Value;
5use std::collections::HashMap;
6use uuid;
7
8const DEFAULT_NAMESPACE: &str = "default";
9
10/// Represents a transfer of a resource between two entities.
11///
12/// Flows are the "MOVEMENT" in enterprise models - they capture
13/// the transfer of resources from one entity to another.
14///
15/// # Examples
16///
17/// ```
18/// use domainforge_core::primitives::{Entity, Resource, Flow};
19/// use domainforge_core::units::unit_from_string;
20/// use rust_decimal::Decimal;
21///
22/// let warehouse = Entity::new_with_namespace("Warehouse".to_string(), "default".to_string());
23/// let factory = Entity::new_with_namespace("Factory".to_string(), "default".to_string());
24/// let product = Resource::new_with_namespace("Widget", unit_from_string("units"), "default".to_string());
25///
26/// let flow = Flow::new(
27///     product.id().clone(),
28///     warehouse.id().clone(),
29///     factory.id().clone(),
30///     Decimal::from(100)
31/// );
32///
33/// assert_eq!(flow.quantity(), Decimal::from(100));
34/// ```
35#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
36pub struct Flow {
37    id: ConceptId,
38    resource_id: ConceptId,
39    from_id: ConceptId,
40    to_id: ConceptId,
41    quantity: Decimal,
42    namespace: String,
43    attributes: HashMap<String, Value>,
44}
45
46impl Flow {
47    /// Creates a new Flow using the default namespace.
48    pub fn new(
49        resource_id: ConceptId,
50        from_id: ConceptId,
51        to_id: ConceptId,
52        quantity: Decimal,
53    ) -> Self {
54        Self::new_with_namespace(resource_id, from_id, to_id, quantity, DEFAULT_NAMESPACE)
55    }
56
57    /// Creates a new Flow with namespace.
58    pub fn new_with_namespace(
59        resource_id: ConceptId,
60        from_id: ConceptId,
61        to_id: ConceptId,
62        quantity: Decimal,
63        namespace: impl Into<String>,
64    ) -> Self {
65        let namespace = namespace.into();
66        // Use UUID v4 for flows: two structurally-identical flows (same
67        // resource/from/to/quantity) are legitimate distinct events and must
68        // coexist in the graph (`Graph::add_flow` rejects duplicate ids), so
69        // the id itself cannot be content-addressed. Fingerprint stability is
70        // instead handled by `compute_graph_hash`, which hashes flows by their
71        // structural content rather than this id.
72        let id = ConceptId::from_uuid(uuid::Uuid::new_v4());
73
74        Self {
75            id,
76            resource_id,
77            from_id,
78            to_id,
79            quantity,
80            namespace,
81            attributes: HashMap::new(),
82        }
83    }
84
85    pub fn id(&self) -> &ConceptId {
86        &self.id
87    }
88    pub fn resource_id(&self) -> &ConceptId {
89        &self.resource_id
90    }
91    pub fn from_id(&self) -> &ConceptId {
92        &self.from_id
93    }
94    pub fn to_id(&self) -> &ConceptId {
95        &self.to_id
96    }
97    pub fn quantity(&self) -> Decimal {
98        self.quantity
99    }
100    pub fn namespace(&self) -> &str {
101        &self.namespace
102    }
103
104    pub fn set_attribute(&mut self, key: impl Into<String>, value: Value) {
105        self.attributes.insert(key.into(), value);
106    }
107
108    pub fn get_attribute(&self, key: &str) -> Option<&Value> {
109        self.attributes.get(key)
110    }
111
112    /// Returns all attributes as a reference.
113    pub fn attributes(&self) -> &HashMap<String, Value> {
114        &self.attributes
115    }
116}