Result construction MPSPARCL-4
authorAdam Dickmeiss <adam@indexdata.dk>
Thu, 22 Jan 2015 14:31:36 +0000 (15:31 +0100)
committerAdam Dickmeiss <adam@indexdata.dk>
Thu, 22 Jan 2015 14:31:36 +0000 (15:31 +0100)
Configuration item "field" replaced by "form", which allows
SELECT/CONSTRUCT to be defined. The triple store is now returning
application/rdf+xml response and this is what's decoded.

bibframe/triplestore.xml
doc/sparql.xml
src/filter_sparql.cpp
src/sparql.c

index 7a6f506..56b80fa 100644 (file)
@@ -3,7 +3,23 @@
     <db path="work" uri="http://bibframe.indexdata.com/sparql/">
       <prefix>rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns</prefix>
       <prefix>bf: http://bibframe.org/vocab/</prefix>
-      <field>DISTINCT ?work ?wtitle ?creatorlabel ?subjectlabel</field>
+      <form>SELECT DISTINCT ?work ?wtitle ?creatorlabel ?subjectlabel</form>
+      <criteria>?work a bf:Work</criteria>
+
+      <criteria>?work bf:workTitle ?wt</criteria>
+      <criteria>?wt bf:titleValue ?wtitle</criteria>
+      <index type="4">?wt bf:titleValue %v FILTER(contains(%v, %s))</index>
+      <criteria>?work bf:creator ?creator</criteria>
+      <criteria>?creator bf:label ?creatorlabel</criteria>
+      <index type="1003">?creator bf:label %v FILTER(contains(%v, %s))</index>
+      <criteria>?work bf:subject ?subject</criteria>
+      <criteria>?subject bf:label ?subjectlabel</criteria>
+      <index type="21">?subject bf:label %v FILTER(contains(%v, %s))</index>
+    </db>
+    <db path="works" uri="http://bibframe.indexdata.com/sparql/">
+      <prefix>rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns</prefix>
+      <prefix>bf: http://bibframe.org/vocab/</prefix>
+      <form>CONSTRUCT {  ?work bf:title ?title . ?work bf:author ?creator . ?work bf:instanceTitle ?it }</form>
       <criteria>?work a bf:Work</criteria>
 
       <criteria>?work bf:workTitle ?wt</criteria>
@@ -19,7 +35,7 @@
     <db path="instance" uri="http://bibframe.indexdata.com/sparql/">
       <prefix>rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns</prefix>
       <prefix>bf: http://bibframe.org/vocab/</prefix>
-      <field>DISTINCT ?instance ?title ?format</field>
+      <form>SELECT DISTINCT ?instance ?title ?format</form>
       <criteria>?instance a bf:Instance</criteria>
 
       <criteria>?instance bf:title ?title</criteria>
index 838dce1..d374ef6 100644 (file)
       </para>
      </listitem>
     </varlistentry>
-    <varlistentry><term>&lt;field/&gt;</term>
+    <varlistentry><term>&lt;form/&gt;</term>
      <listitem>
       <para>
-       Optional section for controlling what data rows are selected in the
-       SPARQL statement and how they are mapped to the output document, all
-       variables (\*) are selected when none is provided.
+       SPARQL Query formulation selection. SHould start with one of the
+       query forms: SELECT or CONSTRUCT.
       </para>
      </listitem>
     </varlistentry>
