Error in "insert query with parameter"

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

Error in "insert query with parameter"

bernard.lhostis
Hello,
I am using owlready2 for:
1-Read a source ontology
2-If individuals have a particular annotation
3- I create a class with the same uri in the target ontology

I use a sparql query for find individuals and  an insert query with parameter
owlready2 gives me this error.  



python-BaseException
Traceback (most recent call last):
  File "/home/bernard/.local/lib/python3.7/site-packages/rdflib/plugin.py", line 100, in get
    p = _plugins[(name, kind)]
TypeError: unhashable type: 'list'

Best Regards,
Bernard
PS1 :
    ontoSource = default_world.get_ontology(args.source).load()

    worldCible = World()
    ontoCible = worldCible.get_ontology(args.cible).load()

    graph = default_world.as_rdflib_graph()
    graph2 = worldCible.as_rdflib_graph()
    # http://www.semanticweb.org/CyberOntology#isConcept
    extract = list(graph.query("""
        PREFIX cyber:<http://www.semanticweb.org/CyberOntology#>
        SELECT ?concept ?annotation WHERE {
            ?concept cyber:isConcept ?annotation .
        }"""))

    for i in extract:
        URIconcept = str(i[0])
        if annotation == "True":
            graph2.update("""
                PREFIX owl: <http://www.w3.org/2002/07/owl#>
                PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
                INSERT {??1 a owl:Class.
                        ??1 rdfs:subClassOf owl:Thing}
                WHERE {}
                """, [URIconcept])
Reply | Threaded
Open this post in threaded view
|

Re: Error in "insert query with parameter"

Jiba
Administrator
Hello,

The error you obtain is located in rdflib; I have limited knowledge in rdflib for solving it.

I suggest to use the new native SPARQL engine included in Owlready, as follows below. Notice the use of world_cible._abbreviate(), because Owlready uses integer internally for identifying entities, and not directly IRI.

Jiba

extract = list(default_world.sparql("""
PREFIX cyber:<http://www.semanticweb.org/CyberOntology#>
SELECT ?concept ?annotation WHERE {
?concept cyber:isConcept ?annotation .
}"""
))

world_cible = World()
onto_cible = world_cible.get_ontology("http://www.semanticweb.org/new_onto#")

with onto_cible:
  for concept, annotation in extract:
    URIconcept = concept.iri
    if annotation == "True": # Use True without "" if the value is a Boolean
      world_cible.sparql("""
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
INSERT {
  ??1 a owl:Class .
  ??1 rdfs:subClassOf owl:Thing .
}
WHERE {}
""", [world_cible._abbreviate(URIconcept)])

world_cible.graph.dump()