Wednesday, February 3, 2010

Using rules to implement a cascading delete

In an RDF model some entities are wholly owned by other entities. These are often referenced to as contained objects. When deleting an entity the desired logic is:

  1. Delete all statements about the entity
  2. Delete all statements about any contained objects referenced by the entity
  3. Delete any statements that reference the contained object.

One way to implement this logic is to add some additional information about the predicates that are used to reference a contained object. For example for a property “foo” we can add the following statement to the ontology.

x:foo x:refType x:containedSubject.

This statement denotes that any entity that is the object value of a statement with the predicate x:foo is a contained object. Using this approach a generic set of recursive delete rules can be implemented for removing contained objects and the statements that reference them. It traditional database terminology this is referred to a a cascading delete.

The SPARQL rulebase and DELETE query shown below provides an example of how to implement this logic.

prefix x: <http://example.org/>

rulebase (
# remove references to the contained object.
select ?s ?p ?o ?r where {?s ?p ?o. 
  ?p x:refType x:containedObject. filter(?o = ?r)} as x:cascadeDelete

# remove all statements about the contained object.
select ?s ?p ?o ?r where {?s ?p ?o. 
  filter(?s=?r).} as x:cascadeDelete

# remove all referenced contained objects
select ?s ?p ?o ?r where {?r ?x ?y. 
  ?x x:refType x:containedObject. 
  x:cascadeDelete(?s, ?p, ?o, ?y).} as x:cascadeDelete
)

# delete a node where @target = the target URI
delete from <xxx> {?s ?p ?o} 
from <xxx>
where {x:cascadeDelete(?s, ?p, ?o, @target).}

0 comments:

Post a Comment