annotate service/reasoning/graphop.py @ 114:4cd065b97fa1

bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4 Ignore-this: 81dc30256f5d2658e53fce60abea66db
author drewp@bigasterisk.com
date Tue, 10 Sep 2013 00:38:52 -0700
parents 0aeb8d6ea124
children 7179284779fd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
1 import logging
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
2 from rdflib import URIRef, ConjunctiveGraph
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
3 log = logging.getLogger()
48
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
4
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
5 def graphWithoutMetadata(g, ignorePredicates=[]):
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
6 """
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
7 graph filter that removes any statements whose subjects are
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
8 contexts in the graph and also any statements with the given
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
9 predicates
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
10 """
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
11 ctxs = map(URIRef, set(g.contexts())) # weird they turned to strings
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
12
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
13 out = ConjunctiveGraph()
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
14 for stmt in g.quads((None, None, None)):
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
15 if stmt[0] not in ctxs and stmt[1] not in ignorePredicates:
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
16 out.addN([stmt])
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
17 return out
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
18
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
19 def graphEqual(a, b, ignorePredicates=[]):
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
20 """
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
21 compare graphs, omitting any metadata statements about contexts
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
22 (especially modification times) and also any statements using the
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
23 given predicates
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
24 """
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
25 stmtsA = set(graphWithoutMetadata(a, ignorePredicates))
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
26 stmtsB = set(graphWithoutMetadata(b, ignorePredicates))
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
27 if stmtsA == stmtsB:
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
28 return True
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
29
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
30 if log.getEffectiveLevel() <= logging.INFO:
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
31 lost = stmtsA - stmtsB
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
32 if lost:
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
33 log.info("lost statements:")
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
34 for s in lost:
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
35 log.info(" %s", s)
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
36 new = stmtsB - stmtsA
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
37 if new:
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
38 log.info("new statements:")
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
39 for s in new:
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
40 log.info(" %s", s)
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
41 return False