index 6bc889e..8aa9bab 100644 (file)
@@ -269,26 +269,67 @@ static xmlNode *get_result(xmlDoc *doc, Odr_int *sz, Odr_int pos)
 {
     xmlNode *ptr = xmlDocGetRootElement(doc);
     Odr_int cur = 0;
-    for (; ptr; ptr = ptr->next)
-        if (ptr->type == XML_ELEMENT_NODE &&
-            !strcmp((const char *) ptr->name, "sparql"))
-            break;
-    if (ptr)
+
+    if (ptr->type == XML_ELEMENT_NODE &&
+        !strcmp((const char *) ptr->name, "RDF"))
     {
-        for (ptr = ptr->children; ptr; ptr = ptr->next)
-            if (ptr->type == XML_ELEMENT_NODE &&
-                !strcmp((const char *) ptr->name, "results"))
-                break;
+        ptr = ptr->children;
+
+        while (ptr && ptr->type != XML_ELEMENT_NODE)
+            ptr = ptr->next;
+        if (ptr && ptr->type == XML_ELEMENT_NODE &&
+            !strcmp((const char *) ptr->name, "Description"))
+        {
+            xmlNode *p = ptr->children;
+
+            while (p && p->type != XML_ELEMENT_NODE)
+                p = p->next;
+            if (p && p->type == XML_ELEMENT_NODE &&
+                !strcmp((const char *) p->name, "type"))
+            { /* SELECT RESULT */
+                for (ptr = ptr->children; ptr; ptr = ptr->next)
+                    if (ptr->type == XML_ELEMENT_NODE &&
+                        !strcmp((const char *) ptr->name, "solution"))
+                    {
+                        if (cur++ == pos)
+                            break;
+                    }
+            }
+            else
+            {   /* CONSTRUCT result */
+                for (; ptr; ptr = ptr->next)
+                    if (ptr->type == XML_ELEMENT_NODE &&
+                        !strcmp((const char *) ptr->name, "Description"))
+                    {
+                        if (cur++ == pos)
+                            break;
+                    }
+            }
+        }
     }
-    if (ptr)
+    else
     {
-        for (ptr = ptr->children; ptr; ptr = ptr->next)
+        for (; ptr; ptr = ptr->next)
             if (ptr->type == XML_ELEMENT_NODE &&
-                !strcmp((const char *) ptr->name, "result"))
-            {
-                if (cur++ == pos)
+                !strcmp((const char *) ptr->name, "sparql"))
+                break;
+        if (ptr)
+        {
+            for (ptr = ptr->children; ptr; ptr = ptr->next)
+                if (ptr->type == XML_ELEMENT_NODE &&
+                    !strcmp((const char *) ptr->name, "results"))
                     break;
-            }
+        }
+        if (ptr)
+        {
+            for (ptr = ptr->children; ptr; ptr = ptr->next)
+                if (ptr->type == XML_ELEMENT_NODE &&
+                    !strcmp((const char *) ptr->name, "result"))
+                {
+                    if (cur++ == pos)
+                        break;
+                }
+        }
     }
     if (sz)
         *sz = cur;
@@ -321,7 +362,6 @@ Z_Records *yf::SPARQL::Session::fetch(
         if (!node)
             break;
         assert(node->type == XML_ELEMENT_NODE);
-        assert(!strcmp((const char *) node->name, "result"));
         xmlNode *tmp = xmlCopyNode(node, 1);
         xmlBufferPtr buf = xmlBufferCreate();
         xmlNodeDump(buf, tmp->doc, tmp, 0, 0);
@@ -353,6 +393,8 @@ Z_APDU *yf::SPARQL::Session::run_sparql(mp::Package &package,
 
     z_HTTP_header_add(odr, &gdu->u.HTTP_Request->headers,
                       "Content-Type", "application/x-www-form-urlencoded");
+    z_HTTP_header_add(odr, &gdu->u.HTTP_Request->headers,
+                      "Accept", "application/rdf+xml");
     const char *names[2];
     names[0] = "query";
     names[1] = 0;
index e551ead..fbd8c91 100644 (file)
@@ -322,7 +322,7 @@ int yaz_sparql_from_rpn_stream(yaz_sparql_t s,
         {
             ;
         }
-        else if (!strncmp(e->pattern, "field", 5))
+        else if (!strcmp(e->pattern, "form"))
         {
             ;
         }
@@ -332,10 +332,9 @@ int yaz_sparql_from_rpn_stream(yaz_sparql_t s,
         }
     }
     pr("\n", client_data);
-    pr("SELECT", client_data);
     for (e = s->conf; e; e = e->next)
     {
-        if (!strncmp(e->pattern, "field", 5))
+        if (!strcmp(e->pattern, "form"))
         {
             pr(" ", client_data);
             pr(e->value, client_data);