Coverage for tests / unit / rdflibplus / test_datasetview_owlrl.py: 100%
26 statements
« prev ^ index » next coverage.py v7.12.0, created at 2025-11-26 21:25 +0000
« prev ^ index » next coverage.py v7.12.0, created at 2025-11-26 21:25 +0000
1"""Test that OWL-RL reasoner works correctly with DatasetView."""
3from owlrl import DeductiveClosure
4from owlrl.OWLRL import OWLRL_Semantics
5from rdflib import RDF, RDFS, Dataset, Namespace
7from pythinfer.rdflibplus import DatasetView
9EX = Namespace("http://example.org/")
12def test_owlrl_inference_with_datasetview() -> None:
13 """Test that OWL-RL reasoner can run inference on a DatasetView.
15 This test verifies that:
16 1. OWL-RL reasoner uses our overridden triples() method (not store directly)
17 2. DatasetView provides union behavior without needing default_union=True
18 3. Inference works correctly across multiple graphs in the view
19 """
20 # Create a dataset with two graphs
21 ds = Dataset()
23 # Graph 1: Class hierarchy
24 g1 = ds.graph(EX.graph1)
25 g1.add((EX.Animal, RDF.type, RDFS.Class))
26 g1.add((EX.Dog, RDFS.subClassOf, EX.Animal))
28 # Graph 2: Instance data
29 g2 = ds.graph(EX.graph2)
30 g2.add((EX.fido, RDF.type, EX.Dog))
32 assert len(g1) == 2
33 assert len(g2) == 1
35 # Create a DatasetView with both graphs
36 view = DatasetView(ds, [EX.graph1, EX.graph2])
38 # Run OWL-RL inference on the view
39 g_inferences = ds.graph(EX.inferences)
40 DeductiveClosure(OWLRL_Semantics).expand(view, g_inferences)
42 # Check that we got the expected transitive inference: fido rdf:type Animal
43 # This proves that the reasoner saw both graphs (the subclass and the instance)
44 expected_triple = (EX.fido, RDF.type, EX.Animal)
45 assert expected_triple in g_inferences, (
46 f"Expected inference {expected_triple} not found. "
47 f"This suggests OWL-RL is not seeing both graphs in the view."
48 )
50 # Verify we got a reasonable number of inferences (should be > 1)
51 assert len(g_inferences) > 1, "Expected multiple inferences from OWL-RL"
54def test_owlrl_with_empty_datasetview() -> None:
55 """Test that OWL-RL reasoner handles empty DatasetView correctly."""
56 ds = Dataset()
58 # Create an empty DatasetView
59 view = DatasetView(ds, [])
61 # Run OWL-RL inference on empty view
62 g_inferences = ds.graph(EX.inferences)
63 DeductiveClosure(OWLRL_Semantics).expand(view, g_inferences)
65 # Should produce minimal or no inferences from empty input
66 # (OWL-RL adds some axiom triples even for empty graphs)
67 assert len(g_inferences) > 0