Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introducing Modules for easier interaction with Banana-RDF. The examples
are updated to explain how to use them.
- Loading branch information
Showing
11 changed files
with
358 additions
and
200 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package org.w3.banana.examples | ||
|
||
import org.w3.banana._ | ||
import scala.util.Properties | ||
import java.io.File | ||
|
||
/* declare your dependencies as a trait with all the modules you need | ||
*/ | ||
trait IOExampleDependencies | ||
extends RDFModule | ||
with RDFOpsModule | ||
with TurtleReaderModule | ||
with RDFXMLWriterModule | ||
|
||
/* Here is an example doing some IO. Read below to see what's | ||
* happening. | ||
* | ||
* As you can see, we never use Jena nor Sesame directly. The binding | ||
* is done later by providing the module implementation you | ||
* want. Hopefully, you'll have the same results :-) | ||
* | ||
* To run this example from sbt: | ||
* project examples | ||
* run-main org.w3.banana.examples.IOExampleWithJena | ||
* run-main org.w3.banana.examples.IOExampleWithSesame | ||
*/ | ||
trait IOExample extends IOExampleDependencies { | ||
|
||
import Ops._ | ||
|
||
def main(args: Array[String]): Unit = { | ||
|
||
/* reads TimBL's card in Turtle */ | ||
|
||
val timblCard = "http://www.w3.org/People/Berners-Lee/card.ttl" | ||
val from = new java.net.URL(timblCard).openStream() | ||
// reading from a stream can fail so in real life, you would have to deal with the Try[Rdf#Graph] | ||
val graph: Rdf#Graph = TurtleReader.read(from, base = timblCard) getOrElse sys.error("couldn't read TimBL's card") | ||
|
||
/* prints TimBL's card to a file as RDF/XML */ | ||
|
||
val tmpFile = new File(Properties.tmpDir, "card.ttl") | ||
val to = new java.io.FileOutputStream(tmpFile) | ||
val ret = RDFXMLWriter.write(graph, to, base = timblCard) | ||
if (ret.isSuccess) | ||
println(s"successfuly wrote TimBL's card to ${tmpFile.getAbsolutePath}") | ||
|
||
/* prints 10 triples to stdout */ | ||
|
||
val graph10Triples = Graph(graph.toIterable.take(10).toSet) | ||
val graphAsString = RDFXMLWriter.asString(graph10Triples, base = timblCard) getOrElse sys.error("coudn't serialize the graph") | ||
println(graphAsString) | ||
} | ||
|
||
} | ||
|
||
/* Here is how you instantiate your modules. Note that this is using | ||
* the default module implementations in banana-rdf but nothing is | ||
* preventing you from implementing your own or re-using part of | ||
* them. */ | ||
|
||
import org.w3.banana.jena.JenaModule | ||
|
||
object IOExampleWithJena extends IOExample with JenaModule | ||
|
||
import org.w3.banana.sesame.SesameModule | ||
|
||
object IOExampleWithSesame extends IOExample with SesameModule |
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package org.w3.banana.examples | ||
|
||
import org.w3.banana._ | ||
import org.w3.banana.diesel._ | ||
import java.net.URL | ||
|
||
/* declare your dependencies as a trait with all the modules you need | ||
*/ | ||
trait SPARQLExampleDependencies | ||
extends RDFModule | ||
with RDFOpsModule | ||
with SparqlOpsModule | ||
with SparqlHttpModule | ||
|
||
/* Here is an example doing some IO. Read below to see what's | ||
* happening. | ||
* | ||
* As you can see, we never use Jena nor Sesame directly. The binding | ||
* is done later by providing the module implementation you | ||
* want. Hopefully, you'll have the same results :-) | ||
* | ||
* To run this example from sbt: | ||
* project examples | ||
* run-main org.w3.banana.examples.SPARQLExampleWithJena | ||
*/ | ||
trait SPARQLExample extends SPARQLExampleDependencies { self => | ||
|
||
import Ops._ | ||
import SparqlOps._ | ||
|
||
def main(args: Array[String]): Unit = { | ||
|
||
/* gets a SparqlEngine out of a Sparql endpoint */ | ||
|
||
val client = SparqlHttp(new URL("http://dbpedia.org/sparql/")) | ||
|
||
/* creates a Sparql Select query */ | ||
|
||
val query = SelectQuery(""" | ||
PREFIX ont: <http://dbpedia.org/ontology/> | ||
SELECT DISTINCT ?language WHERE { | ||
?language a ont:ProgrammingLanguage . | ||
?language ont:influencedBy ?other . | ||
?other ont:influencedBy ?language . | ||
} LIMIT 100 | ||
""") | ||
|
||
/* executes the query */ | ||
|
||
val answers: Rdf#Solutions = client.executeSelect(query).getOrFail() | ||
|
||
/* iterate through the solutions */ | ||
|
||
val languages: Iterable[Rdf#URI] = answers.toIterable map { row => | ||
/* row is an Rdf#Solution, we can get an Rdf#Node from the variable name */ | ||
/* both the #Rdf#Node projection and the transformation to Rdf#URI can fail in the Try type, hense the flatMap */ | ||
row("language").flatMap(_.as[Rdf#URI]) getOrElse sys.error("die") | ||
} | ||
|
||
println(languages.toList) | ||
} | ||
|
||
} | ||
|
||
/* Here is how you instantiate your modules. Note that this is using | ||
* the default module implementations in banana-rdf but nothing is | ||
* preventing you from implementing your own or re-using part of | ||
* them. */ | ||
|
||
import org.w3.banana.jena.JenaModule | ||
|
||
object SPARQLExampleWithJena extends SPARQLExample with JenaModule |
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package org.w3.banana.jena | ||
|
||
import org.w3.banana._ | ||
|
||
trait JenaModule | ||
extends RDFModule | ||
with RDFOpsModule | ||
with RecordBinderModule | ||
with SparqlGraphModule | ||
with SparqlHttpModule | ||
with RDFXMLReaderModule | ||
with TurtleReaderModule | ||
with ReaderSelectorModule | ||
with RDFXMLWriterModule | ||
with TurtleWriterModule | ||
with WriterSelectorModule | ||
with JsonSolutionsWriterModule | ||
with XmlSolutionsWriterModule | ||
with JsonQueryResultsReaderModule | ||
with XmlQueryResultsReaderModule { | ||
|
||
type Rdf = Jena | ||
|
||
implicit val Ops: RDFOps[Jena] = JenaOperations | ||
|
||
implicit val RecordBinder: binder.RecordBinder[Jena] = binder.RecordBinder[Jena] | ||
|
||
implicit val SparqlOps: SparqlOps[Jena] = JenaSparqlOps | ||
|
||
implicit val SparqlGraph: SparqlGraph[Jena] = JenaSparqlGraph | ||
|
||
implicit val SparqlHttp: SparqlHttp[Jena] = JenaSparqlHttp | ||
|
||
implicit val RDFXMLReader: RDFReader[Jena, RDFXML] = JenaRDFReader.rdfxmlReader | ||
|
||
implicit val TurtleReader: RDFReader[Jena, Turtle] = JenaRDFReader.turtleReader | ||
|
||
implicit val ReaderSelector: ReaderSelector[Jena] = JenaRDFReader.selector | ||
|
||
implicit val RDFXMLWriter: RDFWriter[Jena, RDFXML] = JenaRDFWriter.rdfxmlWriter | ||
|
||
implicit val TurtleWriter: RDFWriter[Jena, Turtle] = JenaRDFWriter.turtleWriter | ||
|
||
implicit val WriterSelector: RDFWriterSelector[Jena] = JenaRDFWriter.selector | ||
|
||
implicit val JsonSolutionsWriter: SparqlSolutionsWriter[Jena, SparqlAnswerJson] = | ||
JenaSolutionsWriter.solutionsWriterJson | ||
|
||
implicit val XmlSolutionsWriter: SparqlSolutionsWriter[Jena, SparqlAnswerXml] = | ||
JenaSolutionsWriter.solutionsWriterXml | ||
|
||
implicit val SparqlSolutionsWriterSelector: SparqlSolutionsWriterSelector[Jena] = JenaSolutionsWriter.solutionsWriterSelector | ||
|
||
implicit val JsonQueryResultsReader: SparqlQueryResultsReader[Jena, SparqlAnswerJson] = | ||
JenaQueryResultsReader.queryResultsReaderJson | ||
|
||
implicit val XmlQueryResultsReader: SparqlQueryResultsReader[Jena, SparqlAnswerXml] = | ||
JenaQueryResultsReader.queryResultsReaderXml | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
sbt.version=0.13.0 | ||
sbt.version=0.13.1 | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1 @@ | ||
addSbtPlugin("org.ensime" % "ensime-sbt-cmd" % "0.1.2") | ||
|
||
addSbtPlugin("com.typesafe.sbt" % "sbt-scalariform" % "1.2.0") | ||
|
||
addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.5.1") | ||
addSbtPlugin("com.typesafe.sbt" % "sbt-scalariform" % "1.3.0") |
Oops, something went wrong.