Skip to main content

KnowledgeGraph

Struct KnowledgeGraph 

Source
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, heat

Implementations§

Source§

impl KnowledgeGraph

Source

pub fn new() -> Self

Create an empty graph.

Source

pub fn add_entity(&mut self, entity: Entity) -> EntityId

Add an entity. Returns its ID.

Source

pub fn add_relation(&mut self, relation: Relation)

Add a directed relation.

Source

pub fn entity(&self, id: EntityId) -> Option<&Entity>

Get an entity by ID.

Source

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

Get all entities of a given type.

Source

pub fn neighbors( &self, entity_id: EntityId, relation_type: Option<RelationType>, ) -> Vec<(&Entity, &Relation)>

Get outgoing neighbors, optionally filtered by relation type.

Source

pub fn incoming_neighbors( &self, entity_id: EntityId, relation_type: Option<RelationType>, ) -> Vec<(&Entity, &Relation)>

Get incoming neighbors (reverse edges).

Source

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.

Source

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.

Source

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.

Source

pub fn n_entities(&self) -> usize

Number of entities.

Source

pub fn n_relations(&self) -> usize

Number of relations.

Source

pub fn stats(&self) -> String

Summary statistics.

Trait Implementations§

Source§

impl Clone for KnowledgeGraph

Source§

fn clone(&self) -> KnowledgeGraph

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for KnowledgeGraph

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for KnowledgeGraph

Source§

fn default() -> KnowledgeGraph

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

impl<'de> Deserialize<'de> for KnowledgeGraph

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for KnowledgeGraph

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,