Multiple SPARQL “INSERT WHERE” queries in a single request

我的梦境 提交于 2019-12-10 04:21:13

问题


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.


回答1:


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.




回答2:


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
}


来源:https://stackoverflow.com/questions/10078966/multiple-sparql-insert-where-queries-in-a-single-request

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!