Skip to main content

KnowledgeGraph

Struct KnowledgeGraph 

Source
pub struct KnowledgeGraph { /* private fields */ }
Expand description

In-memory knowledge graph with indexed lookups and graph traversal.

Implementations§

Source§

impl KnowledgeGraph

Source

pub fn new() -> Self

Create an empty knowledge graph.

Source

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.

Source

pub fn get_entity(&self, id: &str) -> Option<&Entity>

Retrieve an entity by ID.

Source

pub fn find_entities(&self, name: &str) -> Vec<&Entity>

Find all entities whose name matches (case-insensitive substring).

Source

pub fn find_by_type(&self, entity_type: &EntityType) -> Vec<&Entity>

Find all entities of a given type.

Source

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.

Source

pub fn remove_entity(&mut self, id: &str) -> bool

Remove an entity and all its incident relationships. Returns true if the entity existed.

Source

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.

Source

pub fn get_relationships_from(&self, entity_id: &str) -> Vec<&Relationship>

Get all relationships originating from a given entity.

Source

pub fn get_relationships_to(&self, entity_id: &str) -> Vec<&Relationship>

Get all relationships pointing to a given entity.

Source

pub fn find_relationships( &self, from: Option<&str>, to: Option<&str>, rel_type: Option<&RelationType>, ) -> Vec<&Relationship>

Find relationships matching optional filters.

Source

pub fn remove_relationship(&mut self, id: &str) -> bool

Remove a relationship by ID. Returns true if it was found and removed.

Source

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.

Source

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.

Source

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.

Source

pub fn entity_count(&self) -> usize

Total number of entities.

Source

pub fn relationship_count(&self) -> usize

Total number of relationships.

Source

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.

Source

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.

Source

pub fn summarize(&self) -> GraphSummary

Compute aggregate statistics about the graph.

Source

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.

Source

pub fn save(&self, path: &Path) -> ArgentorResult<()>

Save the graph to a JSON file.

Source

pub fn load(path: &Path) -> ArgentorResult<Self>

Load a graph from a JSON file.

Trait Implementations§

Source§

impl Default for KnowledgeGraph

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more