瀏覽代碼

Add LSUP_graph_print().

scossu 1 周之前
父節點
當前提交
ffbf120c4d
共有 4 個文件被更改,包括 41 次插入2 次删除
  1. 2 2
      Makefile
  2. 8 0
      include/graph.h
  3. 25 0
      src/graph.c
  4. 6 0
      test/test_graph.c

+ 2 - 2
Makefile

@@ -157,8 +157,8 @@ debug_install: install debug ## Install standard and debug libraries.
 	cp $(DBG_LIBS) $(DESTDIR)$(libdir)
 
 
-.PHONY: clean ## Clean up artifacts, including language parsers.
-clean:
+.PHONY: clean
+clean: ## Clean up artifacts, including language parsers.
 	rm -f ./*.[aod] bin/* src/*.[aod] src/codec/*.[aod] src/codec/*.out
 	rm -rf build/ dist/ lsup_rdf.egg-info/
 	rm -f $(LIBS) $(DBG_LIBS)

+ 8 - 0
include/graph.h

@@ -392,6 +392,14 @@ void
 LSUP_graph_iter_free (LSUP_GraphIterator *it);
 
 
+/** @brief Print graph information and triples to stdout.
+ *
+ * @param[in] gr Graph handle to display.
+ */
+void
+LSUP_graph_print (LSUP_Graph *gr);
+
+
 /** @brief Get term pairs connected to a term in a graph.
  *
  * This returns a #LSUP_LinkMap extracted from a graph for a given term. The

+ 25 - 0
src/graph.c

@@ -622,6 +622,31 @@ LSUP_graph_contains (const LSUP_Graph *gr, const LSUP_Triple *spo)
 }
 
 
+void
+LSUP_graph_print (LSUP_Graph *gr)
+{
+    size_t ct;
+    LSUP_GraphIterator *it = LSUP_graph_lookup (gr, NULL, NULL, NULL, &ct);
+    if (UNLIKELY (!it)) log_error ("Could not print graph contents.");
+
+    printf ("*** Graph %s (%zu triples):\n\n", gr->uri->data, ct);
+
+    LSUP_Triple *spo = NULL;
+    ct = 0;
+    LSUP_rc rc = LSUP_NORESULT;
+    while (rc = LSUP_graph_iter_next (it, &spo) == LSUP_OK) {
+        printf (
+                "#%-6zu  {%s  %s  %s}\n",
+                ct, spo->s->data, spo->p->data, spo->o->data);
+        ct++;
+    }
+    if (rc != LSUP_END)
+        log_error (
+                "Output truncated due to abnormal return: %s",
+                LSUP_strerror (rc));
+}
+
+
 LSUP_LinkMap *
 LSUP_graph_connections (
         const LSUP_Graph *gr, LSUP_Term *t, LSUP_LinkType type)

+ 6 - 0
test/test_graph.c

@@ -348,6 +348,12 @@ _graph_lookup (LSUP_StoreType type)
         LSUP_graph_iter_free (it);
     };
 
+#if 0
+    // Enable to test print functionality and exit early.
+    LSUP_graph_print (gr);
+    return 1;
+#endif
+
     free_triples (trp);
     LSUP_graph_free (gr);
     if (type != LSUP_STORE_HTABLE) LSUP_store_free (store);