sparqltriplestoretriples

What SPARQL query will return the data from the example?


In my RDF store I have the following triples

For schema:

ex:animal rdf:type rdf:Class;
    rdfs:subClassof ex:Package_animal.

ex:height rdf:type rdf:Property;
    rdfs:label "height";
    rdfs:domain ex:animal.

ex:age rdf:type rdf:Property;
    rdfs:label "age";
    rdfs:domain ex:animal.

ex:NumberOfLegs rdf:type rdf:Property;
    rdfs:label "NumberOfLegs";
    rdfs:domain ex:animal.

For instances:

ex:dog rdf:type ex:animal;
    ex:height "10";
    ex:age "2";
    ex:NumberOfLegs "4".

ex:cat rdf:type ex:animal;
    ex:height "10";
    ex:age "3";
    ex:NumberOfLegs "4".

I want to:

  1. Get properties of class ex:animal
ex:height 
ex:age
ex:NumberOfLegs
  1. Get the instance ex:dog of class ex:animal with all of its properties
ex:dog rdf:type ex:animal
ex:dog ex:height "10"
ex:dog ex:age "2"
ex:dog ex:NumberOfLegs "4"
  1. The SPARQL I thought was:
SELECT ?s
WHERE
{
   ?p rdf:type rdf:Property.
   ?s ?p ?y.
   ?s rdf:type ex:dog.
}

Solution

  • This query

    SELECT ?propertyName ?value
    WHERE
    {
        ex:dog ?propertyName ?value.
    }
    

    will give you this result

    propertyName    value
    rdf:type        ex:animal
    ex:height       "10"
    ex:age          "2"
    ex:NumberOfLegs "4"
    

    You can bound ex:dog URI if you want it in the result

    SELECT ?dog ?propertyName ?value
    WHERE
    {
        BIND(ex:dog as ?dog).
        ?dog rdf:type ex:animal.
        ?dog ?propertyName ?value.
    }
    

    which will yield

    dog     propertyName     value
    ex:dog  rdf:type         ex:animal
    ex:dog  ex:height        "10"
    ex:dog  ex:age           "2"
    ex:dog  ex:NumberOfLegs  "4"
    

    If you want to grab all properties of ex:animal you'd need to change your data. I edited your question so it contains the data shown below. For scheme knowledge:

       ex:animal rdf:type rdf:Class;
          rdfs:subClassof ex:Package_animal.
    
       ex:height rdf:type rdf:Property;
          rdfs:label "height";
          rdfs:domain ex:animal.
    
       ex:age rdf:type rdf:Property;
          rdfs:label "age";
          rdfs:domain ex:animal.
    
       ex:NumberOfLegs rdf:type rdf:Property;
          rdfs:label "NumberOfLegs";
          rdfs:domain ex:animal.
    

    For instances knowledge:

       ex:dog rdf:type ex:animal;
          ex:height "10";
          ex:age "2";
          ex:NumberOfLegs "4".
    
       ex:cat rdf:type ex:animal;
          ex:height "10";
          ex:age "3";
          ex:NumberOfLegs "4".
    

    Then you can use this SPARQL query:

    SELECT ?dog ?animalPropertyName ?value
    WHERE
    {
        ?animalPropertyName rdfs:domain ex:animal.
        
        BIND(ex:dog as ?dog).  
        ?dog ?animalPropertyName ?value.
    }
    

    which will yield

    dog     animalPropertyName  value
    ex:dog  ex:height           "10"
    ex:dog  ex:age              "2"
    ex:dog  ex:NumberOfLegs     "4"