Trying out SPARQL 1.1's COPY and MOVE operations

Copying and moving triples between graphs, named or otherwise.

SPARQL 1.1 Update’s COPY and MOVE operations let you copy and move triples between named graphs or between the default graph and a named graph. These operations first appeared in the May 2011 SPARQL 1.1 Update draft, but with the recent 0.2.2 snapshot release of Fuseki I find I can try their full range of capabilities a little more than I could with the 0.2.1 incubating release of Fuseki.

The spec’s description of COPY and MOVE show that neither truly adds anything to the SPARQL Update language; each is a shortcut to a wordier query request that combines DROP and INSERT operations. I think the new ones will be handy.

To try them, I first ran the update request at http://www.learningsparql.com/examples/ex338.ru in Fuseki to create some data to serve as a baseline. This update request inserts two triples in the default graph, two in the named graph d:g1, and two in the named graph d:g2. Running the query that lists all triples in all graphs got me this (with prefixes substituted for the original base URIs to more easily fit the output on this page):

---------------------------------
| g    | s   | p      | o       |
=================================
|      | d:x | dm:tag | "one"   |
|      | d:x | dm:tag | "two"   |
| d:g1 | d:x | dm:tag | "three" |
| d:g1 | d:x | dm:tag | "four"  |
| d:g2 | d:x | dm:tag | "five"  |
| d:g2 | d:x | dm:tag | "six"   |
---------------------------------

The COPY operation copies triples from one graph into another, replacing any existing triples in the destination graph. (To quote the spec, “If the destination graph does not exist, it will be created.”) The following update request copies the triples from the default graph to graph d:g2:

PREFIX d:  <http://learningsparql.com/ns/data#>
COPY DEFAULT TO d:g2

Pretty simple. After running it, running the query that lists all graphs and triples shows that the “five” and “six” triples that were in the d:g2 graph are no longer there and that the “one” and “two” triples are there and still in the default graph as well:

---------------------------------
| g    | s   | p      | o       |
=================================
|      | d:x | dm:tag | "one"   |
|      | d:x | dm:tag | "two"   |
| d:g1 | d:x | dm:tag | "three" |
| d:g1 | d:x | dm:tag | "four"  |
| d:g2 | d:x | dm:tag | "one"   |
| d:g2 | d:x | dm:tag | "two"   |
---------------------------------

The MOVE operation moves triples from one graph to another, also replacing existing triples in the destination graph. Again, if the destination graph doesn’t exist, it will be created. The following update request moves the triples in graph d:g2 to graph d:g1:

PREFIX d: <http://learningsparql.com/ns/data#>
MOVE d:g2 TO d:g1

When run against the result of the COPY update request above, the result shows that there’s nothing left in d:g2 and that d:g1 has the triples that used to be in d:g2:

-------------------------------
| g    | s   | p      | o     |
===============================
|      | d:x | dm:tag | "one" |
|      | d:x | dm:tag | "two" |
| d:g1 | d:x | dm:tag | "one" |
| d:g1 | d:x | dm:tag | "two" |
-------------------------------

You can see more variations on these two operations in the SPARQL 1.1 Test Suite, but the tests are basically different combinations of moving triples between default and named graphs, pre-existing or otherwise.

Like I said, neither of these operations add anything to SPARQL Update that couldn’t be done without them, but I would venture a guess that by making it easier to manipulate the relationships between triples and named graphs, the SPARQL Working Group is encouraging developers to use named graphs more as part of their application architectures. I look forward to asking some of them about this at the Semantic Technology and Business conference in San Francisco this week. And now it’s off to the airport…