|
| 1 | +"""Tests for timeout functionality in SyncReasoner methods.""" |
| 2 | +import os |
| 3 | +import unittest |
| 4 | + |
| 5 | +from owlapy import dl_to_owl_expression |
| 6 | +from owlapy.class_expression import OWLClass |
| 7 | +from owlapy.iri import IRI |
| 8 | +from owlapy.owl_axiom import OWLClassAssertionAxiom |
| 9 | +from owlapy.owl_individual import OWLNamedIndividual |
| 10 | +from owlapy.owl_ontology import SyncOntology |
| 11 | +from owlapy.owl_reasoner import SyncReasoner |
| 12 | + |
| 13 | + |
| 14 | +class TestSyncReasonerTimeout(unittest.TestCase): |
| 15 | + """Test timeout functionality in SyncReasoner.""" |
| 16 | + |
| 17 | + @classmethod |
| 18 | + def setUpClass(cls): |
| 19 | + """Set up test fixtures.""" |
| 20 | + cls.ontology_path = None |
| 21 | + for root, dirs, files in os.walk("."): |
| 22 | + for file in files: |
| 23 | + if file == "family-benchmark_rich_background.owl": |
| 24 | + cls.ontology_path = os.path.abspath(os.path.join(root, file)) |
| 25 | + break |
| 26 | + if cls.ontology_path: |
| 27 | + break |
| 28 | + |
| 29 | + if cls.ontology_path is None: |
| 30 | + raise FileNotFoundError("Could not locate 'family-benchmark_rich_background.owl' within project structure.") |
| 31 | + |
| 32 | + cls.namespace = "http://www.benchmark.org/family#" |
| 33 | + |
| 34 | + try: |
| 35 | + cls.ontology = SyncOntology(cls.ontology_path) |
| 36 | + cls.reasoner = SyncReasoner(cls.ontology, reasoner="Pellet") |
| 37 | + except Exception as e: |
| 38 | + raise RuntimeError(f"Failed to load ontology or initialize reasoner: {e}") |
| 39 | + |
| 40 | + def test_instances_with_short_timeout(self): |
| 41 | + """Test instances() method completes within reasonable timeout.""" |
| 42 | + ce = OWLClass(IRI.create(self.namespace + "Male")) |
| 43 | + |
| 44 | + # Should complete successfully with a reasonable timeout (10 seconds) |
| 45 | + individuals = set(self.reasoner.instances(ce, timeout=10)) |
| 46 | + |
| 47 | + self.assertIsInstance(individuals, set, "instances() should return a set") |
| 48 | + self.assertGreater(len(individuals), 0, "Should find Male individuals") |
| 49 | + |
| 50 | + def test_instances_with_complex_expression(self): |
| 51 | + """Test instances() with complex class expression and timeout.""" |
| 52 | + dl_expr = "∃ hasChild.Male" |
| 53 | + ce = dl_to_owl_expression(dl_expr, self.namespace) |
| 54 | + |
| 55 | + # Complex expression with reasonable timeout |
| 56 | + individuals = set(self.reasoner.instances(ce, timeout=15)) |
| 57 | + |
| 58 | + self.assertIsInstance(individuals, set) |
| 59 | + # We expect some individuals to satisfy this expression |
| 60 | + |
| 61 | + def test_create_axiom_justifications_default_timeout(self): |
| 62 | + """Test create_axiom_justifications with default timeout parameter.""" |
| 63 | + individual = OWLNamedIndividual(IRI.create(self.namespace + "F10M171")) |
| 64 | + dl_expr_str = "∃ hasChild.Male" |
| 65 | + target_class = dl_to_owl_expression(dl_expr_str, self.namespace) |
| 66 | + |
| 67 | + axiom = OWLClassAssertionAxiom(individual, target_class) |
| 68 | + |
| 69 | + # Should work with default timeout |
| 70 | + justifications = self.reasoner.create_axiom_justifications(axiom, save=False) |
| 71 | + |
| 72 | + self.assertIsInstance(justifications, list) |
| 73 | + self.assertGreater(len(justifications), 0, "Should find at least one justification") |
| 74 | + |
| 75 | + for justification in justifications: |
| 76 | + self.assertIsInstance(justification, set) |
| 77 | + self.assertGreater(len(justification), 0, "Each justification should contain axioms") |
| 78 | + |
| 79 | + def test_create_axiom_justifications_custom_timeout(self): |
| 80 | + """Test create_axiom_justifications with custom timeout.""" |
| 81 | + individual = OWLNamedIndividual(IRI.create(self.namespace + "F10M171")) |
| 82 | + dl_expr_str = "∃ hasChild.Male" |
| 83 | + target_class = dl_to_owl_expression(dl_expr_str, self.namespace) |
| 84 | + |
| 85 | + axiom = OWLClassAssertionAxiom(individual, target_class) |
| 86 | + |
| 87 | + # Test with custom timeout of 20 seconds |
| 88 | + justifications = self.reasoner.create_axiom_justifications( |
| 89 | + axiom, |
| 90 | + n_max_justifications=5, |
| 91 | + timeout=20, |
| 92 | + save=False |
| 93 | + ) |
| 94 | + |
| 95 | + self.assertIsInstance(justifications, list) |
| 96 | + # Should get at most 5 justifications |
| 97 | + self.assertLessEqual(len(justifications), 5) |
| 98 | + |
| 99 | + def test_instances_parameter_validation(self): |
| 100 | + """Test that instances method accepts timeout parameter correctly.""" |
| 101 | + ce = OWLClass(IRI.create(self.namespace + "Female")) |
| 102 | + |
| 103 | + # Test with various timeout values |
| 104 | + individuals_short = set(self.reasoner.instances(ce, timeout=5)) |
| 105 | + individuals_long = set(self.reasoner.instances(ce, timeout=30)) |
| 106 | + |
| 107 | + # Both should return the same results (just different timeouts) |
| 108 | + self.assertEqual(individuals_short, individuals_long, |
| 109 | + "Different timeouts should return same results for simple queries") |
| 110 | + |
| 111 | + def test_instances_direct_parameter_with_timeout(self): |
| 112 | + """Test instances method with both direct and timeout parameters.""" |
| 113 | + ce = OWLClass(IRI.create(self.namespace + "Person")) |
| 114 | + |
| 115 | + # Test with direct=False and timeout |
| 116 | + all_instances = set(self.reasoner.instances(ce, direct=False, timeout=10)) |
| 117 | + |
| 118 | + # Test with direct=True and timeout |
| 119 | + direct_instances = set(self.reasoner.instances(ce, direct=True, timeout=10)) |
| 120 | + |
| 121 | + self.assertIsInstance(all_instances, set) |
| 122 | + self.assertIsInstance(direct_instances, set) |
| 123 | + # Direct instances should be a subset of all instances |
| 124 | + self.assertTrue(direct_instances.issubset(all_instances) or len(direct_instances) == 0) |
| 125 | + |
| 126 | + @classmethod |
| 127 | + def tearDownClass(cls): |
| 128 | + """Clean up resources.""" |
| 129 | + if hasattr(cls, 'reasoner'): |
| 130 | + try: |
| 131 | + from owlapy.static_funcs import stopJVM |
| 132 | + stopJVM() |
| 133 | + except Exception: |
| 134 | + pass # JVM might already be stopped |
| 135 | + |
| 136 | + |
| 137 | +if __name__ == '__main__': |
| 138 | + unittest.main() |
0 commit comments