SPARQL/0000755000175100001440000000000012232510242011263 5ustar hornikusersSPARQL/NAMESPACE0000644000175100001440000000010412217124652012506 0ustar hornikusersexport(SPARQL) export(commonns) export(sparqlns) export(sparqltest) SPARQL/R/0000755000175100001440000000000012217124652011475 5ustar hornikusersSPARQL/R/SPARQL.R0000644000175100001440000002160412217124652012625 0ustar hornikuserslibrary(XML) library(RCurl) sparqlns <- c('s'='http://www.w3.org/2005/sparql-results#') commonns <- c('xsd','', 'rdf','', 'rdfs','', 'owl','', 'skos','', 'dc','', 'foaf','', 'wgs84','', 'qb','') sparqltest <- function(...) { SPARQL(url='http://semanticweb.cs.vu.nl/lop/sparql/', query='SELECT ?et ?r ?at ?t WHERE { ?e sem:eventType ?et . ?e sem:hasActor ?a . ?a sem:actorType ?at . ?e sem:hasPlace ?p . ?p eez:inPiracyRegion ?r . ?e sem:hasTimeStamp ?t . }', ns=c('lop','', 'eez',''), ...) } # # Read SPARQL results from end-point # SPARQL <- function(url="http://localhost/", query="", update="", ns=NULL, param="", extra=NULL, format="xml", curl_args=NULL, parser_args=NULL) { if (!is.null(extra)) { extrastr <- paste('&', sapply(seq(1,length(extra)), function (i) { paste(names(extra)[i],'=',URLencode(extra[[i]]), sep="") }), collapse="&", sep='') } else { extrastr <- "" } tf <- tempfile() if (query != "") { if (param == "") { param <- "query" } if(format == 'xml') { tf <- do.call(getURL,append(list(url = paste(url, '?', param, '=', gsub('\\+','%2B',URLencode(query,reserved=TRUE)), extrastr, sep=""), httpheader = c(Accept="application/sparql-results+xml")), curl_args)) DOM <- do.call(xmlParse, append(list(tf), parser_args)) if(length(getNodeSet(DOM, '//s:result[1]', namespaces=sparqlns)) == 0) { rm(DOM) df <- data.frame(c()) } else { attrs <- unlist(xpathApply(DOM, paste('//s:head/s:variable', sep=""), namespaces=sparqlns, quote(xmlGetAttr(x, "name")))) ns2 <- noBrackets(ns) res <- get_attr(attrs, DOM, ns2) df <- data.frame(res) rm(res) rm(DOM) # FIXME: find neater way to unlist columns n = names(df) for(r in 1:length(n)) { name <- n[r] df[name] <- as.vector(unlist(df[name])) } } } else if (format == 'csv') { tf <- do.call(getURL,append(list(url=paste(url, '?', param, '=', gsub('\\+','%2B',URLencode(query,reserved=TRUE)), extrastr, sep="")), curl_args)) df <- do.call(readCSVstring,append(list(tf, blank.lines.skip=TRUE, strip.white=TRUE), parser_args)) if (!is.null(ns)) df <- dropNS(df,ns) } else if (format == 'tsv') { tf <- do.call(getURL,append(list(url=paste(url, '?', param, '=', gsub('\\+','%2B',URLencode(query,reserved=TRUE)), extrastr, sep="")), curl_args)) df <- do.call(readTSVstring,append(list(tf, blank.lines.skip=TRUE, strip.white=TRUE), parser_args)) if (!is.null(ns)) df <- dropNS(df,ns) } else { cat('unknown format "',format,'"\n\n',sep="") return(list(results=NULL,namespaces=ns)) } list(results=df, namespaces=ns) } else if (update != "") { if (param == "") { param <- "update" } extra[[param]] <- update do.call(postForm,append(list(url, .params=extra),curl_args)) } } readTSVstring <- function(text, ...) { dfr <- read.delim(tc <- textConnection(text), ...) close(tc) dfr } readCSVstring <- function(text, ...) { dfr <- read.csv(tc <- textConnection(text), ...) close(tc) dfr } get_attr <- function(attrs, DOM, ns) { rs <- getNodeSet(DOM,'//s:result',namespaces=sparqlns) t(sapply(rs, function(r) { sapply(attrs, function(attr) { get_value(getNodeSet(xmlDoc(r), paste('//s:binding[@name="',attr,'"]/*[1]', sep=''), namespaces=sparqlns)[[1]], ns) },simplify=FALSE) },simplify=TRUE)) } get_value <- function(node,ns) { # FIXME: very slow... if (is.null(node)) { return(NA) } doc <- xmlDoc(node) uri = xpathSApply(doc, '/s:uri', xmlValue, namespaces=sparqlns) if(length(uri) == 0) { literal = xpathSApply(doc, '/s:literal', xmlValue, namespaces=sparqlns) if(length(literal) == 0) { bnode = xpathSApply(doc, '/s:bnode', xmlValue, namespaces=sparqlns) if (length(bnode) == 0) { # error '***oops***' } else { # found bnode paste('_:genid', bnode, sep='') } } else { # found literal lang = xpathApply(doc, '/s:literal', xmlGetAttr, "xml:lang", namespaces=sparqlns) if(is.null(lang[[1]])) { type = xpathApply(doc, '/s:literal', xmlGetAttr, "datatype", namespaces=sparqlns) if(is.null(type[[1]])) { literal } else { interpret_type(type,literal,ns) } } else { paste('"', literal, '"@', lang, sep='') } } } else { # found URI qname = qnames(uri, ns) if(qname == uri) paste('<', uri, '>', sep="") else qname } } noBrackets <- function(ns) { sapply(ns,function(br_ns) { if(substr(br_ns,1,1)=='<') substr(br_ns,2,nchar(br_ns)-1) else br_ns }) } substNS <- function(str0, ns) { regex <- paste('^', ns[2], sep="") gsub(regex, paste(ns[1], ":", sep=""), str0) } qnames <- function(str0, ns_list) { if(!length(ns_list)) str0 else substNS(qnames(str0, ns_list[-1:-2]), ns_list[1:2]) } interpret_type <- function(type, literal,ns) { qname <- qnames(type, ns) if(unlist(qname) == unlist(type)) type_uri <- paste('<', type, '>', sep="") else type_uri <- qname # FIXME: work out all simple types if(type == "http://www.w3.org/2001/XMLSchema#double" || type == "http://www.w3.org/2001/XMLSchema#float" || type == "http://www.w3.org/2001/XMLSchema#decimal") as.double(literal) else if(type == "http://www.w3.org/2001/XMLSchema#integer" || type == "http://www.w3.org/2001/XMLSchema#int" || type == "http://www.w3.org/2001/XMLSchema#long" || type == "http://www.w3.org/2001/XMLSchema#short" || type == "http://www.w3.org/2001/XMLSchema#byte" || type == "http://www.w3.org/2001/XMLSchema#nonNegativeInteger" || type == "http://www.w3.org/2001/XMLSchema#unsignedLong" || type == "http://www.w3.org/2001/XMLSchema#unsignedShort" || type == "http://www.w3.org/2001/XMLSchema#unsignedInt" || type == "http://www.w3.org/2001/XMLSchema#unsignedByte" || type == "http://www.w3.org/2001/XMLSchema#positiveInteger" || type == "http://www.w3.org/2001/XMLSchema#nonPositiveInteger" || type == "http://www.w3.org/2001/XMLSchema#negativeInteger") as.integer(literal) else if(type == "http://www.w3.org/2001/XMLSchema#boolean") as.logical(literal) else if(type == "http://www.w3.org/2001/XMLSchema#string" || type == "http://www.w3.org/2001/XMLSchema#normalizedString") literal else if(type == "http://www.w3.org/2001/XMLSchema#dateTime") as.POSIXct(literal,format="%FT%T") else if(type == "http://www.w3.org/2001/XMLSchema#time") as.POSIXct(literal,format="%T") else if(type == "http://www.w3.org/2001/XMLSchema#date") as.POSIXct(literal) else if(type == "http://www.w3.org/2001/XMLSchema#gYearMonth") as.POSIXct(literal,format="%Y-%m") else if(type == "http://www.w3.org/2001/XMLSchema#gYear") as.POSIXct(literal,format="%Y") else if(type == "http://www.w3.org/2001/XMLSchema#gMonthDay") as.POSIXct(literal,format="--%m-%d") else if(type == "http://www.w3.org/2001/XMLSchema#gDay") as.POSIXct(literal,format="---%d") else if(type == "http://www.w3.org/2001/XMLSchema#gMonth") as.POSIXct(literal,format="--%m") else paste('"', literal, '"^^', type_uri, sep="") } dropNS <- function(df,ns) { data.frame(lapply(df, function(c) { if(is.factor(c)) { c <- as.character(c) c <- qnames(c,ns) return(as.factor(c)) } if (is.character(c)) return(qnames(c,ns)) return(c) } )) } SPARQL/MD50000644000175100001440000000144712232510242011601 0ustar hornikusers9f5e3bc0fd728d4e03026a56cb8f263e *DESCRIPTION c13a79bd05293e6622c034720301c026 *NAMESPACE 09ae1bd87991f407a2fc0c6f5ab3151e *R/SPARQL.R 6abdd1e2517549e1b3a54ff1c9655ab2 *man/SPARQL-package.Rd afc6df2e4a2c854676b74315034190e1 *man/SPARQL.Rd 4054982ed7ee8e506dfb417eda95699d *man/commonns.Rd 18636eccc11a72540891606f06e4df60 *man/get_attr.Rd 528c43bf7fc98c75312321db5b7b94be *man/get_value.Rd ce4682e2465019b64a7787e8e5ad3d62 *man/interpret_type.Rd 15c7073db9a9896bc14878f9198ee30a *man/noBrackets.Rd 6ed17d1489d7c9c6f8ddf6994ecb8da5 *man/qnames.Rd cfb6ce1fdbaa1f98be98a08a76929c3c *man/readCSVstring.Rd a136ccc1a2b9506a9e837f263b924af8 *man/readTSVstring.Rd e154f7e4f732dc20e49bb82be3bfa85b *man/sparqlns.Rd 3f14648bfb5d6b619b63fe53b477e61e *man/sparqltest.Rd 3cf4d4b08ea204796ba1a9288d6ef52c *man/substNS.Rd SPARQL/DESCRIPTION0000644000175100001440000000111412232510242012766 0ustar hornikusersPackage: SPARQL Type: Package Title: SPARQL client Version: 1.16 Date: 2013-10-23 Author: Willem Robert van Hage , with contributions from: Tomi Kauppinen, Benedikt Graeler, Christopher Davis, Jesper Hoeksema, Alan Ruttenberg, and Daniel Bahls. Maintainer: Willem Robert van Hage Description: Use SPARQL to pose SELECT or UPDATE queries to an end-point. License: GPL-3 Depends: XML, RCurl LazyLoad: yes Packaged: 2013-10-24 01:05:19 UTC; wrvhage NeedsCompilation: no Repository: CRAN Date/Publication: 2013-10-25 17:39:14 SPARQL/man/0000755000175100001440000000000012232067746012056 5ustar hornikusersSPARQL/man/SPARQL-package.Rd0000644000175100001440000000325112232067746014741 0ustar hornikusers\name{SPARQL-package} \alias{SPARQL-package} \docType{package} \title{ SPARQL client } \description{ Load SPARQL SELECT query result tables as a data frame, or UPDATE the triple store by connecting to an end-point over HTTP. The development of this library has been developed in part within the COMBINE project supported by the ONR Global NICOP grant N62909-11-1-7060. } \details{ \tabular{ll}{ Package: \tab SPARQL\cr Type: \tab Package\cr Version: \tab 1.15\cr Date: \tab 2013-10-23\cr License: \tab GPL-3\cr Depends: \tab XML\cr LazyLoad: \tab yes\cr } } \author{ Willem Robert van Hage , with contributions from: Tomi Kauppinen, Benedikt Graeler, Christopher Davis, Jesper Hoeksema, Alan Ruttenberg, and Daniel Bahls. Maintainer: Willem Robert van Hage } \references{ SPARQL specification, \url{http://www.w3.org/TR/rdf-sparql-query/}.\cr Examples of SPARQL end-points, \url{http://www.w3.org/wiki/SparqlEndpoints}. } \keyword{ package } \examples{ \dontrun{ d <- SPARQL(url="http://services.data.gov.uk/reference/sparql", query="SELECT * WHERE { ?s ?p ?o . } LIMIT 10", ns=c('time','')) is.data.frame(d$results) # draw a pie chart from data from the Linked Open Piracy data set endpoint <- "http://semanticweb.cs.vu.nl/lop/sparql/" q <- "SELECT * WHERE { ?event sem:hasPlace ?place . ?place eez:inPiracyRegion ?region . } LIMIT 20" prefix <- c("lop","http://semanticweb.cs.vu.nl/poseidon/ns/instances/", "eez","http://semanticweb.cs.vu.nl/poseidon/ns/eez/") res <- SPARQL(endpoint,q,prefix)$results pie(sort(table(res$region)),col=rainbow(12)) } } SPARQL/man/qnames.Rd0000644000175100001440000000026312217124652013623 0ustar hornikusers\name{qnames} \alias{qnames} \title{qnames} \description{Applies substNR to an IRI for all prefix-namespace pairs in a list.} \author{ Willem Robert van Hage } \keyword{internal} SPARQL/man/substNS.Rd0000644000175100001440000000024412217124652013737 0ustar hornikusers\name{substNS} \alias{substNS} \title{substNS} \description{Substitutes namespace part of an IRI by a prefix.} \author{ Willem Robert van Hage } \keyword{internal} SPARQL/man/get_attr.Rd0000644000175100001440000000022412217124652014145 0ustar hornikusers\name{get_attr} \alias{get_attr} \title{get_attr} \description{Support function for SPARQL().} \author{ Willem Robert van Hage } \keyword{internal} SPARQL/man/interpret_type.Rd0000644000175100001440000000031012217124652015405 0ustar hornikusers\name{interpret_type} \alias{interpret_type} \title{interpret_type} \description{Casts literals to R data types depending on XML Schema datatype.} \author{ Willem Robert van Hage } \keyword{internal} SPARQL/man/SPARQL.Rd0000644000175100001440000001247212232067676013357 0ustar hornikusers\name{SPARQL} \alias{SPARQL} %- Also NEED an '\alias' for EACH other topic documented here. \title{ SPARQL client } \description{ This function connects to a SPARQL end-point over HTTP or HTTPs, poses a SELECT query or an update query (LOAD, INSERT, DELETE). If given a SELECT query it returns the results as a data frame with a named column for each variable from the SELECT query, a list of prefixes and namespaces that were shortened to qnames is also returned. If given an update query nothing is returned. If the parameter "query" is given, it is assumed the given query is a SELECT query and a GET request will be done to get the results from the URL of the end point. Otherwise, if the parameter "update" is given, it is assumed the given query is an update query and a POST request will be done to send the request to the URL of the end point. } \usage{ SPARQL(url = "http://localhost/", query = "", update="", ns = NULL, param = "", extra = NULL, format="xml", curl_args=NULL, parser_args=NULL) } %%- maybe also 'usage' for other objects documented here. \arguments{ \item{url}{ %% ~~Describe \code{url} here~~ The URL of the SPARQL end-point. } \item{query}{ %% ~~Describe \code{query} here~~ A SPARQL SELECT query to fire at the end-point. } \item{update}{ %% ~~Describe \code{query} here~~ A SPARQL update query (LOAD, INSERT, DELETE)) to fire at the end-point. } \item{ns}{ %% ~~Describe \code{ns} here~~ Prefixes to shorten IRIs returned by the SPARQL end-point. For example, \preformatted{ns=c('dc','', 'rdfs','')} will shorten the IRIs \code{'http://purl.org/dc/elements/1.1/title'} to \code{'dc:title'} and \code{'http://www.w3.org/2000/01/rdf-schema#label'} to \code{'rdfs:label'}. } \item{param}{ %% ~~Describe \code{param} here~~ By default a SPARQL end-point accepts queries in the \code{"query"} HTTP parameter and updates in the \code{"update"} parameter. If the end-point uses a different parameter you can specify this here. } \item{extra}{ %% ~~Describe \code{extra} here~~ Extra parameters and their values that will be added to the HTTP request. Some SPARQL end-points require extra parameters to work. These can be supplied, in URL encoded form, as a character vector with this parameter. This field can be used to specify the various ways in which different end-points can be told to return a certain format. For example, extra=list(resultFormat="xml") or extra=list(output="xml",queryLn="SPARQL") } \item{format}{ Can be used to explicitly state what kind of format is returned by the output. This version supports "xml", "csv" and "tsv". } \item{curl_args}{ A list of arguments that will be passed to RCurl when fetching the SPARQL results over HTTP. This can be used, for example, to pass authentication arguments, or to change the mime type of a post request from multipart/form-data to application/x-www-form-urlencoded, by passing curl_args=list(style="post"). } \item{parser_args}{ A list of arguments that will be passed to the XML, CSV, TSV, etc. parser that processed the returned SPARQL result table. } } %%\details{ %% ~~ If necessary, more details than the description above ~~ %%} \value{ The returned data frame contains a column for each variable in the SELECT query. For example, the query \code{"SELECT * WHERE { ?s ?p ?o . } LIMIT 10"} will yield three columns named "s", "p", and "o". The query \code{"SELECT ?s WHERE { ?s ?p ?o . } LIMIT 10"} will yield only one column named "s". %% ~Describe the value returned %% If it is a LIST, use %% \item{comp1 }{Description of 'comp1'} %% \item{comp2 }{Description of 'comp2'} %% ... } \references{ SPARQL specification, \url{http://www.w3.org/TR/rdf-sparql-query/}.\cr SPARQL Update specification, \url{http://www.w3.org/TR/sparql11-update/}.\cr Examples of SPARQL end-points, \url{http://www.w3.org/wiki/SparqlEndpoints}. %% ~put references to the literature/web site here ~ } \author{ Willem Robert van Hage and Tomi Kauppinen } %%\note{ %% ~~further notes~~ %%} %% ~Make other sections like Warning with \section{Warning }{....} ~ %%\seealso{ %% ~~objects to See Also as \code{\link{help}}, ~~~ %%} \examples{ \dontrun{ d <- SPARQL(url="http://services.data.gov.uk/reference/sparql", query="SELECT * WHERE { ?s ?p ?o . } LIMIT 10", ns=c('time','')) is.data.frame(d$results) # draw a pie chart from data from the Linked Open Piracy data set endpoint <- "http://semanticweb.cs.vu.nl/lop/sparql/" q <- "SELECT * WHERE { ?event sem:hasPlace ?place . ?place eez:inPiracyRegion ?region . }" prefix <- c("lop","http://semanticweb.cs.vu.nl/poseidon/ns/instances/", "eez","http://semanticweb.cs.vu.nl/poseidon/ns/eez/") res <- SPARQL(endpoint,q,prefix)$results pie(sort(table(res$region)),col=rainbow(12)) # draw a stacked bar chart from data from the Linked Open Piracy data set q <- "SELECT * WHERE { ?event sem:eventType ?event_type . ?event sem:hasPlace ?place . ?place eez:inPiracyRegion ?region . }" res <- SPARQL(endpoint,q,ns=prefix)$results restable <- table(res$event_type,res$region) par(mar=c(4,10,1,1)) barplot(restable,col=rainbow(10),horiz=TRUE,las=1,cex.names=0.8) legend("topright",rownames(restable), cex=0.8,bty="n",fill=rainbow(10)) } } \keyword{ SPARQL } SPARQL/man/noBrackets.Rd0000644000175100001440000000025512217124652014433 0ustar hornikusers\name{noBrackets} \alias{noBrackets} \title{noBrackets} \description{Removes the pointy brackets from a non-lossy URI.} \author{ Willem Robert van Hage } \keyword{internal} SPARQL/man/readTSVstring.Rd0000644000175100001440000000024312217124652015074 0ustar hornikusers\name{readTSVstring} \alias{readTSVstring} \title{readTSVstring} \description{Parses a tab separated string.} \author{ Willem Robert van Hage } \keyword{internal} SPARQL/man/get_value.Rd0000644000175100001440000000022712217124652014312 0ustar hornikusers\name{get_value} \alias{get_value} \title{get_value} \description{Support function for SPARQL().} \author{ Willem Robert van Hage } \keyword{internal} SPARQL/man/sparqltest.Rd0000644000175100001440000000023112217124652014534 0ustar hornikusers\name{sparqltest} \alias{sparqltest} \title{sparqltest} \description{Testing routine for SPARQL().} \author{ Willem Robert van Hage } \keyword{internal} SPARQL/man/commonns.Rd0000644000175100001440000000022412217124652014165 0ustar hornikusers\name{commonns} \alias{commonns} \title{commonns} \description{A vector of common namespaces and their prefixes.} \author{ Willem Robert van Hage } SPARQL/man/readCSVstring.Rd0000644000175100001440000000024512217124652015055 0ustar hornikusers\name{readCSVstring} \alias{readCSVstring} \title{readCSVstring} \description{Parses a comma separated string.} \author{ Willem Robert van Hage } \keyword{internal} SPARQL/man/sparqlns.Rd0000644000175100001440000000021312217124652014175 0ustar hornikusers\name{sparqlns} \alias{sparqlns} \title{sparqlns} \description{The SPARQL namespace.} \author{ Willem Robert van Hage } \keyword{internal}