How to retrieve elements of OWL enumerated datatyp

2019-07-13 07:53发布

问题:

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?

回答1:

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.



标签: sparql rdf owl