~ use explicit separators --- to split CONSTRUCTs
authorAlexandre Bertails <bertails@gmail.com>
Thu, 26 May 2011 02:12:50 +0200
changeset 377 b2781f6e50c0
parent 376 573239651458
child 378 a9b267ffe012
~ use explicit separators --- to split CONSTRUCTs
directmapping-webapp/src/main/resources/queries/emp_addr2FOAF.construct
directmapping-webapp/src/main/resources/queries/ivan.construct
directmapping-webapp/src/main/resources/queries/ivan.ddl
directmapping-webapp/src/main/scala/code/snippet/HelloWorld.scala
rdf2rdf/src/main/scala/RDF2RDF.scala
rdf2rdf/src/test/scala/RDF2RDFTest.scala
--- a/directmapping-webapp/src/main/resources/queries/emp_addr2FOAF.construct	Thu May 05 19:57:36 2011 -0400
+++ b/directmapping-webapp/src/main/resources/queries/emp_addr2FOAF.construct	Thu May 26 02:12:50 2011 +0200
@@ -2,7 +2,7 @@
 PREFIX vcard:  <http://www.w3.org/2006/vcard/ns#>
 
 CONSTRUCT { ?who foaf:given ?fname } WHERE { ?who <People#fname> ?fname }
-
+---
 CONSTRUCT { ?addr vcard:region ?state ; vcard:locality ?city } WHERE { ?addr <Addresses#city> ?city ; <Addresses#state> ?state }
-
+---
 CONSTRUCT { ?who vcard:homeAddr ?addr } WHERE { ?who <People#addr> ?addr }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/directmapping-webapp/src/main/resources/queries/ivan.construct	Thu May 26 02:12:50 2011 +0200
@@ -0,0 +1,24 @@
+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 .
+  }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/directmapping-webapp/src/main/resources/queries/ivan.ddl	Thu May 26 02:12:50 2011 +0200
@@ -0,0 +1,16 @@
+CREATE TABLE Book (ISBN STRING PRIMARY KEY,
+                   Author STRING,
+                   Title STRING,
+                   Publisher String,
+                   Year INT,
+                   FOREIGN KEY (Author) REFERENCES Author(ID),
+                   FOREIGN KEY (Publisher) REFERENCES Publisher(ID));
+CREATE TABLE Author (ID STRING PRIMARY KEY,
+                     Name STRING,
+                     Homepage STRING);
+CREATE TABLE Publisher (ID STRING PRIMARY KEY,
+                        Name STRING,
+                        City STRING);
+INSERT INTO Book (ISBN, Author, Title, Publisher, Year) VALUES ("0006511409X", "id_xyz", "The Glass Palace", "id_qpr", 2000);
+INSERT INTO Author (ID, Name, Homepage) VALUES ("id_xyz", "Ghosh, Amitav", "http://www.amitavghosh.com");
+INSERT INTO Publisher (ID, Name, City) VALUES ("id_qpr", "Harper Collins", "London");
--- a/directmapping-webapp/src/main/scala/code/snippet/HelloWorld.scala	Thu May 05 19:57:36 2011 -0400
+++ b/directmapping-webapp/src/main/scala/code/snippet/HelloWorld.scala	Thu May 26 02:12:50 2011 +0200
@@ -14,7 +14,7 @@
 
 import _root_.org.w3.net.liftweb.widgets.autocomplete._
 
-import org.w3.directmapping.servlet._
+//import org.w3.directmapping.servlet._
 
 
 
--- a/rdf2rdf/src/main/scala/RDF2RDF.scala	Thu May 05 19:57:36 2011 -0400
+++ b/rdf2rdf/src/main/scala/RDF2RDF.scala	Thu May 26 02:12:50 2011 +0200
@@ -22,7 +22,15 @@
 
   def extractAll(query:String, base:String = RDF2RDFModule.base):List[Construct] = {
     val prefixes = query.split("\n").filter{ _.matches("""(?i)^\s*prefix.*$""")}.mkString("\n")
-    pattern.findAllIn(query) map { construct => Construct(prefixes+construct, base) } toList
+    def acc(lines:List[String], currentQuery:String, queries:List[String]):List[String] = {
+      lines match {
+        case line :: lines if line startsWith "---" => acc(lines, "", currentQuery :: queries)
+        case line :: lines => acc(lines, currentQuery+"\n"+line, queries)
+        case Nil => currentQuery :: queries
+      }
+    }
+    val constructs = acc(query.split("\n").toList, "", Nil)
+    constructs map { construct => Construct(prefixes+construct.replaceAll("CONSTRUCT$", ""), base) } toList
   }
 
 }
--- a/rdf2rdf/src/test/scala/RDF2RDFTest.scala	Thu May 05 19:57:36 2011 -0400
+++ b/rdf2rdf/src/test/scala/RDF2RDFTest.scala	Thu May 26 02:12:50 2011 +0200
@@ -129,13 +129,13 @@
 } 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 {