Example updates

The main question on updates seems to be around referring to multiple
graphs, so these operations are based on that idea. I'll be using some
common prefixes (foaf:, contact:), so I hope no one minds if I don't
define them.

I can't recall if I'm supposed to be writing according to a specific
syntax, so I'll offer a couple of variations.

The first case has one source for pattern matching, and one
destination for insertion.
This example copies the contents of one graph into another.

(1) The original form would be:

INSERT INTO <destination> { ?s ?p ?o }
WHERE { GRAPH <source> { ?s ?p ?o } }

(2) With the recent changes:

WITH <source>
INSERT INTO <destination> { ?s ?p ?o }
WHERE { ?s ?p ?o }


(3) Or using GRAPH instead of INTO:

WITH <source>
INSERT { GRAPH <destination> { ?s ?p ?o } }
WHERE { ?s ?p ?o }

(4) Then dropping WITH:

INSERT { GRAPH <destination> { ?s ?p ?o } }
WHERE { GRAPH <source> { ?s ?p ?o } }

(The last would be valid even if WITH is adopted)


===

This example has 2 sources for pattern matching and 1 destination for insertion.

To create entries for people in a new graph if they live in London and
have an email address stored in a separate graph:

(1)
INSERT INTO <people_graph> { ?person a foaf:Person }
WHERE {
  GRAPH <address_graph> {
    ?person contact:home [ contact:city "London" ]
  } . GRAPH <mail_graph> {
    ?person foaf:mbox ?mail
  }
}

(2)
WITH <address_graph>
INSERT INTO <people_graph> { ?person a foaf:Person }
WHERE {
  ?person contact:home [ contact:city "London" ] .
  GRAPH <mail_graph> { ?person foaf:mbox ?mail }
}

(3)
WITH <address_graph>
INSERT { GRAPH <people_graph> { ?person a foaf:Person } }
WHERE {
  ?person contact:home [ contact:city "London" ] .
  GRAPH <mail_graph> { ?person foaf:mbox ?mail }
}

(4)
INSERT { GRAPH <people_graph> { ?person a foaf:Person } }
WHERE {
  GRAPH <address_graph> {
    ?person contact:home [ contact:city "London" ]
  } . GRAPH <mail_graph> {
    ?person foaf:mbox ?mail
  }
}

===

This example has multiple sources for pattern matching, multiple
sources for deletion, and a single destination for insertion.

To delete email addresses from graphs a, b, and c, and insert them
into a graph called email_graph:

(1) can't be done in one step

(2)
# WITH is being used on the destination, but could be applied elsewhere
WITH <email_graph>
DELETE FROM <a> { ?person foaf:mbox ?email };
DELETE FROM <b> { ?person foaf:mbox ?email };
DELETE FROM <c> { ?person foaf:mbox ?email };
INSERT { ?person foaf:mbox ?email }
WHERE {
  GRAPH <a> {?person foaf:mbox ?email}
  UNION GRAPH <b> {?person foaf:mbox ?email}
  UNION GRAPH <c> {?person foaf:mbox ?email}
 }

(looks like a good case for FROM NAMED)

(3)
WITH <email_graph>
DELETE { GRAPH <a> { ?person foaf:mbox ?email } };
DELETE { GRAPH <b> { ?person foaf:mbox ?email } };
DELETE { GRAPH <c> { ?person foaf:mbox ?email } };
INSERT { ?person foaf:mbox ?email }
WHERE {
  GRAPH <a> {?person foaf:mbox ?email}
  UNION GRAPH <b> {?person foaf:mbox ?email}
  UNION GRAPH <c> {?person foaf:mbox ?email}
 }

(4)
DELETE { GRAPH <a> { ?person foaf:mbox ?email } };
DELETE { GRAPH <b> { ?person foaf:mbox ?email } };
DELETE { GRAPH <c> { ?person foaf:mbox ?email } };
INSERT { GRAPH <email_graph> {?person foaf:mbox ?email } }
WHERE {
  GRAPH <a> {?person foaf:mbox ?email}
  UNION GRAPH <b> {?person foaf:mbox ?email}
  UNION GRAPH <c> {?person foaf:mbox ?email}
 }


Alternatively, instead of the union it could be something like:
DELETE { GRAPH <?g> { ?person foaf:mbox ?email } };
INSERT { GRAPH <email_graph> {?person foaf:mbox ?email } }
WHERE {
  GRAPH <?g> {?person foaf:mbox ?email} .
  FILTER (?g == <a> || ?g == <b> || ?g == <c>)
 }

