use std::path::Path;
use std::sync::Arc;
use crate::error::Result;
use crate::graph::{GraphEngine, TurtleDocument};
use crate::schema_dispatch::{self, ParsedGgenToml};
use crate::sync::{new_graph_engine, read_ontology_file, EngineKind};
pub fn load_for_query(root: &Path) -> Result<Arc<dyn GraphEngine>> {
load_for_query_with_engine(root, EngineKind::default())
}
pub fn load_for_query_with_engine(root: &Path, engine: EngineKind) -> Result<Arc<dyn GraphEngine>> {
let graph: Arc<dyn GraphEngine> = new_graph_engine(engine)?;
let sources = match schema_dispatch::load(root)? {
ParsedGgenToml::DeclarativeRules(manifest) => {
let mut sources = Vec::with_capacity(1 + manifest.ontology.imports.len());
let ontology_path = root.join(&manifest.ontology.source);
sources.push(read_ontology_file(root, &ontology_path)?);
for import in &manifest.ontology.imports {
let import_path = root.join(import);
sources.push(read_ontology_file(root, &import_path)?);
}
sources
}
ParsedGgenToml::Frontmatter(config) => {
let packs = crate::pack::resolve_read_only(&config, root)?;
let ontology_path = root.join(&config.ontology.source);
let mut sources = Vec::with_capacity(
2 + packs
.iter()
.map(|p| 1 + p.extra_ontology_paths.len())
.sum::<usize>(),
);
sources.push(read_ontology_file(root, &ontology_path)?);
for import in &config.ontology.imports {
sources.push(read_ontology_file(root, &root.join(import))?);
}
for pack in &packs {
sources.push(read_ontology_file(root, &pack.ontology_path)?);
for (declared, extra_path) in &pack.extra_ontology_paths {
let (_, content) = read_ontology_file(root, extra_path)?;
sources.push((declared.clone(), content));
}
}
let pack_topology_ttl = crate::pack_scope::topology_turtle(&packs);
if !pack_topology_ttl.is_empty() {
sources.push(("urn:ggen:pack-topology".to_string(), pack_topology_ttl));
}
sources
}
};
let documents: Vec<TurtleDocument<'_>> = sources
.iter()
.map(|(label, content)| TurtleDocument::new(label, content))
.collect();
let _receipt = graph.insert_turtle_documents(&documents)?;
Ok(graph)
}