Browse Source

Shorten TTL rdf:type; reinstate correct loop for LSUP_graph_connections.

Stefano Cossu 2 years ago
parent
commit
f7b22ea57b
3 changed files with 50 additions and 19 deletions
  1. 6 3
      include/term.h
  2. 32 15
      src/codec/codec_ttl.c
  3. 12 1
      src/graph.c

+ 6 - 3
include/term.h

@@ -9,9 +9,12 @@
 
 #define UUID4_URN_SIZE UUIDSTR_SIZE + 10
 
-/** @brief Default data type for untyped literals (prefixed IRI).
- */
-#define DEFAULT_DTYPE           "http://www.w3.org/2001/XMLSchema#string"
+// Some common RDF term values.
+#define LSUP_RDF_TYPE       "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
+#define LSUP_RDF_TYPE_NS    "rdf:type"
+/// Default data type for untyped literals (prefixed IRI).
+#define DEFAULT_DTYPE       "http://www.w3.org/2001/XMLSchema#string"
+#define DEFAULT_DTYPE_NS    "xsd:string"
 
 /** @brief URI parsing regular expression.
  *

+ 32 - 15
src/codec/codec_ttl.c

@@ -30,23 +30,40 @@ term_to_ttl (const LSUP_Term *term, const LSUP_NSMap *nsm, char **out_p)
     LSUP_rc md_rc = LSUP_NORESULT;
     switch (term->type) {
         case LSUP_TERM_IRIREF:
-            md_rc = LSUP_nsmap_denormalize_uri (nsm, term->data, &tmp);
-            PRCCK (md_rc);
-            if (md_rc == LSUP_NORESULT) {
-                // If URI counld not be shortened, add `<>`
-                out = realloc (*out_p, strlen (tmp) + 3);
-                if (UNLIKELY (!out)) return LSUP_MEM_ERR;
-                sprintf (out, "<%s>", tmp);
-                free (tmp);
-
-            } else out = tmp;
+            if (strcmp (term->data, LSUP_RDF_TYPE) == 0) {
+                // Shorten RDF type
+                buf_len = 2;
+                tmp = "a";
+            } else {
+                md_rc = LSUP_nsmap_denormalize_uri (nsm, term->data, &tmp);
+                PRCCK (md_rc);
+                if (md_rc == LSUP_NORESULT) {
+                    // If URI counld not be shortened, add `<>`
+                    out = realloc (*out_p, strlen (tmp) + 3);
+                    if (UNLIKELY (!out)) return LSUP_MEM_ERR;
+                    sprintf (out, "<%s>", tmp);
+                    free (tmp);
+
+                } else {
+                    free (*out_p);
+                    out = tmp;
+                }
+            }
             rc = LSUP_OK;
             break;
 
         case LSUP_TERM_NS_IRIREF:
-            out = realloc (*out_p, strlen (term->data) + 1);
+            if (strcmp (term->data, LSUP_RDF_TYPE_NS) == 0) {
+                // Shorten RDF type
+                tmp = "a";
+                buf_len = 2;
+            } else {
+                tmp = term->data;
+                buf_len = strlen (term->data) + 1;
+            }
+            out = realloc (*out_p, buf_len);
             if (UNLIKELY (!out)) return LSUP_MEM_ERR;
-            strcpy (out, term->data);
+            strcpy (out, tmp);
             rc = LSUP_OK;
             break;
 
@@ -222,7 +239,7 @@ gr_to_ttl_iter (void *h, char **res_p) {
         res = strcat (strcat (tmp, p_join), p_str);
 
         free (p_str);
-        p_join = "; ";
+        p_join = " ; ";
 
         // Add objects for predicate.
         size_t i = 0;
@@ -236,12 +253,12 @@ gr_to_ttl_iter (void *h, char **res_p) {
                     res, strlen (res) + strlen (o_str) + strlen (o_join) + 1);
             if (UNLIKELY (!tmp)) goto memfail;
             res = strcat (strcat (tmp, o_join), o_str);
-            o_join = ", ";
+            o_join = " , ";
         }
         free (o_str);
     }
 
-    char *s_sep = ".\n";
+    char *s_sep = " .\n";
     char *tmp = realloc (res, strlen (res) + strlen (s_sep) + 1);
     if (UNLIKELY (!tmp)) goto memfail;
 

+ 12 - 1
src/graph.c

@@ -534,7 +534,18 @@ LSUP_graph_connections (
     }
 
     // Gather all linking terms in a set first.
-    LSUP_TermSet *lts = LSUP_graph_unique_terms (gr, pos2);
+    LSUP_GraphIterator *it = LSUP_graph_lookup (gr, s, p, o, NULL);
+    LSUP_TermSet *lts = LSUP_term_set_new();
+    while (graph_iter_next_buffer (it) != LSUP_END) {
+        LSUP_Term
+            *ex = NULL,
+            *ins = LSUP_term_new_from_buffer (
+                    LSUP_btriple_pos (it->sspo, pos2));
+        LSUP_term_set_add (lts, ins, &ex);
+
+        if (ex) LSUP_term_free (ins);
+    }
+    LSUP_graph_iter_free(it);
 
     LSUP_LinkMap *ret = LSUP_link_map_new (type);
     size_t i = 0;