diff --git a/NAMESPACE b/NAMESPACE index 62ae34b..4548657 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -5,6 +5,7 @@ export(build_ont) export(extract_list_edges) export(extract_restrictions) export(extract_schema_owl) +export(extract_schema_void) export(local_name) export(make_display_list) export(namespace_of) @@ -19,3 +20,4 @@ importFrom(dplyr,distinct) importFrom(dplyr,filter) importFrom(dplyr,mutate) importFrom(dplyr,select) +importFrom(dplyr,transmute) diff --git a/R/schema_void.R b/R/schema_void.R new file mode 100644 index 0000000..51216f8 --- /dev/null +++ b/R/schema_void.R @@ -0,0 +1,35 @@ +#' @importFrom dplyr %>% filter transmute distinct +NULL + + +#' Extract a schema from a VoID dataset description,extracted information +#' +#' @param source A SPARQL endpoint URL or a parsed rdflib file. +#' @return A schema list, see `.empty_schema()`; only `classes` and +#' `properties` are populated. +#' @export +extract_schema_void <- function(source) { + schema <- .empty_schema() + + void_classes <- safe_query(source, ' + PREFIX void: + SELECT DISTINCT ?class WHERE { [] void:class ?class . }', expected_cols = "class") + + void_props <- safe_query(source, ' + PREFIX void: + SELECT DISTINCT ?prop WHERE { + { [] void:property ?prop } UNION { [] void:linkPredicate ?prop } + }', expected_cols = "prop") + + schema$classes <- void_classes %>% + filter(!is.na(class), nzchar(class)) %>% + transmute(uri = class, label = local_name(class)) %>% + distinct(uri, .keep_all = TRUE) + + schema$properties <- void_props %>% + filter(!is.na(prop), nzchar(prop)) %>% + transmute(property = prop, label = local_name(prop)) %>% + distinct(property, .keep_all = TRUE) + + schema +} diff --git a/R/utils.R b/R/utils.R index bd2abac..4ade5ce 100644 --- a/R/utils.R +++ b/R/utils.R @@ -44,6 +44,7 @@ local_name <- function(uri) { normalize_scheme <- function(x) sub("^https://", "http://", x) + .empty_schema <- function() { list( classes = data.frame(uri = character(), label = character(), stringsAsFactors = FALSE), diff --git a/man/extract_schema_void.Rd b/man/extract_schema_void.Rd new file mode 100644 index 0000000..673cccd --- /dev/null +++ b/man/extract_schema_void.Rd @@ -0,0 +1,18 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/schema_void.R +\name{extract_schema_void} +\alias{extract_schema_void} +\title{Extract a schema from a VoID dataset description,extracted information} +\usage{ +extract_schema_void(source) +} +\arguments{ +\item{source}{A SPARQL endpoint URL or a parsed rdflib file.} +} +\value{ +A schema list, see \code{.empty_schema()}; only \code{classes} and +\code{properties} are populated. +} +\description{ +Extract a schema from a VoID dataset description,extracted information +} diff --git a/tests/testthat/test-schema_void.R b/tests/testthat/test-schema_void.R new file mode 100644 index 0000000..a3eeb8a --- /dev/null +++ b/tests/testthat/test-schema_void.R @@ -0,0 +1,40 @@ +test_that("extract_schema_void reads void:class/property declarations", { + fake_sparql <- function(url, query, timeout = 60) { + if (grepl("void:class", query, fixed = TRUE)) { + data.frame(class = "http://ex.org/Dataset", stringsAsFactors = FALSE) + } else { + data.frame(prop = "http://purl.org/dc/terms/title", stringsAsFactors = FALSE) + } + } + testthat::local_mocked_bindings(sparql_query = fake_sparql) + + schema <- extract_schema_void("https://example.org/sparql") + + expect_equal(schema$classes$uri, "http://ex.org/Dataset") + expect_equal(schema$classes$label, "Dataset") + expect_equal(schema$properties$property, "http://purl.org/dc/terms/title") + expect_equal(schema$properties$label, "title") +}) + +test_that("extract_schema_void returns empty tables when there is no VoID data", { + testthat::local_mocked_bindings( + sparql_query = function(url, query, timeout = 60) data.frame() + ) + + schema <- extract_schema_void("https://example.org/sparql") + + expect_equal(nrow(schema$classes), 0) + expect_equal(names(schema$classes), c("uri", "label")) + expect_equal(nrow(schema$properties), 0) +}) + +test_that("extract_schema_void tolerates a failing endpoint", { + testthat::local_mocked_bindings( + sparql_query = function(url, query, timeout = 60) stop("timeout") + ) + + schema <- suppressWarnings(extract_schema_void("https://example.org/sparql")) + + expect_equal(nrow(schema$classes), 0) + expect_equal(nrow(schema$properties), 0) +})