===

This example has a single source for pattern matching, and multiple
destinations for insertion:

(1) not possible

(2)
WITH <people_graph>
INSERT INTO <email_graph> {?person foaf:mbox ?email };
INSERT INTO <name_graph> {?person foaf:name ?name }
WHERE {
  ?person a foaf:Person
  OPTIONAL { ?person foaf:mbox ?email }
  OPTIONAL { ?person foaf:name ?name }
}

(Have we decided what happens with an unbound value? That entry gets
skipped? I'm guessing so, but I don't recall it being addressed)

(3)
WITH <people_graph>
INSERT { GRAPH <email_graph> {?person foaf:mbox ?email } };
INSERT { GRAPH <name_graph> {?person foaf:name ?name } }
WHERE {
  ?person a foaf:Person
  OPTIONAL { ?person foaf:mbox ?email }
  OPTIONAL { ?person foaf:name ?name }
}

(4)
INSERT { GRAPH <email_graph> {?person foaf:mbox ?email } };
INSERT { GRAPH <name_graph> {?person foaf:name ?name } }
WHERE {
  GRAPH <people_graph> {
    ?person a foaf:Person
    OPTIONAL { ?person foaf:mbox ?email }
    OPTIONAL { ?person foaf:name ?name }
  }
}

===
This example does pattern matching on multiple sources, deletions from
a variable source, and insertions into another.

For any people appearing in the central_data graph, remove them from
all graphs, and place the entries in people_graph.

(1)
DELETE FROM <?g> { ?person ?p ?o }
INSERT INTO <people_graph> { ?person ?p ?o }
WHERE {
  GRAPH <central_data> { ?person a foaf:Person } .
  GRAPH <?g> { ?person ?p ?o }
}

(2)
WITH <central_data>
DELETE FROM <?g> { ?person ?p ?o };
INSERT INTO <people_graph> { ?person ?p ?o }
WHERE {
  ?person a foaf:Person .
  GRAPH <?g> { ?person ?p ?o }
}

(3)
WITH <central_data>
DELETE { GRAPH <?g> { ?person ?p ?o } };
INSERT { GRAPH <people_graph> { ?person ?p ?o } }
WHERE {
  ?person a foaf:Person .
  GRAPH <?g> { ?person ?p ?o }
}

(4)
DELETE { GRAPH <?g> { ?person ?p ?o } };
INSERT { GRAPH <people_graph> { ?person ?p ?o } }
WHERE {
  GRAPH <central_data> { ?person a foaf:Person } .
  GRAPH <?g> { ?person ?p ?o }
}

===
This example does pattern matching on 1 source, deletions from 2
graphs, and insertions into 2 graphs.

For any people appearing in the graph_a, remove their emails from
graph_a and graph_b, and insert their presence into graph_c and
graph_d.

(1) Not possible

(2)
WITH <graph_a>
DELETE { ?person foaf:email ?x };
DELETE FROM <graph_b> { ?person foaf:email ?x };
INSERT INTO <graph_c> { ?person a foaf:Person };
INSERT INTO <graph_d> { ?person a foaf:Person }
WHERE {
  ?person a foaf:Person .
  GRAPH <?g> { ?person foaf:email ?x }
}

(3)
WITH <graph_a>
DELETE { ?person foaf:email ?x };
DELETE { GRAPH <graph_b> { ?person foaf:email ?x } };
INSERT { GRAPH <graph_c> { ?person a foaf:Person } };
INSERT { GRAPH <graph_d> { ?person a foaf:Person } }
WHERE {
  ?person a foaf:Person .
  GRAPH <?g> { ?person ?p ?o }
}

(4)
DELETE { GRAPH <graph_a> { ?person foaf:email ?x } };
DELETE { GRAPH <graph_b> { ?person foaf:email ?x } };
INSERT { GRAPH <graph_c> { ?person a foaf:Person } };
INSERT { GRAPH <graph_d> { ?person a foaf:Person } }
WHERE {
  GRAPH <graph_a> { ?person a foaf:Person } .
  GRAPH <?g> { ?person ?p ?o }
}


Regards,
Paul Gearon

Received on Tuesday, 1 December 2009 21:06:37 UTC