How to use a kleene star operator (*) or it's variant (+) with variables in sparql?












0















I have some working code for getting all the ancestors of a term in a hierarchy.
Following:



    PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX skos-xl: <http://www.w3.org/2008/05/skos-xl#>
PREFIX rdf: <http://www.w3.org/2000/01/rdf-schema#>

select ?grandparentliteralform (count(?parent) as ?distance)
{ ?iri skos:broader+ ?parent .
?parent skos:broader* ?grandparent .
?grandparent skos-xl:prefLabel ?grandparentlabel .
?grandparentlabel skos-xl:literalForm ?grandparentliteralform .
}
group by ?grandparent
order by DESC(?distance)


It breaks when an IRI's broader predicate is a subproperty (?p rdf:subPropertyOf skos:broader)
So right now I am doing this to capture all the subproperty predicates:



select  ?grandparentliteralform (count(?parent) as ?distance)
{ ?iri ?p ?parent .
?parent skos:broader* ?grandparent .
?grandparent skos-xl:prefLabel ?grandparentlabel .
?grandparentlabel skos-xl:literalForm ?grandparentliteralform .
?p rdf:subPropertyOf skos:broader .
}
group by ?grandparent
order by DESC(?distance)


What i would really like to do is :



select  ?grandparentliteralform (count(?parent) as ?distance)
{ ?iri ?p+ ?parent .
?parent ?p* ?grandparent .
?grandparent skos-xl:prefLabel ?grandparentlabel .
?grandparentlabel skos-xl:literalForm ?grandparentliteralform .
?p rdf:subPropertyOf skos:broader .
}
group by ?grandparent
order by DESC(?distance)


but using ?p+ or ?p* throws an error.




Unexpected token syntax error, unexpected <variable>, expecting <decimal literal> or <double literal> or <integer literal>




How can I use */+ with variables?










share|improve this question




















  • 2





    (<p>|!<p>)* is the workaround here given that it's not part of the specs. Note, it should be clear that this might work bad in worst case regarding performance given - path queries are not that simple to evaluate without appropriate index etc

    – AKSW
    Nov 14 '18 at 3:55








  • 1





    I forgot to say, indeed you won't be able to reuse the variable bound to all the subproperties of the given property. Still, don't know whether you really need this in your case. I mean, how many subproperties do you have in your dataset?

    – AKSW
    Nov 14 '18 at 7:59











  • Thanks, turns out i have 3. But the reason i would have liked to use a variable is to capture the dynamic addition of more.

    – user1411110
    Nov 14 '18 at 18:52
















0















I have some working code for getting all the ancestors of a term in a hierarchy.
Following:



    PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX skos-xl: <http://www.w3.org/2008/05/skos-xl#>
PREFIX rdf: <http://www.w3.org/2000/01/rdf-schema#>

select ?grandparentliteralform (count(?parent) as ?distance)
{ ?iri skos:broader+ ?parent .
?parent skos:broader* ?grandparent .
?grandparent skos-xl:prefLabel ?grandparentlabel .
?grandparentlabel skos-xl:literalForm ?grandparentliteralform .
}
group by ?grandparent
order by DESC(?distance)


It breaks when an IRI's broader predicate is a subproperty (?p rdf:subPropertyOf skos:broader)
So right now I am doing this to capture all the subproperty predicates:



select  ?grandparentliteralform (count(?parent) as ?distance)
{ ?iri ?p ?parent .
?parent skos:broader* ?grandparent .
?grandparent skos-xl:prefLabel ?grandparentlabel .
?grandparentlabel skos-xl:literalForm ?grandparentliteralform .
?p rdf:subPropertyOf skos:broader .
}
group by ?grandparent
order by DESC(?distance)


What i would really like to do is :



select  ?grandparentliteralform (count(?parent) as ?distance)
{ ?iri ?p+ ?parent .
?parent ?p* ?grandparent .
?grandparent skos-xl:prefLabel ?grandparentlabel .
?grandparentlabel skos-xl:literalForm ?grandparentliteralform .
?p rdf:subPropertyOf skos:broader .
}
group by ?grandparent
order by DESC(?distance)


but using ?p+ or ?p* throws an error.




Unexpected token syntax error, unexpected <variable>, expecting <decimal literal> or <double literal> or <integer literal>




How can I use */+ with variables?










share|improve this question




















  • 2





    (<p>|!<p>)* is the workaround here given that it's not part of the specs. Note, it should be clear that this might work bad in worst case regarding performance given - path queries are not that simple to evaluate without appropriate index etc

    – AKSW
    Nov 14 '18 at 3:55








  • 1





    I forgot to say, indeed you won't be able to reuse the variable bound to all the subproperties of the given property. Still, don't know whether you really need this in your case. I mean, how many subproperties do you have in your dataset?

    – AKSW
    Nov 14 '18 at 7:59











  • Thanks, turns out i have 3. But the reason i would have liked to use a variable is to capture the dynamic addition of more.

    – user1411110
    Nov 14 '18 at 18:52














