Mercurial > code > home > repos > homeauto
annotate service/reasoning/graphop.py @ 1754:92999dfbf321 default tip
add shelly support
author | drewp@bigasterisk.com |
---|---|
date | Tue, 04 Jun 2024 13:03:43 -0700 |
parents | f3f667769aef |
children |
rev | line source |
---|---|
49 | 1 import logging |
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 | 4 log = logging.getLogger() |
48 | 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 | 8 """ |
9 graph filter that removes any statements whose subjects are | |
10 contexts in the graph and also any statements with the given | |
11 predicates | |
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 | 15 |
16 out = ConjunctiveGraph() | |
17 for stmt in g.quads((None, None, None)): | |
18 if stmt[0] not in ctxs and stmt[1] not in ignorePredicates: | |
19 out.addN([stmt]) | |
20 return out | |
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 | 25 """ |
26 compare graphs, omitting any metadata statements about contexts | |
27 (especially modification times) and also any statements using the | |
28 given predicates | |
29 """ | |
30 stmtsA = set(graphWithoutMetadata(a, ignorePredicates)) | |
31 stmtsB = set(graphWithoutMetadata(b, ignorePredicates)) | |
32 if stmtsA == stmtsB: | |
33 return True | |
756
f3f667769aef
python 3! and some types and cleanups
drewp@bigasterisk.com
parents:
179
diff
changeset
|
34 |
48 | 35 if log.getEffectiveLevel() <= logging.INFO: |
36 lost = stmtsA - stmtsB | |
37 if lost: | |
118 | 38 log.info("graph A only:") |
48 | 39 for s in lost: |
40 log.info(" %s", s) | |
41 new = stmtsB - stmtsA | |
42 if new: | |
118 | 43 log.info("graph B only:") |
48 | 44 for s in new: |
45 log.info(" %s", s) | |
46 return False |