changeset 1609:34f2817320cc

new tests for a smaller part of the inner loop
author drewp@bigasterisk.com
date Mon, 06 Sep 2021 18:06:11 -0700
parents f928eb06a4f6
children 6fc48ef4c696
files service/mqtt_to_rdf/inference.py service/mqtt_to_rdf/inference_test.py
diffstat 2 files changed, 32 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- 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
--- 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