use crate::entity::{Entity, NewEntity};
use crate::error::Result;
use crate::project::{Project, ProjectId};
use crate::query::{PaginatedResults, SearchQuery};
use crate::relation::{Direction, NewRelation, Relation};
use async_trait::async_trait;
#[derive(Debug, Clone, Default)]
pub struct Graph {
pub entities: Vec<Entity>,
pub relations: Vec<Relation>,
}
impl Graph {
pub fn new() -> Self {
Self::default()
}
pub fn with_entities(mut self, entities: Vec<Entity>) -> Self {
self.entities = entities;
self
}
pub fn with_relations(mut self, relations: Vec<Relation>) -> Self {
self.relations = relations;
self
}
}
#[async_trait]
pub trait KnowledgeGraph: Send + Sync {
async fn create_entity(&self, entity: NewEntity, project: &ProjectId) -> Result<Entity>;
async fn get_entity(&self, name: &str, project: &ProjectId) -> Result<Option<Entity>>;
async fn get_entities(&self, names: &[String], project: &ProjectId) -> Result<Vec<Entity>>;
async fn update_entity(&self, entity: &Entity) -> Result<Entity>;
async fn delete_entity(&self, name: &str, project: &ProjectId) -> Result<()>;
async fn add_observations(
&self,
name: &str,
observations: Vec<String>,
project: &ProjectId,
) -> Result<Entity>;
async fn remove_observations(
&self,
name: &str,
observation_ids: &[String],
project: &ProjectId,
) -> Result<Entity>;
async fn add_tags(&self, name: &str, tags: Vec<String>, project: &ProjectId) -> Result<Entity>;
async fn remove_tags(
&self,
name: &str,
tags: &[String],
project: &ProjectId,
) -> Result<Entity>;
async fn create_relation(&self, relation: NewRelation, project: &ProjectId)
-> Result<Relation>;
async fn get_relations(
&self,
entity_name: &str,
direction: Direction,
project: &ProjectId,
) -> Result<Vec<Relation>>;
async fn delete_relation(
&self,
from: &str,
to: &str,
relation_type: &str,
project: &ProjectId,
) -> Result<()>;
async fn read_graph(&self, project: &ProjectId) -> Result<Graph>;
async fn traverse(
&self,
start: &str,
depth: u32,
direction: Direction,
project: &ProjectId,
) -> Result<Graph>;
async fn search(&self, query: SearchQuery) -> Result<PaginatedResults<Entity>>;
async fn list_projects(&self) -> Result<Vec<Project>>;
async fn create_project(&self, name: &str, description: Option<&str>) -> Result<Project>;
async fn get_project(&self, name: &str) -> Result<Option<Project>>;
async fn get_project_by_id(&self, id: &ProjectId) -> Result<Option<Project>>;
async fn delete_project(&self, name: &str) -> Result<()>;
async fn get_or_create_default_project(&self) -> Result<Project>;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_graph_builder() {
let graph = Graph::new()
.with_entities(vec![])
.with_relations(vec![]);
assert!(graph.entities.is_empty());
assert!(graph.relations.is_empty());
}
}