Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 60 additions & 14 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,20 +207,14 @@ pub async fn do_query_with_dataset_with_options<W: Write>(
} else {
let mut dir_path_vec = Vec::new();
let mut source_hdt_cache = HashMap::new();
let mut hdt_path_vec = Vec::new();
for source_file in &dataset_key.default_data_files {
let hdt_path = prepare_source_hdt_path(
source_file,
entailment_mode,
&mut source_hdt_cache,
&mut dir_path_vec,
)
.await
.map_err(|e| anyhow::anyhow!("Error reading data files: {e}"))?;
hdt_path_vec.push(hdt_path);
}
hdt_path_vec.sort_unstable();
hdt_path_vec.dedup();
let hdt_path_vec = prepare_default_graph_hdt_paths(
&dataset_key.default_data_files,
entailment_mode,
&mut source_hdt_cache,
&mut dir_path_vec,
)
.await
.map_err(|e| anyhow::anyhow!("Error reading data files: {e}"))?;

let mut named_hdt_graphs = Vec::new();
for binding in &dataset_key.named_graphs {
Expand Down Expand Up @@ -640,6 +634,58 @@ async fn prepare_source_hdt_path(
Ok(resolved_hdt_path)
}

/// Prepares the files of the default graph, returning the HDT paths to query.
///
/// The default graph is the merge of its files, so with entailment its closure
/// has to be computed over all of them at once: an ontology in one file must
/// apply to the data in another. Without entailment each file is prepared on
/// its own, which lets a file that is also bound as a named graph be converted
/// only once. Named graphs always keep a closure of their own.
async fn prepare_default_graph_hdt_paths(
default_data_files: &[String],
entailment_mode: EntailmentMode,
source_hdt_cache: &mut HashMap<String, String>,
dir_path_vec: &mut Vec<String>,
) -> anyhow::Result<Vec<String>> {
if entailment_mode == EntailmentMode::OwlRl && default_data_files.len() > 1 {
let merged =
prepare_merged_hdt_path(default_data_files, entailment_mode, dir_path_vec).await?;
return Ok(vec![merged]);
}
let mut hdt_paths = Vec::with_capacity(default_data_files.len());
for source_file in default_data_files {
hdt_paths.push(
prepare_source_hdt_path(source_file, entailment_mode, source_hdt_cache, dir_path_vec)
.await?,
);
}
hdt_paths.sort_unstable();
hdt_paths.dedup();
Ok(hdt_paths)
}

/// Prepares several source files as one HDT, so that entailment is computed
/// over their merge rather than over each file on its own.
async fn prepare_merged_hdt_path(
source_files: &[String],
entailment_mode: EntailmentMode,
dir_path_vec: &mut Vec<String>,
) -> anyhow::Result<String> {
let prepared = handle_files(source_files.to_vec(), entailment_mode).await?;
dir_path_vec.extend(prepared.cleanup_dirs);
match prepared.hdt_paths.as_slice() {
[] => {
let (dir, path) = create_empty_hdt_for_named_graph()?;
dir_path_vec.push(dir);
Ok(path)
}
[path] => Ok(path.clone()),
paths => Err(anyhow::anyhow!(
"multiple prepared HDT paths for sources {source_files:?}: {paths:?}"
)),
}
}

#[allow(clippy::too_many_lines)]
async fn handle_files(
files: Vec<String>,
Expand Down
3 changes: 3 additions & 0 deletions tests/goldens/query-mixed-inputs.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fruit
http://example.org/Pineapple
http://example.org/Banana
3 changes: 3 additions & 0 deletions tests/goldens/query-named-graphs.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
g,fruit
http://example.org/banana,http://example.org/Banana
http://example.org/pineapple,http://example.org/Pineapple
2 changes: 2 additions & 0 deletions tests/goldens/query-owl-rl.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
food
http://example.org/Banana
4 changes: 4 additions & 0 deletions tests/goldens/query-two-files.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fruit
http://example.org/Pineapple
fruit,color
http://example.org/Pineapple,yellow
2 changes: 2 additions & 0 deletions tests/resources/ask-banana-yellow.rq
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PREFIX ex: <http://example.org/>
ASK { ex:Banana ex:hasColor "yellow" }
27 changes: 27 additions & 0 deletions tests/resources/banana.jsonld
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"@context": {
"ex": "http://example.org/",
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
"xsd": "http://www.w3.org/2001/XMLSchema#"
},
"@graph": [
{
"@id": "ex:Banana",
"@type": "ex:Fruit",
"ex:hasColor": "yellow",
"ex:hasTaste": "sweet",
"ex:hasShape": "curved",
"ex:hasWeight": "120 grams",
"ex:growsIn": { "@id": "ex:TropicalClimate" },
"ex:isEdible": { "@value": "true", "@type": "xsd:boolean" },
"ex:hasVitamin": { "@id": "ex:VitaminB6" },
"ex:hasPrice": "$0.50",
"ex:hasOrigin": "Ecuador"
},
{
"@id": "ex:Fruit",
"@type": "rdfs:Class",
"rdfs:label": "Fruit"
}
]
}
17 changes: 17 additions & 0 deletions tests/resources/banana.n3
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@prefix ex: <http://example.org/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

ex:Banana a ex:Fruit ;
ex:hasColor "yellow" ;
ex:hasTaste "sweet" ;
ex:hasShape "curved" ;
ex:hasWeight "120 grams" ;
ex:growsIn ex:TropicalClimate ;
ex:isEdible "true"^^xsd:boolean ;
ex:hasVitamin ex:VitaminB6 ;
ex:hasPrice "$0.50" ;
ex:hasOrigin "Ecuador" .

ex:Fruit a rdfs:Class ;
rdfs:label "Fruit" .
12 changes: 12 additions & 0 deletions tests/resources/banana.nq
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<http://example.org/Banana> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/Fruit> .
<http://example.org/Banana> <http://example.org/hasColor> "yellow" .
<http://example.org/Banana> <http://example.org/hasTaste> "sweet" .
<http://example.org/Banana> <http://example.org/hasShape> "curved" .
<http://example.org/Banana> <http://example.org/hasWeight> "120 grams" .
<http://example.org/Banana> <http://example.org/growsIn> <http://example.org/TropicalClimate> .
<http://example.org/Banana> <http://example.org/isEdible> "true"^^<http://www.w3.org/2001/XMLSchema#boolean> .
<http://example.org/Banana> <http://example.org/hasVitamin> <http://example.org/VitaminB6> .
<http://example.org/Banana> <http://example.org/hasPrice> "$0.50" .
<http://example.org/Banana> <http://example.org/hasOrigin> "Ecuador" .
<http://example.org/Fruit> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#Class> .
<http://example.org/Fruit> <http://www.w3.org/2000/01/rdf-schema#label> "Fruit" .
19 changes: 19 additions & 0 deletions tests/resources/banana.rdf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:ex="http://example.org/">
<ex:Fruit rdf:about="http://example.org/Banana">
<ex:hasColor>yellow</ex:hasColor>
<ex:hasTaste>sweet</ex:hasTaste>
<ex:hasShape>curved</ex:hasShape>
<ex:hasWeight>120 grams</ex:hasWeight>
<ex:growsIn rdf:resource="http://example.org/TropicalClimate"/>
<ex:isEdible rdf:datatype="http://www.w3.org/2001/XMLSchema#boolean">true</ex:isEdible>
<ex:hasVitamin rdf:resource="http://example.org/VitaminB6"/>
<ex:hasPrice>$0.50</ex:hasPrice>
<ex:hasOrigin>Ecuador</ex:hasOrigin>
</ex:Fruit>
<rdfs:Class rdf:about="http://example.org/Fruit">
<rdfs:label>Fruit</rdfs:label>
</rdfs:Class>
</rdf:RDF>
20 changes: 20 additions & 0 deletions tests/resources/banana.trig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@prefix ex: <http://example.org/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

{
ex:Banana a ex:Fruit ;
ex:hasColor "yellow" ;
ex:hasTaste "sweet" ;
ex:hasShape "curved" ;
ex:hasWeight "120 grams" ;
ex:growsIn ex:TropicalClimate ;
ex:isEdible "true"^^xsd:boolean ;
ex:hasVitamin ex:VitaminB6 ;
ex:hasPrice "$0.50" ;
ex:hasOrigin "Ecuador" .

ex:Fruit a rdfs:Class ;
rdfs:label "Fruit" .
}
1 change: 1 addition & 0 deletions tests/resources/construct-all.rq
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o }
4 changes: 4 additions & 0 deletions tests/resources/fruit-ontology.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@prefix ex: <http://example.org/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

ex:Fruit rdfs:subClassOf ex:Food .
1 change: 1 addition & 0 deletions tests/resources/graph1.nq
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<http://example.org/Banana> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/Fruit> <http://example.org/g1> .
1 change: 1 addition & 0 deletions tests/resources/graph2.nq
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<http://example.org/Pineapple> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/Fruit> <http://example.org/g2> .
1 change: 1 addition & 0 deletions tests/resources/invalid.nt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<http://example.org/Banana> <http://example.org/hasColor> .
5 changes: 5 additions & 0 deletions tests/resources/query-food.rq
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
PREFIX ex: <http://example.org/>

SELECT ?food
WHERE { ?food a ex:Food }
ORDER BY ?food
5 changes: 5 additions & 0 deletions tests/resources/query-fruit.rq
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
PREFIX ex: <http://example.org/>

SELECT ?fruit
WHERE { ?fruit a ex:Fruit }
ORDER BY ?fruit
5 changes: 5 additions & 0 deletions tests/resources/query-graphs.rq
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
PREFIX ex: <http://example.org/>

SELECT ?g ?fruit
WHERE { GRAPH ?g { ?fruit a ex:Fruit } }
ORDER BY ?g ?fruit
1 change: 1 addition & 0 deletions tests/resources/query-syntax-error.rq
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELEC ?fruit WHERE { ?fruit ?p ?o }
69 changes: 69 additions & 0 deletions tests/support/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#![allow(dead_code)]

use oxrdfio::{RdfFormat, RdfParser};
use std::collections::BTreeSet;
use std::{fs::OpenOptions, io::BufWriter, path::Path, process::Output};

pub fn devnull_writer() -> std::io::Result<BufWriter<std::fs::File>> {
Expand Down Expand Up @@ -66,3 +68,70 @@ pub fn run_de_ok_stdout(cwd: &Path, args: &[&str]) -> anyhow::Result<String> {
.trim()
.to_string())
}

/// Runs `de` with `TMPDIR` set to `tmpdir`, so a test can check what it leaves behind.
pub fn run_de_with_tmpdir(cwd: &Path, tmpdir: &Path, args: &[&str]) -> anyhow::Result<Output> {
std::process::Command::new(env!("CARGO_BIN_EXE_de"))
.current_dir(cwd)
.env("TMPDIR", tmpdir)
.args(args)
.output()
.map_err(|e| anyhow::anyhow!("failed to execute de {}: {e}", args.join(" ")))
}

/// Runs `de` expecting it to fail, and returns its stderr.
pub fn run_de_err(cwd: &Path, args: &[&str]) -> anyhow::Result<String> {
let output = run_de_command(cwd, args)?;
if output.status.success() {
return Err(anyhow::anyhow!(
"de {} succeeded but was expected to fail\nstdout:\n{}",
args.join(" "),
String::from_utf8_lossy(&output.stdout)
));
}
Ok(String::from_utf8_lossy(&output.stderr).to_string())
}

/// Entries left in a temporary directory, apart from the cache lock directory
/// that hdt keeps there on purpose so concurrent processes share it.
pub fn leftover_temp_files(dir: &Path) -> anyhow::Result<Vec<String>> {
let mut left = Vec::new();
for entry in std::fs::read_dir(dir)? {
let name = entry?.file_name().to_string_lossy().to_string();
if name != "hdt-hybrid-cache-locks" {
left.push(name);
}
}
left.sort();
Ok(left)
}

/// Parses RDF into a set of N-Triples statements, so that two graphs can be
/// compared whatever their serialization or statement order.
pub fn triple_set(rdf: &[u8], format: RdfFormat) -> anyhow::Result<BTreeSet<String>> {
let mut triples = BTreeSet::new();
for quad in RdfParser::from_format(format).for_reader(rdf) {
let quad = quad?;
triples.insert(format!(
"{} {} {} .",
quad.subject, quad.predicate, quad.object
));
}
Ok(triples)
}

/// The triples of `tests/resources/banana.nt`, the reference the format tests compare against.
pub fn banana_triples() -> anyhow::Result<BTreeSet<String>> {
triple_set(
&std::fs::read("tests/resources/banana.nt")?,
RdfFormat::NTriples,
)
}

/// A committed expected result from `tests/goldens/`, with line endings normalized.
pub fn golden(name: &str) -> anyhow::Result<String> {
Ok(std::fs::read_to_string(format!("tests/goldens/{name}"))?
.replace('\r', "")
.trim()
.to_string())
}
Loading
Loading