use std::collections::BTreeMap;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use crate::evidence::EvidenceId;
use crate::ids::{EdgeId, TypeName, VertexId};
pub use crate::property::PropertyValue;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Vertex {
pub id: VertexId,
pub name: String,
pub label: TypeName,
pub properties: BTreeMap<String, PropertyValue>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Edge {
pub id: EdgeId,
pub src: VertexId,
pub src_type: TypeName,
pub dst: VertexId,
pub dst_type: TypeName,
pub label: String,
pub properties: BTreeMap<String, PropertyValue>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub valid_from: Option<DateTime<Utc>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub valid_to: Option<DateTime<Utc>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub observed_at: Option<DateTime<Utc>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub extracted_at: Option<DateTime<Utc>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub confidence: Option<f32>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub evidenced_by: Vec<EvidenceId>,
}
impl Edge {
pub fn valid_at(&self, t: DateTime<Utc>) -> bool {
match (self.valid_from, self.valid_to) {
(None, None) => true,
(Some(f), None) => t >= f,
(None, Some(u)) => t < u,
(Some(f), Some(u)) => t >= f && t < u,
}
}
pub fn valid_between(&self, t1: DateTime<Utc>, t2: DateTime<Utc>) -> bool {
if t2 <= t1 {
return false;
}
let lo = self.valid_from.unwrap_or(DateTime::<Utc>::MIN_UTC);
let hi = self.valid_to.unwrap_or(DateTime::<Utc>::MAX_UTC);
lo < t2 && t1 < hi
}
pub fn still_holds(&self) -> bool {
self.valid_to.is_none()
}
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct SubGraph {
pub nodes: Vec<Vertex>,
pub edges: Vec<Edge>,
}