--- a/rdf2rdf/src/main/scala/RDF2RDF.scala Tue Mar 15 11:58:27 2011 -0400
+++ b/rdf2rdf/src/main/scala/RDF2RDF.scala Fri Mar 18 18:37:43 2011 -0400
@@ -4,15 +4,20 @@
import com.hp.hpl.jena.query._
import com.hp.hpl.jena.rdf.model._
+object RDF2RDFModule {
+ val base = "dm:"
+}
+
+import RDF2RDFModule._
+
trait RDF2RDFModule extends JenaModel {
case class Construct(query:Query) {
assert(query.getQueryType == Query.QueryTypeConstruct, "You must provide a valid SPARQL 1.1 Construct query")
-
}
object Construct {
- def apply(query:String, base:String = null):Construct = {
+ def apply(query:String, base:String = base):Construct = {
val construct = QueryFactory.create(query, base, Syntax.syntaxSPARQL_11)
Construct(construct)
}
@@ -21,11 +26,22 @@
case class RDF2RDF(graph:Graph) {
+ val model = {
+ val m1 = ModelFactory.createModelForGraph(graph.jenaGraph)
+ val buffer = new java.io.ByteArrayOutputStream
+ m1.write(buffer, "TURTLE", base)
+ // m1.write(System.out, "TURTLE", base)
+ val m2 = ModelFactory.createDefaultModel()
+ m2.read(new java.io.StringReader(buffer.toString("UTF-8")), base, "TURTLE")
+ // m2.write(System.out, "TURTLE", null)
+ m2
+ }
+
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)
+ val g1 = new Graph(resultModel.getGraph)
+ g1
}
}
@@ -34,4 +50,59 @@
implicit def graph2RDF2RDF(graph:Graph):RDF2RDF = RDF2RDF(graph)
}
+ import com.hp.hpl.jena.rdf.model._
+
+ def getGraph(s:String, base:String = null):Graph = {
+ val model = ModelFactory.createDefaultModel()
+ model.read(new java.io.StringReader(s), base, "TURTLE")
+ return new Graph(model.getGraph)
+ }
+
+ def dumpCutingBase(g:Graph):Unit = {
+ val model = ModelFactory.createModelForGraph(g.jenaGraph)
+ val buffer = new java.io.ByteArrayOutputStream
+ model.write(buffer, "TURTLE", base)
+ val result = buffer.toString("UTF-8").replaceAll("<"+base, "<")
+ println(result)
+ }
+
}
+
+
+object Main {
+
+ import com.hp.hpl.jena.rdf.model._
+
+ def main(args: Array[String]): Unit = {
+
+ val turtleString = """
+<a> <b> <c> .
+"""
+
+ val queryString = """
+CONSTRUCT {
+ <otherA> ?p <otherC>
+} WHERE {
+ <a> ?p <c>
+}
+"""
+
+ val base = "/"
+
+ val model:Model = ModelFactory.createDefaultModel()
+ model.read(new java.io.StringReader(turtleString), base, "TURTLE")
+ // val s = model.createResource("a")
+ // val p = model.createResource("b")
+ // val o = model.createResource("c")
+ // val statement = model.create
+ model.write(System.out, "TURTLE", base)
+
+ val query:Query = QueryFactory.create(queryString, base)
+ val qexec:QueryExecution = QueryExecutionFactory.create(query, model)
+ val resultModel:Model = qexec.execConstruct()
+
+ resultModel.write(System.out, "TURTLE", base)
+
+ }
+
+}
--- a/rdf2rdf/src/test/scala/RDF2RDFTest.scala Tue Mar 15 11:58:27 2011 -0400
+++ b/rdf2rdf/src/test/scala/RDF2RDFTest.scala Fri Mar 18 18:37:43 2011 -0400
@@ -5,24 +5,11 @@
import org.w3.rdf.jena._
import org.w3.directmapping._
-trait Util extends JenaModel {
-
- // 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)
- }
-
-}
-
-class RDF2RDFTest extends FunSuite with ShouldMatchers with RDF2RDFModule with Util {
+class RDF2RDFTest extends FunSuite with ShouldMatchers with RDF2RDFModule {
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" ;
@@ -33,7 +20,7 @@
<Author#ID> "id_xyz" ;
<Author#Name> "Ghosh, Amitav" ;
<Author#Homepage> "http://www.amitavghosh.com" .
-""")
+""", base="http://book.example/")
val construct = Construct("""
@@ -66,13 +53,12 @@
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> .
-""")
+""", base="http://ivan.book.example/")
assert(expectedGraph === resultGraph)
@@ -84,7 +70,7 @@
import org.w3.sql
import org.w3.rdf.turtle.TurtleModule
-class RDB2RDF2RDFTest extends FunSuite with ShouldMatchers with RDF2RDFModule with DirectMappingModule with Util with TurtleModule {
+class RDB2RDF2RDFTest extends FunSuite with ShouldMatchers with RDF2RDFModule with DirectMappingModule with TurtleModule {
import DirectMapping._
@@ -112,8 +98,21 @@
val graph = databaseSemantics(db)
- println(graph)
+ val construct = Construct("""
+CONSTRUCT {
+ ?s <p> ?o
+} WHERE {
+ ?s <Address#ID> ?o
+}
+""")
+
+ println(construct)
+
+ val resultGraph = RDF2RDF(graph) | construct
+
+ dumpCutingBase(resultGraph)
}
}
+