1use std::collections::BTreeMap;
4
5use chrono::{DateTime, Utc};
6use serde::{Deserialize, Serialize};
7
8use crate::evidence::EvidenceId;
9use crate::ids::{EdgeId, TypeName, VertexId};
10
11pub use crate::property::PropertyValue;
12
13#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
14pub struct Vertex {
15 pub id: VertexId,
16 pub name: String,
17 pub label: TypeName,
19 pub properties: BTreeMap<String, PropertyValue>,
20}
21
22#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
23pub struct Edge {
24 pub id: EdgeId,
25 pub src: VertexId,
26 pub src_type: TypeName,
27 pub dst: VertexId,
28 pub dst_type: TypeName,
29 pub label: String,
31 pub properties: BTreeMap<String, PropertyValue>,
32
33 #[serde(default, skip_serializing_if = "Option::is_none")]
40 pub valid_from: Option<DateTime<Utc>>,
41 #[serde(default, skip_serializing_if = "Option::is_none")]
42 pub valid_to: Option<DateTime<Utc>>,
43 #[serde(default, skip_serializing_if = "Option::is_none")]
44 pub observed_at: Option<DateTime<Utc>>,
45 #[serde(default, skip_serializing_if = "Option::is_none")]
46 pub extracted_at: Option<DateTime<Utc>>,
47
48 #[serde(default, skip_serializing_if = "Option::is_none")]
51 pub confidence: Option<f32>,
52
53 #[serde(default, skip_serializing_if = "Vec::is_empty")]
56 pub evidenced_by: Vec<EvidenceId>,
57}
58
59impl Edge {
60 pub fn valid_at(&self, t: DateTime<Utc>) -> bool {
65 match (self.valid_from, self.valid_to) {
66 (None, None) => true,
67 (Some(f), None) => t >= f,
68 (None, Some(u)) => t < u,
69 (Some(f), Some(u)) => t >= f && t < u,
70 }
71 }
72
73 pub fn valid_between(&self, t1: DateTime<Utc>, t2: DateTime<Utc>) -> bool {
75 if t2 <= t1 {
76 return false;
77 }
78 let lo = self.valid_from.unwrap_or(DateTime::<Utc>::MIN_UTC);
79 let hi = self.valid_to.unwrap_or(DateTime::<Utc>::MAX_UTC);
80 lo < t2 && t1 < hi
81 }
82
83 pub fn still_holds(&self) -> bool {
85 self.valid_to.is_none()
86 }
87}
88
89#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
90pub struct SubGraph {
91 pub nodes: Vec<Vertex>,
92 pub edges: Vec<Edge>,
93}