1

I am in need of some help regarding SPARQL construct queries. How is it possible to make a CONSTRUCT query with an IF condition to get triples that enable rules representation?

Like a CONSTRUCT query that enables us to express the following rules:

  1. If X is an instance of a C1 class, and C1 is a subclass of a C2 class, then X is an instance of C2.

and

  1. If X has in P1 property the value V, and P1 is a subproperty of P2, then X has in property P2 the value V.

For this Data File (Beatles.ttl)

 @prefix c:     <http://beatlesExample/ns/class/> .
    @prefix t:     <http://beatlesExample/ns/title/> .
    @prefix pi:     <http://beatlesExample/ns/playsInstrument/> .
    @prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> .
    @prefix m:     <http://beatlesExample/ns/musician/> .


t:MagicalMysteryTour  a         c:Song ;
        rdfs:label              "Magical Mystery Tour" ;
        pi:acousticrhythmguitar  m:JohnLennon ;
        pi:bass                  m:PaulMcCartney ;
        pi:drums                 m:RingoStarr ;
        pi:leadguitar            m:GeorgeHarrison ;
        pi:percussion            m:RingoStarr , m:NeilAspinall , m:MalEvans , m:GeorgeHarrison , m:JohnLennon ;
        pi:piano                 m:PaulMcCartney ;
        pi:trumpet               m:ElgarHowarth , m:JohnWilbraham , m:DavidMason , m:RoyCopestake ;
        pi:vocals                m:JohnLennon , m:PaulMcCartney , m:GeorgeHarrison .

t:PennyLane  a                  c:Song ;
        rdfs:label              "Penny Lane" ;
        pi:acousticguitar        m:JohnLennon ;
        pi:backingvocals         m:JohnLennon , m:GeorgeHarrison ;
        pi:bassguitar            m:PaulMcCartney ;
        pi:doublebass            m:FrankClarke ;
        pi:drums                 m:RingoStarr ;
        pi:electricguitar        m:JohnLennon ;
        pi:flutesorpiccolos      m:PGoody , m:RaySwinfield , m:MannyWinters , m:DennisWalton ;
        pi:guitar                m:GeorgeHarrison ;
        pi:handclaps             m:GeorgeHarrison , m:JohnLennon , m:PaulMcCartney ;
        pi:harmonium             m:PaulMcCartney ;
        pi:oboesorcoranglais     m:DickMorgan , m:MikeWinfield ;
        pi:piano                 m:JohnLennon , m:PaulMcCartney ;
        pi:tambourine            m:RingoStarr ;
        pi:trumpetsorflugelhorn  m:FreddyClayton , m:LeonCalvert , m:BertCourtley , m:DuncanCampbell , m:DavidMason ;
        pi:tubularbells          m:RingoStarr ;
        pi:vocals                m:PaulMcCartney .

t:NotASecondTime  a             c:Song ;
        rdfs:label              "Not A Second Time" ;
        pi:acousticrhythmguitar  m:JohnLennon ;
        pi:bass                  m:PaulMcCartney ;
        pi:drums                 m:RingoStarr ;
        pi:piano                 m:GeorgeMartin ;
        pi:vocals                m:JohnLennon .

t:Yesterday  a      c:Song ;
        rdfs:label  "Yesterday" ;
        pi:cello     m:FranciscoGabarro ;
        pi:guitar    m:PaulMcCartney ;
        pi:viola     m:KennethEssex ;
        pi:violin    m:TonyGilbert , m:SidneySax ;
        pi:vocals    m:PaulMcCartney .

t:LosParanoias  a         c:Song ;
        rdfs:label        "Los Paranoias" ;
        pi:acousticguitar  m:PaulMcCartney ;
        pi:percussion      m:JohnLennon ;
        pi:shaker          m:RingoStarr ;
        pi:vocals          m:PaulMcCartney .


t:LikeDreamersDo  a     c:Song ;
        rdfs:label      "Like Dreamers Do" ;
        pi:bass          m:PaulMcCartney ;
        pi:drums         m:PeteBest ;
        pi:leadguitar    m:GeorgeHarrison ;
        pi:rhythmguitar  m:JohnLennon ;
        pi:vocals        m:PaulMcCartney .

t:IGotToFindMyBaby  a   c:Song ;
        rdfs:label      "I Got To Find My Baby" ;
        pi:bass          m:PaulMcCartney ;
        pi:drums         m:RingoStarr ;
        pi:harmonica     m:JohnLennon ;
        pi:leadguitar    m:GeorgeHarrison ;
        pi:rhythmguitar  m:JohnLennon ;
        pi:vocals        m:JohnLennon .

