dice-group / owlapy

OWLAPY is a Python Framework for creating and manipulating OWL Ontologies.
MIT License
16 stars 1 forks source link

SPARQL Query Generation for OWLDataSomeValuesFrom with OWLDataOneOf filler #54

Closed Demirrr closed 2 months ago

Demirrr commented 2 months ago
from owlapy.class_expression import OWLDataOneOf
from owlapy.owl_property import OWLDataProperty
from owlapy.owl_literal import OWLLiteral
from owlapy import owl_expression_to_sparql
from owlapy.class_expression import OWLDataSomeValuesFrom
from owlapy.iri import IRI

x = OWLDataSomeValuesFrom(property=OWLDataProperty(IRI('https://www.gennarovessio.com/artgraph-schema#', 'date')),
                          filler=OWLDataOneOf((OWLLiteral("1885 ; Nunen \/ Nuenen , Netherlands  "))))

print(owl_expression_to_dl(x)) # ∃ date.{1885 ; Nunen \/ Nuenen , Netherlands  }
print(owl_expression_to_sparql(expression=x))

Here is the traceback

Traceback (most recent call last):
  File "/home/cdemir/Desktop/Softwares/owlapy/example.py", line 12, in <module>
    print(owl_expression_to_sparql(expression=x))
  File "/home/cdemir/Desktop/Softwares/owlapy/owlapy/converter.py", line 657, in owl_expression_to_sparql
    return converter.as_query(root_variable, expression, count=False, values=values,
  File "/home/cdemir/Desktop/Softwares/owlapy/owlapy/converter.py", line 634, in as_query
    parseQuery(query)
  File "/home/cdemir/anaconda3/envs/temp_owlapy/lib/python3.10/site-packages/rdflib/plugins/sparql/parser.py", line 1542, in parseQuery
    return Query.parseString(q, parseAll=True)
  File "/home/cdemir/anaconda3/envs/temp_owlapy/lib/python3.10/site-packages/pyparsing/util.py", line 256, in _inner
    return fn(self, *args, **kwargs)
  File "/home/cdemir/anaconda3/envs/temp_owlapy/lib/python3.10/site-packages/pyparsing/core.py", line 1200, in parse_string
    raise exc.with_traceback(None)
pyparsing.exceptions.ParseException: Expected SelectQuery, found 'FILTER'  (at char 94), (line:4, col:2)

owlapy 1.1.1

Demirrr commented 2 months ago

@nkaralis and @alkidbaci any idea ?

nkaralis commented 2 months ago

Can you please post the query that is passed to the parseQuery function?

Demirrr commented 2 months ago

parseQuery takes the following as input

∃ date.{1885 ; Nunen \/ Nuenen , Netherlands  }
SELECT
 DISTINCT ?x WHERE { 
?x <https://www.gennarovessio.com/artgraph-schema#date> ?s_1 . 
 FILTER ( ?s_1 IN ( 
"1885 ; Nunen \/ Nuenen , Netherlands  "^^<http://www.w3.org/2001/XMLSchema#string>
 ) ) 
 }
nkaralis commented 2 months ago

Is ∃ date.{1885 ; Nunen \/ Nuenen , Netherlands } part of the query?

Also, FILTER ( ?s_1 IN ( "1885 ; Nunen \/ Nuenen , Netherlands "^^<http://www.w3.org/2001/XMLSchema#string> ) ) is problematic. "1885 ; Nunen \/ Nuenen , Netherlands "^^<http://www.w3.org/2001/XMLSchema#string> is this supposed to be three different values? \/ is not a valid combination of characters.

Demirrr commented 2 months ago

Is ∃ date.{1885 ; Nunen \/ Nuenen , Netherlands } part of the query?

yes

1885 ; Nunen \/ Nuenen , Netherlands represents a single object in a triple that is a literal.

Below, there are examples of such literals. Please take a look at

  1. artgraph:date "1885 ; Nunen \\/ Nuenen , Netherlands " ;
  2. artgraph:date "1890 ; Auvers-sur-oise , France " ;
  3. artgraph:date "1886 ; Paris , France " ;
<https://www.gennarovessio.com/artgraph-resources#21582>
    a artgraph:Artwork ;
    artgraph:about <https://www.gennarovessio.com/artgraph-resources#2746>, <https://www.gennarovessio.com/artgraph-resources#2893> ;
    artgraph:createdBy <https://www.gennarovessio.com/artgraph-resources#1099> ;
    artgraph:date "1885 ; Nunen \\/ Nuenen , Netherlands  " ;
    artgraph:hasGenre <https://www.gennarovessio.com/artgraph-resources#9> ;
    artgraph:hasStyle <https://www.gennarovessio.com/artgraph-resources#43> ;
    artgraph:image_url "https://uploads8.wikiart.org/images/vincent-van-gogh/gordina-de-groot-head-1885(1).jpg" ;
    artgraph:madeOf <https://www.gennarovessio.com/artgraph-resources#2563>, <https://www.gennarovessio.com/artgraph-resources#2567> ;
    artgraph:name "vincent-van-gogh_gordina-de-groot-head-1885.jpg" ;
    artgraph:title "Gordina de Groot, Head" .

<https://www.gennarovessio.com/artgraph-resources#21580>
    a artgraph:Artwork ;
    artgraph:about <https://www.gennarovessio.com/artgraph-resources#2746>, <https://www.gennarovessio.com/artgraph-resources#2893> ;
    artgraph:createdBy <https://www.gennarovessio.com/artgraph-resources#1099> ;
    artgraph:date "1890 ; Auvers-sur-oise , France  " ;
    artgraph:hasGenre <https://www.gennarovessio.com/artgraph-resources#14> ;
    artgraph:hasStyle <https://www.gennarovessio.com/artgraph-resources#39> ;
    artgraph:image_url "https://uploads0.wikiart.org/images/vincent-van-gogh/girl-with-straw-hat-sitting-in-the-wheat-1890(1).jpg" ;
    artgraph:locatedIn <https://www.gennarovessio.com/artgraph-resources#134348>, <https://www.gennarovessio.com/artgraph-resources#134879>, <https://www.gennarovessio.com/artgraph-resources#135043> ;
    artgraph:name "vincent-van-gogh_girl-with-straw-hat-sitting-in-the-wheat-1890.jpg" ;
    artgraph:title "Girl with Straw Hat, Sitting in the Wheat" .

<https://www.gennarovessio.com/artgraph-resources#21581>
    a artgraph:Artwork ;
    artgraph:about <https://www.gennarovessio.com/artgraph-resources#2723>, <https://www.gennarovessio.com/artgraph-resources#2769> ;
    artgraph:createdBy <https://www.gennarovessio.com/artgraph-resources#1099> ;
    artgraph:date "1886 ; Paris , France  " ;
    artgraph:hasGenre <https://www.gennarovessio.com/artgraph-resources#10> ;
    artgraph:hasStyle <https://www.gennarovessio.com/artgraph-resources#43> ;
    artgraph:image_url "https://uploads3.wikiart.org/images/vincent-van-gogh/glass-with-roses-1886(1).jpg" ;
    artgraph:locatedIn <https://www.gennarovessio.com/artgraph-resources#134348>, <https://www.gennarovessio.com/artgraph-resources#134879>, <https://www.gennarovessio.com/artgraph-resources#135043> ;
    artgraph:madeOf <https://www.gennarovessio.com/artgraph-resources#2551>, <https://www.gennarovessio.com/artgraph-resources#2552> ;
    artgraph:name "vincent-van-gogh_glass-with-roses-1886.jpg" ;
    artgraph:title "Glass with Roses" .
Demirrr commented 2 months ago

We need to use repr().

from owlapy.class_expression import OWLDataOneOf
from owlapy.owl_property import OWLDataProperty
from owlapy.owl_literal import OWLLiteral
from owlapy import owl_expression_to_sparql
from owlapy.class_expression import OWLDataSomeValuesFrom

p={'type': 'uri', 'value': 'https://www.gennarovessio.com/artgraph-schema#date'}
o={'type': 'literal', 'value': "1885 ; Nunen \\/ Nuenen , Netherlands  ", 'datatype': 'http://www.w3.org/2001/XMLSchema#string'}
data_prob=OWLDataProperty(p["value"])
x = OWLDataSomeValuesFrom(property=OWLDataProperty(p["value"]),
                          filler=OWLDataOneOf(OWLLiteral(value=repr(o["value"]))))
print(owl_expression_to_sparql(expression=x))

x = OWLDataSomeValuesFrom(property=OWLDataProperty(p["value"]),
                          filler=OWLDataOneOf(OWLLiteral(value=o["value"])))
print(owl_expression_to_sparql(expression=x))

Do you have any suggestion in covering these cases without asking user to use repr() ? @nkaralis @alkidbaci ?

Demirrr commented 2 months ago

This issue is tackled in ontolearn!

alkidbaci commented 2 months ago

We can update the rendering of the OWLLiteral as shown below:

149    # owlapy/converter.py
150    @render.register
151    def _(self, lit: OWLLiteral):
152        if lit.is_string():
153            return f'{repr(lit.get_literal())}^^<{lit.get_datatype().to_string_id()}>'
154        return f'"{lit.get_literal()}"^^<{lit.get_datatype().to_string_id()}>'

but the problem is that the query will look like this as it does in Demir's code also:

SELECT
  DISTINCT ?x WHERE { 
  ?x <https://www.gennarovessio.com/artgraph-schema#date> ?s_1 . 
   FILTER ( ?s_1 IN ( 
  '1885 ; Nunen \\/ Nuenen , Netherlands  '^^<http://www.w3.org/2001/XMLSchema#string>
 ) ) 
 }

if you notice there is a double backslash \\/ and not \/ as the string is supposed to look like. Im not sure if SPARQL need such escaping characters right?

Demirrr commented 2 months ago

I have suggested this workaround in the ontolearn