comparison service/mqtt_to_rdf/stmt_chunk.py @ 1661:00a5624d1d14

cleanups and optimizations
author drewp@bigasterisk.com
date Sun, 19 Sep 2021 16:51:51 -0700
parents 31f7dab6a60b
children 1a7c1261302c
comparison
equal deleted inserted replaced
1660:31f7dab6a60b 1661:00a5624d1d14
6 from rdflib.graph import Graph 6 from rdflib.graph import Graph
7 from rdflib.namespace import RDF 7 from rdflib.namespace import RDF
8 from rdflib.term import BNode, Literal, Node, URIRef, Variable 8 from rdflib.term import BNode, Literal, Node, URIRef, Variable
9 9
10 from candidate_binding import CandidateBinding 10 from candidate_binding import CandidateBinding
11 from inference_types import BindingUnknown, Inconsistent, Triple 11 from inference_types import BindingUnknown, Inconsistent
12 from rdf_debug import graphDump
13 12
14 log = logging.getLogger('infer') 13 log = logging.getLogger('infer')
15 14
16 INDENT = ' ' 15 INDENT = ' '
17 16
18 ChunkPrimaryTriple = Tuple[Optional[Node], Node, Optional[Node]] 17 ChunkPrimaryTriple = Tuple[Optional[Node], Node, Optional[Node]]
19 18
20 19
21 @dataclass 20 @dataclass
22 class Chunk: # rename this 21 class Chunk: # rename this
23 """a statement, maybe with variables in it, except *the object can be an rdf list*. 22 """A statement, maybe with variables in it, except *the subject or object
24 This is done to optimize list comparisons (a lot) at the very minor expense of not 23 can be rdf lists*. This is done to optimize list comparisons (a lot) at the
25 handling certain exotic cases, such as a branching list. 24 very minor expense of not handling certain exotic cases, such as a branching
25 list.
26 26
27 Also the subject could be a list, e.g. for (?x ?y) math:sum ?z . 27 Example: (?x ?y) math:sum ?z . <-- this becomes one Chunk.
28 28
29 Also a function call in a rule is always contained in exactly one chunk. 29 A function call in a rule is always contained in exactly one chunk.
30
31 https://www.w3.org/TeamSubmission/n3/#:~:text=Implementations%20may%20treat%20list%20as%20a%20data%20type
30 """ 32 """
31 # all immutable 33 # all immutable
32 primary: ChunkPrimaryTriple 34 primary: ChunkPrimaryTriple
33 subjList: Optional[List[Node]] = None 35 subjList: Optional[List[Node]] = None
34 objList: Optional[List[Node]] = None 36 objList: Optional[List[Node]] = None
69 return outBinding 71 return outBinding
70 72
71 def myMatches(self, g: 'ChunkedGraph') -> List['Chunk']: 73 def myMatches(self, g: 'ChunkedGraph') -> List['Chunk']:
72 """Chunks from g where self, which may have BindableTerm wildcards, could match that chunk in g.""" 74 """Chunks from g where self, which may have BindableTerm wildcards, could match that chunk in g."""
73 out: List['Chunk'] = [] 75 out: List['Chunk'] = []
74 log.debug(f'{INDENT*6} {self}.myMatches({g}') 76 if log.isEnabledFor(logging.DEBUG):
77 log.debug(f'{INDENT*6} {self}.myMatches({g}')
75 for ch in g.allChunks(): 78 for ch in g.allChunks():
76 if self.matches(ch): 79 if self.matches(ch):
77 out.append(ch) 80 out.append(ch)
78 return out 81 return out
79 82