简体   繁体   中英

rdfs remove items if a condition happens

In a previous question

SPARQL if an instance has a property, others must as well

I asked that if i have an instance that has a value for a specific predicate, then the all the output of my sparql query must have the same value for the same predicate.

I got an excellent answer there,

now I am trying to expand it because i've found a new scenario,

the new scenario is:

if an instance has a value to a specific predicate then all the output items must either have the same value for the same predicate or if they have another value, these two values must be from the same class

I extended that sparql query to the following:

    PREFIX : <http://example.org/rs#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

select ?item ?predicate ?similarity where 
{
  values ?x {:instance1}
  ?x ?predicate ?value.
  ?item ?predicate ?value.
  ?predicate :hasSimilarityValue ?similarity.
  filter (?x != ?item)
  filter not exists {
    ?x ?p ?v1.
    ?v1 a   ?class.
    ?p  rdfs:subPropertyOf  :isCriticalPredicate.
    filter not exists {
        ?item ?p ?v2.
        ?p rdfs:subPropertyOf :isCriticalPredicate.
        ?v2 a   ?class.
        ?class  rdfs:subClassOf :ImportantClass
    }
  }
}

and here is my data:

  @prefix : <http://example.org/rs#>
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>

:instance1  :p1 :value1.
:instance2  :p1 :value2.
:p1 :hasSimilarityValue 0.5.
:value1 a   :class1.
:value2 a   :class1.
:class1 rdfs:subClassOf :ImportantClass.
:p1 rdfs:subPropertyOf  :isCriticalPredicate.

but the result is always empty i don't know why.

note1

by specific predicate, i mean the predicate that are rdfs:subClassOf :isCriticalPredicate

by specific class, I mean that class that are rdfs:subClassOf :ImportantClass

The Immediate Problem: There's No Matching Data!

As written, this is a pretty simple issue: there's actually no data that matches the query you've written. As such, I don't think the data and query really reproduce the problem that you're describing. You query begins with

  values ?x {:instance1}
  ?x ?predicate ?value.
  ?item ?predicate ?value.
  filter (?x != ?item)

Your data has:

:instance1  :p1 :value1.
:instance2  :p1 :value2.

There are no possible values for ?instance. The only candidate would be :instance2, but it doesn't have any values in common with :instance1, so there's no candidate for ?value.

The Query You Want

Now, even though the question as written doesn't really have the issue that you're describing, I think I see what you're asking about. When you're trying to create minimal data for your debugging process, you should try to include all the relevant cases . In this case, you'd want to have at least an instance that has the same value for a critical predicate, and an instance that has a different value for a critical predicate but where both values are important. Here's some data:

@prefix : <urn:ex:>

:instance1 :p :v1 .
:instance1 :q :v2 .

:instance2 :p :v1 . 
:instance2 :q :v2 .  #-- same value for :q

:instance3 :p :v1 .
:instance3 :q :v3 .  #-- different, but important, value for :q

:instance4 :p :v1 .
:instance4 :q :v4 .  #-- different, non-important, value for :q

:q a :CriticalPredicate .

:v2 a :ImportantValue .
:v3 a :ImportantValue .

Given the query that you're already working with, I think this one will be relatively clear. It's not much different from what you've already got.

prefix : <urn:ex:>

select distinct ?item where  {
  values ?x { :instance1 }
  ?x    ?predicate ?value .
  ?item ?predicate ?value .
  filter (?x != ?item)

  #-- Exclude any results where
  #-- ?x has a value ?v1 for a
  #-- critical predicate ?p...
  filter not exists {
    ?x ?p ?v1 .
    ?p a :CriticalPredicate .

    #-- ...unless either ?item has
    #-- the same value ?v1 for the
    #-- predicate ?p, or ?item has
    #-- another value ?v2 and both
    #-- ?v1 and ?v2 are
    #-- :ImportantValues.
    filter not exists {
      { ?item ?p ?v1 }
      union
      { ?item ?p ?v2 .
        :ImportantValue ^a ?v1, ?v2 . }
    }
  }
}
--------------
| item       |
==============
| :instance2 |
| :instance3 |
--------------

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