use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use crate::EntityId;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum RelationType {
Precedes,
Requires,
LocatedAt,
Uses,
SimilarTo,
IsA,
Produces,
Custom(String),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Relation {
pub source: EntityId,
pub target: EntityId,
pub relation_type: RelationType,
pub weight: f32,
pub properties: HashMap<String, String>,
}
impl Relation {
pub fn new(
source: EntityId,
target: EntityId,
relation_type: RelationType,
weight: f32,
) -> Self {
Self {
source,
target,
relation_type,
weight,
properties: HashMap::new(),
}
}
pub fn with_property(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.properties.insert(key.into(), value.into());
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn relation_creation() {
let r = Relation::new(1, 2, RelationType::Precedes, 1.0).with_property("order", "1");
assert_eq!(r.source, 1);
assert_eq!(r.target, 2);
assert_eq!(r.relation_type, RelationType::Precedes);
}
}