在 SPARQL 中更改前缀

Change PREFIX in SPARQL

我创建了一个具有不同前缀的 ontology(rdf:rdfs:owl:example:car:bike:, ...)。我用它们来划分不同的域和示例。

一小段摘录:

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

有什么方法可以将 PREFIXcar:”更改为例如“plane:”并保持关系:

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

我还需要所有的关系。 PREFIX "car:" 的对象不必替换;使用新 PREFIX 创建新对象并将旧对象保留在数据库中就足够了..

感谢您的任何建议!

依次替换主语、谓语和宾语中的前缀。

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)
} ;

未在 Allegrograph 中测试,可能存在特定于 Allegrograph 的解决方案。

更新

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.

"Standalone" URI 未存储在 triplestore 中。