pub struct SemanticKnowledgeGraph {
pub entities: HashMap<u64, GraphEntity>,
pub edges: Vec<GraphEdge>,
pub next_entity_id: u64,
pub next_edge_id: u64,
}Expand description
A queryable semantic knowledge graph that links entities via typed, weighted edges.
Supports:
- Adding/removing entities and edges.
- Neighbour lookup with optional relation filtering (sorted by weight desc).
- BFS traversal with relation and entity-kind filters.
- Cosine-similarity–based entity retrieval for entities that carry embeddings.
Fields§
§entities: HashMap<u64, GraphEntity>All entities, keyed by their entity_id.
edges: Vec<GraphEdge>All directed edges in insertion order.
next_entity_id: u64Monotonically increasing counter for entity ids.
next_edge_id: u64Monotonically increasing counter for edge ids.
Implementations§
Source§impl SemanticKnowledgeGraph
impl SemanticKnowledgeGraph
Sourcepub fn add_entity(
&mut self,
name: &str,
kind: EntityKind,
embedding: Option<Vec<f32>>,
) -> u64
pub fn add_entity( &mut self, name: &str, kind: EntityKind, embedding: Option<Vec<f32>>, ) -> u64
Add a new entity and return its freshly assigned entity_id.
Sourcepub fn add_edge(
&mut self,
from_id: u64,
to_id: u64,
relation: &str,
weight: f32,
) -> u64
pub fn add_edge( &mut self, from_id: u64, to_id: u64, relation: &str, weight: f32, ) -> u64
Add a directed edge between two entities and return its edge_id.
The entities referenced by from_id and to_id need not exist yet;
no validation is performed here so that callers can build the graph in
any order.
Sourcepub fn get_entity(&self, id: u64) -> Option<&GraphEntity>
pub fn get_entity(&self, id: u64) -> Option<&GraphEntity>
Look up an entity by id.
Sourcepub fn remove_entity(&mut self, entity_id: u64) -> bool
pub fn remove_entity(&mut self, entity_id: u64) -> bool
Remove an entity and all edges that touch it.
Returns true if the entity existed, false otherwise.
Sourcepub fn neighbors(
&self,
entity_id: u64,
relation: Option<&str>,
) -> Vec<&GraphEntity>
pub fn neighbors( &self, entity_id: u64, relation: Option<&str>, ) -> Vec<&GraphEntity>
Return all entities reachable via a single outgoing edge from entity_id.
- When
relationisSome(r), only edges whoserelation == rare considered. - The result list is sorted by edge weight descending.
Sourcepub fn traverse(&self, query: &GraphQuery) -> Vec<&GraphEntity>
pub fn traverse(&self, query: &GraphQuery) -> Vec<&GraphEntity>
BFS traversal from query.start_entity_id up to query.max_hops hops.
At every hop the relation filter and entity-kind filter (from the
GraphQuery) are applied to the destination entity. The start
entity itself is excluded from the result. Results are sorted by
entity_id ascending.
Sourcepub fn similar_entities(
&self,
entity_id: u64,
threshold: f32,
) -> Vec<(&GraphEntity, f32)>
pub fn similar_entities( &self, entity_id: u64, threshold: f32, ) -> Vec<(&GraphEntity, f32)>
Find all entities (excluding entity_id itself) whose embedding has a
cosine similarity ≥ threshold with the embedding of entity_id.
Returns an empty vec if the query entity does not exist or has no embedding. Results are sorted by similarity descending.
Sourcepub fn stats(&self) -> KnowledgeGraphStats
pub fn stats(&self) -> KnowledgeGraphStats
Return aggregate statistics for this graph.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for SemanticKnowledgeGraph
impl RefUnwindSafe for SemanticKnowledgeGraph
impl Send for SemanticKnowledgeGraph
impl Sync for SemanticKnowledgeGraph
impl Unpin for SemanticKnowledgeGraph
impl UnsafeUnpin for SemanticKnowledgeGraph
impl UnwindSafe for SemanticKnowledgeGraph
Blanket Implementations§
impl<T> Allocation for T
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.