Search code examples
sparqlrdfontology

Insert into position in Ordered List Ontology


I'm having a list implementation of Ordered List Ontology and I want to write SPARQL query to insert into specific position into the list. My idea is this is 2 steps process. Given my list is 0-based and I want to insert in position N:

  1. Change index of items with index >= N to index +1
  2. Insert new Slot to position N

I'm struggling with the update which should re-index the slots. What I'm trying is:

DELETE {
    <http://listTest> <http://purl.org/ontology/olo/core#slot> ?slot .
    ?slot a <http://purl.org/ontology/olo/core#Slot> .
    ?slot <http://purl.org/ontology/olo/core#index> ?index .
    ?slot <http://purl.org/ontology/olo/core#item> ?item
}
INSERT {
    <http://listTest> <http://purl.org/ontology/olo/core#slot> ?slot .
    ?slot a <http://purl.org/ontology/olo/core#Slot> .
    ?slot <http://purl.org/ontology/olo/core#index> ?newIndex .
    ?slot <http://purl.org/ontology/olo/core#item> ?item
}
WHERE {
    FILTER(?index >= TARGET_INDEX) # Here would be the numeric value of target index 
    BIND((?index + 1) AS ?newIndex)
}

Even though the query runs to completion indexes of the slots are not updated. Any suggestions on what I'm doing wrong will be much appreciated.

NOTE: The Slots are blank nodes, not sure whether this is important but just to make sure you have all the details.


Solution

  • Your WHERE has no triple patterns, so it doesn't produce any bindings, so there's nothing for the INSERT and DELETE to do. The WHERE part needs to find bindings for item, index, etc. For just the index part, you need to select all the slots that need an updated index, and their new index, and you do that with the WHERE part. Then you delete the single triple with the old index, and insert the single triple with the new index. Note that you don't even need to select the value of ?item, just the slot and the index. It would look like this:

    DELETE {
        ?slot <http://purl.org/ontology/olo/core#index> ?index .
    }
    INSERT {
        ?slot <http://purl.org/ontology/olo/core#index> ?newIndex .
    }
    WHERE {
        <http://listTest> <http://purl.org/ontology/olo/core#slot> ?slot .
        ?slot a <http://purl.org/ontology/olo/core#Slot> .
        ?slot <http://purl.org/ontology/olo/core#index> ?index .
        FILTER(?index >= TARGET_INDEX) # Here would be the numeric value of target index 
        BIND((?index + 1) AS ?newIndex)
    }