Prepare SPARQL: Variable ?v cannot be both datas and objs!

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Prepare SPARQL: Variable ?v cannot be both datas and objs!

franzlst
I want to use owlready2's new feature to run SPARQL queries as SQL queries on a sqlite database. When running an arguably relatively simple query, I'm running into an exception.

This is the command I use:
my_world.prepare_sparql(query)

where query is the following:

SELECT DISTINCT ?v ?c   
WHERE {
    <https://my.ontology.com#some_iri> <http://www.w3.org/2000/01/rdf-schema#label> ?v .  
    OPTIONAL { ?v <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?c }
}

This is the exception I get:

ValueError: Variable ?v cannot be both datas and objs!
Traceback (most recent call last):
  File "venv/lib/python3.6/site-packages/owlready2_protocol/reader.py", line 94, in run_query
    rdf_prepared_query = RDFPreparedQuery(self.graph.world, query)
  File "venv/lib/python3.6/site-packages/owlready2_protocol/reader.py", line 115, in __init__
    sq = world.prepare_sparql(query, error_on_undefined_entities=False)
  File "venv/lib/python3.6/site-packages/owlready2/namespace.py", line 525, in prepare_sparql
    return self._prepare_sparql(sparql, error_on_undefined_entities)
  File "venv/lib/python3.6/site-packages/owlready2/namespace.py", line 522, in _prepare_sparql
    return owlready2.sparql.main.Translator(self, error_on_undefined_entities).parse(sparql)
  File "venv/lib/python3.6/site-packages/owlready2/sparql/main.py", line 61, in parse
    self.main_query = PARSER.parse(LEXER.lex(sparql))
  File "venv/lib/python3.6/site-packages/owlready2/rply.py", line 417, in parse
    t, symstack, statestack, state
  File "venv/lib/python3.6/site-packages/owlready2/rply.py", line 465, in _reduce_production
    if state is None: value = p.func(targ)
  File "venv/lib/python3.6/site-packages/owlready2/sparql/parser.py", line 133, in f
    def f(p): return _parse_select_query(p[1])
  File "venv/lib/python3.6/site-packages/owlready2/sparql/parser.py", line 140, in _parse_select_query
    main_query = translator.new_sql_query("main", p[4], p[2], p[1], p[5])
  File "venv/lib/python3.6/site-packages/owlready2/sparql/main.py", line 264, in new_sql_query
    s.parse_triples(block)
  File "venv/lib/python3.6/site-packages/owlready2/sparql/main.py", line 764, in parse_triples
    var.update_type("objs")
  File "venv/lib/python3.6/site-packages/owlready2/sparql/main.py", line 590, in update_type
    raise ValueError("Variable %s cannot be both %s and %s!" % (self.name, self.type, type))
ValueError: Variable ?v cannot be both datas and objs! 

I'm using v0.35 of Owlready2.

Any ideas?
Reply | Threaded
Open this post in threaded view
|

Re: Prepare SPARQL: Variable ?v cannot be both datas and objs!

franzlst
While digging into this issue, I found out that this exception always occurs when the property is an annotation property.
Reply | Threaded
Open this post in threaded view
|

Re: Prepare SPARQL: Variable ?v cannot be both datas and objs!

Jiba
Administrator
In reply to this post by franzlst
Hi,

The problem is that Owlready assume that RDFS label and comment are data, for increasing performances. However, in your example, it seems that the label is/may be an entity.

You can prevent this assumption by adding the following code before performing the SPARQL query:

import owlready2.sparql.parser
owlready2.sparql.parser._DATA_PROPS = set()

I also fixed a bug in the development version of Owlready that you will probably encounter: Owlready tries to guess variable type (entity/object or data), but OPTIONAL clauses were considered for guessing, while they should not.

Jiba
Reply | Threaded
Open this post in threaded view
|

Re: Prepare SPARQL: Variable ?v cannot be both datas and objs!

franzlst
Thanks a lot for the quick fix, this helps me a lot! Now I only wait for the release to make use of it ;-)

At least in my case, the assumption that label/comment are data properties holds true, the OPTIONAL part of the query is in there, as this is a general query structure for all properties as generated by SuRF (as mentioned here).

Can I add further properties to _DATA_PROPS if I know they are data properties? Would this improve the speed?
Reply | Threaded
Open this post in threaded view
|

Re: Prepare SPARQL: Variable ?v cannot be both datas and objs!

Jiba
Administrator
Hi,

franzlst wrote
Can I add further properties to _DATA_PROPS if I know they are data properties? Would this improve the speed?
You can, but this will have an effect only for annotation properties (data properties are already assumed as data, but annotation can be both). For annotation, it will increase peformances

Jiba