Skip to main content

relay_knowledge/domain/core/
entity.rs

1use serde::{Deserialize, Serialize};
2
3use super::{DomainError, OntologyEntityKind, OntologyIdentity};
4
5/// A minimal entity model used by early graph-building code.
6#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
7pub struct KnowledgeEntity {
8    id: String,
9    label: String,
10    #[serde(default)]
11    entity_kind: OntologyEntityKind,
12    #[serde(default, skip_serializing_if = "Option::is_none")]
13    ontology_identity: Option<OntologyIdentity>,
14}
15
16impl KnowledgeEntity {
17    /// Creates a new knowledge entity with a stable identifier and display label.
18    pub fn new(id: impl Into<String>, label: impl Into<String>) -> Self {
19        Self {
20            id: id.into(),
21            label: label.into(),
22            entity_kind: OntologyEntityKind::Untyped,
23            ontology_identity: None,
24        }
25    }
26
27    /// Creates a typed entity whose id is derived from scoped ontology identity, not its label.
28    pub fn from_ontology(
29        identity: OntologyIdentity,
30        label: impl Into<String>,
31    ) -> Result<Self, DomainError> {
32        let label = label.into();
33        if label.trim().is_empty() {
34            return Err(DomainError::invalid("label", "must not be empty"));
35        }
36        Ok(Self {
37            id: identity.stable_entity_id(),
38            label,
39            entity_kind: identity.entity_kind,
40            ontology_identity: Some(identity),
41        })
42    }
43
44    /// Returns the stable entity identifier.
45    pub fn id(&self) -> &str {
46        &self.id
47    }
48
49    /// Returns the human-readable entity label.
50    pub fn label(&self) -> &str {
51        &self.label
52    }
53
54    /// Returns `untyped` for legacy label-only entities and the ontology type otherwise.
55    pub const fn entity_kind(&self) -> OntologyEntityKind {
56        self.entity_kind
57    }
58
59    /// Returns scoped ontology identity when this is a typed entity.
60    pub fn ontology_identity(&self) -> Option<&OntologyIdentity> {
61        self.ontology_identity.as_ref()
62    }
63}
64
65#[cfg(test)]
66#[path = "entity_tests.rs"]
67mod tests;