0












0








0








I have some working code for getting all the ancestors of a term in a hierarchy.
Following:



    PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX skos-xl: <http://www.w3.org/2008/05/skos-xl#>
PREFIX rdf: <http://www.w3.org/2000/01/rdf-schema#>

select ?grandparentliteralform (count(?parent) as ?distance)
{ ?iri skos:broader+ ?parent .
?parent skos:broader* ?grandparent .
?grandparent skos-xl:prefLabel ?grandparentlabel .
?grandparentlabel skos-xl:literalForm ?grandparentliteralform .
}
group by ?grandparent
order by DESC(?distance)


It breaks when an IRI's broader predicate is a subproperty (?p rdf:subPropertyOf skos:broader)
So right now I am doing this to capture all the subproperty predicates:



select  ?grandparentliteralform (count(?parent) as ?distance)
{ ?iri ?p ?parent .
?parent skos:broader* ?grandparent .
?grandparent skos-xl:prefLabel ?grandparentlabel .
?grandparentlabel skos-xl:literalForm ?grandparentliteralform .
?p rdf:subPropertyOf skos:broader .
}
group by ?grandparent
order by DESC(?distance)


What i would really like to do is :



select  ?grandparentliteralform (count(?parent) as ?distance)
{ ?iri ?p+ ?parent .
?parent ?p* ?grandparent .
?grandparent skos-xl:prefLabel ?grandparentlabel .
?grandparentlabel skos-xl:literalForm ?grandparentliteralform .
?p rdf:subPropertyOf skos:broader .
}
group by ?grandparent
order by DESC(?distance)


but using ?p+ or ?p* throws an error.




Unexpected token syntax error, unexpected <variable>, expecting <decimal literal> or <double literal> or <integer literal>




How can I use */+ with variables?










share|improve this question
















I have some working code for getting all the ancestors of a term in a hierarchy.
Following:



    PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX skos-xl: <http://www.w3.org/2008/05/skos-xl#>
PREFIX rdf: <http://www.w3.org/2000/01/rdf-schema#>

select ?grandparentliteralform (count(?parent) as ?distance)
{ ?iri skos:broader+ ?parent .
?parent skos:broader* ?grandparent .
?grandparent skos-xl:prefLabel ?grandparentlabel .
?grandparentlabel skos-xl:literalForm ?grandparentliteralform .
}
group by ?grandparent
order by DESC(?distance)


It breaks when an IRI's broader predicate is a subproperty (?p rdf:subPropertyOf skos:broader)
So right now I am doing this to capture all the subproperty predicates:



select  ?grandparentliteralform (count(?parent) as ?distance)
{ ?iri ?p ?parent .
?parent skos:broader* ?grandparent .
?grandparent skos-xl:prefLabel ?grandparentlabel .
?grandparentlabel skos-xl:literalForm ?grandparentliteralform .
?p rdf:subPropertyOf skos:broader .
}
group by ?grandparent
order by DESC(?distance)


What i would really like to do is :



select  ?grandparentliteralform (count(?parent) as ?distance)
{ ?iri ?p+ ?parent .
?parent ?p* ?grandparent .
?grandparent skos-xl:prefLabel ?grandparentlabel .
?grandparentlabel skos-xl:literalForm ?grandparentliteralform .
?p rdf:subPropertyOf skos:broader .
}
group by ?grandparent
order by DESC(?distance)


but using ?p+ or ?p* throws an error.




Unexpected token syntax error, unexpected <variable>, expecting <decimal literal> or <double literal> or <integer literal>




How can I use */+ with variables?







sparql kleene-star






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 2:42









TallTed

6,28321527




6,28321527










asked Nov 14 '18 at 1:56









user1411110user1411110

89811424




89811424








  • 2





    (<p>|!<p>)* is the workaround here given that it's not part of the specs. Note, it should be clear that this might work bad in worst case regarding performance given - path queries are not that simple to evaluate without appropriate index etc

    – AKSW
    Nov 14 '18 at 3:55








  • 1





    I forgot to say, indeed you won't be able to reuse the variable bound to all the subproperties of the given property. Still, don't know whether you really need this in your case. I mean, how many subproperties do you have in your dataset?

    – AKSW
    Nov 14 '18 at 7:59











  • Thanks, turns out i have 3. But the reason i would have liked to use a variable is to capture the dynamic addition of more.

    – user1411110
    Nov 14 '18 at 18:52














  • 2





    (<p>|!<p>)* is the workaround here given that it's not part of the specs. Note, it should be clear that this might work bad in worst case regarding performance given - path queries are not that simple to evaluate without appropriate index etc

    – AKSW
    Nov 14 '18 at 3:55








  • 1





    I forgot to say, indeed you won't be able to reuse the variable bound to all the subproperties of the given property. Still, don't know whether you really need this in your case. I mean, how many subproperties do you have in your dataset?

    – AKSW
    Nov 14 '18 at 7:59











  • Thanks, turns out i have 3. But the reason i would have liked to use a variable is to capture the dynamic addition of more.

    – user1411110
    Nov 14 '18 at 18:52








