changeset 853:39857f82922e

refactor graph ops Ignore-this: 60494d1602278528c51f138d6e56c32c darcs-hash:20121231204758-312f9-4f3382463757a6f17d0409905922a42207cad987
author drewp <drewp@bigasterisk.com>
date Mon, 31 Dec 2012 12:47:58 -0800
parents 6ccd930834d1
children 1815d2343a3f
files service/reasoning/graphop.py service/reasoning/reasoning.py
diffstat 2 files changed, 39 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/service/reasoning/graphop.py	Mon Dec 31 12:47:58 2012 -0800
@@ -0,0 +1,38 @@
+
+def graphWithoutMetadata(g, ignorePredicates=[]):
+    """
+    graph filter that removes any statements whose subjects are
+    contexts in the graph and also any statements with the given
+    predicates
+    """
+    ctxs = map(URIRef, set(g.contexts())) # weird they turned to strings
+
+    out = ConjunctiveGraph()
+    for stmt in g.quads((None, None, None)):
+        if stmt[0] not in ctxs and stmt[1] not in ignorePredicates:
+            out.addN([stmt])
+    return out
+
+def graphEqual(a, b, ignorePredicates=[]):
+    """
+    compare graphs, omitting any metadata statements about contexts
+    (especially modification times) and also any statements using the
+    given predicates
+    """
+    stmtsA = set(graphWithoutMetadata(a, ignorePredicates))
+    stmtsB = set(graphWithoutMetadata(b, ignorePredicates))
+    if stmtsA == stmtsB:
+        return True
+    
+    if log.getEffectiveLevel() <= logging.INFO:
+        lost = stmtsA - stmtsB
+        if lost:
+            log.info("lost statements:")
+            for s in lost:
+                log.info("  %s", s)
+        new = stmtsB - stmtsA
+        if new:
+            log.info("new statements:")
+            for s in new:
+                log.info("  %s", s)
+    return False
--- a/service/reasoning/reasoning.py	Mon Dec 31 00:47:12 2012 -0800
+++ b/service/reasoning/reasoning.py	Mon Dec 31 12:47:58 2012 -0800
@@ -26,6 +26,7 @@
 from FuXi.Rete.RuleStore import N3RuleStore
 import cyclone.web
 from inference import addTrig, infer
+from graphop import graphEqual
 
 sys.path.append("../../lib")
 from logsetup import log
@@ -34,35 +35,6 @@
 ROOM = Namespace("http://projects.bigasterisk.com/room/")
 DEV = Namespace("http://projects.bigasterisk.com/device/")
 
-def graphWithoutMetadata(g, ignorePredicates=[]):
-    """
-    graph filter that removes any statements whose subjects are
-    contexts in the graph and also any statements with the given
-    predicates
-    """
-    ctxs = map(URIRef, set(g.contexts())) # weird they turned to strings
-
-    out = ConjunctiveGraph()
-    for stmt in g.quads((None, None, None)):
-        if stmt[0] not in ctxs and stmt[1] not in ignorePredicates:
-            out.addN([stmt])
-    return out
-
-def graphEqual(a, b, ignorePredicates=[]):
-    """
-    compare graphs, omitting any metadata statements about contexts
-    (especially modification times) and also any statements using the
-    given predicates
-    """
-    stmtsA = graphWithoutMetadata(a, ignorePredicates)
-    stmtsB = graphWithoutMetadata(b, ignorePredicates)
-    if log.getEffectiveLevel() <= logging.DEBUG:
-        diff = set(stmtsA).symmetric_difference(set(stmtsB))
-        if diff:
-            log.debug("changing statements:")
-            for s in diff:
-                log.debug(str(s))
-    return set(stmtsA) == set(stmtsB)
 
 class InputGraph(object):
     def __init__(self, inputDirs, onChange):