Makes NamedNode store a String internally

pull/10/head
Tpt 5 years ago
parent 4ad8aa6fcf
commit de8997c750
  1. 28
      lib/src/model/named_node.rs
  2. 25
      lib/src/rio.rs
  3. 8
      lib/src/sparql/parser.rs
  4. 2
      lib/src/sparql/sparql_grammar.rustpeg
  5. 5
      lib/src/store/memory.rs
  6. 19
      lib/src/store/numeric_encoder.rs
  7. 5
      lib/src/store/rocksdb.rs
  8. 69
      lib/tests/rdf_test_cases.rs
  9. 156
      lib/tests/sparql_test_cases.rs

@ -3,8 +3,6 @@ use crate::Result;
use rio_api::model as rio;
use std::fmt;
use std::str::FromStr;
use std::sync::Arc;
use url::Url;
/// A RDF [IRI](https://www.w3.org/TR/rdf11-concepts/#dfn-iri)
///
@ -24,7 +22,7 @@ use url::Url;
///
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)]
pub struct NamedNode {
iri: Arc<Url>,
iri: String,
}
impl fmt::Display for NamedNode {
@ -38,30 +36,16 @@ impl fmt::Display for NamedNode {
impl NamedNode {
/// Builds a RDF [IRI](https://www.w3.org/TR/rdf11-concepts/#dfn-iri)
pub fn new(iri: impl Into<Url>) -> Self {
Self {
iri: Arc::new(iri.into()),
}
pub fn new(iri: impl Into<String>) -> Self {
Self { iri: iri.into() }
}
pub fn as_str(&self) -> &str {
self.iri.as_str()
}
pub fn as_url(&self) -> &Url {
&self.iri
}
}
impl From<Url> for NamedNode {
fn from(url: Url) -> Self {
Self { iri: Arc::new(url) }
}
}
impl From<NamedNode> for Url {
fn from(named_node: NamedNode) -> Self {
Arc::try_unwrap(named_node.iri).unwrap_or_else(|iri| (*iri).clone())
pub fn into_string(self) -> String {
self.iri
}
}
@ -69,6 +53,6 @@ impl FromStr for NamedNode {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
Ok(Self::new(Url::parse(s)?))
Ok(Self::new(s))
}
}

@ -9,7 +9,6 @@ use rio_xml::RdfXmlParser;
use std::collections::BTreeMap;
use std::io::BufRead;
use std::str::FromStr;
use url::Url;
/// Reads a [N-Triples](https://www.w3.org/TR/n-triples/) file from a Rust `BufRead` and returns an iterator of the read `Triple`s
pub fn read_ntriples<R: BufRead>(reader: R) -> Result<impl Iterator<Item = Result<Triple>>> {
@ -18,27 +17,23 @@ pub fn read_ntriples<R: BufRead>(reader: R) -> Result<impl Iterator<Item = Resul
}
/// Reads a [Turtle](https://www.w3.org/TR/turtle/) file from a Rust `BufRead` and returns an iterator of the read `Triple`s
pub fn read_turtle<R: BufRead>(
pub fn read_turtle<'a, R: BufRead + 'a>(
reader: R,
base_url: Option<Url>,
) -> Result<impl Iterator<Item = Result<Triple>>> {
base_url: Option<&'a str>,
) -> Result<impl Iterator<Item = Result<Triple>> + 'a> {
let mut bnode_map = BTreeMap::default();
Ok(
TurtleParser::new(reader, base_url.as_ref().map_or("", |url| url.as_str()))?
.into_iter(move |t| convert_triple(t, &mut bnode_map)),
)
Ok(TurtleParser::new(reader, base_url.unwrap_or(""))?
.into_iter(move |t| convert_triple(t, &mut bnode_map)))
}
/// Reads a [RDF XML](https://www.w3.org/TR/rdf-syntax-grammar/) file from a Rust `BufRead` and returns an iterator of the read `Triple`s
pub fn read_rdf_xml<R: BufRead>(
pub fn read_rdf_xml<'a, R: BufRead + 'a>(
reader: R,
base_url: Option<Url>,
) -> Result<impl Iterator<Item = Result<Triple>>> {
base_url: Option<&'a str>,
) -> Result<impl Iterator<Item = Result<Triple>> + 'a> {
let mut bnode_map = BTreeMap::default();
Ok(
RdfXmlParser::new(reader, base_url.as_ref().map_or("", |url| url.as_str()))?
.into_iter(move |t| convert_triple(t, &mut bnode_map)),
)
Ok(RdfXmlParser::new(reader, base_url.unwrap_or(""))?
.into_iter(move |t| convert_triple(t, &mut bnode_map)))
}
fn convert_triple(

@ -531,10 +531,14 @@ mod grammar {
pub fn read_sparql_query<'a, R: Read + 'a>(
source: R,
base_uri: impl Into<Option<Url>>,
base_uri: Option<&'a str>,
) -> super::super::super::Result<Query> {
let mut state = ParserState {
base_uri: base_uri.into(),
base_uri: if let Some(base_uri) = base_uri {
Some(Url::parse(base_uri)?)
} else {
None
},
namespaces: HashMap::default(),
bnodes_map: BTreeMap::default(),
aggregations: BTreeMap::default(),

@ -907,7 +907,7 @@ String -> String = STRING_LITERAL_LONG1 / STRING_LITERAL_LONG2 / STRING_LITERAL1
//[136]
iri -> NamedNode = i:(IRIREF / PrefixedName) {?
match state.url_parser().parse(&i) {
Ok(url) => Ok(NamedNode::new(url)),
Ok(url) => Ok(NamedNode::new(url.into_string())),
Err(error) => Err("IRI parsing failed")
}
}

@ -7,7 +7,6 @@ use std::collections::BTreeSet;
use std::sync::RwLock;
use std::sync::RwLockReadGuard;
use std::sync::RwLockWriteGuard;
use url::Url;
/// Memory based implementation of the `rudf::model::Dataset` trait.
/// They are cheap to build using the `MemoryDataset::default()` method.
@ -78,10 +77,6 @@ impl StringStore for MemoryStore {
self.string_store.get_str(id)
}
fn get_url(&self, id: u64) -> Result<Url> {
self.string_store.get_url(id)
}
fn get_language_tag(&self, id: u64) -> Result<LanguageTag> {
self.string_store.get_language_tag(id)
}

@ -16,7 +16,6 @@ use std::ops::Deref;
use std::str;
use std::sync::PoisonError;
use std::sync::RwLock;
use url::Url;
use uuid::Uuid;
const EMPTY_STRING_ID: u64 = 0;
@ -36,7 +35,6 @@ pub trait StringStore {
fn insert_str(&self, value: &str) -> Result<u64>;
fn get_str(&self, id: u64) -> Result<Self::StringType>;
fn get_url(&self, id: u64) -> Result<Url>;
fn get_language_tag(&self, id: u64) -> Result<LanguageTag>;
/// Should be called when the bytes store is created
@ -73,10 +71,6 @@ impl<'a, S: StringStore> StringStore for &'a S {
(*self).get_str(id)
}
fn get_url(&self, id: u64) -> Result<Url> {
(*self).get_url(id)
}
fn get_language_tag(&self, id: u64) -> Result<LanguageTag> {
(*self).get_language_tag(id)
}
@ -121,15 +115,6 @@ impl StringStore for MemoryStringStore {
}
}
fn get_url(&self, id: u64) -> Result<Url> {
let id2str = self.id2str.read().map_err(MutexPoisonError::from)?;
if id2str.len() as u64 <= id {
Err(format_err!("value not found in the dictionary"))
} else {
Ok(Url::parse(&id2str[id as usize])?)
}
}
fn get_language_tag(&self, id: u64) -> Result<LanguageTag> {
let id2str = self.id2str.read().map_err(MutexPoisonError::from)?;
if id2str.len() as u64 <= id {
@ -709,7 +694,7 @@ impl<S: StringStore> Encoder<S> {
Err(format_err!("The default graph tag is not a valid term"))
}
EncodedTerm::NamedNode { iri_id } => {
Ok(NamedNode::from(self.string_store.get_url(iri_id)?).into())
Ok(NamedNode::new(self.string_store.get_str(iri_id)?).into())
}
EncodedTerm::BlankNode(id) => Ok(BlankNode::from(id).into()),
EncodedTerm::StringLiteral { value_id } => {
@ -728,7 +713,7 @@ impl<S: StringStore> Encoder<S> {
datatype_id,
} => Ok(Literal::new_typed_literal(
self.string_store.get_str(value_id)?,
NamedNode::from(self.string_store.get_url(datatype_id)?),
NamedNode::new(self.string_store.get_str(datatype_id)?),
)
.into()),
EncodedTerm::BooleanLiteral(value) => Ok(Literal::from(value).into()),

@ -18,7 +18,6 @@ use std::ops::Deref;
use std::path::Path;
use std::str;
use std::sync::Mutex;
use url::Url;
/// `rudf::model::Dataset` trait implementation based on the [RocksDB](https://rocksdb.org/) key-value store
///
@ -118,10 +117,6 @@ impl StringStore for RocksDbStore {
}
}
fn get_url(&self, id: u64) -> Result<Url> {
Ok(Url::parse(&self.get_str(id)?)?)
}
fn get_language_tag(&self, id: u64) -> Result<LanguageTag> {
Ok(LanguageTag::parse(&self.get_str(id)?)?)
}

@ -11,26 +11,25 @@ use std::fmt;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::PathBuf;
use url::Url;
#[test]
fn turtle_w3c_testsuite() {
let manifest_url = Url::parse("http://w3c.github.io/rdf-tests/turtle/manifest.ttl").unwrap();
let manifest_url = "http://w3c.github.io/rdf-tests/turtle/manifest.ttl";
for test_result in TestManifest::new(manifest_url) {
let test = test_result.unwrap();
if test.kind == "TestTurtlePositiveSyntax" {
if let Err(error) = load_turtle(test.action.clone()) {
if let Err(error) = load_turtle(test.action.as_str()) {
assert!(false, "Failure on {} with error: {}", test, error)
}
} else if test.kind == "TestTurtleNegativeSyntax" {
assert!(
load_turtle(test.action.clone()).is_err(),
load_turtle(test.action.as_str()).is_err(),
"Failure on {}",
test
);
} else if test.kind == "TestTurtleEval" {
match load_turtle(test.action.clone()) {
Ok(action_graph) => match load_turtle(test.result.clone().unwrap()) {
match load_turtle(test.action.as_str()) {
Ok(action_graph) => match load_turtle(test.result.as_ref().unwrap()) {
Ok(result_graph) => assert!(
action_graph.is_isomorphic(&result_graph),
"Failure on {}. Expected file:\n{}\nParsed file:\n{}\n",
@ -41,7 +40,7 @@ fn turtle_w3c_testsuite() {
Err(error) => assert!(
false,
"Failure to parse the Turtle result file {} of {} with error: {}",
test.result.clone().unwrap(),
test.result.as_ref().unwrap(),
test,
error
),
@ -49,11 +48,11 @@ fn turtle_w3c_testsuite() {
Err(error) => assert!(false, "Failure to parse {} with error: {}", test, error),
}
} else if test.kind == "TestTurtleNegativeEval" {
let action_graph = load_turtle(test.action.clone());
let action_graph = load_turtle(test.action.as_str());
let result_graph = test
.result
.clone()
.map(|r| load_turtle(r))
.map(|r| load_turtle(r.as_str()))
.unwrap_or_else(|| Ok(SimpleGraph::default()));
assert!(
action_graph.is_err()
@ -69,16 +68,16 @@ fn turtle_w3c_testsuite() {
#[test]
fn ntriples_w3c_testsuite() {
let manifest_url = Url::parse("http://w3c.github.io/rdf-tests/ntriples/manifest.ttl").unwrap();
let manifest_url = "http://w3c.github.io/rdf-tests/ntriples/manifest.ttl";
for test_result in TestManifest::new(manifest_url) {
let test = test_result.unwrap();
if test.kind == "TestNTriplesPositiveSyntax" {
if let Err(error) = load_ntriples(test.action.clone()) {
if let Err(error) = load_ntriples(test.action.as_str()) {
assert!(false, "Failure on {} with error: {}", test, error)
}
} else if test.kind == "TestNTriplesNegativeSyntax" {
if let Ok(graph) = load_ntriples(test.action.clone()) {
if let Ok(graph) = load_ntriples(test.action.as_str()) {
assert!(false, "Failure on {}, found:\n{}", test, graph);
}
} else {
@ -89,20 +88,20 @@ fn ntriples_w3c_testsuite() {
#[test]
fn rdf_xml_w3c_testsuite() -> Result<()> {
let manifest_url = Url::parse("http://www.w3.org/2013/RDFXMLTests/manifest.ttl")?;
let manifest_url = "http://www.w3.org/2013/RDFXMLTests/manifest.ttl";
for test_result in TestManifest::new(manifest_url) {
let test = test_result?;
if test.kind == "TestXMLNegativeSyntax" {
assert!(
load_rdf_xml(test.action.clone()).is_err(),
load_rdf_xml(test.action.as_str()).is_err(),
"Failure on {}",
test
);
} else if test.kind == "TestXMLEval" {
match load_rdf_xml(test.action.clone()) {
Ok(action_graph) => match load_ntriples(test.result.clone().unwrap()) {
match load_rdf_xml(test.action.as_str()) {
Ok(action_graph) => match load_ntriples(test.result.as_ref().unwrap()) {
Ok(result_graph) => assert!(
action_graph.is_isomorphic(&result_graph),
"Failure on {}. Expected file:\n{}\nParsed file:\n{}\n",
@ -127,20 +126,19 @@ fn rdf_xml_w3c_testsuite() -> Result<()> {
Ok(())
}
fn load_turtle(url: Url) -> Result<SimpleGraph> {
read_turtle(read_file(&url)?, Some(url))?.collect()
fn load_turtle(url: &str) -> Result<SimpleGraph> {
read_turtle(read_file(url)?, Some(url))?.collect()
}
fn load_ntriples(url: Url) -> Result<SimpleGraph> {
read_ntriples(read_file(&url)?)?.collect()
fn load_ntriples(url: &str) -> Result<SimpleGraph> {
read_ntriples(read_file(url)?)?.collect()
}
fn load_rdf_xml(url: Url) -> Result<SimpleGraph> {
read_rdf_xml(read_file(&url)?, Some(url))?.collect()
fn load_rdf_xml(url: &str) -> Result<SimpleGraph> {
read_rdf_xml(read_file(url)?, Some(url))?.collect()
}
fn to_relative_path(url: &Url) -> Result<String> {
let url = url.as_str();
fn to_relative_path(url: &str) -> Result<String> {
if url.starts_with("http://w3c.github.io/rdf-tests/") {
Ok(url.replace("http://w3c.github.io/", ""))
} else if url.starts_with("http://www.w3.org/2013/RDFXMLTests/") {
@ -150,7 +148,7 @@ fn to_relative_path(url: &Url) -> Result<String> {
}
}
fn read_file(url: &Url) -> Result<impl BufRead> {
fn read_file(url: &str) -> Result<impl BufRead> {
let mut base_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
base_path.push("tests");
base_path.push(to_relative_path(url)?);
@ -165,8 +163,8 @@ pub struct Test {
pub kind: String,
pub name: Option<String>,
pub comment: Option<String>,
pub action: Url,
pub result: Option<Url>,
pub action: String,
pub result: Option<String>,
}
impl fmt::Display for Test {
@ -186,15 +184,15 @@ impl fmt::Display for Test {
pub struct TestManifest {
graph: SimpleGraph,
tests_to_do: Vec<Term>,
manifests_to_do: Vec<Url>,
manifests_to_do: Vec<String>,
}
impl TestManifest {
pub fn new(url: Url) -> TestManifest {
pub fn new(url: impl Into<String>) -> TestManifest {
Self {
graph: SimpleGraph::default(),
tests_to_do: Vec::default(),
manifests_to_do: vec![url],
manifests_to_do: vec![url.into()],
}
}
}
@ -258,7 +256,7 @@ impl Iterator for TestManifest {
.graph
.object_for_subject_predicate(&test_subject, &*mf::ACTION)
{
Some(Term::NamedNode(n)) => n.as_url().clone(),
Some(Term::NamedNode(n)) => n.as_str().to_string(),
Some(_) => return Some(Err(format_err!("invalid action"))),
None => return Some(Err(format_err!("action not found"))),
};
@ -266,7 +264,7 @@ impl Iterator for TestManifest {
.graph
.object_for_subject_predicate(&test_subject, &*mf::RESULT)
{
Some(Term::NamedNode(n)) => Some(n.as_url().clone()),
Some(Term::NamedNode(n)) => Some(n.as_str().to_string()),
Some(_) => return Some(Err(format_err!("invalid result"))),
None => None,
};
@ -283,8 +281,9 @@ impl Iterator for TestManifest {
None => {
match self.manifests_to_do.pop() {
Some(url) => {
let manifest = NamedOrBlankNode::from(NamedNode::new(url.clone()));
match load_turtle(url) {
let manifest =
NamedOrBlankNode::from(NamedNode::new(url.as_str().to_string()));
match load_turtle(&url) {
Ok(g) => self.graph.extend(g.into_iter()),
Err(e) => return Some(Err(e.into())),
}
@ -298,7 +297,7 @@ impl Iterator for TestManifest {
self.manifests_to_do.extend(
RdfListIterator::iter(&self.graph, list.clone().into())
.filter_map(|m| match m {
Term::NamedNode(nm) => Some(nm.as_url().clone()),
Term::NamedNode(nm) => Some(nm.as_str().to_string()),
_ => None,
}),
);

@ -18,17 +18,12 @@ use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::PathBuf;
use std::str::FromStr;
use url::Url;
#[test]
fn sparql_w3c_syntax_testsuite() {
let manifest_10_url =
Url::parse("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/manifest-syntax.ttl")
.unwrap();
let manifest_11_url = Url::parse(
"http://www.w3.org/2009/sparql/docs/tests/data-sparql11/syntax-query/manifest.ttl",
)
.unwrap();
let manifest_10_url = "http://www.w3.org/2001/sw/DataAccess/tests/data-r2/manifest-syntax.ttl";
let manifest_11_url =
"http://www.w3.org/2009/sparql/docs/tests/data-sparql11/syntax-query/manifest.ttl";
let test_blacklist = vec![
NamedNode::from_str("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/syntax-sparql2/manifest#syntax-form-construct02").unwrap(),
//TODO: Deserialization of the serialization failing:
@ -44,7 +39,7 @@ fn sparql_w3c_syntax_testsuite() {
continue;
}
if test.kind == "PositiveSyntaxTest" || test.kind == "PositiveSyntaxTest11" {
match load_sparql_query(test.query.clone()) {
match load_sparql_query(&test.query) {
Err(error) => assert!(false, "Failure on {} with error: {}", test, error),
Ok(query) => {
if let Err(error) = read_sparql_query(query.to_string().as_bytes(), None) {
@ -60,7 +55,7 @@ fn sparql_w3c_syntax_testsuite() {
}
} else if test.kind == "NegativeSyntaxTest" || test.kind == "NegativeSyntaxTest11" {
//TODO
if let Ok(result) = load_sparql_query(test.query.clone()) {
if let Ok(result) = load_sparql_query(&test.query) {
eprintln!("Failure on {}. The output tree is: {}", test, result);
}
} else {
@ -73,49 +68,27 @@ fn sparql_w3c_syntax_testsuite() {
fn sparql_w3c_query_evaluation_testsuite() {
//TODO: dataset open-world
let manifest_10_urls = vec![
Url::parse("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/algebra/manifest.ttl")
.unwrap(),
Url::parse("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/ask/manifest.ttl")
.unwrap(),
Url::parse("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/basic/manifest.ttl")
.unwrap(),
Url::parse(
"http://www.w3.org/2001/sw/DataAccess/tests/data-r2/bnode-coreference/manifest.ttl",
).unwrap(),
Url::parse(
"http://www.w3.org/2001/sw/DataAccess/tests/data-r2/boolean-effective-value/manifest.ttl",
).unwrap(),
Url::parse(
"http://www.w3.org/2001/sw/DataAccess/tests/data-r2/bound/manifest.ttl",
).unwrap(),
Url::parse("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/cast/manifest.ttl").unwrap(),
Url::parse("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/construct/manifest.ttl")
.unwrap(),
Url::parse("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/distinct/manifest.ttl")
.unwrap(),
Url::parse("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/expr-builtin/manifest.ttl")
.unwrap(),
Url::parse("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/expr-equals/manifest.ttl")
.unwrap(),
Url::parse("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/expr-ops/manifest.ttl")
.unwrap(),
Url::parse("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/graph/manifest.ttl")
.unwrap(),
Url::parse("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/i18n/manifest.ttl").unwrap(),
Url::parse("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/optional/manifest.ttl")
.unwrap(),
Url::parse("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/reduced/manifest.ttl")
.unwrap(),
Url::parse("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/regex/manifest.ttl")
.unwrap(),
Url::parse("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/solution-seq/manifest.ttl")
.unwrap(),
Url::parse("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/sort/manifest.ttl").unwrap(),
Url::parse("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/triple-match/manifest.ttl")
.unwrap(),
Url::parse(
"http://www.w3.org/2001/sw/DataAccess/tests/data-r2/type-promotion/manifest.ttl",
).unwrap(),
"http://www.w3.org/2001/sw/DataAccess/tests/data-r2/algebra/manifest.ttl",
"http://www.w3.org/2001/sw/DataAccess/tests/data-r2/ask/manifest.ttl",
"http://www.w3.org/2001/sw/DataAccess/tests/data-r2/basic/manifest.ttl",
"http://www.w3.org/2001/sw/DataAccess/tests/data-r2/bnode-coreference/manifest.ttl",
"http://www.w3.org/2001/sw/DataAccess/tests/data-r2/boolean-effective-value/manifest.ttl",
"http://www.w3.org/2001/sw/DataAccess/tests/data-r2/bound/manifest.ttl",
"http://www.w3.org/2001/sw/DataAccess/tests/data-r2/cast/manifest.ttl",
"http://www.w3.org/2001/sw/DataAccess/tests/data-r2/construct/manifest.ttl",
"http://www.w3.org/2001/sw/DataAccess/tests/data-r2/distinct/manifest.ttl",
"http://www.w3.org/2001/sw/DataAccess/tests/data-r2/expr-builtin/manifest.ttl",
"http://www.w3.org/2001/sw/DataAccess/tests/data-r2/expr-equals/manifest.ttl",
"http://www.w3.org/2001/sw/DataAccess/tests/data-r2/expr-ops/manifest.ttl",
"http://www.w3.org/2001/sw/DataAccess/tests/data-r2/graph/manifest.ttl",
"http://www.w3.org/2001/sw/DataAccess/tests/data-r2/i18n/manifest.ttl",
"http://www.w3.org/2001/sw/DataAccess/tests/data-r2/optional/manifest.ttl",
"http://www.w3.org/2001/sw/DataAccess/tests/data-r2/reduced/manifest.ttl",
"http://www.w3.org/2001/sw/DataAccess/tests/data-r2/regex/manifest.ttl",
"http://www.w3.org/2001/sw/DataAccess/tests/data-r2/solution-seq/manifest.ttl",
"http://www.w3.org/2001/sw/DataAccess/tests/data-r2/sort/manifest.ttl",
"http://www.w3.org/2001/sw/DataAccess/tests/data-r2/triple-match/manifest.ttl",
"http://www.w3.org/2001/sw/DataAccess/tests/data-r2/type-promotion/manifest.ttl",
];
let test_blacklist = vec![
//Multiple writing of the same xsd:integer. Our system does strong normalization.
@ -179,7 +152,7 @@ fn sparql_w3c_query_evaluation_testsuite() {
Some(data) => {
let dataset = MemoryDataset::default();
let dataset_default = dataset.default_graph();
load_graph(data.clone())
load_graph(&data)
.unwrap()
.iter()
.for_each(|triple| dataset_default.insert(triple).unwrap());
@ -189,9 +162,9 @@ fn sparql_w3c_query_evaluation_testsuite() {
};
for graph_data in &test.graph_data {
let named_graph = data
.named_graph(&NamedNode::from(graph_data.clone()).into())
.named_graph(&NamedNode::new(graph_data.clone()).into())
.unwrap();
load_graph(graph_data.clone())
load_graph(&graph_data)
.unwrap()
.iter()
.for_each(|triple| named_graph.insert(triple).unwrap());
@ -210,21 +183,21 @@ fn sparql_w3c_query_evaluation_testsuite() {
),
Ok(result) => {
let expected_graph =
load_sparql_query_result_graph(test.result.clone().unwrap()).unwrap();
load_sparql_query_result_graph(test.result.as_ref().unwrap()).unwrap();
let with_order = expected_graph
.triples_for_predicate(&rs::INDEX)
.next()
.is_some();
let actual_graph = to_graph(result, with_order).unwrap();
assert!(
actual_graph.is_isomorphic(&expected_graph),
"Failure on {}.\nExpected file:\n{}\nOutput file:\n{}\nParsed query:\n{}\nData:\n{}\n",
test,
expected_graph,
actual_graph,
load_sparql_query(test.query.clone()).unwrap(),
data
)
actual_graph.is_isomorphic(&expected_graph),
"Failure on {}.\nExpected file:\n{}\nOutput file:\n{}\nParsed query:\n{}\nData:\n{}\n",
test,
expected_graph,
actual_graph,
load_sparql_query(&test.query).unwrap(),
data
)
}
},
}
@ -234,30 +207,29 @@ fn sparql_w3c_query_evaluation_testsuite() {
}
}
fn load_graph(url: Url) -> Result<SimpleGraph> {
if url.as_str().ends_with(".ttl") {
read_turtle(read_file(&url)?, Some(url))?.collect()
} else if url.as_str().ends_with(".rdf") {
read_rdf_xml(read_file(&url)?, Some(url))?.collect()
fn load_graph(url: &str) -> Result<SimpleGraph> {
if url.ends_with(".ttl") {
read_turtle(read_file(url)?, Some(url))?.collect()
} else if url.ends_with(".rdf") {
read_rdf_xml(read_file(url)?, Some(url))?.collect()
} else {
Err(format_err!("Serialization type not found for {}", url))
}
}
fn load_sparql_query(url: Url) -> Result<Query> {
read_sparql_query(read_file(&url)?, Some(url))
fn load_sparql_query(url: &str) -> Result<Query> {
read_sparql_query(read_file(url)?, Some(url))
}
fn load_sparql_query_result_graph(url: Url) -> Result<SimpleGraph> {
if url.as_str().ends_with(".srx") {
to_graph(read_xml_results(read_file(&url)?)?, false)
fn load_sparql_query_result_graph(url: &str) -> Result<SimpleGraph> {
if url.ends_with(".srx") {
to_graph(read_xml_results(read_file(url)?)?, false)
} else {
load_graph(url)
}
}
fn to_relative_path(url: &Url) -> Result<String> {
let url = url.as_str();
fn to_relative_path(url: &str) -> Result<String> {
if url.starts_with("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/") {
Ok(url.replace(
"http://www.w3.org/2001/sw/DataAccess/tests/",
@ -273,7 +245,7 @@ fn to_relative_path(url: &Url) -> Result<String> {
}
}
fn read_file(url: &Url) -> Result<impl BufRead> {
fn read_file(url: &str) -> Result<impl BufRead> {
let mut base_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
base_path.push("tests");
base_path.push(to_relative_path(url)?);
@ -397,10 +369,10 @@ pub struct Test {
pub kind: String,
pub name: Option<String>,
pub comment: Option<String>,
pub query: Url,
pub data: Option<Url>,
pub graph_data: Vec<Url>,
pub result: Option<Url>,
pub query: String,
pub data: Option<String>,
pub graph_data: Vec<String>,
pub result: Option<String>,
}
impl fmt::Display for Test {
@ -429,15 +401,15 @@ impl fmt::Display for Test {
pub struct TestManifest {
graph: SimpleGraph,
tests_to_do: Vec<Term>,
manifests_to_do: Vec<Url>,
manifests_to_do: Vec<String>,
}
impl TestManifest {
pub fn new(url: Url) -> TestManifest {
pub fn new(url: impl Into<String>) -> TestManifest {
Self {
graph: SimpleGraph::default(),
tests_to_do: Vec::default(),
manifests_to_do: vec![url],
manifests_to_do: vec![url.into()],
}
}
}
@ -519,23 +491,23 @@ impl Iterator for TestManifest {
.graph
.object_for_subject_predicate(&test_subject, &*mf::ACTION)
{
Some(Term::NamedNode(n)) => (n.clone().into(), None, vec![]),
Some(Term::NamedNode(n)) => (n.as_str().to_string(), None, vec![]),
Some(Term::BlankNode(n)) => {
let n = n.clone().into();
let query = match self.graph.object_for_subject_predicate(&n, &qt::QUERY) {
Some(Term::NamedNode(q)) => q.clone().into(),
Some(Term::NamedNode(q)) => q.as_str().to_string(),
Some(_) => return Some(Err(format_err!("invalid query"))),
None => return Some(Err(format_err!("query not found"))),
};
let data = match self.graph.object_for_subject_predicate(&n, &qt::DATA) {
Some(Term::NamedNode(q)) => Some(q.clone().into()),
Some(Term::NamedNode(q)) => Some(q.as_str().to_string()),
_ => None,
};
let graph_data = self
.graph
.objects_for_subject_predicate(&n, &qt::GRAPH_DATA)
.filter_map(|g| match g {
Term::NamedNode(q) => Some(q.clone().into()),
Term::NamedNode(q) => Some(q.as_str().to_string()),
_ => None,
})
.collect();
@ -553,7 +525,7 @@ impl Iterator for TestManifest {
.graph
.object_for_subject_predicate(&test_subject, &*mf::RESULT)
{
Some(Term::NamedNode(n)) => Some(n.clone().into()),
Some(Term::NamedNode(n)) => Some(n.as_str().to_string()),
Some(_) => return Some(Err(format_err!("invalid result"))),
None => None,
};
@ -573,7 +545,7 @@ impl Iterator for TestManifest {
match self.manifests_to_do.pop() {
Some(url) => {
let manifest = NamedOrBlankNode::from(NamedNode::new(url.clone()));
match load_graph(url) {
match load_graph(&url) {
Ok(g) => self.graph.extend(g.into_iter()),
Err(e) => return Some(Err(e.into())),
}
@ -587,7 +559,7 @@ impl Iterator for TestManifest {
self.manifests_to_do.extend(
RdfListIterator::iter(&self.graph, list.clone().into())
.filter_map(|m| match m {
Term::NamedNode(nm) => Some(nm.as_url().clone()),
Term::NamedNode(nm) => Some(nm.as_str().to_string()),
_ => None,
}),
);

Loading…
Cancel
Save