pub struct KnowledgeGraph { /* private fields */ }Expand description
A knowledge graph with typed entities and relations.
§Example
use cvx_graph::{KnowledgeGraph, Entity, EntityType, Relation, RelationType};
let mut kg = KnowledgeGraph::new();
// Define a task plan
let task = kg.add_entity(Entity::new(1, EntityType::Task, "heat_then_place"));
let find = kg.add_entity(Entity::new(2, EntityType::Action, "find"));
let take = kg.add_entity(Entity::new(3, EntityType::Action, "take"));
let heat = kg.add_entity(Entity::new(4, EntityType::Action, "heat"));
kg.add_relation(Relation::new(task, find, RelationType::Requires, 1.0));
kg.add_relation(Relation::new(find, take, RelationType::Precedes, 1.0));
kg.add_relation(Relation::new(take, heat, RelationType::Precedes, 1.0));
// Query: what steps does heat_then_place require?
let steps = kg.neighbors(task, Some(RelationType::Requires));
assert_eq!(steps.len(), 1);
// Multi-hop: what comes after find?
let chain = kg.traverse(find, &[RelationType::Precedes], 3);
assert!(chain.len() >= 2); // take, heatImplementations§
Source§impl KnowledgeGraph
impl KnowledgeGraph
Sourcepub fn add_entity(&mut self, entity: Entity) -> EntityId
pub fn add_entity(&mut self, entity: Entity) -> EntityId
Add an entity. Returns its ID.
Sourcepub fn add_relation(&mut self, relation: Relation)
pub fn add_relation(&mut self, relation: Relation)
Add a directed relation.
Sourcepub fn entities_by_type(&self, entity_type: &EntityType) -> Vec<&Entity>
pub fn entities_by_type(&self, entity_type: &EntityType) -> Vec<&Entity>
Get all entities of a given type.
Sourcepub fn neighbors(
&self,
entity_id: EntityId,
relation_type: Option<RelationType>,
) -> Vec<(&Entity, &Relation)>
pub fn neighbors( &self, entity_id: EntityId, relation_type: Option<RelationType>, ) -> Vec<(&Entity, &Relation)>
Get outgoing neighbors, optionally filtered by relation type.
Sourcepub fn incoming_neighbors(
&self,
entity_id: EntityId,
relation_type: Option<RelationType>,
) -> Vec<(&Entity, &Relation)>
pub fn incoming_neighbors( &self, entity_id: EntityId, relation_type: Option<RelationType>, ) -> Vec<(&Entity, &Relation)>
Get incoming neighbors (reverse edges).
Sourcepub fn traverse(
&self,
start: EntityId,
relation_types: &[RelationType],
max_hops: usize,
) -> Vec<(EntityId, usize)>
pub fn traverse( &self, start: EntityId, relation_types: &[RelationType], max_hops: usize, ) -> Vec<(EntityId, usize)>
Multi-hop traversal following specified relation types.
Returns all reachable entities with their hop distance.
Sourcepub fn find_path(
&self,
from: EntityId,
to: EntityId,
relation_types: &[RelationType],
max_hops: usize,
) -> Option<Vec<EntityId>>
pub fn find_path( &self, from: EntityId, to: EntityId, relation_types: &[RelationType], max_hops: usize, ) -> Option<Vec<EntityId>>
Find a path between two entities following given relation types.
Returns the path as a sequence of entity IDs, or None if no path.
Sourcepub fn task_plan(&self, task_id: EntityId) -> Vec<EntityId> ⓘ
pub fn task_plan(&self, task_id: EntityId) -> Vec<EntityId> ⓘ
Get the ordered sequence of steps for a task.
Follows Requires from the task, then Precedes between steps.
Sourcepub fn n_entities(&self) -> usize
pub fn n_entities(&self) -> usize
Number of entities.
Sourcepub fn n_relations(&self) -> usize
pub fn n_relations(&self) -> usize
Number of relations.
Trait Implementations§
Source§impl Clone for KnowledgeGraph
impl Clone for KnowledgeGraph
Source§fn clone(&self) -> KnowledgeGraph
fn clone(&self) -> KnowledgeGraph
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for KnowledgeGraph
impl Debug for KnowledgeGraph
Source§impl Default for KnowledgeGraph
impl Default for KnowledgeGraph
Source§fn default() -> KnowledgeGraph
fn default() -> KnowledgeGraph
Returns the “default value” for a type. Read more
Source§impl<'de> Deserialize<'de> for KnowledgeGraph
impl<'de> Deserialize<'de> for KnowledgeGraph
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations§
impl Freeze for KnowledgeGraph
impl RefUnwindSafe for KnowledgeGraph
impl Send for KnowledgeGraph
impl Sync for KnowledgeGraph
impl Unpin for KnowledgeGraph
impl UnsafeUnpin for KnowledgeGraph
impl UnwindSafe for KnowledgeGraph
Blanket Implementations§
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
Mutably borrows from an owned value. Read more