Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

owl2sparql conversion #5

Merged
merged 2 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ ones and finally render and print the last of them in description logics syntax.
from owlapy.render import DLSyntaxObjectRenderer
from owlapy.model import IRI, OWLClass, OWLObjectProperty, OWLObjectSomeValuesFrom, \
OWLObjectIntersectionOf
from owlapy.owl2sparql.converter import owl_expression_to_sparql

# Create an IRI object using the iri as a string for 'male' class.
male_iri = IRI.create('http://example.com/society#male')
Expand All @@ -44,7 +45,7 @@ male_teachers_with_children = OWLObjectIntersectionOf([males_with_children, teac
# You can render and print owl class expressions in description logics syntax
print(DLSyntaxObjectRenderer().render(male_teachers_with_children))
# (∃ hasChild.male) ⊓ teacher
print(Owl2SparqlConverter().as_query("?x", male_teachers_with_children))
print(owl_expression_to_sparql("?x", male_teachers_with_children))
# SELECT DISTINCT ?x WHERE { ?x <http://example.com/society#hasChild> ?s_1 . ?s_1 a <http://example.com/society#male> . ?x a <http://example.com/society#teacher> . } }
```
For more, you can check the [API documentation](https://ontolearn-docs-dice-group.netlify.app/autoapi/owlapy/#module-owlapy).
Expand Down
13 changes: 12 additions & 1 deletion owlapy/owl2sparql/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ def _(self, ce: OWLObjectOneOf):
self.append(",")
assert isinstance(ind, OWLNamedIndividual)
self.append(f"<{ind.to_string_id()}>")
self.append(f" )")
self.append(f" ) )")

@process.register
def _(self, ce: OWLDataSomeValuesFrom):
Expand Down Expand Up @@ -626,3 +626,14 @@ def as_query(self,
query = "\n".join(qs)
parseQuery(query)
return query


converter = Owl2SparqlConverter()


def owl_expression_to_sparql(root_variable: str,
ce: OWLClassExpression,
count: bool = False,
values: Optional[Iterable[OWLNamedIndividual]] = None,
named_individuals: bool = False):
return converter.as_query(root_variable, ce, count, values, named_individuals)