t:Revolution1  a          c:Song ;
        rdfs:label        "Revolution 1" ;
        pi:Hammondorgan    m:PaulMcCartney ;
        pi:acousticguitar  m:JohnLennon ;
        pi:backingvocals   m:FrancieSchwartz , m:GeorgeHarrison , m:PaulMcCartney ;
        pi:bass            m:PaulMcCartney ;
        pi:drums           m:RingoStarr ;
        pi:leadguitar      m:GeorgeHarrison , m:JohnLennon ;
        pi:piano           m:PaulMcCartney ;
        pi:trombone        m:JPower , m:DonLang , m:BillPovey , m:RexMorris ;
        pi:trumpet         m:FreddyClayton , m:DerekWatkins ;
        pi:vocals          m:JohnLennon .

t:DizzyMissLizzy  a                  c:Song ;
        rdfs:label                   "Dizzy Miss Lizzy" ;
        pi:Hammondorgan               m:JohnLennon ;
        pi:HohnerPianetelectricpiano  m:PaulMcCartney ;
        pi:bass                       m:PaulMcCartney ;
        pi:cowbell                    m:RingoStarr ;
        pi:double-trackedleadguitar   m:GeorgeHarrison ;
        pi:drums                      m:RingoStarr ;
        pi:rhythmguitar               m:JohnLennon ;
        pi:vocals                     m:JohnLennon .

t:YesItIs  a                    c:Song ;
        rdfs:label              "Yes It Is" ;
        pi:acousticrhythmguitar  m:JohnLennon ;
        pi:bass                  m:PaulMcCartney ;
        pi:drums                 m:RingoStarr ;
        pi:harmonyvocals         m:GeorgeHarrison , m:PaulMcCartney ;
        pi:leadguitar            m:GeorgeHarrison ;
        pi:tambourine            m:RingoStarr ;
        pi:vocals                m:JohnLennon .

t:IWannaBeYourMan  a     c:Song ;
        rdfs:label       "I Wanna Be Your Man" ;
        pi:Hammondorgan   m:GeorgeMartin ;
        pi:backingvocals  m:PaulMcCartney , m:JohnLennon ;
        pi:bass           m:PaulMcCartney ;
        pi:drums          m:RingoStarr ;
        pi:leadguitar     m:GeorgeHarrison ;
        pi:maracas        m:RingoStarr ;
        pi:rhythmguitar   m:JohnLennon ;
        pi:vocals         m:RingoStarr .

t:ThingsWeSaidToday  a          c:Song ;
        rdfs:label              "Things We Said Today" ;
        pi:acousticrhythmguitar  m:JohnLennon ;
        pi:bass                  m:PaulMcCartney ;
        pi:drums                 m:RingoStarr ;
        pi:leadguitar            m:GeorgeHarrison ;
        pi:piano                 m:JohnLennon ;
        pi:tambourine            m:RingoStarr ;
        pi:vocals                m:PaulMcCartney .

t:HallelujahILoveHerSo
        a           c:Song ;
        rdfs:label  "Hallelujah, I Love Her So" ;
        pi:bass      m:StuartSutcliffe ;
        pi:guitar    m:GeorgeHarrison , m:PaulMcCartney , m:JohnLennon ;
        pi:vocals    m:PaulMcCartney .

t:OldBrownShoe  a        c:Song ;
        rdfs:label       "Old Brown Shoe" ;
        pi:backingvocals  m:JohnLennon , m:PaulMcCartney ;
        pi:bass           m:GeorgeHarrison ;
        pi:drums          m:RingoStarr ;
        pi:guitar         m:GeorgeHarrison , m:PaulMcCartney ;
        pi:organ          m:GeorgeHarrison ;
        pi:piano          m:PaulMcCartney ;
        pi:vocals         m:GeorgeHarrison .

t:GoodNight  a           c:Song ;
        rdfs:label       "Good Night" ;
        pi:12violins      m:Uncredited ;
        pi:backingvocals  m:MikeRedway , m:PatWhitmore , m:FredLucas , m:ValStockwell , m:IreneKing , m:IngridThomas , m:KenBarrie , m:RossGilmour ;
        pi:celesta        m:GeorgeMartin ;
        pi:clarinet       m:Uncredited ;
        pi:doublebass     m:Uncredited ;
        pi:harp           m:Uncredited ;
        pi:horn           m:Uncredited ;
        pi:threecellos    m:Uncredited ;
        pi:threeflutes    m:Uncredited ;
        pi:threeviolas    m:Uncredited ;
        pi:vibraphone     m:Uncredited ;
        pi:vocals         m:RingoStarr .

t:MemphisTennessee  a   c:Song ;
        rdfs:label      "Memphis, Tennessee" ;
        pi:bass          m:PaulMcCartney ;
        pi:drums         m:RingoStarr ;
        pi:leadguitar    m:GeorgeHarrison ;
        pi:rhythmguitar  m:JohnLennon ;
        pi:vocals        m:JohnLennon .

t:TicketToRide  a                   c:Song ;
        rdfs:label                  "Ticket To Ride" ;
        pi:bass                      m:PaulMcCartney ;
        pi:double-trackedleadvocals  m:JohnLennon ;
        pi:drums                     m:RingoStarr ;
        pi:handclaps                 m:RingoStarr ;
        pi:harmonyvocals             m:PaulMcCartney ;
        pi:leadguitar                m:PaulMcCartney ;
        pi:rhythmguitar              m:GeorgeHarrison , m:JohnLennon ;
        pi:tambourine                m:RingoStarr .

t:Ob-La-DiOb-La-Da  a      c:Song ;
        rdfs:label         "Ob-La-Di, Ob-La-Da" ;
        pi:acousticguitar   m:GeorgeHarrison ;
        pi:backingvocals    m:JohnLennon , m:GeorgeHarrison ;
        pi:bass             m:PaulMcCartney ;
        pi:bongo            m:RingoStarr ;
        pi:drums            m:RingoStarr ;
        pi:handclaps        m:JohnLennon , m:PaulMcCartney , m:GeorgeHarrison , m:RingoStarr ;
        pi:percussion       m:RingoStarr ;
        pi:piano            m:JohnLennon ;
        pi:threesaxophones  m:Uncredited ;
        pi:vocals           m:PaulMcCartney .

t:LetItBe  a              c:Song ;
        rdfs:label        "Let It Be" ;
        pi:backingvocals   m:JohnLennon , m:LindaMcCartney , m:PaulMcCartney , m:GeorgeHarrison ;
        pi:bassguitar      m:PaulMcCartney ;
        pi:cello           m:Uncredited ;
        pi:drums           m:RingoStarr ;
        pi:electricpiano   m:BillyPreston ;
        pi:leadguitar      m:GeorgeHarrison ;
        pi:maracas         m:PaulMcCartney ;
        pi:organ           m:BillyPreston ;
        pi:piano           m:PaulMcCartney ;
        pi:tenorsaxophone  m:Uncredited ;
        pi:twotrombones    m:Uncredited ;
        pi:twotrumpets     m:Uncredited ;
        pi:vocals          m:PaulMcCartney .


t:IGotAWoman  a         c:Song ;
        rdfs:label      "I Got A Woman" ;
        pi:bass          m:PaulMcCartney ;
        pi:drums         m:RingoStarr ;
        pi:leadguitar    m:GeorgeHarrison ;
        pi:rhythmguitar  m:JohnLennon ;
        pi:vocals        m:JohnLennon .

t:TheSaints  a          c:Song ;
        rdfs:label      "The Saints" ;
        pi:bass          m:PaulMcCartney ;
        pi:drums         m:PeteBest ;
        pi:leadguitar    m:TonySheridan , m:GeorgeHarrison ;
        pi:rhythmguitar  m:JohnLennon ;
        pi:vocals        m:TonySheridan .

t:LendMeYourComb  a     c:Song ;
        rdfs:label      "Lend Me Your Comb" ;
        pi:bass          m:PaulMcCartney ;
        pi:drums         m:RingoStarr ;
        pi:leadguitar    m:GeorgeHarrison ;
        pi:rhythmguitar  m:JohnLennon ;
        pi:vocals        m:GeorgeHarrison , m:PaulMcCartney , m:JohnLennon .

t:BesameMucho  a        c:Song ;
        rdfs:label      "Besame Mucho" ;
        pi:bass          m:PaulMcCartney ;
        pi:drums         m:PeteBest ;
        pi:leadguitar    m:GeorgeHarrison ;
        pi:rhythmguitar  m:JohnLennon ;
        pi:vocals        m:PaulMcCartney .
  • Does this answer your question? [Using condition in SPARQL query](https://stackoverflow.com/questions/22985157/using-condition-in-sparql-query) – TallTed Dec 19 '19 at 14:52
  • There's also [Conditional Binding in SPARQL/CONSTRUCT](https://stackoverflow.com/questions/42461194/conditional-binding-in-sparql-construct) and [Binding a variable to one of two values with IF?](https://stackoverflow.com/questions/21116223/binding-a-variable-to-one-of-two-values-with-if) among others. – TallTed Dec 19 '19 at 14:53
  • 2
    People already mapped RDFS inference rules to SPARQL CONSTRUCT. This is trivial. @TallTed here is no need for an `IF` - it's just two triple patterns in the WHERE clause and a single triple pattern in the CONSTRUCT part. The question title is more or less misleading - or at least I don't see where you'd need `IF` here. – UninformedUser Dec 19 '19 at 15:32
  • 1
    `construct {?s a ?B} where {?s a ?A . ?A rdfs:subClassOf ?B }` – UninformedUser Dec 19 '19 at 15:34
  • @AKSW can you join me in a conversation here in the Stack chat? please –  Dec 19 '19 at 16:31

1 Answers1

3

As suggested in the comments: you don't actually need a conditional to express this. All you're doing in the left-hand side of the rule is matching a combination of triple patterns. This is exactly what the WHERE clause in a SPARQL query does. Similarly, the right-hand side of the rule simply recombines matched resources into new triple patterns - this is exactly what the CONSTRUCT clause does. For example:

If X is an instance of a C1 class, and C1 is a subclass of a C2 class, then X is an instance of C2.

That could be expressed as:

 CONSTRUCT { ?X a ?C2 }
 WHERE  { ?X a ?C1. ?C1 rdfs:subClassOf+ ?C2 }

Similarly:

If X has in P1 property the value V, and P1 is a subproperty of P2, then X has in property P2 the value V.

could be expressed as:

 CONSTRUCT { ?X ?P2 ?V }
 WHERE { ?X ?P1 ?V . ?P1 rdfs:subPropertyOf+ ?P2 }

That's all you need.

Jeen Broekstra
  • 21,642
  • 4
  • 51
  • 73
  • Does this make any sense for the first one? `prefix pi: prefix rdfs: prefix rdf: prefix m: prefix : CONSTRUCT { ?trombone a ?Instrument } WHERE { ?trombone a ?Trombone. ?Trombone rdfs:subClassOf+ ?Instrument }` –  Dec 19 '19 at 22:53
  • That query is technically fine, but you're getting confused because you're using variable names like `?trombone` and `?Instrument` in patterns that will match anything (not just trombones and instruments, but also, as you can see in your output, musicians). Just because you call your variable "trombone" doesn't mean it will only match trombones in your data – Jeen Broekstra Dec 19 '19 at 23:17
  • sorry I am new to this whole SPARQL theme. what variables should I use? @jeenbroekstra –  Dec 19 '19 at 23:55
  • My point is that the variable names have no intrinsic meaning themselves - you just give your variables certain names to make your query easier to read. I suggest you work your way through one of the many SPARQL tutorials available on the Web if this is unclear. A bit of practice with some simple examples will be useful. – Jeen Broekstra Dec 20 '19 at 00:14
  • yup that's what I have done... I've watched a sparql tutorial and tried to do this exercises but with no success. Could you check if I am getting closer? `CONSTRUCT { ?musician a ?Pianist . ?Pianist a ?Musician . ?musician a ?Musician . } WHERE { ?musician a ?Pianist . ?Pianist rdfs:subClassOf+ ?Musician . }` –  Dec 20 '19 at 01:10
  • Closer to _what_? It's not very clear to me what you're trying to achieve here: you're basically just doing more or less the same query, just with different variable names. – Jeen Broekstra Dec 20 '19 at 09:21
  • What I want is to use construct queries to obtain triples to be able to express those rules in the post. –  Dec 20 '19 at 12:58
  • I am having some difficulties with the second one, can you help? @JeenBroekstra –  Dec 20 '19 at 22:27
  • I gave you the _exact_ queries that you need for that, in the original answer. If that is somehow not working for you, please ask a new question instead of adding comments to this one. Be precise, provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Jeen Broekstra Dec 20 '19 at 23:03
  • ok! but my question is what will be each of the variables in my case P1, P2, X and V, just that, if you could assist me I'd appreciate. @Jeen Broekstra –  Dec 20 '19 at 23:29
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/204636/discussion-between-jeen-broekstra-and-ack31). – Jeen Broekstra Dec 21 '19 at 00:28