changeset 374:6903cc3274b5

~ can parse a list of CONSTRUCTs and apply it to an RDF graph
author Alexandre Bertails <bertails@w3.org>
date Thu, 05 May 2011 19:08:53 -0400
parents 842aa232bce3
children 8ccb0dda0afd
files rdf2rdf/src/main/scala/RDF2RDF.scala rdf2rdf/src/test/scala/RDF2RDFTest.scala
diffstat 2 files changed, 101 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/rdf2rdf/src/main/scala/RDF2RDF.scala	Thu May 05 19:08:10 2011 -0400
+++ b/rdf2rdf/src/main/scala/RDF2RDF.scala	Thu May 05 19:08:53 2011 -0400
@@ -17,6 +17,12 @@
     val construct = QueryFactory.create(query, base, Syntax.syntaxSPARQL_11)
     Construct(construct)
   }
+
+  val pattern = """(?s)(?i)CONSTRUCT +?\{.*?\} +?WHERE +?\{.*?\}""".r
+
+  def extractAll(query:String, base:String = RDF2RDFModule.base):List[Construct] =
+    pattern.findAllIn(query) map { Construct(_, base) } toList
+
 }
 
 
@@ -44,6 +50,15 @@
       g1
     }
 
+    /**
+     * the union of all the CONSTRUCTs applied in parallel against to the
+     * same RDF dataset
+     */
+    def | (constructs:List[Construct]):Graph = {
+      val graphs:List[Graph] = constructs map { construct => this | construct }
+      graphs.foldLeft(Graph.empty) { _ ++ _ }
+    }
+
   }
 
   object RDF2RDF {
--- a/rdf2rdf/src/test/scala/RDF2RDFTest.scala	Thu May 05 19:08:10 2011 -0400
+++ b/rdf2rdf/src/test/scala/RDF2RDFTest.scala	Thu May 05 19:08:53 2011 -0400
@@ -64,7 +64,93 @@
 
   }
 
+  test("// CONSTRUCTs") {
+
+    val graph = getGraph("""
+<P:8> <P:fname> "Bob" ; <P:lname> "Smith" ; <P:addr> <A:18> .
+<P:9> <P:fname> "Sue" ; <P:lname> "Jones" .
+<A:18> <A:streetNo> 32 ; <A:streetName> "Vassar" .
+<A:76> <A:streetNo> 77 ; <A:streetName> "Mass Ave" .
+""", base="")
+
+
+    val c1 = Construct("""
+CONSTRUCT {
+  ?who <foaf:given> ?fname ; <foaf:family> ?lname
+} WHERE {
+  ?who <P:fname> ?fname ; <P:lname> ?lname
 }
+""", base="")
+
+    val c2 = Construct("""
+CONSTRUCT {
+  ?addr <vcard:streetNumber> ?no ; <vcard:streetName> ?name
+} WHERE {
+  ?addr <A:streetNo> ?no ; <A:streetName> ?name
+}
+""", base="")
+
+    val c3 = Construct("""
+CONSTRUCT {
+  ?who <vcard:homeAddr> ?addr
+} WHERE {
+  ?who <P:addr> ?addr
+}
+""", base="")
+
+
+    val resultGraph = RDF2RDF(graph) | List(c1, c2, c3)
+
+//     val expectedGraph = getGraph("""
+// <0006511409X> <title> "The Glass Palace" ;
+//               <year>  "2000" ;
+//               <author> _:y .
+// _:y <name> "Ghosh, Amitav" ;
+//     <homepage> <http://www.amitavghosh.com> .
+// """, base="http://ivan.book.example/")
+
+//     dumpCutingBase(resultGraph)
+
+  }
+
+  test("// CONSTRUCTs parsed") {
+
+    val graph = getGraph("""
+<P:8> <P:fname> "Bob" ; <P:lname> "Smith" ; <P:addr> <A:18> .
+<P:9> <P:fname> "Sue" ; <P:lname> "Jones" .
+<A:18> <A:streetNo> 32 ; <A:streetName> "Vassar" .
+<A:76> <A:streetNo> 77 ; <A:streetName> "Mass Ave" .
+""", base="")
+
+
+    val constructs = Construct.extractAll("""
+CONSTRUCT {
+  ?who <foaf:given> ?fname ; <foaf:family> ?lname
+} WHERE {
+  ?who <P:fname> ?fname ; <P:lname> ?lname
+}
+
+CONSTRUCT {
+  ?addr <vcard:streetNumber> ?no ; <vcard:streetName> ?name
+} WHERE {
+  ?addr <A:streetNo> ?no ; <A:streetName> ?name
+}
+
+CONSTRUCT {
+  ?who <vcard:homeAddr> ?addr
+} WHERE {
+  ?who <P:addr> ?addr
+}
+""", base="")
+
+
+    val resultGraph = RDF2RDF(graph) | constructs
+
+    println(resultGraph)
+
+  }
+}
+
 
 import org.w3.rdb.RDB._
 import org.w3.sql
Set up and maintained by W3C Systems Team, please report bugs to sysreq@w3.org.

W3C would like to thank Microsoft who donated the server that allows us to run this service.