--- a/rdf2rdf/src/main/scala/RDF2RDF.scala Mon Mar 14 18:33:33 2011 -0400
+++ b/rdf2rdf/src/main/scala/RDF2RDF.scala Tue Mar 15 11:03:58 2011 -0400
@@ -2,6 +2,7 @@
import org.w3.rdf.jena._
import com.hp.hpl.jena.query._
+import com.hp.hpl.jena.rdf.model._
trait RDF2RDFModule extends JenaModel {
@@ -11,18 +12,26 @@
}
object Construct {
- def apply(query:String):Construct = {
- val construct = QueryFactory.create(query, Syntax.syntaxSPARQL_11)
+ def apply(query:String, base:String = null):Construct = {
+ val construct = QueryFactory.create(query, base, Syntax.syntaxSPARQL_11)
Construct(construct)
}
}
- trait RDF2RDF {
+ case class RDF2RDF(graph:Graph) {
-
-
+ def | (construct:Construct):Graph = {
+ val model = ModelFactory.createModelForGraph(graph.jenaGraph)
+ val qe = QueryExecutionFactory.create(construct.query, model)
+ val resultModel = qe.execConstruct
+ new Graph(resultModel.getGraph)
+ }
}
+ object RDF2RDF {
+ implicit def graph2RDF2RDF(graph:Graph):RDF2RDF = RDF2RDF(graph)
+ }
+
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/rdf2rdf/src/test/scala/RDF2RDFTest.scala Tue Mar 15 11:03:58 2011 -0400
@@ -0,0 +1,75 @@
+package org.w3.rdf2rdf
+
+import org.scalatest.FunSuite
+import org.scalatest.matchers.ShouldMatchers
+
+class RDF2RDFTest extends FunSuite with ShouldMatchers with RDF2RDFModule {
+
+ // TODO move that elsewhere
+ def getGraph(s:String, base:String = null):Graph = {
+ import com.hp.hpl.jena.rdf.model._
+ val model = ModelFactory.createDefaultModel()
+ model.read(new java.io.StringReader(s), base, "TURTLE")
+ return new Graph(model.getGraph)
+ }
+
+ test("http://ivan-herman.name/2010/11/19/my-first-mapping-from-direct-mapping/") {
+
+ val graph = getGraph("""
+@base <http://book.example/> .
+<Book/ID=0006511409X#_> a <Book> ;
+ <Book#ISBN> "0006511409X" ;
+ <Book#Title> "The Glass Palace" ;
+ <Book#Year> "2000" ;
+ <Book#Author> <Author/ID=id_xyz#_> .
+
+<Author/ID=id_xyz#_> a <Author> ;
+ <Author#ID> "id_xyz" ;
+ <Author#Name> "Ghosh, Amitav" ;
+ <Author#Homepage> "http://www.amitavghosh.com" .
+""")
+
+
+ val construct = Construct("""
+PREFIX a: <http://ivan.book.example/>
+PREFIX fn: <http://www.w3.org/2005/xpath-functions#>
+CONSTRUCT {
+ ?id a:title ?title ;
+ a:year ?year ;
+ a:author _:x .
+ _:x a:name ?name ;
+ a:homepage ?hp .
+}
+WHERE {
+ SELECT (IRI(fn:concat("http://ivan.book.example/", ?isbn)) AS ?id)
+ ?title ?year ?name
+ (IRI(?homepage) AS ?hp)
+ {
+ ?book a <Book> ;
+ <Book#ISBN> ?isbn ;
+ <Book#Title> ?title ;
+ <Book#Year> ?year ;
+ <Book#Author> ?author .
+ ?author a <Author> ;
+ <Author#Name> ?name ;
+ <Author#Homepage> ?homepage .
+ }
+}""", base="http://book.example/")
+
+
+ val resultGraph = RDF2RDF(graph) | construct
+
+ val expectedGraph = getGraph("""
+@base <http://ivan.book.example/> .
+<0006511409X> <title> "The Glass Palace" ;
+ <year> "2000" ;
+ <author> _:y .
+_:y <name> "Ghosh, Amitav" ;
+ <homepage> <http://www.amitavghosh.com> .
+""")
+
+ assert(expectedGraph === resultGraph)
+
+ }
+
+}