annotate service/reasoning/graphop.py @ 1732:3f4b447d65f5

port to starlette/asyncio
author drewp@bigasterisk.com
date Mon, 10 Jul 2023 17:37:58 -0700
parents f3f667769aef
children
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
756
f3f667769aef python 3! and some types and cleanups
drewp@bigasterisk.com
parents: 179
diff changeset
3 from typing import List
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
4 log = logging.getLogger()
48
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
5
756
f3f667769aef python 3! and some types and cleanups
drewp@bigasterisk.com
parents: 179
diff changeset
6
f3f667769aef python 3! and some types and cleanups
drewp@bigasterisk.com
parents: 179
diff changeset
7 def graphWithoutMetadata(g: ConjunctiveGraph, ignorePredicates=[]):
48
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
8 """
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
9 graph filter that removes any statements whose subjects are
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
10 contexts in the graph and also any statements with the given
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
11 predicates
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
12 """
179
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 118
diff changeset
13
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 118
diff changeset
14 ctxs = [ctx.identifier for ctx in g.contexts()]
48
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
15
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
16 out = ConjunctiveGraph()
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
17 for stmt in g.quads((None, None, None)):
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
18 if stmt[0] not in ctxs and stmt[1] not in ignorePredicates:
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
19 out.addN([stmt])
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
20 return out
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
21
756
f3f667769aef python 3! and some types and cleanups
drewp@bigasterisk.com
parents: 179
diff changeset
22
f3f667769aef python 3! and some types and cleanups
drewp@bigasterisk.com
parents: 179
diff changeset
23 def graphEqual(a: ConjunctiveGraph, b: ConjunctiveGraph,
f3f667769aef python 3! and some types and cleanups
drewp@bigasterisk.com
parents: 179
diff changeset
24 ignorePredicates: List[URIRef]=[]):
48
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
25 """
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
26 compare graphs, omitting any metadata statements about contexts
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
27 (especially modification times) and also any statements using the
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
28 given predicates
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
29 """
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
30 stmtsA = set(graphWithoutMetadata(a, ignorePredicates))
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
31 stmtsB = set(graphWithoutMetadata(b, ignorePredicates))
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
32 if stmtsA == stmtsB:
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
33 return True
756
f3f667769aef python 3! and some types and cleanups
drewp@bigasterisk.com
parents: 179
diff changeset
34
48
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
35 if log.getEffectiveLevel() <= logging.INFO:
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
36 lost = stmtsA - stmtsB
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
37 if lost:
118
7179284779fd errors and logging in reasoning
drewp@bigasterisk.com
parents: 49
diff changeset
38 log.info("graph A only:")
48
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
39 for s in lost:
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 new = stmtsB - stmtsA
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
42 if new:
118
7179284779fd errors and logging in reasoning
drewp@bigasterisk.com
parents: 49
diff changeset
43 log.info("graph B only:")
48
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
44 for s in new:
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
45 log.info(" %s", s)
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents:
diff changeset
46 return False