Mercurial > code > home > repos > homeauto
comparison service/mqtt_to_rdf/inference.py @ 1649:bb5d2b5370ac
add nonRuleStatments to Inference api. there's already a test in an eariler commit
author | drewp@bigasterisk.com |
---|---|
date | Fri, 17 Sep 2021 11:11:24 -0700 |
parents | 3059f31b2dfa |
children | 2061df259224 |
comparison
equal
deleted
inserted
replaced
1648:3059f31b2dfa | 1649:bb5d2b5370ac |
---|---|
8 from collections import defaultdict | 8 from collections import defaultdict |
9 from dataclasses import dataclass | 9 from dataclasses import dataclass |
10 from typing import (Dict, Iterator, List, Optional, Sequence, Set, Tuple, Union, cast) | 10 from typing import (Dict, Iterator, List, Optional, Sequence, Set, Tuple, Union, cast) |
11 | 11 |
12 from prometheus_client import Histogram, Summary | 12 from prometheus_client import Histogram, Summary |
13 from rdflib import RDF, BNode, Graph, Namespace | 13 from rdflib import RDF, BNode, Graph, Literal, Namespace |
14 from rdflib.graph import ConjunctiveGraph, ReadOnlyGraphAggregate | 14 from rdflib.graph import ConjunctiveGraph, ReadOnlyGraphAggregate |
15 from rdflib.term import Node, URIRef, Variable | 15 from rdflib.term import Node, URIRef, Variable |
16 | 16 |
17 from candidate_binding import BindingConflict, CandidateBinding | 17 from candidate_binding import BindingConflict, CandidateBinding |
18 from inference_types import BindingUnknown, ReadOnlyWorkingSet, Triple | 18 from inference_types import BindingUnknown, ReadOnlyWorkingSet, Triple |
403 self.rules = [] | 403 self.rules = [] |
404 self._nonRuleStmts = [] | 404 self._nonRuleStmts = [] |
405 for stmt in g: | 405 for stmt in g: |
406 if stmt[1] == LOG['implies']: | 406 if stmt[1] == LOG['implies']: |
407 self.rules.append(Rule(stmt[0], stmt[2])) | 407 self.rules.append(Rule(stmt[0], stmt[2])) |
408 # other stmts should go to a default working set? | 408 else: |
409 self._nonRuleStmts.append(stmt) | |
410 | |
411 def nonRuleStatements(self) -> List[Triple]: | |
412 return self._nonRuleStmts | |
409 | 413 |
410 @INFER_CALLS.time() | 414 @INFER_CALLS.time() |
411 def infer(self, graph: Graph): | 415 def infer(self, graph: Graph): |
412 """ | 416 """ |
413 returns new graph of inferred statements. | 417 returns new graph of inferred statements. |
415 n = graph.__len__() | 419 n = graph.__len__() |
416 INFER_GRAPH_SIZE.observe(n) | 420 INFER_GRAPH_SIZE.observe(n) |
417 log.info(f'{INDENT*0} Begin inference of graph len={n} with rules len={len(self.rules)}:') | 421 log.info(f'{INDENT*0} Begin inference of graph len={n} with rules len={len(self.rules)}:') |
418 startTime = time.time() | 422 startTime = time.time() |
419 stats: Dict[str, Union[int, float]] = defaultdict(lambda: 0) | 423 stats: Dict[str, Union[int, float]] = defaultdict(lambda: 0) |
424 | |
420 # everything that is true: the input graph, plus every rule conclusion we can make | 425 # everything that is true: the input graph, plus every rule conclusion we can make |
421 workingSet = Graph() | 426 workingSet = Graph() |
427 workingSet += self._nonRuleStmts | |
422 workingSet += graph | 428 workingSet += graph |
423 | 429 |
424 # just the statements that came from RHS's of rules that fired. | 430 # just the statements that came from RHS's of rules that fired. |
425 implied = ConjunctiveGraph() | 431 implied = ConjunctiveGraph() |
426 | 432 |