annotate service/reasoning/graphop.py @ 48:571e773c77c3

refactor graph ops Ignore-this: 60494d1602278528c51f138d6e56c32c
author drewp@bigasterisk.com
date Mon, 31 Dec 2012 12:47:58 -0800
parents
children 0aeb8d6ea124
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
48
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
1
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
2 def graphWithoutMetadata(g, ignorePredicates=[]):
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
3 """
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
4 graph filter that removes any statements whose subjects are
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
5 contexts in the graph and also any statements with the given
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
6 predicates
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
7 """
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
8 ctxs = map(URIRef, set(g.contexts())) # weird they turned to strings
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
9
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
10 out = ConjunctiveGraph()
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
11 for stmt in g.quads((None, None, None)):
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
12 if stmt[0] not in ctxs and stmt[1] not in ignorePredicates:
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
13 out.addN([stmt])
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
14 return out
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
15
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
16 def graphEqual(a, b, ignorePredicates=[]):
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
17 """
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
18 compare graphs, omitting any metadata statements about contexts
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
19 (especially modification times) and also any statements using the
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
20 given predicates
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
21 """
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
22 stmtsA = set(graphWithoutMetadata(a, ignorePredicates))
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
23 stmtsB = set(graphWithoutMetadata(b, ignorePredicates))
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
24 if stmtsA == stmtsB:
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
25 return True
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
26
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
27 if log.getEffectiveLevel() <= logging.INFO:
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
28 lost = stmtsA - stmtsB
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
29 if lost:
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
30 log.info("lost statements:")
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
31 for s in lost:
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
32 log.info(" %s", s)
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
33 new = stmtsB - stmtsA
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
34 if new:
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
35 log.info("new statements:")
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
36 for s in new:
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
37 log.info(" %s", s)
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
38 return False