在 SPARQL 中使用条件 CONSTRUCT 查询

using conditional CONSTRUCT queries in SPARQL

我需要一些有关 SPARQL 构造查询的帮助。如何使用 IF 条件进行 CONSTRUCT 查询以获得启用规则表示的三元组?

就像一个 CONSTRUCT 查询,使我们能够表达以下规则:

  1. 如果 XC1 class 的实例,并且 C1C2 [=] 的子 class class,那么 XC2.
  2. 的实例

  1. 如果 XP1 属性 中有值 V,并且 P1 是 [=24= 的子 属性 ],则 X 在 属性 P2 中具有值 V

对于此数据文件 (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 .

正如评论中所建议的那样:您实际上不需要条件来表达这一点。您在规则左侧所做的只是匹配三重模式的组合。这正是 SPARQL 查询中的 WHERE 子句所做的。同样,规则的右侧只是将匹配的资源重新组合成新的三元组模式——这正是 CONSTRUCT 子句所做的。例如:

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.

可以表示为:

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

同样:

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.

可以表示为:

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

这就是您所需要的。