annotate service/reasoning/graphop.py @ 1556:d36d3b9ae516

python 3! and some types and cleanups Ignore-this: 3453a547ee745fa83668f36956c835cd darcs-hash:d9bd8e8a584681078c5de1b6e06c894995400f1f
author drewp <drewp@bigasterisk.com>
date Fri, 14 Feb 2020 00:07:23 -0800
parents 5da9200418db
children
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
1556
d36d3b9ae516 python 3! and some types and cleanups
drewp <drewp@bigasterisk.com>
parents: 984
diff changeset
3 from typing import List
854
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
4 log = logging.getLogger()
853
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
5
1556
d36d3b9ae516 python 3! and some types and cleanups
drewp <drewp@bigasterisk.com>
parents: 984
diff changeset
6
d36d3b9ae516 python 3! and some types and cleanups
drewp <drewp@bigasterisk.com>
parents: 984
diff changeset
7 def graphWithoutMetadata(g: ConjunctiveGraph, ignorePredicates=[]):
853
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
8 """
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
9 graph filter that removes any statements whose subjects are
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
10 contexts in the graph and also any statements with the given
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
11 predicates
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
12 """
984
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
13
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
14 ctxs = [ctx.identifier for ctx in g.contexts()]
853
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
15
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
16 out = ConjunctiveGraph()
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
17 for stmt in g.quads((None, None, None)):
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
18 if stmt[0] not in ctxs and stmt[1] not in ignorePredicates:
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
19 out.addN([stmt])
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
20 return out
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
21
1556
d36d3b9ae516 python 3! and some types and cleanups
drewp <drewp@bigasterisk.com>
parents: 984
diff changeset
22
d36d3b9ae516 python 3! and some types and cleanups
drewp <drewp@bigasterisk.com>
parents: 984
diff changeset
23 def graphEqual(a: ConjunctiveGraph, b: ConjunctiveGraph,
d36d3b9ae516 python 3! and some types and cleanups
drewp <drewp@bigasterisk.com>
parents: 984
diff changeset
24 ignorePredicates: List[URIRef]=[]):
853
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
25 """
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
26 compare graphs, omitting any metadata statements about contexts
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
27 (especially modification times) and also any statements using the
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
28 given predicates
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
29 """
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
30 stmtsA = set(graphWithoutMetadata(a, ignorePredicates))
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
31 stmtsB = set(graphWithoutMetadata(b, ignorePredicates))
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
32 if stmtsA == stmtsB:
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
33 return True
1556
d36d3b9ae516 python 3! and some types and cleanups
drewp <drewp@bigasterisk.com>
parents: 984
diff changeset
34
853
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
35 if log.getEffectiveLevel() <= logging.INFO:
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
36 lost = stmtsA - stmtsB
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
37 if lost:
923
4ae49c6adecb errors and logging in reasoning
drewp <drewp@bigasterisk.com>
parents: 854
diff changeset
38 log.info("graph A only:")
853
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
39 for s in lost:
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
40 log.info(" %s", s)
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
41 new = stmtsB - stmtsA
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
42 if new:
923
4ae49c6adecb errors and logging in reasoning
drewp <drewp@bigasterisk.com>
parents: 854
diff changeset
43 log.info("graph B only:")
853
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
44 for s in new:
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
45 log.info(" %s", s)
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents:
diff changeset
46 return False