Importing rdf graph from rdflib

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

Importing rdf graph from rdflib

Stefan
Hi,

I'd like to use rdflib to run SPARQL Update queries on my ontology, similar to regular SPARQL queries. However, it is my understanding that my SPARQL Update queries would only run on the exported copy of my ontology, and not the original.
So, apart from saving my rdf graph using rdflib, and the loading the ontology again from that file with owlready2, is there a good way to get my changes back to owlready?
Reply | Threaded
Open this post in threaded view
|

Re: Importing rdf graph from rdflib

Stefan
Sadly, it appears SPARQL Update queries are not supported yet, similar to prefixes recently.
Running a update query using rdflib.graph.Graph.update produces the following error:

AttributeError: 'Graph' object has no attribute '_add_obj_triple_raw_spo'

Would it be possible to implement the necessary methods for SPARQL Update support?
Reply | Threaded
Open this post in threaded view
|

Re: Importing rdf graph from rdflib

Jiba
Administrator
Hi,

Can you give me a simple example of an update query ? add() and remove() methods are present in RDFlib support, so it should be working... I need to investigate this.

Jiba
Reply | Threaded
Open this post in threaded view
|

Re: Importing rdf graph from rdflib

Stefan
Hi,

here's a simple example about closing a door.

Ontology:
<?xml version="1.0"?>
<rdf:RDF xmlns="http://www.example.org#"
     xml:base="http://www.example.org"
     xmlns:owl="http://www.w3.org/2002/07/owl#"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     xmlns:www="http://www.example.org#"
     xmlns:xml="http://www.w3.org/XML/1998/namespace"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
    <owl:Ontology rdf:about="http://www.example.org"/>
   


   

   


   

    <owl:ObjectProperty rdf:about="http://www.example.org#hasStatus"/>
   


   

   


   

    <owl:Class rdf:about="http://www.example.org#Door"/>
   


   

    <owl:Class rdf:about="http://www.example.org#Status"/>
   


   

   


   

    <owl:NamedIndividual rdf:about="http://www.example.org#Closed">
        <rdf:type rdf:resource="http://www.example.org#Status"/>
    </owl:NamedIndividual>
   


   

    <owl:NamedIndividual rdf:about="http://www.example.org#Door1">
        <rdf:type rdf:resource="http://www.example.org#Door"/>
        <hasStatus rdf:resource="http://www.example.org#Open"/>
    </owl:NamedIndividual>
   


   

    <owl:NamedIndividual rdf:about="http://www.example.org#Open">
        <rdf:type rdf:resource="http://www.example.org#Status"/>
    </owl:NamedIndividual>
</rdf:RDF>






Code:
import owlready2 as or2

o = or2.get_ontology("file://example.owl").load()

rdf_graph = or2.default_world.as_rdflib_graph()
rdf_graph.bind("ontology", "http://www.example.org#")
rdf_graph.bind("w3", "http://www.w3.org/1999/02/22-rdf-syntax-ns#")

simple_query = """
SELECT ?door WHERE {
    ?door w3:type ontology:Door .
    ?door ontology:hasStatus ontology:Open .
}
"""

results = rdf_graph.query(simple_query)

for result in results:
    print(result)

update_query = """
DELETE {
    ?door ontology:hasStatus ontology:Open .
} INSERT {
    ?door ontology:hasStatus ontology:Closed .
} WHERE {
    ?door w3:type ontology:Door .
    ?door ontology:hasStatus ontology:Open .
}
"""

rdf_graph.update(update_query)

rdf_graph.serialize("example2.owl")


It should fail in rdf_graph.update(update_query)





Reply | Threaded
Open this post in threaded view
|

Re: Importing rdf graph from rdflib

Jiba
Administrator
Hi,

The error is caused by the fact that you cannot add a triple without specifying in which ontology it is added.

I've added in the development version of Owlready several methods for that (choose the one you prefer):

1) use a 'with' block as in Owlready:

with o:
       rdf_graph.update(update_query)

2) use a context RDFlib graph (you can get the context graph either with the ontology or its IRI):

context_graph = rdf_graph.get_context(o) # OR
context_graph = rdf_graph.get_context(URIRef("http://www.example.org#"))

context_graph.update(update_query)


Best regards,
Jiba