Skip to content

Commit

Permalink
Introducing Modules for easier interaction with Banana-RDF. The examples
Browse files Browse the repository at this point in the history
are updated to explain how to use them.
  • Loading branch information
betehess committed Mar 28, 2014
1 parent e903f85 commit 92fbbd0
Show file tree
Hide file tree
Showing 11 changed files with 358 additions and 200 deletions.
68 changes: 68 additions & 0 deletions examples/src/main/scala/examples/IOExample.scala
@@ -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
112 changes: 0 additions & 112 deletions examples/src/main/scala/examples/Main.scala

This file was deleted.

72 changes: 72 additions & 0 deletions examples/src/main/scala/examples/SPARQLExample.scala
@@ -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
9 changes: 0 additions & 9 deletions examples/src/main/scala/examples/package.scala

This file was deleted.

42 changes: 0 additions & 42 deletions jena/src/main/scala/jena/Jena.scala
Expand Up @@ -34,45 +34,3 @@ trait Jena extends RDF {
}

object Jena extends JenaModule

trait JenaModule {

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 solutionsWriterJson: SparqlSolutionsWriter[Jena, SparqlAnswerJson] =
JenaSolutionsWriter.solutionsWriterJson

implicit val solutionsWriterXml: SparqlSolutionsWriter[Jena, SparqlAnswerXml] =
JenaSolutionsWriter.solutionsWriterXml

implicit val solutionsWriterSelector: SparqlSolutionsWriterSelector[Jena] = JenaSolutionsWriter.solutionsWriterSelector

implicit val queryResultsReaderJson: SparqlQueryResultsReader[Jena, SparqlAnswerJson] =
JenaQueryResultsReader.queryResultsReaderJson

implicit val queryResultsReaderXml: SparqlQueryResultsReader[Jena, SparqlAnswerXml] =
JenaQueryResultsReader.queryResultsReaderXml



}
60 changes: 60 additions & 0 deletions jena/src/main/scala/jena/JenaModule.scala
@@ -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

}
2 changes: 1 addition & 1 deletion project/build.properties
@@ -1,2 +1,2 @@
sbt.version=0.13.0
sbt.version=0.13.1

6 changes: 1 addition & 5 deletions project/plugins.sbt
@@ -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")

0 comments on commit 92fbbd0

Please sign in to comment.