1use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5
6use crate::EntityId;
7
8#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
10pub enum RelationType {
11 Precedes,
13 Requires,
15 LocatedAt,
17 Uses,
19 SimilarTo,
21 IsA,
23 Produces,
25 Custom(String),
27}
28
29#[derive(Debug, Clone, Serialize, Deserialize)]
31pub struct Relation {
32 pub source: EntityId,
34 pub target: EntityId,
36 pub relation_type: RelationType,
38 pub weight: f32,
40 pub properties: HashMap<String, String>,
42}
43
44impl Relation {
45 pub fn new(
47 source: EntityId,
48 target: EntityId,
49 relation_type: RelationType,
50 weight: f32,
51 ) -> Self {
52 Self {
53 source,
54 target,
55 relation_type,
56 weight,
57 properties: HashMap::new(),
58 }
59 }
60
61 pub fn with_property(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
63 self.properties.insert(key.into(), value.into());
64 self
65 }
66}
67
68#[cfg(test)]
69mod tests {
70 use super::*;
71
72 #[test]
73 fn relation_creation() {
74 let r = Relation::new(1, 2, RelationType::Precedes, 1.0).with_property("order", "1");
75
76 assert_eq!(r.source, 1);
77 assert_eq!(r.target, 2);
78 assert_eq!(r.relation_type, RelationType::Precedes);
79 }
80}