Created
September 20, 2013 00:30
-
-
Save rpietro/6631800 to your computer and use it in GitHub Desktop.
rrdf code from http://goo.gl/OwjblF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# script below from http://goo.gl/OwjblF | |
# install.packages("rrdf") | |
require(rrdf) | |
#------------------------------------------------------------------------------ | |
# creating store | |
ontStore = new.rdf() | |
store = new.rdf(ontology=FALSE) | |
store | |
summarize.rdf(ontStore) | |
summarize.rdf(store) | |
#------------------------------------------------------------------------------ | |
# adding triples | |
add.triple(store, | |
subject="http://example.org/Subject", | |
predicate="http://example.org/Predicate", | |
object="http://example.org/Object" | |
) | |
summarize.rdf(store) | |
#------------------------------------------------------------------------------ | |
add.data.triple(store, | |
subject="http://example.org/Subject", | |
predicate="http://example.org/Predicate", | |
data = "Value" | |
) | |
#------------------------------------------------------------------------------ | |
add.data.triple(store, | |
subject = "http://example.org/Subject", | |
predicate = "http://example.org/Predicate", | |
data = "Value", | |
type = "string" | |
) | |
#------------------------------------------------------------------------------ | |
# serialization | |
tmpfile = tempfile(fileext = ".rdfxml") | |
save.rdf(store, tmpfile) | |
save.rdf(store, tempfile(fileext = ".ardfxml"), format="RDF/XML-ABBREV") | |
save.rdf(store, tempfile(fileext = ".n3"), format="N3") | |
add.prefix(store, "ex", "http://example.org/") | |
add.prefix(store, "xsd", "http://www.w3.org/2001/XMLSchema#") | |
save.rdf(store, tempfile(fileext=".n3"), format = "N3") | |
#------------------------------------------------------------------------------ | |
# loading triple files | |
setwd("~/Desktop") # change path to your desktop | |
exData <- load.rdf("ex.n3", format = "N3") # download the N3 file at http://goo.gl/MjLfqe and place it on your desktop | |
summarize.rdf(exData) | |
#------------------------------------------------------------------------------ | |
# querying RDF stores | |
sparql.rdf(exData, | |
paste( | |
"SELECT ?name ?carbons WHERE {", | |
" ?alkane <http://www.w3.org/rdf/schema/#label> ?name ; ", | |
" <http://example.org/carbonCount> ?carbons ", | |
"}" | |
) | |
) | |
sparql.rdf(exData, | |
paste( | |
"SELECT ?name ?carbons WHERE {", | |
" ?alkane <http://www.w3.org/rdf/schema/#label> ?name ; ", | |
" <http://example.org/carbonCount> ?carbons ", | |
"}" | |
), | |
rowvarname="name" | |
) | |
#------------------------------------------------------------------------------ | |
# remote SPARQL | |
sparql.remote( | |
"http://rdf.farmbio.uu.se/chembl/sparql", | |
paste( | |
"SELECT DISTINCT ?predict WHERE {", | |
" ?subject ?predict ?object ", | |
"}" | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment