Sparql - Applying Limit Criteria to Predicates

I am new to RDF / Sparql, so I apologize for any incorrect terminology, as well as for a rather scary example:

Given the following RDF dataset:

@prefix owl:   <http://www.w3.org/2002/07/owl#> .
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix e:     <http://www.example.com/#> .
@prefix foaf:  <http://xmlns.com/foaf/0.1/> .

e:Freemason a owl:Class .
e:Civilian a owl:Class .

e:Marty a e:Freemason .
e:Eugene a e:Freemason .
e:Mike a e:Freemason .
e:Alan a e:Freemason .

e:Paul a e:Civilian .

e:Marty foaf:knows e:Eugene .
e:Eugene foaf:knows e:Mike .
e:Eugene foaf:knows e:Paul .
e:Paul foaf:knows e:Alan .

I am trying to identify friends of friends who e:Martyonly know through e:Freemason.

So:

  • Marty knows Mike through Eugene, and they are all Masons, so that's fine.
  • Marty knows Eugene, who has a Civil friend Paul. Paul has a freemason friend Alan. However, Marty has no "Masons only" path to Alan, so he should be excluded.

Here's my SPARQL query:

prefix e: <http://www.example.com/#>
prefix foaf:  <http://xmlns.com/foaf/0.1/>

SELECT *
{
  <http://www.example.com/#Marty> foaf:knows+ ?target .
  ?target a e:Freemason .
}

This returns:

 http://www.example.com/#Eugene
 http://www.example.com/#Mike
 http://www.example.com/#Alan

Alan is included here because he meets the criteria is-a-freemason.

How do I modify the query to exclude Alan?

+3
source share
2

SPARQL, . OpenLink Virtuoso SPARQL-BI

prefix e: <http://www.example.com/#>
prefix foaf:  <http://xmlns.com/foaf/0.1/>
select * 
where
  {
    { select ?orig ?target 
      where
       { ?orig   foaf:knows ?target . 
         ?target a          e:Freemason .
       } 
    } 
    option ( TRANSITIVE, 
             T_IN(?orig), 
             T_OUT(?target), 
             T_DISTINCT, 
             T_MIN(1)
           )
    filter ( ?orig = <http://www.example.com/#Marty> )
  }

- -

orig                               target
<http://www.example.com/#Marty>    <http://www.example.com/#Eugene>
<http://www.example.com/#Marty>    <http://www.example.com/#Mike>
+1

SPARQL, ( , ), Virtuoso ( )

PREFIX e: <http://www.example.com/#>
PREFIX foaf:  <http://xmlns.com/foaf/0.1/>

SELECT *
FROM <http://kingsley.idehen.net/DAV/home/kidehen/Public/Linked%20Data%20Documents/Tutorials/club-member-test.ttl>
{
  <http://www.example.com/#Marty> foaf:knows{2} ?target .
  ?target a e:Freemason .
}

:

+1

Source: https://habr.com/ru/post/1694940/


All Articles