Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple SPARQL "INSERT WHERE" queries in a single request

Tags:

rdf

sparql

My question is similar to what was asked on this thread Is it possible to combine those 2 SPARQL INSERT into one?

I want to have multiple INSERT WHERE statements in a query, but for different subjects. I will like to test a particular value ("testValueN") and if present would like to insert a new triple for that subject.

An example of it would be,

PREFIX Sensor: <http://example.com/Equipment.owl#> 
{
    INSERT { 
        ?subject1 Sensor:test2 'newValue1' . 
           }
    WHERE {
        ?subject1 Sensor:test1  'testValue1' . 
          }
};
{
    INSERT { 
        ?subject2 Sensor:test2 'newValue2' . 
           }
    WHERE {
        ?subject2 Sensor:test1  'testValue2' . 
          }
};

I know the above query is wrong. I would like to know if something similar is possible in SPARQL.

like image 327
Nikhil Avatar asked Apr 09 '12 19:04

Nikhil


2 Answers

Yes, this is possible. In fact, your example is almost completely fine, just lose the brackets around each insert:

PREFIX Sensor: <http://example.com/Equipment.owl#> 
INSERT { 
    ?subject1 Sensor:test2 'newValue1' . 
}
WHERE {
    ?subject1 Sensor:test1  'testValue1' . 
};
INSERT { 
   ?subject2 Sensor:test2 'newValue2' . 
}
WHERE {
   ?subject2 Sensor:test1  'testValue2' . 
}

is a valid SPARQL update sequence.

like image 153
Jeen Broekstra Avatar answered Nov 16 '22 03:11

Jeen Broekstra


I want to have multiple INSERT WHERE statements in a query, but for different subjects. I will like to test a particular value ("testValueN") and if present would like to insert a new triple for that subject.

You can do this using values to specify the oldValue/newValue pairs that you want, and it only requires a single insert. It also scales more nicely to new pairs, since you only have to add one line to the query.

PREFIX Sensor: <http://example.com/Equipment.owl#> 
INSERT { 
    ?subject Sensor:test2 ?newValue 
}
WHERE {
    values (?oldValue ?newValue) { 
        ('testValue1' 'newValue1')
        ('testValue2' 'newValue2')
    }
    ?subject Sensor:test1 ?oldValue
}
like image 26
Joshua Taylor Avatar answered Nov 16 '22 03:11

Joshua Taylor