Create dinamically SPARQL query and work with the result as owlready object

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

Create dinamically SPARQL query and work with the result as owlready object

Hamico
This post was updated on .
Hi guys,
I have a question that troubles me. I want to have an element in input (object) to the SPARQL query so that I have as result a subject if I put the predicate static and the object dinamically.

Query = ’’’PREFIX...SELECT ?owner WHERE {?owner :has_bought :’’’ +name_car+”}”

Its result with reasoner is an empty field. Why? Can you write a simple example of dinamical creation of SPARQL query?

Then I want to have an owlready object from the result of SPARQL query. I have seen the method .query_owlready() on the answer of another question:

http://owlready.8326.n8.nabble.com/No-results-when-running-the-same-SPARQL-query-in-Owlready-compared-to-Protege-td528.html#a546

How does this method work? Can you write an example of the method .query_owlready()?

Thanks to all for the support. I hope my question to have a solution and to be useful for others who have the same problems.
I wish a good weekend to all.


Best regards,
Your friend Hamico
Reply | Threaded
Open this post in threaded view
|

Re: Create dinamically SPARQL query and work with the result as owlready object

Jiba
Administrator
Hi,

you can use the IRI of entities in SPARQL queries. IRI must be put inside <> in SPARQL, for example :

Query = ’’’PREFIX...SELECT ?owner WHERE {?owner :has_bought <’’’ +car.iri +”>.}”

where car is an Owlready individual.
Then you can execute the query:

results = default_world.as_rdflib_graph().query_owlready(Query)

results is a generator yielding the results of the query (here individuals). You can make a for loop over results, of transform it in a list as follows:

results = list(results)
print(results)

Best regards,
Jiba
Reply | Threaded
Open this post in threaded view
|

Re: Create dinamically SPARQL query and work with the result as owlready object

Hamico
This post was updated on .
Hi Jiba,
Can you tell me how to implement the results of a SPARQL query as input for another SPARQL query?

Many thanks.
Best regards,
Hamico
Reply | Threaded
Open this post in threaded view
|

Re: Create dinamically SPARQL query and work with the result as owlready object

Jiba
Administrator
Hi,

If you use default_world.as_rdflib_graph().query_owlready(), it returns the results as Python datatypes and/or references to Owlready objets. You can use the Owlready objects in future SPARQL query by converting thme to IRI and adding "<" and ">" at the extremities, for example "<%s>" % result.iri

Datatype values can directly be included in the request for most datatypes (strings, numbers,...).

Jiba