Преглед изворни кода

Support edge connection list.

Stefano Cossu пре 2 година
родитељ
комит
71eef5e738
5 измењених фајлова са 57 додато и 49 уклоњено
  1. 7 6
      include/graph.h
  2. 24 21
      include/term.h
  3. 7 7
      src/codec/grammar_ttl.y
  4. 6 2
      src/graph.c
  5. 13 13
      src/term.c

+ 7 - 6
include/graph.h

@@ -322,20 +322,21 @@ LSUP_graph_iter_free (LSUP_GraphIterator *it);
 
 /** @brief Add triples for a term and related connection list to a graph.
  *
- * The connection list can be of inbound or outbound type; depending on that,
- * triples are added with the given term as the subject, or the object.
+ * The connection list can be of inbound, outbound, or edge type; depending on
+ * that, triples are added with the given term as the subject, the predicate,
+ * or the object.
  *
- * @param[in] it Graph iterator to use for insertion.
+ * @param[in] it Graph iterator obtained with #LSUP_graph_add_init().
  *
- * @param[in] s Subject of all the triples.
+ * @param[in] t Term to be associated with the collection list.
  *
- * @param[in] po Predicate-object list.
+ * @param[in] cl Connection list.
  *
  * @return Number of triples parsed on success, or <0 (LSUP_*_ERR) on error.
  */
 size_t
 LSUP_spo_list_add_triples (
-        LSUP_GraphIterator *it, LSUP_Term *s, const LSUP_ConnectionList *po);
+        LSUP_GraphIterator *it, LSUP_Term *t, const LSUP_ConnectionList *cl);
 
 
 /** @brief Add triples for an anonymous collection to a graph.

+ 24 - 21
include/term.h

@@ -100,10 +100,11 @@ typedef struct term_cache_entry_t {
 } LSUP_KeyedTerm;
 
 
-/// Connection type: inbound or outbound.
+/// Connection type.
 typedef enum {
-    LSUP_CONN_INBOUND,              ///< Inbound connection.
-    LSUP_CONN_OUTBOUND,             ///< Outbound connection.
+    LSUP_CONN_INBOUND,              ///< Inbound connection (sp).
+    LSUP_CONN_OUTBOUND,             ///< Outbound connection (po).
+    LSUP_CONN_EDGE,                 ///< Edge connection (so).
 } LSUP_ConnectionType;
 
 
@@ -112,20 +113,21 @@ typedef enum {
  * A list of predicates and related lists of terms, that can be used to list
  * inbound or outbound connections to a node.
  *
- * Each term in the NUL-terminated `p` list represent a predicate which is
- * paired with a list of terms in the `tl` list. The index of each predicate
- * corresponds to the same index of a term list.
+ * Each term in the NUL-terminated `p` list represent a term which is
+ * paired with a list of terms in the `tl` list. The index of each term in this
+ * list corresponds to the same index of a term list in `tl`.
  *
  * If the type of the connection list is `LSUP_CONN_INBOUND`, the term list
  * represent subjects and a term that is associated with the connection list is
  * the related object; if `LSUP_CONN_OUTBOUND`, the term list represents
  * objects, and a term that is associated with the connection list represents
- * the subject.
+ * the subject. If `LSUP_CONN_EDGE`, the members of the connection list
+ * represent subjects and objects, and the associated term is the predicate.
  *
  */
 typedef struct {
     LSUP_ConnectionType type;       ///< Inbound or outbound connection.
-    LSUP_Term **    p;              ///< NUL-terminated array of term handles.
+    LSUP_Term **    t;              ///< NUL-terminated array of term handles.
     LSUP_Term ***   tl;             /**<
                                       * NUL-terminated array of
                                       * NUL-terminated arrays of term handles.
@@ -527,9 +529,9 @@ LSUP_Term **
 LSUP_term_list_add (LSUP_Term **tl, LSUP_Term *t);
 
 
-/** @brief New predicate-object list.
+/** @brief New connection list.
  *
- * The initial state of the returned list is: `{p: [NULL], tl: [NULL]}`
+ * The initial state of the returned list is: `{t: [NULL], tl: [NULL]}`
  *
  * Predicates and term lists can be added with #LSUP_conn_list_add, and terms
  * can be added to a term list with #LSUP_term_list_add.
@@ -537,7 +539,7 @@ LSUP_term_list_add (LSUP_Term **tl, LSUP_Term *t);
  * @return a new empty predicate-object list.
  */
 LSUP_ConnectionList *
-LSUP_conn_list_list_new (LSUP_ConnectionType type);
+LSUP_conn_list_new (LSUP_ConnectionType type);
 
 
 /** @brief Free a predicate-object list.
@@ -545,27 +547,28 @@ LSUP_conn_list_list_new (LSUP_ConnectionType type);
  * All arrays and term handles are recursively freed.
  *
  * @param[in] pol Predicate-object list handle obtained with
- *  #LSUP_conn_list_list_new().
+ *  #LSUP_conn_list_new().
  */
 void
-LSUP_conn_list_list_free (LSUP_ConnectionList *pol);
+LSUP_conn_list_free (LSUP_ConnectionList *pol);
 
 
-/** @brief Add a predicate-object list pair to a PO list.
+/** @brief Add a term - term list pair to a connection list.
  *
- * @param[in] pol Predicate-object list handle obtained with
- *  #LSUP_conn_list_list_new().
+ * @param[in] cl Connection list handle obtained with
+ *  #LSUP_conn_list_new().
  *
- *  @param[in] p Predicate to be associated with the given object list. The
- *   PO structure takes ownership of the term.
+ *  @param[in] t Term to be associated with the given object list. The
+ *   connection list structure takes ownership of the term.
  *
  *  @param[in] o NULL-terminated array of object term handles to be associated
- *   with the given predicate. The PO structire takes ownership of the whole
- *   term array.
+ *   with the given predicate. The connection list structire takes ownership of
+ *   the whole term array.
  *
  * @return LSUP_OK on success; LSUP_MEM_ERR on allocation error.
  */
 LSUP_rc
-LSUP_conn_list_list_add (LSUP_ConnectionList *pol, LSUP_Term *p, LSUP_Term **o);
+LSUP_conn_list_add (
+        LSUP_ConnectionList *cl, LSUP_Term *t, LSUP_Term **tl);
 
 #endif

+ 7 - 7
src/codec/grammar_ttl.y

@@ -85,7 +85,7 @@ triples 	::= subject(S) ows predObjList(L) PERIOD . {
                 log_trace ("Added %lu triples.", ct);
 
                 LSUP_term_free (S);
-                LSUP_conn_list_list_free (L);
+                LSUP_conn_list_free (L);
             }
 triples 	::= subject(S) ows predObjList(L) SEMICOLON PERIOD . [PERIOD] {
                 size_t ct = LSUP_spo_list_add_triples (state->it, S, L);
@@ -94,17 +94,17 @@ triples 	::= subject(S) ows predObjList(L) SEMICOLON PERIOD . [PERIOD] {
                 log_trace ("Added %lu triples.", ct);
 
                 LSUP_term_free (S);
-                LSUP_conn_list_list_free (L);
+                LSUP_conn_list_free (L);
             }
 
 %type predObjList       { LSUP_ConnectionList * }
-%destructor predObjList { LSUP_conn_list_list_free ($$); }
+%destructor predObjList { LSUP_conn_list_free ($$); }
 predObjList(A) ::= predicate(P) ows objectList(O) . [SEMICOLON] {
-                A = LSUP_conn_list_list_new(LSUP_CONN_OUTBOUND);
-                LSUP_conn_list_list_add (A, P, O);
+                A = LSUP_conn_list_new(LSUP_CONN_OUTBOUND);
+                LSUP_conn_list_add (A, P, O);
             }
 predObjList(A) ::= predObjList(L) SEMICOLON predicate(P) ows objectList(O) . {
-                LSUP_conn_list_list_add (L, P, O);
+                LSUP_conn_list_add (L, P, O);
                 A = L;
             }
 
@@ -222,7 +222,7 @@ blank(A)    ::= LBRACKET predObjList(L) RBRACKET . [BNODE_ID] {
                 state->ct += LSUP_spo_list_add_triples (state->it, A, L);
                 log_trace ("Created list BN: _:%s", A->data);
 
-                LSUP_conn_list_list_free (L);
+                LSUP_conn_list_free (L);
             }
 blank       ::= collection . [BNODE_ID]
 blank(A)    ::= LPAREN RPAREN . [BNODE_ID] {

+ 6 - 2
src/graph.c

@@ -522,10 +522,14 @@ LSUP_spo_list_add_triples (
     LSUP_Triple *spo = LSUP_triple_new (NULL, NULL, NULL);
     if (cl->type == LSUP_CONN_INBOUND)
         spo->o = t;
-    else spo->s = t;
+    else if (cl->type == LSUP_CONN_OUTBOUND)
+        spo->s = t;
+    else spo->p = t;
 
     for (size_t i = 0; cl->p[i]; i++) {
-        spo->p = cl->p[i];
+        if (cl->type == LSUP_CONN_EDGE) spo->s = cl->p[i];
+        else spo->p = cl->p[i];
+
         for (size_t j = 0; cl->tl[i][j]; j++) {
             if (cl->type == LSUP_CONN_INBOUND)
                 spo->s = cl->tl[i][j];

+ 13 - 13
src/term.c

@@ -477,13 +477,13 @@ LSUP_term_list_add (LSUP_Term **tl, LSUP_Term *t)
 
 
 LSUP_ConnectionList *
-LSUP_conn_list_list_new (LSUP_ConnectionType type)
+LSUP_conn_list_new (LSUP_ConnectionType type)
 {
     LSUP_ConnectionList *cl;
     MALLOC_GUARD (cl, NULL);
     cl->type = type;
     // Set sentinels.
-    CALLOC_GUARD (cl->p, NULL);
+    CALLOC_GUARD (cl->t, NULL);
     CALLOC_GUARD (cl->tl, NULL);
 
     return cl;
@@ -491,15 +491,15 @@ LSUP_conn_list_list_new (LSUP_ConnectionType type)
 
 
 void
-LSUP_conn_list_list_free (LSUP_ConnectionList *cl)
+LSUP_conn_list_free (LSUP_ConnectionList *cl)
 {
     log_debug ("Freeing predicate object list.");
-    for (size_t i = 0; cl->p[i]; i++) {
+    for (size_t i = 0; cl->t[i]; i++) {
         // Free individual predicate handles.
-        LSUP_term_free (cl->p[i]);
+        LSUP_term_free (cl->t[i]);
     }
     // Free pred list.
-    free (cl->p);
+    free (cl->t);
 
     for (size_t i = 0; cl->tl[i]; i++) {
         for (size_t j = 0; cl->tl[i][j]; j++) {
@@ -517,17 +517,17 @@ LSUP_conn_list_list_free (LSUP_ConnectionList *cl)
 
 
 LSUP_rc
-LSUP_conn_list_list_add (LSUP_ConnectionList *cl, LSUP_Term *p, LSUP_Term **tl)
+LSUP_conn_list_add (LSUP_ConnectionList *cl, LSUP_Term *t, LSUP_Term **tl)
 {
     size_t i;
 
     i = 0;
-    while (cl->p[i++]);  // Count includes sentinel.
-    LSUP_Term **tmp_p = realloc (cl->p, sizeof (*cl->p) * (i + 1));
-    if (!tmp_p) return LSUP_MEM_ERR;
-    tmp_p[i - 1] = p;
-    tmp_p[i] = NULL;
-    cl->p = tmp_p;
+    while (cl->t[i++]);  // Count includes sentinel.
+    LSUP_Term **tmp_t = realloc (cl->t, sizeof (*cl->t) * (i + 1));
+    if (!tmp_t) return LSUP_MEM_ERR;
+    tmp_t[i - 1] = t;
+    tmp_t[i] = NULL;
+    cl->t = tmp_t;
 
     i = 0;
     while (cl->tl[i++]);