annotate service/reasoning/graphop.py @ 984:5da9200418db

reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands Ignore-this: 7646fba7b5496eb1f33a87452af6c6de darcs-hash:20150514083000-312f9-ef4db22cf33250e690b6021b6b1968cc788d14cb
author drewp <drewp@bigasterisk.com>
date Thu, 14 May 2015 01:30:00 -0700
parents 4ae49c6adecb
children f3f667769aef
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
854
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
1 import logging
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
2 from rdflib import URIRef, ConjunctiveGraph
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
3 log = logging.getLogger()
853
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
4
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
5 def graphWithoutMetadata(g, ignorePredicates=[]):
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
6 """
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
7 graph filter that removes any statements whose subjects are
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
8 contexts in the graph and also any statements with the given
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
9 predicates
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
10 """
984
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
11
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
12 ctxs = [ctx.identifier for ctx in g.contexts()]
853
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
13
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
14 out = ConjunctiveGraph()
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
15 for stmt in g.quads((None, None, None)):
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
16 if stmt[0] not in ctxs and stmt[1] not in ignorePredicates:
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
17 out.addN([stmt])
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
18 return out
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
19
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
20 def graphEqual(a, b, ignorePredicates=[]):
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
21 """
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
22 compare graphs, omitting any metadata statements about contexts
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
23 (especially modification times) and also any statements using the
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
24 given predicates
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
25 """
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
26 stmtsA = set(graphWithoutMetadata(a, ignorePredicates))
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
27 stmtsB = set(graphWithoutMetadata(b, ignorePredicates))
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
28 if stmtsA == stmtsB:
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
29 return True
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
30
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
31 if log.getEffectiveLevel() <= logging.INFO:
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
32 lost = stmtsA - stmtsB
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
33 if lost:
923
4ae49c6adecb errors and logging in reasoning
drewp <drewp@bigasterisk.com>
parents: 854
diff changeset
34 log.info("graph A only:")
853
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
35 for s in lost:
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
36 log.info(" %s", s)
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
37 new = stmtsB - stmtsA
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
38 if new:
923
4ae49c6adecb errors and logging in reasoning
drewp <drewp@bigasterisk.com>
parents: 854
diff changeset
39 log.info("graph B only:")
853
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
40 for s in new:
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
41 log.info(" %s", s)
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
42 return False