pub struct KnowledgeGraph { /* private fields */ }Expand description
In-memory knowledge graph with indexed lookups and graph traversal.
Implementations§
Source§impl KnowledgeGraph
impl KnowledgeGraph
Sourcepub fn add_entity(&mut self, entity: Entity) -> String
pub fn add_entity(&mut self, entity: Entity) -> String
Add an entity to the graph. Returns the entity ID.
If the entity’s id field is empty, a new UUID is generated.
Sourcepub fn get_entity(&self, id: &str) -> Option<&Entity>
pub fn get_entity(&self, id: &str) -> Option<&Entity>
Retrieve an entity by ID.
Sourcepub fn find_entities(&self, name: &str) -> Vec<&Entity>
pub fn find_entities(&self, name: &str) -> Vec<&Entity>
Find all entities whose name matches (case-insensitive substring).
Sourcepub fn find_by_type(&self, entity_type: &EntityType) -> Vec<&Entity>
pub fn find_by_type(&self, entity_type: &EntityType) -> Vec<&Entity>
Find all entities of a given type.
Sourcepub fn update_entity(
&mut self,
id: &str,
properties: HashMap<String, Value>,
) -> bool
pub fn update_entity( &mut self, id: &str, properties: HashMap<String, Value>, ) -> bool
Update an entity’s properties. Returns true if the entity was found and updated.
Sourcepub fn remove_entity(&mut self, id: &str) -> bool
pub fn remove_entity(&mut self, id: &str) -> bool
Remove an entity and all its incident relationships.
Returns true if the entity existed.
Sourcepub fn add_relationship(&mut self, rel: Relationship) -> String
pub fn add_relationship(&mut self, rel: Relationship) -> String
Add a relationship to the graph. Returns the relationship ID.
If the relationship’s id field is empty, a new UUID is generated.
Sourcepub fn get_relationships_from(&self, entity_id: &str) -> Vec<&Relationship>
pub fn get_relationships_from(&self, entity_id: &str) -> Vec<&Relationship>
Get all relationships originating from a given entity.
Sourcepub fn get_relationships_to(&self, entity_id: &str) -> Vec<&Relationship>
pub fn get_relationships_to(&self, entity_id: &str) -> Vec<&Relationship>
Get all relationships pointing to a given entity.
Sourcepub fn find_relationships(
&self,
from: Option<&str>,
to: Option<&str>,
rel_type: Option<&RelationType>,
) -> Vec<&Relationship>
pub fn find_relationships( &self, from: Option<&str>, to: Option<&str>, rel_type: Option<&RelationType>, ) -> Vec<&Relationship>
Find relationships matching optional filters.
Sourcepub fn remove_relationship(&mut self, id: &str) -> bool
pub fn remove_relationship(&mut self, id: &str) -> bool
Remove a relationship by ID. Returns true if it was found and removed.
Sourcepub fn neighbors(&self, entity_id: &str, depth: usize) -> Vec<&Entity>
pub fn neighbors(&self, entity_id: &str, depth: usize) -> Vec<&Entity>
BFS traversal to collect all neighbor entities within a given depth.
Treats the graph as undirected for traversal purposes.
Sourcepub fn shortest_path(&self, from: &str, to: &str) -> Option<Vec<String>>
pub fn shortest_path(&self, from: &str, to: &str) -> Option<Vec<String>>
BFS shortest path between two entities. Returns the list of entity IDs along the path
(including start and end), or None if no path exists.
Treats the graph as undirected.
Sourcepub fn connected_component(&self, entity_id: &str) -> Vec<&Entity>
pub fn connected_component(&self, entity_id: &str) -> Vec<&Entity>
Return all entities in the connected component containing entity_id.
Treats the graph as undirected.
Sourcepub fn entity_count(&self) -> usize
pub fn entity_count(&self) -> usize
Total number of entities.
Sourcepub fn relationship_count(&self) -> usize
pub fn relationship_count(&self) -> usize
Total number of relationships.
Sourcepub fn extract_entities_from_text(
&mut self,
text: &str,
source: &str,
) -> Vec<String>
pub fn extract_entities_from_text( &mut self, text: &str, source: &str, ) -> Vec<String>
Extract entities from free text using regex-based heuristics.
Detects:
- Capitalized multi-word phrases (potential names / organizations)
- Email addresses (creates Person entities)
- URLs (creates Custom(“Url”) entities)
- @mentions (creates Person entities)
- #hashtags (creates Concept entities)
- File paths (creates File entities)
Returns the IDs of all newly created entities.
Sourcepub fn merge_entity(
&mut self,
source_id: &str,
target_id: &str,
) -> ArgentorResult<()>
pub fn merge_entity( &mut self, source_id: &str, target_id: &str, ) -> ArgentorResult<()>
Merge source_id entity into target_id.
All properties from the source are copied to the target (existing keys are not overwritten). All relationships that reference the source entity are redirected to the target. The source entity is then removed.
Sourcepub fn summarize(&self) -> GraphSummary
pub fn summarize(&self) -> GraphSummary
Compute aggregate statistics about the graph.
Sourcepub fn to_context_string(&self, entity_id: &str, depth: usize) -> String
pub fn to_context_string(&self, entity_id: &str, depth: usize) -> String
Generate a human-readable context string about an entity and its neighborhood.
Output includes entity properties, direct relationships, and optionally relationships of neighbors up to the given depth.
Sourcepub fn save(&self, path: &Path) -> ArgentorResult<()>
pub fn save(&self, path: &Path) -> ArgentorResult<()>
Save the graph to a JSON file.
Sourcepub fn load(path: &Path) -> ArgentorResult<Self>
pub fn load(path: &Path) -> ArgentorResult<Self>
Load a graph from a JSON file.