2

What SPARQL query should I use to show all values of union of two datatypes? Additionally how can I count the number of values in this union of datatypes? Each datatype was defined with DataOneOf(...) axiom.

EDIT: to start with something: what SPARQL query should I use to show all values of the selected datatype?

Annabelle
  • 734
  • 9
  • 23

1 Answers1

4

In the future, it would be much easier if you can provide minimal data that we can work with. It would also make the question a bit clearer. As I understand it, you've defined some datatypes using the dataOneOf construct to enumerate several literals. First, let's create some sample data. Here's an OWL ontology with two properties, each of which has an enmerated datatype expression as its range. One of them has {"one", "two"} as the range, and the other has {1, 2}:

@prefix :      <http://example.org/datatypes#> .
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix owl:   <http://www.w3.org/2002/07/owl#> .
@prefix xsd:   <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> .

:p1     a           owl:DatatypeProperty ;
        rdfs:range  [ a          rdfs:Datatype ;
                      owl:oneOf  [ a          rdf:List ;
                                   rdf:first  1 ;
                                   rdf:rest   [ a          rdf:List ;
                                                rdf:first  2 ;
                                                rdf:rest   ()

                                              ]
                                 ]
                    ] .

:p2     a           owl:DatatypeProperty ;
        rdfs:range  [ a          rdfs:Datatype ;
                      owl:oneOf  [ a          rdf:List ;
                                   rdf:first  "one" ;
                                   rdf:rest   [ a          rdf:List ;
                                                rdf:first  "two" ;
                                                rdf:rest   ()

                                              ]
                                 ]
                    ] .

Notice that the list of possible values in the datatype expression are given as an RDF list. It's easiest to query RDF lists using property paths, and there are lots of other examples of that here on Stack Overflow. Here's what the query could look like that will retrieve the datatypes along with each of their elements and the datatype of those elements (in case you want that, too).

prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix owl:   <http://www.w3.org/2002/07/owl#>
prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#>

select ?dt ?element ?elementType {
  ?dt a rdfs:Datatype ;
      owl:oneOf/rdf:rest*/rdf:first ?element .
  bind(datatype(?element) as ?elementType)
}
---------------------------------------------------------------
| dt   | element | elementType                                |
===============================================================
| _:b0 | "one"   | <http://www.w3.org/2001/XMLSchema#string>  |
| _:b0 | "two"   | <http://www.w3.org/2001/XMLSchema#string>  |
| _:b1 | 1       | <http://www.w3.org/2001/XMLSchema#integer> |
| _:b1 | 2       | <http://www.w3.org/2001/XMLSchema#integer> |
---------------------------------------------------------------

Note that often time such datatypes are represented by blank nodes, which means that they don't have associated IRIs. In this case, for example, they show up as _:b0 and _:b1. Hopefully, though, you have some way to identify the particular datatype expressions that you're interested in, e.g., by asking for the range of some particular property, or something like that.

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
  • I've got a small subquestion to your great answer. ?dt a rdfs:Datatype; selects all enums in the ontology. How can I modify it, in order to bind elements of only p1 and p2 from your example (and not others)? – Annabelle May 06 '16 at 13:27
  • @Annabelle They're not the "elements of p1 and p2", but the values of the ranges of p1 and p2. In this case, you could add `values ?p { :p1 :p2 } ?p rdfs:range ?dt .` to the query. That says that you only want ?dt values that are the rdfs:range of the specified properties ?p, which values sets to be either :p1 or :p2. – Joshua Taylor May 06 '16 at 14:15
  • I'm indeed confusing this issue, but this is incorrect: DatatypeDefinition( :A DataOneOf( "a1" "a2" ) ) DatatypeDefinition( :B DataOneOf( "b1" "b2" ) ) DatatypeDefinition( :C DataOneOf( "c1" "c2" ) ) select ?dt ?element ?elementType { ?dt a rdfs:Datatype ; owl:oneOf/rdf:rest*/rdf:first ?element . bind(datatype(?element) as ?elementType) . values ?p { my:A my:C } ?p rdfs:range ?dt } – Annabelle May 06 '16 at 14:30
  • @Annabelle I was basing the retrieval methods on the ontology that I provided. There are certainly other ways to select the data types if they're identified by IRIs. In your case, it looks like it would be `values ?dt {:A :B }` if you only want ?dt to be A or B. – Joshua Taylor May 06 '16 at 14:33
  • @annaBelle if you show the ontology you've got, we mit actually be able to solve this, but without some real data, it's not really an examine-able issue – Joshua Taylor May 06 '16 at 15:38
  • @annabelle you added an owl ontology, but sparql is an rdf query language. Could you add the rdf version of the ontology? – Joshua Taylor May 06 '16 at 23:35
  • I edited it - for me it is just easier to read in functional-style syntax – Annabelle May 07 '16 at 11:05
  • @annabelle agreed, it's definitely easier to read the functional syntax. The problem is that sparql has to query the rdf mapping of the ontology, so we really need those details. – Joshua Taylor May 07 '16 at 11:19
  • In this case, note that the axiom is encoded by saying that :A is owl:equivalentClass to the datatype class expression. That's the extra link you need between the class IRI and the expression. – Joshua Taylor May 07 '16 at 11:20
  • I agree, and that is why your SPARQL query returns all datatype results, I mean: { "a1" "a2" "b1" "b2" "c1" "c2" }. This is already helpful, but I need a little bit more precise result. And I am intending to make the link to A and C IRIs (what you also have just mentioned), unsuccessfully unfortunatelly. – Annabelle May 07 '16 at 11:46