diff service/mqtt_to_rdf/inference/rdf_debug.py @ 1727:23e6154e6c11

file moves
author drewp@bigasterisk.com
date Tue, 20 Jun 2023 23:26:24 -0700
parents service/mqtt_to_rdf/rdf_debug.py@7d3797ed6681
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/service/mqtt_to_rdf/inference/rdf_debug.py	Tue Jun 20 23:26:24 2023 -0700
@@ -0,0 +1,31 @@
+import logging
+from typing import List, Union
+
+from rdflib.graph import Graph
+from rdflib.namespace import Namespace
+
+from inference.inference_types import Triple
+
+log = logging.getLogger('infer')
+
+ROOM = Namespace("http://projects.bigasterisk.com/room/")
+
+
+def graphDump(g: Union[Graph, List[Triple]], oneLine=True):
+    # this is very slow- debug only!
+    if not log.isEnabledFor(logging.DEBUG):
+        return "(skipped dump)"
+    try:
+        if not isinstance(g, Graph):
+            g2 = Graph()
+            g2 += g
+            g = g2
+        g.bind('', ROOM)
+        g.bind('ex', Namespace('http://example.com/'))
+        lines = g.serialize(format='n3').splitlines()
+        lines = [line for line in lines if not line.startswith('@prefix')]
+        if oneLine:
+            lines = [line.strip() for line in lines]
+        return ' '.join(lines)
+    except TypeError:
+        return repr(g)