diff --git a/owlapy/converter.py b/owlapy/converter.py index 75979c9..8a8a2f8 100644 --- a/owlapy/converter.py +++ b/owlapy/converter.py @@ -624,10 +624,10 @@ def as_query(self, q = [f"VALUES {root_variable} {{ "] for x in values: q.append(f"<{x.to_string_id()}>") - q.append("}} . ") + q.append("} . ") qs.extend(q) qs.extend(tp) - qs.append(" }}") + qs.append(" }") query = "\n".join(qs) diff --git a/tests/test_owlapy_owl2sparql_converter.py b/tests/test_owlapy_owl2sparql_converter.py index 398df23..b240cb7 100644 --- a/tests/test_owlapy_owl2sparql_converter.py +++ b/tests/test_owlapy_owl2sparql_converter.py @@ -8,14 +8,14 @@ from owlapy.iri import IRI from owlapy.owl_property import OWLObjectProperty -# from owlapy.owl_ontology_manager import OntologyManager -# from owlapy.owl_reasoner import OntologyReasoner, FastInstanceCheckerReasoner -# from owlapy.parser import DLSyntaxParser -# from rdflib import Graph +from owlapy.owl_ontology_manager import OntologyManager +from owlapy.owl_reasoner import OntologyReasoner, FastInstanceCheckerReasoner +from owlapy.parser import DLSyntaxParser +from rdflib import Graph from owlapy.converter import Owl2SparqlConverter -PATH_FAMILY = '../KGs/Family/family-benchmark_rich_background.owl' +PATH_FAMILY = 'KGs/Family/family-benchmark_rich_background.owl' # checks whether all individuals returned by the reasoner are found in results generated by the sparql query @@ -93,164 +93,164 @@ def test_as_query(self): # )""" self.assertEqual(query, query_t) # add assertion here - # def test_Single(self): - # # rdf graph - using rdflib - # family_rdf_graph = Graph() - # family_rdf_graph.parse(location=PATH_FAMILY) - # # knowledge base - using OWLReasoner - # mgr = OntologyManager() - # onto = mgr.load_ontology(IRI.create(PATH_FAMILY)) - # base_reasoner = OntologyReasoner(onto) - # family_kb_reasoner = FastInstanceCheckerReasoner(onto, base_reasoner=base_reasoner, negation_default=True) - # - # ce_str = "Brother" - # ce_parsed = DLSyntaxParser(namespace="http://www.benchmark.org/family#").parse_expression(expression_str=ce_str) - # actual_query = Owl2SparqlConverter().as_query(root_variable=self._root_var_, ce=ce_parsed, count=False, - # values=None, named_individuals=True) - # expected_query = """SELECT DISTINCT ?x - # WHERE { - # ?x . - # }""" - # - # sparql_results_actual = family_rdf_graph.query(actual_query) - # sparql_results_expected = family_rdf_graph.query(expected_query) - # reasoner_results = set(family_kb_reasoner.instances(ce_parsed)) - # - # self.assertEqual(len(sparql_results_actual), len(sparql_results_expected)) - # self.assertEqual(len(sparql_results_actual), len(reasoner_results)) - # self.assertTrue(check_reasoner_instances_in_sparql_results(sparql_results_actual, reasoner_results)) - # - # ce_str = "Male" - # ce_parsed = DLSyntaxParser(namespace="http://www.benchmark.org/family#").parse_expression(expression_str=ce_str) - # actual_query = Owl2SparqlConverter().as_query(root_variable=self._root_var_, ce=ce_parsed, count=False, - # values=None, named_individuals=True) - # expected_query = """SELECT DISTINCT ?x - # WHERE { - # ?x . - # }""" - # - # sparql_results_actual = family_rdf_graph.query(actual_query) - # sparql_results_expected = family_rdf_graph.query(expected_query) - # reasoner_results = set(family_kb_reasoner.instances(ce_parsed)) - # - # self.assertEqual(len(sparql_results_actual), len(sparql_results_expected)) - # self.assertEqual(len(sparql_results_actual), len(reasoner_results)) - # self.assertTrue(check_reasoner_instances_in_sparql_results(sparql_results_actual, reasoner_results)) - # - # def test_Intersection(self): - # # rdf graph - using rdflib - # family_rdf_graph = Graph() - # family_rdf_graph.parse(location=PATH_FAMILY) - # # knowledge base - using OWLReasoner - # mgr = OntologyManager() - # onto = mgr.load_ontology(IRI.create(PATH_FAMILY)) - # base_reasoner = OntologyReasoner(onto) - # family_kb_reasoner = FastInstanceCheckerReasoner(onto, base_reasoner=base_reasoner, negation_default=True) - # - # ce_str = "Brother ⊓ Father" - # ce_parsed = DLSyntaxParser(namespace="http://www.benchmark.org/family#").parse_expression(expression_str=ce_str) - # actual_query = Owl2SparqlConverter().as_query(root_variable=self._root_var_, ce=ce_parsed, count=False, - # values=None, named_individuals=True) - # expected_query = """SELECT DISTINCT ?x - # WHERE { - # ?x . - # ?x . - # }""" - # - # sparql_results_actual = family_rdf_graph.query(actual_query) - # sparql_results_expected = family_rdf_graph.query(expected_query) - # reasoner_results = set(family_kb_reasoner.instances(ce_parsed)) - # - # self.assertEqual(len(sparql_results_actual), len(sparql_results_expected)) - # self.assertEqual(len(sparql_results_actual), len(reasoner_results)) - # self.assertTrue(check_reasoner_instances_in_sparql_results(sparql_results_actual, reasoner_results)) - # - # def test_Union(self): - # # rdf graph - using rdflib - # family_rdf_graph = Graph() - # family_rdf_graph.parse(location=PATH_FAMILY) - # # knowledge base - using OWLReasoner - # mgr = OntologyManager() - # onto = mgr.load_ontology(IRI.create(PATH_FAMILY)) - # base_reasoner = OntologyReasoner(onto) - # family_kb_reasoner = FastInstanceCheckerReasoner(onto, base_reasoner=base_reasoner, negation_default=True) - # - # ce_str = "Sister ⊔ Mother" - # ce_parsed = DLSyntaxParser(namespace="http://www.benchmark.org/family#").parse_expression(expression_str=ce_str) - # actual_query = Owl2SparqlConverter().as_query(root_variable=self._root_var_, ce=ce_parsed, count=False, - # values=None, named_individuals=True) - # expected_query = """SELECT DISTINCT ?x - # WHERE { - # { ?x . } - # UNION - # { ?x . } - # }""" - # - # sparql_results_actual = family_rdf_graph.query(actual_query) - # sparql_results_expected = family_rdf_graph.query(expected_query) - # reasoner_results = set(family_kb_reasoner.instances(ce_parsed)) - # - # self.assertEqual(len(sparql_results_actual), len(sparql_results_expected)) - # self.assertEqual(len(sparql_results_actual), len(reasoner_results)) - # self.assertTrue(check_reasoner_instances_in_sparql_results(sparql_results_actual, reasoner_results)) - # - # def test_Complement(self): - # # rdf graph - using rdflib - # family_rdf_graph = Graph() - # family_rdf_graph.parse(location=PATH_FAMILY) - # # knowledge base - using OWLReasoner - # mgr = OntologyManager() - # onto = mgr.load_ontology(IRI.create(PATH_FAMILY)) - # base_reasoner = OntologyReasoner(onto) - # family_kb_reasoner = FastInstanceCheckerReasoner(onto, base_reasoner=base_reasoner, negation_default=True) - # - # ce_str = "¬Mother" - # ce_parsed = DLSyntaxParser(namespace="http://www.benchmark.org/family#").parse_expression(expression_str=ce_str) - # actual_query = Owl2SparqlConverter().as_query(root_variable=self._root_var_, ce=ce_parsed, count=False, - # values=None, named_individuals=True) - # expected_query = """SELECT DISTINCT ?x - # WHERE { - # ?x a . - # ?x ?p ?o . - # FILTER NOT EXISTS { ?x a . } - # }""" - # - # sparql_results_actual = family_rdf_graph.query(actual_query) - # sparql_results_expected = family_rdf_graph.query(expected_query) - # reasoner_results = set(family_kb_reasoner.instances(ce_parsed)) - # - # self.assertEqual(len(sparql_results_actual), len(sparql_results_expected)) - # self.assertEqual(len(sparql_results_actual), len(reasoner_results)) - # self.assertTrue(check_reasoner_instances_in_sparql_results(sparql_results_actual, reasoner_results)) - # - # def test_Exists(self): - # # rdf graph - using rdflib - # family_rdf_graph = Graph() - # family_rdf_graph.parse(location=PATH_FAMILY) - # # knowledge base - using OWLReasoner - # mgr = OntologyManager() - # onto = mgr.load_ontology(IRI.create(PATH_FAMILY)) - # base_reasoner = OntologyReasoner(onto) - # family_kb_reasoner = FastInstanceCheckerReasoner(onto, base_reasoner=base_reasoner, negation_default=True) - # - # ce_str = "∃hasChild.Male" - # ce_parsed = DLSyntaxParser(namespace="http://www.benchmark.org/family#").parse_expression(expression_str=ce_str) - # actual_query = Owl2SparqlConverter().as_query(root_variable=self._root_var_, ce=ce_parsed, count=False, - # values=None, named_individuals=True) - # expected_query = """SELECT DISTINCT ?x - # WHERE { - # ?x ?s . - # ?s a . - # }""" - # - # sparql_results_actual = family_rdf_graph.query(actual_query) - # sparql_results_expected = family_rdf_graph.query(expected_query) - # reasoner_results = set(family_kb_reasoner.instances(ce_parsed)) - # - # self.assertEqual(len(sparql_results_actual), len(sparql_results_expected)) - # self.assertEqual(len(sparql_results_actual), len(reasoner_results)) - # self.assertTrue(check_reasoner_instances_in_sparql_results(sparql_results_actual, reasoner_results)) - # + def test_Single(self): + # rdf graph - using rdflib + family_rdf_graph = Graph() + family_rdf_graph.parse(location=PATH_FAMILY) + # knowledge base - using OWLReasoner + mgr = OntologyManager() + onto = mgr.load_ontology(IRI.create(PATH_FAMILY)) + base_reasoner = OntologyReasoner(onto) + family_kb_reasoner = FastInstanceCheckerReasoner(onto, base_reasoner=base_reasoner, negation_default=True) + + ce_str = "Brother" + ce_parsed = DLSyntaxParser(namespace="http://www.benchmark.org/family#").parse_expression(expression_str=ce_str) + actual_query = Owl2SparqlConverter().as_query(root_variable=self._root_var_, ce=ce_parsed, count=False, + values=None, named_individuals=True) + expected_query = """SELECT DISTINCT ?x + WHERE { + ?x . + }""" + + sparql_results_actual = family_rdf_graph.query(actual_query) + sparql_results_expected = family_rdf_graph.query(expected_query) + reasoner_results = set(family_kb_reasoner.instances(ce_parsed)) + + self.assertEqual(len(sparql_results_actual), len(sparql_results_expected)) + self.assertEqual(len(sparql_results_actual), len(reasoner_results)) + self.assertTrue(check_reasoner_instances_in_sparql_results(sparql_results_actual, reasoner_results)) + + ce_str = "Male" + ce_parsed = DLSyntaxParser(namespace="http://www.benchmark.org/family#").parse_expression(expression_str=ce_str) + actual_query = Owl2SparqlConverter().as_query(root_variable=self._root_var_, ce=ce_parsed, count=False, + values=None, named_individuals=True) + expected_query = """SELECT DISTINCT ?x + WHERE { + ?x . + }""" + + sparql_results_actual = family_rdf_graph.query(actual_query) + sparql_results_expected = family_rdf_graph.query(expected_query) + reasoner_results = set(family_kb_reasoner.instances(ce_parsed)) + + self.assertEqual(len(sparql_results_actual), len(sparql_results_expected)) + self.assertEqual(len(sparql_results_actual), len(reasoner_results)) + self.assertTrue(check_reasoner_instances_in_sparql_results(sparql_results_actual, reasoner_results)) + + def test_Intersection(self): + # rdf graph - using rdflib + family_rdf_graph = Graph() + family_rdf_graph.parse(location=PATH_FAMILY) + # knowledge base - using OWLReasoner + mgr = OntologyManager() + onto = mgr.load_ontology(IRI.create(PATH_FAMILY)) + base_reasoner = OntologyReasoner(onto) + family_kb_reasoner = FastInstanceCheckerReasoner(onto, base_reasoner=base_reasoner, negation_default=True) + + ce_str = "Brother ⊓ Father" + ce_parsed = DLSyntaxParser(namespace="http://www.benchmark.org/family#").parse_expression(expression_str=ce_str) + actual_query = Owl2SparqlConverter().as_query(root_variable=self._root_var_, ce=ce_parsed, count=False, + values=None, named_individuals=True) + expected_query = """SELECT DISTINCT ?x + WHERE { + ?x . + ?x . + }""" + + sparql_results_actual = family_rdf_graph.query(actual_query) + sparql_results_expected = family_rdf_graph.query(expected_query) + reasoner_results = set(family_kb_reasoner.instances(ce_parsed)) + + self.assertEqual(len(sparql_results_actual), len(sparql_results_expected)) + self.assertEqual(len(sparql_results_actual), len(reasoner_results)) + self.assertTrue(check_reasoner_instances_in_sparql_results(sparql_results_actual, reasoner_results)) + + def test_Union(self): + # rdf graph - using rdflib + family_rdf_graph = Graph() + family_rdf_graph.parse(location=PATH_FAMILY) + # knowledge base - using OWLReasoner + mgr = OntologyManager() + onto = mgr.load_ontology(IRI.create(PATH_FAMILY)) + base_reasoner = OntologyReasoner(onto) + family_kb_reasoner = FastInstanceCheckerReasoner(onto, base_reasoner=base_reasoner, negation_default=True) + + ce_str = "Sister ⊔ Mother" + ce_parsed = DLSyntaxParser(namespace="http://www.benchmark.org/family#").parse_expression(expression_str=ce_str) + actual_query = Owl2SparqlConverter().as_query(root_variable=self._root_var_, ce=ce_parsed, count=False, + values=None, named_individuals=True) + expected_query = """SELECT DISTINCT ?x + WHERE { + { ?x . } + UNION + { ?x . } + }""" + + sparql_results_actual = family_rdf_graph.query(actual_query) + sparql_results_expected = family_rdf_graph.query(expected_query) + reasoner_results = set(family_kb_reasoner.instances(ce_parsed)) + + self.assertEqual(len(sparql_results_actual), len(sparql_results_expected)) + self.assertEqual(len(sparql_results_actual), len(reasoner_results)) + self.assertTrue(check_reasoner_instances_in_sparql_results(sparql_results_actual, reasoner_results)) + + def test_Complement(self): + # rdf graph - using rdflib + family_rdf_graph = Graph() + family_rdf_graph.parse(location=PATH_FAMILY) + # knowledge base - using OWLReasoner + mgr = OntologyManager() + onto = mgr.load_ontology(IRI.create(PATH_FAMILY)) + base_reasoner = OntologyReasoner(onto) + family_kb_reasoner = FastInstanceCheckerReasoner(onto, base_reasoner=base_reasoner, negation_default=True) + + ce_str = "¬Mother" + ce_parsed = DLSyntaxParser(namespace="http://www.benchmark.org/family#").parse_expression(expression_str=ce_str) + actual_query = Owl2SparqlConverter().as_query(root_variable=self._root_var_, ce=ce_parsed, count=False, + values=None, named_individuals=True) + expected_query = """SELECT DISTINCT ?x + WHERE { + ?x a . + ?x ?p ?o . + FILTER NOT EXISTS { ?x a . } + }""" + + sparql_results_actual = family_rdf_graph.query(actual_query) + sparql_results_expected = family_rdf_graph.query(expected_query) + reasoner_results = set(family_kb_reasoner.instances(ce_parsed)) + + self.assertEqual(len(sparql_results_actual), len(sparql_results_expected)) + self.assertEqual(len(sparql_results_actual), len(reasoner_results)) + self.assertTrue(check_reasoner_instances_in_sparql_results(sparql_results_actual, reasoner_results)) + + def test_Exists(self): + # rdf graph - using rdflib + family_rdf_graph = Graph() + family_rdf_graph.parse(location=PATH_FAMILY) + # knowledge base - using OWLReasoner + mgr = OntologyManager() + onto = mgr.load_ontology(IRI.create(PATH_FAMILY)) + base_reasoner = OntologyReasoner(onto) + family_kb_reasoner = FastInstanceCheckerReasoner(onto, base_reasoner=base_reasoner, negation_default=True) + + ce_str = "∃hasChild.Male" + ce_parsed = DLSyntaxParser(namespace="http://www.benchmark.org/family#").parse_expression(expression_str=ce_str) + actual_query = Owl2SparqlConverter().as_query(root_variable=self._root_var_, ce=ce_parsed, count=False, + values=None, named_individuals=True) + expected_query = """SELECT DISTINCT ?x + WHERE { + ?x ?s . + ?s a . + }""" + + sparql_results_actual = family_rdf_graph.query(actual_query) + sparql_results_expected = family_rdf_graph.query(expected_query) + reasoner_results = set(family_kb_reasoner.instances(ce_parsed)) + + self.assertEqual(len(sparql_results_actual), len(sparql_results_expected)) + self.assertEqual(len(sparql_results_actual), len(reasoner_results)) + self.assertTrue(check_reasoner_instances_in_sparql_results(sparql_results_actual, reasoner_results)) + # def test_ForAll(self): # # rdf graph - using rdflib # family_rdf_graph = Graph()