2




2





(<p>|!<p>)* is the workaround here given that it's not part of the specs. Note, it should be clear that this might work bad in worst case regarding performance given - path queries are not that simple to evaluate without appropriate index etc

– AKSW
Nov 14 '18 at 3:55







(<p>|!<p>)* is the workaround here given that it's not part of the specs. Note, it should be clear that this might work bad in worst case regarding performance given - path queries are not that simple to evaluate without appropriate index etc

– AKSW
Nov 14 '18 at 3:55






1




1





I forgot to say, indeed you won't be able to reuse the variable bound to all the subproperties of the given property. Still, don't know whether you really need this in your case. I mean, how many subproperties do you have in your dataset?

– AKSW
Nov 14 '18 at 7:59





I forgot to say, indeed you won't be able to reuse the variable bound to all the subproperties of the given property. Still, don't know whether you really need this in your case. I mean, how many subproperties do you have in your dataset?

– AKSW
Nov 14 '18 at 7:59













Thanks, turns out i have 3. But the reason i would have liked to use a variable is to capture the dynamic addition of more.

– user1411110
Nov 14 '18 at 18:52





Thanks, turns out i have 3. But the reason i would have liked to use a variable is to capture the dynamic addition of more.

– user1411110
Nov 14 '18 at 18:52












2 Answers
2






active

oldest

votes


















2














You can't. As the Property Paths section of the SPARQL 1.1 spec states:




The ends of the path may be RDF terms or variables. Variables can not be used as part of the path itself, only the ends.







share|improve this answer































    0














    You could potentially use alternatives to capture this:



    ?parent (skos:broader|your:alternative)* ?grandparent


    Exact form will need to reflect your data structure and whether you want to allow mixes of skos:broader and your alternative (which my example allows). You can move the * operator inside the brackets and add it to each alternative if you want pure chains of specific properties.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292101%2fhow-to-use-a-kleene-star-operator-or-its-variant-with-variables-in-spar%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      You can't. As the Property Paths section of the SPARQL 1.1 spec states:




      The ends of the path may be RDF terms or variables. Variables can not be used as part of the path itself, only the ends.







      share|improve this answer




























        2














        You can't. As the Property Paths section of the SPARQL 1.1 spec states:




        The ends of the path may be RDF terms or variables. Variables can not be used as part of the path itself, only the ends.







        share|improve this answer


























          2












          2








          2







          You can't. As the Property Paths section of the SPARQL 1.1 spec states:




          The ends of the path may be RDF terms or variables. Variables can not be used as part of the path itself, only the ends.







          share|improve this answer













          You can't. As the Property Paths section of the SPARQL 1.1 spec states:




          The ends of the path may be RDF terms or variables. Variables can not be used as part of the path itself, only the ends.








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 2:38









          TallTedTallTed

          6,28321527




          6,28321527

























              0














              You could potentially use alternatives to capture this:



              ?parent (skos:broader|your:alternative)* ?grandparent


              Exact form will need to reflect your data structure and whether you want to allow mixes of skos:broader and your alternative (which my example allows). You can move the * operator inside the brackets and add it to each alternative if you want pure chains of specific properties.






              share|improve this answer




























                0














                You could potentially use alternatives to capture this:



                ?parent (skos:broader|your:alternative)* ?grandparent


                Exact form will need to reflect your data structure and whether you want to allow mixes of skos:broader and your alternative (which my example allows). You can move the * operator inside the brackets and add it to each alternative if you want pure chains of specific properties.






                share|improve this answer


























                  0












                  0








                  0







                  You could potentially use alternatives to capture this:



                  ?parent (skos:broader|your:alternative)* ?grandparent


                  Exact form will need to reflect your data structure and whether you want to allow mixes of skos:broader and your alternative (which my example allows). You can move the * operator inside the brackets and add it to each alternative if you want pure chains of specific properties.






                  share|improve this answer













                  You could potentially use alternatives to capture this:



                  ?parent (skos:broader|your:alternative)* ?grandparent


                  Exact form will need to reflect your data structure and whether you want to allow mixes of skos:broader and your alternative (which my example allows). You can move the * operator inside the brackets and add it to each alternative if you want pure chains of specific properties.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 20 '18 at 11:04









                  RobVRobV

                  22.8k964106




                  22.8k964106






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292101%2fhow-to-use-a-kleene-star-operator-or-its-variant-with-variables-in-spar%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Bressuire

                      Vorschmack

                      Quarantine