Skip to content

Commit

Permalink
fix setting of a base uri in jsonld
Browse files Browse the repository at this point in the history
uses localhost:// as base uri for now, configurable as option "rdflib_base_uri", though not documented. (since really users wanting a particular base_uri should probably be declaring their own base explicitly in the json-ld rather than setting it here).  closes #5
  • Loading branch information
cboettig committed Jan 2, 2018
1 parent 9621d05 commit 0700294
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ importClassesFrom(redland,Serializer)
importClassesFrom(redland,Statement)
importClassesFrom(redland,Storage)
importClassesFrom(redland,World)
importFrom(jsonld,jsonld_compact)
importFrom(jsonld,jsonld_expand)
importFrom(jsonld,jsonld_to_rdf)
importFrom(utils,download.file)
importMethodsFrom(redland,addStatement)
Expand Down
28 changes: 24 additions & 4 deletions R/rdf.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,15 @@ rdf_parse <- function(doc,
doc <- text_or_url_to_doc(doc)

## redlands doesn't support jsonld. So rewrite as nquads using jsonld package
## We use tmp to avoid altering input doc, since parsing a local file should
## be a read-only task!
if(format == "jsonld"){
tmp <- tempfile()
writeLines(jsonld::jsonld_to_rdf(doc), tmp)
doc <- tmp
tmp <- add_base_uri(doc, tmp)
rdf <- jsonld::jsonld_to_rdf(tmp)
writeLines(rdf, tmp)
format <- "nquads"
doc <- tmp
}

x <- rdf()
Expand All @@ -83,6 +87,22 @@ rdf_parse <- function(doc,
x
}

# Whenever we convert JSON-LD to RDF we should set a @base if not set.
# https://json-ld.org/playground does this (with it's own url) but jsonld R package does not.
# For details, see https://github.com/cboettig/rdflib/issues/5
#
#' @importFrom jsonld jsonld_expand jsonld_compact
add_base_uri <- function(doc, tmp = tempfile()){

## Cannot assume it has context, may already be expanded (e.g. from rdf_serialize)
## Expanding will also make any preset @base context take precedence
expanded <- jsonld::jsonld_expand(doc)
base <- getOption("rdflib_base_uri", "localhost://")
context <- paste0('{"@base": "', base, '"}')
compact <- jsonld::jsonld_compact(expanded, context)
writeLines(compact, tmp)
tmp
}


#' Serialize RDF docs
Expand Down Expand Up @@ -127,7 +147,7 @@ rdf_serialize <- function(x,
format <- match.arg(format)


## redlands doesn't support jsonld. So write as nquads and then trnasform
## redlands doesn't support jsonld. So write as nquads and then transform
jsonld_output <- format == "jsonld"
if(jsonld_output){
format <- "nquads"
Expand Down Expand Up @@ -201,7 +221,7 @@ rdf_query <- function(x, query, ...){
rectangularize_query_results(out)
}

## FIXME: cast data type based on Type-string?
## FIXME: coerce data type based on Type-string?
rectangularize_query_results <- function(out){
vars <- unique(names(out))
X <- lapply(vars, function(v)
Expand Down
2 changes: 1 addition & 1 deletion man/rdflib-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/testthat/test-rdf.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
testthat::context("test-rdf.R")

doc <- system.file("extdata/example.rdf", package="redland")
out <- tempfile()
out <- "testing.rdf"

testthat::test_that("we can parse (in rdfxml)
and serialize (in nquads) a simple rdf graph", {
Expand All @@ -19,7 +19,7 @@ testthat::test_that("we can make sparql queries", {

rdf <- rdf_parse(doc)
match <- rdf_query(rdf, sparql)
expect_length(match, 2)
testthat::expect_length(match, 2)
})

testthat::test_that("we can initialize add triples to rdf graph", {
Expand Down

0 comments on commit 0700294

Please sign in to comment.