annotate service/reasoning/graphop.py @ 179:0f2ac014d9ea

reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands Ignore-this: 7646fba7b5496eb1f33a87452af6c6de
author drewp@bigasterisk.com
date Thu, 14 May 2015 01:30:00 -0700
parents 7179284779fd
children f3f667769aef
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 """
179
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 118
diff changeset
11
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 118
diff changeset
12 ctxs = [ctx.identifier for ctx in g.contexts()]
48
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
13
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
14 out = ConjunctiveGraph()
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
15 for stmt in g.quads((None, None, None)):
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
16 if stmt[0] not in ctxs and stmt[1] not in ignorePredicates:
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
17 out.addN([stmt])
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
18 return out
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
19
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
20 def graphEqual(a, b, ignorePredicates=[]):
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
21 """
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
22 compare graphs, omitting any metadata statements about contexts
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
23 (especially modification times) and also any statements using the
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
24 given predicates
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
25 """
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
26 stmtsA = set(graphWithoutMetadata(a, ignorePredicates))
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
27 stmtsB = set(graphWithoutMetadata(b, ignorePredicates))
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
28 if stmtsA == stmtsB:
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
29 return True
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
30
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
31 if log.getEffectiveLevel() <= logging.INFO:
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
32 lost = stmtsA - stmtsB
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
33 if lost:
118
7179284779fd errors and logging in reasoning
drewp@bigasterisk.com
parents: 49
diff changeset
34 log.info("graph A only:")
48
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
35 for s in lost:
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
36 log.info(" %s", s)
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
37 new = stmtsB - stmtsA
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
38 if new:
118
7179284779fd errors and logging in reasoning
drewp@bigasterisk.com
parents: 49
diff changeset
39 log.info("graph B only:")
48
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
40 for s in new:
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
41 log.info(" %s", s)
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
42 return False