# HG changeset patch # User drewp@bigasterisk.com # Date 1630976771 25200 # Node ID 34f2817320cca0b2ab52b94c5272cd0ac8d4bcb1 # Parent f928eb06a4f6a34f86944ada163053c2cc67ee6b new tests for a smaller part of the inner loop diff -r f928eb06a4f6 -r 34f2817320cc service/mqtt_to_rdf/inference.py --- a/service/mqtt_to_rdf/inference.py Mon Sep 06 17:03:19 2021 -0700 +++ b/service/mqtt_to_rdf/inference.py Mon Sep 06 18:06:11 2021 -0700 @@ -50,6 +50,9 @@ self.evaluations = list(Evaluation.findEvals(self.graph)) + def __repr__(self): + return f"Lhs({graphDump(self.graph)})" + def findCandidateBindings(self, workingSet: ReadOnlyWorkingSet, stats) -> Iterator['BoundLhs']: """bindings that fit the LHS of a rule, using statements from workingSet and functions from LHS""" @@ -143,7 +146,6 @@ @dataclass class BoundLhs: lhs: Lhs - binding: CandidateBinding # mutable def __post_init__(self): self.usedByFuncs = Graph(identifier=GRAPH_ID) @@ -188,7 +190,6 @@ return False return True - def _logVerifyBanner(self, boundLhs, workingSet: ReadOnlyWorkingSet, boundUsedByFuncs): if not log.isEnabledFor(logging.DEBUG): return diff -r f928eb06a4f6 -r 34f2817320cc service/mqtt_to_rdf/inference_test.py --- a/service/mqtt_to_rdf/inference_test.py Mon Sep 06 17:03:19 2021 -0700 +++ b/service/mqtt_to_rdf/inference_test.py Mon Sep 06 18:06:11 2021 -0700 @@ -1,14 +1,16 @@ """ also see https://github.com/w3c/N3/tree/master/tests/N3Tests """ +from collections import defaultdict from decimal import Decimal from typing import cast import unittest from rdflib import RDF, BNode, ConjunctiveGraph, Graph, Literal, Namespace +from rdflib.graph import ReadOnlyGraphAggregate from rdflib.parser import StringInputSource -from inference import Inference +from inference import Inference, Lhs from rdflib_debug_patches import patchBnodeCounter, patchSlimReprs patchSlimReprs() @@ -283,3 +285,29 @@ # inf = makeInferenceWithRules("{ :a :b (:e0 :e1 :e2 :e3) . } => { :new :stmt :here } .") # implied = inf.infer(N3(":a :b (:e0 :e1 :e2 :e3) .")) # self.assertGraphEqual(implied, N3(":new :stmt :here .")) + + +def fakeStats(): + return defaultdict(lambda: 0) + +class TestLhsFindCandidateBindings(WithGraphEqual): + + def testBnodeMatchesStmt(self): + l = Lhs(N3("[] :a :b .")) + ws = ReadOnlyGraphAggregate([N3("[] :a :b .")]) + cands = list(l.findCandidateBindings(ws, fakeStats())) + self.assertEqual(len(cands), 1) + + def testVarMatchesStmt(self): + l = Lhs(N3("?x :a :b .")) + ws = ReadOnlyGraphAggregate([N3("[] :a :b .")]) + cands = list(l.findCandidateBindings(ws, fakeStats())) + self.assertEqual(len(cands), 1) + + def testListsOnlyMatchEachOther(self): + l = Lhs(N3(":a :b (:e0 :e1) .")) + ws = ReadOnlyGraphAggregate([N3(":a :b (:e0 :e1) .")]) + stats = fakeStats() + cands = list(l.findCandidateBindings(ws, stats)) + self.assertLess(stats['permCountFailingVerify'], 20) + self.fail(str(cands)) \ No newline at end of file