简体   繁体   中英

SPARQL: DELETE if subject contains 1 triple and has predicate

I was wondering if anyone knew of a way to DELETE a triple based on 2 conditions:

  • Subject has a triple count of 1.
  • The only triple on the subject matches a predicate.

The obstacle I'm coming across with these 2 conditions is that in order to COUNT the number of statements on a subject, you must do GROUP BY?s . Then you do not have the ability to filter any ?p value.

The most likely solution would be a subquery, but I am not sure how this would be structured.

I would write it like this:

DELETE { ?s ?p ?o }
WHERE {
    {
        SELECT ?s (COUNT(*) AS ?c) {
            ?s ?p ?o
        }
        GROUP BY ?s
    }
    FILTER (?c = 1)
    ?s ?p ?o
}

Does something like this work?

I assume that by "Subject has a triple count of 1" you mean that there is only one triple with this subject. For the case of 1, this is a bit easier since we can just check that "there does not exist another triple for the same subject."

DELETE { ?s ?p ?o }
WHERE {
  ?s ?p ?o 

  #-- the value of ?p is the predicate of interest.
  #-- Alternatively, this could be a FILTER involving
  #-- the variable ?p .
  values ?p { <the-predicate> }

  #-- There is no other triple with ?s as the subject
  #-- that has a different subject or object.  (I.e., 
  #-- the only triple with ?s as a subject is with ?p 
  #-- and ?o .
  filter not exists {
    ?s ?pp ?oo
    filter (?pp != ?p || ?oo != ?o)
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM