# HG changeset patch # User drewp@bigasterisk.com # Date 1632034439 25200 # Node ID 2061df2592248d03891787152e51006eccb342af # Parent bb5d2b5370ac817146d0b1f20d7181def7c6370a move graphDump (on its way out, since reprs are getting better) diff -r bb5d2b5370ac -r 2061df259224 service/mqtt_to_rdf/inference.py --- a/service/mqtt_to_rdf/inference.py Fri Sep 17 11:11:24 2021 -0700 +++ b/service/mqtt_to_rdf/inference.py Sat Sep 18 23:53:59 2021 -0700 @@ -17,6 +17,7 @@ from candidate_binding import BindingConflict, CandidateBinding from inference_types import BindingUnknown, ReadOnlyWorkingSet, Triple from lhs_evaluation import functionsFor, lhsStmtsUsedByFuncs, rulePredicates +from rdf_debug import graphDump log = logging.getLogger('infer') INDENT = ' ' @@ -471,20 +472,3 @@ for stmt in sorted(r.lhsGraph, reverse=True): log.debug(f'{INDENT*4} {stmt}') log.debug(f'{INDENT*3} rule def rhs: {graphDump(r.rhsGraph)}') - - -def graphDump(g: Union[Graph, List[Triple]], oneLine=True): - # this is very slow- debug only! - if not log.isEnabledFor(logging.DEBUG): - return "(skipped dump)" - if not isinstance(g, Graph): - g2 = Graph() - g2 += g - g = g2 - g.bind('', ROOM) - g.bind('ex', Namespace('http://example.com/')) - lines = cast(bytes, g.serialize(format='n3')).decode('utf8').splitlines() - lines = [line for line in lines if not line.startswith('@prefix')] - if oneLine: - lines = [line.strip() for line in lines] - return ' '.join(lines) diff -r bb5d2b5370ac -r 2061df259224 service/mqtt_to_rdf/rdf_debug.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/service/mqtt_to_rdf/rdf_debug.py Sat Sep 18 23:53:59 2021 -0700 @@ -0,0 +1,31 @@ +import logging +from typing import List, Union, cast + +from rdflib.graph import Graph +from rdflib.namespace import Namespace + +from 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 = cast(bytes, g.serialize(format='n3')).decode('utf8').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) \ No newline at end of file