inference_engine/cli/
mod.rs1mod args;
3mod db;
4mod input;
5mod output;
6mod quota;
7mod source;
8use crate::reasoning::{Reasoner, RunLimits};
9use args::Args;
10use clap::Parser;
11use std::{
12 ffi::OsString,
13 io,
14 sync::{Arc, Mutex},
15};
16
17pub fn run(args: Vec<OsString>) -> u8 {
23 let cli =
24 match Args::try_parse_from(std::iter::once(OsString::from("inference-engine")).chain(args))
25 {
26 Ok(cli) => cli,
27 Err(error) => {
28 let status = if error.use_stderr() { 2 } else { 0 };
29 return if error.print().is_ok() { status } else { 1 };
30 }
31 };
32 let worker = std::thread::Builder::new()
33 .name("inference-engine-cli".into())
34 .spawn(move || {
35 let runtime = tokio::runtime::Builder::new_current_thread().build()?;
36 runtime.block_on(execute(cli))
37 });
38 let result = match worker {
39 Ok(worker) => worker
40 .join()
41 .unwrap_or_else(|_| Err(io::Error::other("native CLI worker panicked").into())),
42 Err(error) => Err(error.into()),
43 };
44 match result {
45 Ok(()) => 0,
46 Err(error) => {
47 eprintln!("inference-engine: {error:#}");
48 1
49 }
50 }
51}
52async fn execute(args: Args) -> anyhow::Result<()> {
53 args.validate()?;
54 let work = Arc::new(db::Work::new(args.max_steps.get()));
55 let work_for_load = work.clone();
56 let loaded = tokio::task::spawn_blocking(move || -> anyhow::Result<_> {
57 let directory = match &args.scratch_dir {
58 Some(parent) => tempfile::tempdir_in(parent)?,
59 None => tempfile::tempdir()?,
60 };
61 let mut state = db::State::create(&directory.path().join("reasoning.redb"), &args)?;
62 input::load(&mut state, &args, &work_for_load)?;
63 Ok((directory, Arc::new(Mutex::new(state)), args))
64 })
65 .await??;
66 let (directory, state, args) = loaded;
67 let mut after = None;
68 while let Some(graph) = source::next_graph(&state, after).await? {
69 let mut store = source::Store {
70 state: state.clone(),
71 graph,
72 work: work.clone(),
73 };
74 Reasoner::new(
75 args.profile,
76 RunLimits::new(args.max_rounds, args.max_inferred, args.max_steps),
77 )
78 .run(&mut store)
79 .await?;
80 after = Some(graph);
81 }
82 tokio::task::spawn_blocking(move||->anyhow::Result<()> {
83 let mut state=Arc::try_unwrap(state).map_err(|_|io::Error::other("scratch readers outlived reasoning"))?.into_inner().map_err(|_|io::Error::other("scratch state poisoned"))?;
84 let(emitted,omitted)=output::publish(&mut state,&args,&work,directory.path())?;
85 if !args.quiet {eprintln!("profile={}; asserted={}; inferred={}; emitted={}; restricted profile, not full RDFS",args.profile.name(),state.asserted,state.inferred,emitted);eprintln!("profile exclusions: {:?}",args.profile.exclusions());}
86 if omitted>0 {eprintln!("RDF projection omitted {omitted} generalized tuples after reasoning");}
87 drop(state);directory.close()?;Ok(())
88 }).await??;
89 Ok(())
90}