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

1"""Test that OWL-RL reasoner works correctly with DatasetView.""" 

2 

3from owlrl import DeductiveClosure 

4from owlrl.OWLRL import OWLRL_Semantics 

5from rdflib import RDF, RDFS, Dataset, Namespace 

6 

7from pythinfer.rdflibplus import DatasetView 

8 

9EX = Namespace("http://example.org/") 

10 

11 

12def test_owlrl_inference_with_datasetview() -> None: 

13 """Test that OWL-RL reasoner can run inference on a DatasetView. 

14 

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() 

22 

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)) 

27 

28 # Graph 2: Instance data 

29 g2 = ds.graph(EX.graph2) 

30 g2.add((EX.fido, RDF.type, EX.Dog)) 

31 

32 assert len(g1) == 2 

33 assert len(g2) == 1 

34 

35 # Create a DatasetView with both graphs 

36 view = DatasetView(ds, [EX.graph1, EX.graph2]) 

37 

38 # Run OWL-RL inference on the view 

39 g_inferences = ds.graph(EX.inferences) 

40 DeductiveClosure(OWLRL_Semantics).expand(view, g_inferences) 

41 

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 ) 

49 

50 # Verify we got a reasonable number of inferences (should be > 1) 

51 assert len(g_inferences) > 1, "Expected multiple inferences from OWL-RL" 

52 

53 

54def test_owlrl_with_empty_datasetview() -> None: 

55 """Test that OWL-RL reasoner handles empty DatasetView correctly.""" 

56 ds = Dataset() 

57 

58 # Create an empty DatasetView 

59 view = DatasetView(ds, []) 

60 

61 # Run OWL-RL inference on empty view 

62 g_inferences = ds.graph(EX.inferences) 

63 DeductiveClosure(OWLRL_Semantics).expand(view, g_inferences) 

64 

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