-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
70 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use std::convert::Infallible; | ||
|
||
use anyhow::Result; | ||
use sophia::{ | ||
api::{quad::Spog, sparql::SparqlDataset}, | ||
sparql::{SparqlQuery, SparqlWrapper, SparqlWrapperError}, | ||
term::ArcTerm, | ||
}; | ||
|
||
use crate::common::{pipe::PipeSubcommand, quad_handler::QuadHandler, quad_iter::QuadIter}; | ||
|
||
/// Keep only quads that match a SPARQL expression. | ||
/// | ||
/// In the expression, ?s, ?p, ?o and ?g are bound to the subject, | ||
/// predicare, object and graph name of the quad, respectively. | ||
#[derive(clap::Args, Clone, Debug)] | ||
pub struct Args { | ||
/// SPARQL expression | ||
#[arg()] | ||
expression: String, | ||
|
||
#[command(subcommand)] | ||
pipeline: Option<PipeSubcommand>, | ||
} | ||
|
||
pub fn run(mut quads: QuadIter, args: Args) -> Result<()> { | ||
log::trace!("filter args: {args:#?}"); | ||
|
||
let ask_query = make_query(&args.expression)?; | ||
let handler = QuadHandler::new(args.pipeline); | ||
handler.handle_quads(QuadIter::new(quads.into_iter().filter_map(|res| { | ||
let Ok(quad) = res else { | ||
return Some(res); // always keep errors | ||
}; | ||
let dataset = [quad]; | ||
let sparql = SparqlWrapper(&dataset[..]); | ||
let resp = sparql.query(&ask_query).ok()?.into_boolean(); | ||
let [quad] = dataset; | ||
resp.then(|| Ok(quad)) | ||
}))) | ||
} | ||
|
||
fn make_query( | ||
expression: &str, | ||
) -> Result<SparqlQuery<[Spog<ArcTerm>]>, SparqlWrapperError<Infallible>> { | ||
let empty_dataset: [Spog<ArcTerm>; 0] = []; | ||
let sparql = SparqlWrapper(&empty_dataset[..]); | ||
sparql.prepare_query(&format!( | ||
"ASK {{ {{ ?s ?p ?o }} UNION {{ GRAPH ?g {{ ?s ?p ?o }} }} FILTER ({expression}) }}" | ||
)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters