Search code examples
sparqlrdfowlallegrographtopbraid-composer

Change PREFIX in SPARQL


I created an ontology with different prefixes (rdf:, rdfs:, owl:, example:, car:, bike:, ...). I use them to demarcate different domains and examples.

A small extract:

car:Software rdf:type demo:CyberObject.
car:Hardware rdf:type spdm:PhysicalObject.
car:Software car:hasMaturity "ten".
car:Hardware demo:isProducedIn loc:NorthPole.

Is there any way to change a PREFIX "car:" to, e.g., "plane:", and keep the relations:

plane:Software rdf:type demo:CyberObject.
plane:Hardware rdf:type spdm:PhysicalObject.
plane:Software plane:hasMaturity "ten".
plane:Hardware demo:isProducedIn loc:NorthPole.

I still need all the relations. The objects with PREFIX "car:" do not have to be replaced; it would be enough to create new ones with the new PREFIX and keep the old object in the database..

Thank you for any advice!


Solution

  • Replace prefixes in subjects, predicates and objects successively.

    prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    prefix car: <http://example.com/car/>
    prefix demo: <http://example.com/demo/>
    prefix spdm: <http://example.com/spdm/>
    prefix loc: <http://example.com/loc/>
    prefix plane: <http://example.com/plane/>
    
    DELETE {?s ?p1 ?o} INSERT {?s ?p2 ?o} WHERE
    {
    ?s ?p1 ?o .
    FILTER (strstarts(str(?p1), str(car:)))
    BIND (IRI(replace(str(?p1), str(car:), str(plane:)))  AS ?p2)
    } ;
    
    DELETE {?s1 ?p ?o} INSERT {?s2 ?p ?o} WHERE
    {
    ?s1 ?p ?o .
    FILTER (strstarts(str(?s1), str(car:)))
    BIND (IRI(replace(str(?s1), str(car:), str(plane:)))  AS ?s2)
    } ; 
    
    DELETE {?s ?p ?o1} INSERT {?s ?p ?o2} WHERE
    {
    ?s ?p ?o1 .
    FILTER (strstarts(str(?o1), str(car:)) && isIRI(?o1))
    BIND (IRI(replace(str(?o1), str(car:), str(plane:)))  AS ?o2)
    } ;
    

    Not tested in Allegrograph, and possibly there exist Allegrоgraph-specific solutions.

    Update

    I still need all the relations, the objects with PREFIX "car" do not have to be replaced…

    Then do not replace prefixes in objects. However, keep in mind that an object in one triple can be a subject in another triple.

    …it would be enough to create new ones with the new PREFIX and keep the old object in the database.

    "Standalone" URIs are not stored in triplestore.