2013-08-20 24 views
12

我正在尝试使用SPARQL构造查询从现有的图创建一个新的命名图。我查询的数据库包含http://graph.com/old作为现有的命名图。我使用Jena TDB作为数据库,通过Jena Fuseki端点访问。下面的查询给我一个错误:构建一个命名图

CONSTRUCT 
{ 
    GRAPH <http://graph.com/new> { 
     ?s ?p ?o 
    } 
} 

WHERE 
{ 
    GRAPH <http://graph.com/old> { 
     ?s ?p ?o 
    } 
} 

如果我从该构造块取出图声明,查询作品完美,但我想给三元组放入一个名为graph我指定的默认,而不是一。

据我所知,SPARQL 1.1 section on CONSTRUCT没有说任何关于构造命名图的内容。有没有办法做到这一点?

回答

14

就像在有兴趣获取一组变量绑定时使用SELECT查询一样,使用CONSTRUCT查询有兴趣获取模型。就像在SELECT结果集中绑定的变量没有放入任何模型或持久绑定集合一样,CONSTRUCT建立的模型也不会存储在任何地方。您想要使用SPARQL 1.1 INSERT。更新功能在3 SPARQL 1.1 Update Language中描述。您的更新请求因此可以写为:

INSERT { 
    GRAPH <http://graph.com/new> { 
    ?s ?p ?o 
    } 
} 
WHERE { 
    GRAPH <http://graph.com/old> { 
    ?s ?p ?o 
    } 
} 

对于这种特殊的情况下,虽然,你也许能够使用3.2.3 COPY描述的复制操作。尽管COPY首先删除了目标图中的所有数据,但它可能不适用于您的实际情况(了解您提供的代码可能只是一个简单示例,并不一定是您尝试执行的实际更新)。关于COPY的标准说:

The COPY operation is a shortcut for inserting all data from an input graph into a destination graph. Data from the input graph is not affected, but data from the destination graph, if any, is removed before insertion.

COPY (SILENT)? ((GRAPH)? IRIref_from | DEFAULT) TO ((GRAPH)? IRIref_to | DEFAULT) 

is similar in operation to:

DROP SILENT (GRAPH IRIref_to | DEFAULT); 
     INSERT { (GRAPH IRIref_to)? { ?s ?p ?o } } WHERE { (GRAPH IRIref_from)? { ?s ?p ?o } } 

The difference between COPY and the DROP/INSERT combination is that if COPY is used to copy a graph onto itself then no operation will be performed and the data will be left as it was. Using DROP/INSERT in this situation would result in an empty graph.

If the destination graph does not exist, it will be created. By default, the service may return failure if the input graph does not exist. If SILENT is present, the result of the operation will always be success.

如果COPY不适合,那么ADD可能是你在找什么:

3.2.5 ADD

The ADD operation is a shortcut for inserting all data from an input graph into a destination graph. Data from the input graph is not affected, and initial data from the destination graph, if any, is kept intact.

ADD (SILENT)? ((GRAPH)? IRIref_from | DEFAULT) TO ((GRAPH)? IRIref_to | DEFAULT) 

is equivalent to:

INSERT { (GRAPH IRIref_to)? { ?s ?p ?o } } WHERE { (GRAPH IRIref_from)? { ?s ?p ?o } } 

If the destination graph does not exist, it will be created. By default, the service may return failure if the input graph does not exist. If SILENT is present, the result of the operation will always be success.

+2

的替代'COPY'当然'ADD',这将数据从源复制到目标并保留目标图现有数据 – RobV

+0

@RobV好点!我没有做太多的SPARQL 1.1更新,我想我在阅读COPY后没有阅读到规范。我将添加ADD。 –