Skip to content

Commit

Permalink
Changed the behavior of rdf_serialize to accept a vector of prefix …
Browse files Browse the repository at this point in the history
…to namespace bindings. (#22)

* Update rdf_serialize.R

 Changed the `rdf_serialize` function to accept multiple prefix to namespace mappings via the `namespace` parameter.
    The `namespace` parameter needs now to be a named character. `names(namespace)` contains the prefixes, whereas
    `namespace` itself contains the namespaces. Example:

```
    infile <- system.file("extdata", "dc.rdf", package="redland")
    out <- tempfile("file", fileext = ".rdf")
    some_rdf <- rdf_parse(infile)
    rdf_add(some_rdf, subject = "http://www.dajobe.org/dave-beckett", predicate = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type", object = "http://xmlns.com/foaf/0.1/Person")
    rdf_serialize(some_rdf, out, format = "turtle", namespace = c(dc = "http://purl.org/dc/elements/1.1/", foaf = "http://xmlns.com/foaf/0.1/") )
    readLines(out)
```

* changes to documentation regarding  fix

* backward compatibility change

* fixed documentation as well

* added check for length(prefix) == length(namespace)

* added myself as contributor :)
  • Loading branch information
pdatascience authored and cboettig committed Mar 31, 2018
1 parent 39ff7c9 commit d5105ed
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 23 deletions.
6 changes: 5 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ Authors@R: c(person("Carl", "Boettiger",
comment=c(ORCID = "0000-0002-0381-3766")),
person("Anna", "Krystalli",
role = "rev",
comment=c(ORCID = "0000-0002-2378-4915")))
comment=c(ORCID = "0000-0002-2378-4915")),
person("Viktor", "Senderov",
email = "vsenderov@gmail.com",
role = "ctb",
comment = c(ORCID = "0000-0003-3340-5963")))
Description: The Resource Description Framework, or 'RDF' is a widely used
data representation model that forms the cornerstone of the
Semantic Web. 'RDF' represents data as a graph rather than
Expand Down
36 changes: 23 additions & 13 deletions R/rdf_serialize.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#' @inheritParams rdf_parse
#' @inheritParams rdf_query
#' @param doc file path to write out to. If null, will write to character.
#' @param namespace string giving the namespace to set
#' @param prefix string giving the prefix associated with the namespace
#' @param namespace a named character containing the prefix to namespace bindings. \code{names(namespace)} are the prefixes, whereas \code{namespace} are the namespaces
#' @param prefix (optional) for backward compatibility. See \code{namespace}. It contains the matching prefixes to the namespaces in \code{namespace} and is set automatically if you provide \code{namespace} as a named character vector.
#' @param ... additional arguments to \code{redland::serializeToFile}
#' @return rdf_serialize returns the output file path `doc` invisibly.
#' This makes it easier to use rdf_serialize in pipe chains with
Expand All @@ -19,15 +19,22 @@
#' infile <- system.file("extdata", "dc.rdf", package="redland")
#' out <- tempfile("file", fileext = ".rdf")
#'
#' rdf <- rdf_parse(infile)
#' rdf_serialize(rdf, out)
#' some_rdf <- rdf_parse(infile)
#' rdf_add(some_rdf,
#' subject = "http://www.dajobe.org/dave-beckett",
#' predicate = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
#' object = "http://xmlns.com/foaf/0.1/Person")
#' rdf_serialize(some_rdf, out)
#'
#' ## With a namespace
#' rdf_serialize(rdf,
#' rdf_serialize(some_rdf,
#' out,
#' namespace = "http://purl.org/dc/elements/1.1/",
#' prefix = "dc")
#' format = "turtle",
#' namespace = c(dc = "http://purl.org/dc/elements/1.1/",
#' foaf = "http://xmlns.com/foaf/0.1/")
#' )
#'
#' readLines(out)
rdf_serialize <- function(rdf,
doc = NULL,
format = c("guess",
Expand All @@ -37,7 +44,7 @@ rdf_serialize <- function(rdf,
"turtle",
"jsonld"),
namespace = NULL,
prefix = NULL,
prefix = names(namespace),
base = getOption("rdf_base_uri", "localhost://"),
...){

Expand All @@ -59,11 +66,14 @@ rdf_serialize <- function(rdf,
new("Serializer", rdf$world,
name = format, mimeType = mimetype)

if(!is.null(namespace)){
redland::setNameSpace(serializer,
rdf$world,
namespace = namespace,
prefix = prefix)
if(!is.null(namespace) && is.character(namespace) && length(namespace) >= 1 && length(namespace) == length(prefix)){
ix = 1:length(namespace)
for (i in ix) {
redland::setNameSpace(serializer,
rdf$world,
namespace = namespace[i],
prefix = prefix[i])
}
}

if(is.null(doc)){
Expand Down
26 changes: 17 additions & 9 deletions man/rdf_serialize.Rd

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

0 comments on commit d5105ed

Please sign in to comment.