Querying with SPARQL

latest update 2014-04-20  

Introduction

The use of SPARQL is essential for the implementation of ISO 15926.

For detailed information about SPARQL read http://www.w3.org/TR/sparql11-query/

In this document a growing number of queries will be demonstrated. These may not always be the most efficient, but they work.

One detail to be aware of is, that the SPARQL 1.1 capabilities of the triple stores may, unfortunately, sometimes vary. So in case a query doesn't work on your triple store, that may the reason. Please report such problems.

The queries below will get a temporary identifier, but that is communication reasons only.

Tutorials

See first https://www.youtube.com/watch?v=FvGndkpa4K0 and then http://jena.apache.org/tutorials/sparql.html

In case you seriously want to use SPARQL, then buy the book "Learning SPARQL: Querying and Updating with SPARQL 1.1" Second Edition, written by Bob DuCharme, published by O'Reilly, ISBN 978-1-449-37143-2 .

Triples

In this paper we will work from the results of the mapping of a Line List, as we stored them in a triple store. Click here for that file, so that you can store it in your triple store.

We added three small files to experiment with changes:

  1. An update, dated 2013-03-25T23:59:59Z, changing the nominal diameter of Line CO_RZ17801-a from 3 to 4 inch;
  2. An update, dated 2013-07-15T23:59:59Z, changing the nominal diameter of Line CO_RZ17801-a from 4 to 6 inch;
  3. An update, dated 2013-08-15T15:31:00Z, adding assembly information of Line CO_RZ17801, one template about CO_RZ17801-a and one about CO_RZ17801-b;
  4. An update, dated 2013-09-21T00:00:00Z, changing the volume flow rate of CO_RZ17801-S from 8.55 to 9.3 m3/hr.

PREFIX's

A query starts with PREFIX's, but only those that are actually used in the query. Here is the list:

PREFIX owl: <http://www.w3.org/2002/07/owl#> 
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> 
PREFIX fn: <http://www.w3.org/2005/xpath-functions#> 
PREFIX sfn: <http://www.w3.org/ns/sparql#> 
PREFIX dc:  <http://purl.org/dc/elements/1.1/> 
PREFIX skos: <http://www.w3.org/2004/02/skos/core#> 
PREFIX dm:  <http://rds.posccaesar.org/2008/02/OWL/ISO-15926-2_2003#> 
PREFIX tpl: <http://15926.blog/templates/> 
PREFIX rdl: <http://data.15926.org/rdl/> 

QUERY #1 - Find all

If you want to see everything in the triple store, use this query:

SELECT *

WHERE { ?s ?p ?o }

ORDER BY ?s

LIMIT 24

This results in:

The variables are here: ?s = ?subject, ?p = ? property, ?o=?object. Instead of these you can use anything else in any natural language, as is shown in the following queries.

ORDER BY ?s means (optionally) that all triples are ordered by the given variable

LIMIT 24means (optionally) that you limit the number of returned triples to 24 (you might be waiting for ten million triples to appear!)


QUERY #2 - Find UUID for a Tag

During declaration every OOI (Object Of Interest)will get a Tag of any kind (tagnumber P-101, Serial Number DF-843982BN, etc).

The query:

# Find UUID for a Tag

PREFIX skos: <http://www.w3.org/2004/02/skos/core#>

SELECT ?tag ?OOI

WHERE {

 ?OOI skos:prefLabel ?tag .

 FILTER(?tag = "CO_RZ17801-S") # input

}

returns the related UUID of a given tag:


QUERY #3 - Find all superclasses of a Class

Assume:

<owl:Class rdf:ID="C1"> <rdfs:subclassOf rdf:resource="#C2"/> </owl:Class>

<owl:Class rdf:ID="C2"> <rdfs:subclassOf rdf:resource="#C3"/> </owl:Class>

<owl:Class rdf:ID="C3"> <rdfs:subclassOf rdf:resource="#C4"/> <rdfs:subclassOf rdf:resource="#C8"/> </owl:Class>

<owl:Class rdf:ID="C4"> <rdfs:subclassOf rdf:resource="#C5"/> </owl:Class>

<owl:Class rdf:ID="C5"> <rdfs:subclassOf rdf:resource="#C6"/> </owl:Class>

<owl:Class rdf:ID="C6"/>

<owl:Class rdf:ID="C8"> <rdfs:subclassOf rdf:resource="#C9"/> </owl:Class>

<owl:Class rdf:ID="C9"/>

We want to know all superclasses of C2 (mind the * ):

# Find all superclasses of a Class

SELECT ?class

WHERE { http://example.org/C2 rdfs:subClassOf* ?class }

this results in:

NOTES

  1. Note the random order of the result.
  2. Not possible with the RDL, because there dm:Specialization has been used instead of rdfs:subClassOf

0050.rq - Find the OIM (Object Information Model) for a  given Tag, with history

The OIM is the collection of templates in which a thing plays a role. Here we ask for the OIM of a Tag, valid at all time in history.

NOTE - The "Tag" = the hash URI ending with the tag.The preceeding colon : refers to the base URI, see PREFIX :

# file 0050.rq

# Find OIM (Object Information Model) of a given tag, with history

PREFIX : <http://www.p1234.xyz-corp.com/>

PREFIX xsd:  <http://www.w3.org/2001/XMLSchema#>

PREFIX skos: <http://www.w3.org/2004/02/skos/core#>

PREFIX meta: <http://data.15926.org/meta/>

SELECT ?tag ?template ?information ?effective_date ?deprecation_date

WHERE {

 ?template ?p ?tag . FILTER (?tag = :CO_RZ17801-S ) .

 FILTER NOT EXISTS { ?tag meta:valDeprecationDate ?deprecation_date } .

 ?template skos:definition ?information .

 ?template meta:valEffectiveDate ?effective_date .

 OPTIONAL { ?template meta:valDeprecationDate ?deprecation_date } .

 }

results in:

The VOLUME FLOW RATE of CO_RZ17801-S has changed from 8.55 to 9.3 m3/hr on 2013-09-29T00:00:00Z (the list is not ordered in any way).


0051.rq - Find OIM (Object Information Model) of a given tag at a given dateTime

The OIM is the collection of templates in which a thing plays a role. Here we ask for the OIM of a Tag, valid at a given date-time.

NOTE - The "Tag" = the slash URI ending with the tag.The preceeding colon : refers to the base URI, see PREFIX :

# file 0051.rq

# Find OIM (Object Information Model) of a given tag at a given dateTime

PREFIX : <http://www.p1234.xyz-corp.com/>

PREFIX xsd:  <http://www.w3.org/2001/XMLSchema#>

PREFIX skos: <http://www.w3.org/2004/02/skos/core#>

PREFIX meta: <http://data.15926.org/meta/>

SELECT ?tag ?template ?information ?effective_date

WHERE {

 ?template ?p ?tag . FILTER (?tag = :CO_RZ17801-S ) . # input

 FILTER NOT EXISTS { ?tag meta:valDeprecationDate ?deprecation_date } .

 ?template skos:definition ?information .

 ?template meta:valEffectiveDate ?effective_date .

 OPTIONAL { ?template meta:valDeprecationDate ?deprecation_date } .

 FILTER ( xsd:dateTime(?effective_date) <= "2013-09-19T00:00:00Z"^^xsd:dateTime ) . # input

 FILTER ( xsd:dateTime(?deprecation_date) > "2013-09-19T00:00:00Z"^^xsd:dateTime || NOT EXISTS {?template meta:valDeprecationDate ?deprecation_date } ) .

}

results in:

The VOLUME FLOW RATE of CO_RZ17801-S is still 8.55 m3/hr on 2013-09-19T00:00:00Z .

If we now shift the dateTime of query to 2013-09-21T00:00:00Z:

# file 0051.rq

# Find OIM (Object Information Model) of a given tag at a given dateTime

PREFIX : <http://www.p1234.xyz-corp.com/>

PREFIX xsd:  <http://www.w3.org/2001/XMLSchema#>

PREFIX skos: <http://www.w3.org/2004/02/skos/core#>

PREFIX meta: <http://data.15926.org/meta/>

SELECT ?tag ?template ?information ?effective_date

WHERE {

 ?template ?p ?tag . FILTER (?tag = :CO_RZ17801-S ) . # input

 FILTER NOT EXISTS { ?tag meta:valDeprecationDate ?deprecation_date } .

 ?template skos:definition ?information .

 ?template meta:valEffectiveDate ?effective_date .

 OPTIONAL { ?template meta:valDeprecationDate ?deprecation_date } .

 FILTER ( xsd:dateTime(?effective_date) <= "2013-09-21T00:00:00Z"^^xsd:dateTime ) . # input

 FILTER ( xsd:dateTime(?deprecation_date) > "2013-09-21T00:00:00Z"^^xsd:dateTime || NOT EXISTS {?template meta:valDeprecationDate ?deprecation_date } ) .

}

we get the new value of 9.3 m3/hr (on the 5th row), since that replaced the 8.55 value on 2013-09-20T00:00:00Z:

(the ordering of such lists are random).


0052.rq - Find OIM (Object Information Model) of a given tag at query dateTime

The OIM is the collection of templates in which a thing plays a role. Here we ask for the OIM of a Tag, valid at query dateTime. This means that we ask for templates in which the given tag plays a role AND that are not deprecated.

NOTE - The "Tag" = the slash URI ending with the tag.The preceeding colon : refers to the base URI, see PREFIX :

# file 0052.rq

# Find OIM (Object Information Model) of a given tag at query dateTime

PREFIX : <http://www.p1234.xyz-corp.com/>

PREFIX xsd:  <http://www.w3.org/2001/XMLSchema#>

PREFIX meta: <http://data.15926.org/meta/>

SELECT ?tag ?template ?information ?effective_date

WHERE {

 ?template ?p ?tag . FILTER (?tag = :CO_RZ17801-S ) .

 ?template skos:definition ?information .

 ?template meta:valEffectiveDate ?effective_date .

 FILTER ( NOT EXISTS {?template meta:valDeprecationDate ?deprecation_date } ) .

 }

results in:


0055.rq - Find the value of a given indirect property of a given tag, valid at a given dateTime

Now we want to find that Volume Flow Rate (RDS380834) of stream CO_RZ17801-S as valid on 2013-09-19T00:00:00Z:

# 0055.rq

# Find the value of a given indirect property of a given tag, valid at a given dateTime

PREFIX : <http://www.p1234.xyz-corp.com/>

PREFIX xsd:  <http://www.w3.org/2001/XMLSchema#>

PREFIX skos: <http://www.w3.org/2004/02/skos/core#>

PREFIX meta: <http://data.15926.org/meta/>

PREFIX tpl: <http://15926.blog/templates/>

PREFIX rdl: <http://data.15926.org/rdl/>

SELECT ?tag ?template ?property_name ?value ?scale_name ?effective_date ?deprecation_date

WHERE {

 ?template ?p ?tag . FILTER (?tag = :CO_RZ17801-S ) .

 FILTER NOT EXISTS { ?tag meta:valDeprecationDate ?deprecation_date } .

 ?template tpl:hasPropertyRangeType ?property_ID .

 FILTER ( ?property_ID = rdl:RDS380834 ) .

 SERVICE <http://15926.org/endpoint/sparql> { ?property_ID RDL:hasDesignation ?property_name . } .

 ?template tpl:valPointValue ?value .

 ?template tpl:hasPropertyScale ?scale_ID .

 ?template meta:valEffectiveDate ?effective_date .

 OPTIONAL { ?template meta:valDeprecationDate ?deprecation_date } .

 FILTER ( xsd:dateTime(?effective_date) <= "2013-09-19T00:00:00Z"^^xsd:dateTime ) . # input

 FILTER ( xsd:dateTime(?deprecation_date) > "2013-09-19T00:00:00Z"^^xsd:dateTime || NOT EXISTS {?template meta:valDeprecationDate ?deprecation_date } ) .

 SERVICE <http://15926.org/endpoint/sparql> { ?scale_ID RDL:hasDesignation ?scale_name . } .

}

results in:

and if we change the dateTime to 2013-09-21T00:00:00Z, we get its successor template instance with a value of 9.3 m3/hr:


0060.rq - Find templates that are about a particular indirect property

# 0060.rq

# Find the templates of all times that are about a particular indirect property

PREFIX tpl: <http://15926.blog/templates/>

PREFIX rdl:  <http://data.15926.org/rdl/>

SELECT ?template_ID ?property_ID ?property_name

WHERE { ?template_ID tpl:hasIndirectPropertyType ?property_ID

 FILTER ( ?property_ID = rdl:RDS366794 ) # input

 SERVICE <http://15926.org/endpoint/sparql> { ?property_ID RDL:hasDesignation ?property_name . }

}

results in:


0100.rq - Find the tags to which are connected to a given tag, valid at a given dateTime

This query, that uses the Mapping of a P & ID and the Mapping of a LineList>:

# 0100.rq

# Find the tags to which a given tag is directly connected, valid at a given dateTime

PREFIX : <http://www.p1234.xyz-corp.com/>

PREFIX meta: <http://data.15926.org/meta/>

PREFIX rdl:  <http://data.15926.org/rdl/>

PREFIX tpl:  <http://15926.blog/templates/>

SELECT ?tag_side1 ?tag_side2 ?connectiontype_name ?effective_date ?deprecation_date

WHERE {

 ?template ?p ?tag . FILTER (?tag = :CO_B14-RZ17801-S1 ) .

 FILTER NOT EXISTS { ?tag meta:valDeprecationDate ?tag_deprecation_date } .

 ?template tpl:hasUrClassOfSide1 ?tag_side1 .

 ?template tpl:hasUrClassOfSide2 ?tag_side2 .

 ?template tpl:hasConnectionType ?connection_type .

 ?connection_type rdfs:label ?connectiontype_name .

 ?template meta:valEffectiveDate ?effective_date .

 OPTIONAL { ?template meta:valDeprecationDate ?deprecation_date } .

 FILTER ( xsd:dateTime(?effective_date) <= "2013-09-19T00:00:00Z"^^xsd:dateTime ) . # input

 FILTER ( xsd:dateTime(?deprecation_date) > "2013-09-19T00:00:00Z"^^xsd:dateTime || NOT EXISTS {?template meta:valDeprecationDate ?deprecation_date } ) .

}

results in:

See for reference the line marked-up in red below:


0900.rq - Find the sameAs objects

Assume that we have the following information in the façade:

<owl:Thing rdf:about="TAG_A">

    <rdf:type rdf:resource="&dm;ClassOfInanimatePhysicalObject"/>

    <rdfs:subClassOf rdf:resource="&rdl;RDS459873"/>

    <meta:valEffectiveDate rdf:datatype="&xsd;dateTime">2004-11-24T14:57:00Z</meta:valEffectiveDate>

</owl:Thing>

 

<owl:Thing rdf:about="TAG_B">

    <owl:sameAs rdf:resource="TAG_A"/>

    <meta:valEffectiveDate rdf:datatype="&xsd;dateTime">2011-09-04T13:45:00Z</meta:valEffectiveDate>

</owl:Thing>

To find one or more objects that are owl:sameAs a given object, the following query applies:

# 0900.rq

# Find sameAs Tag (if any)

PREFIX owl:  <http://www.w3.org/2002/07/owl#>

PREFIX : <http://www.p1234.xyz-corp.com/>

SELECT ?sameAsTag

WHERE { ?sameAsTag owl:sameAs :TAG_A . }

This results in:


0910.rq - Find all objects in the RDL that are of a given Part 2 entity type

SELECT ?class ?className  
WHERE {
    ?class rdfs:label ?className .
    ?class rdf:type <http://data.15926.org/dm/ClassOfFunctionalObject>}
ORDER BY ?className
LIMIT 10


This results in (list is shortened):


class className
http://data.15926.org/rdl/RDS2225333 ABSOLUTE HUMIDITY MEASURING ELEMENT
http://data.15926.org/rdl/RDS2224044 ABSOLUTE HUMIDITY MEASURING INSTRUMENT
http://data.15926.org/rdl/RDS6501474 ABSOLUTE HUMIDITY TRANSMITTER
http://data.15926.org/rdl/RDS222588 ABSOLUTE PRESSURE MEASURING ELEMENT
http://data.15926.org/rdl/RDS2224060 ABSOLUTE PRESSURE MEASURING INSTRUMENT
http://data.15926.org/rdl/RDS819044 ABSOLUTE PRESSURE TRANSMITTER
http://data.15926.org/rdl/RDS779695261 ABSORBER
http://data.15926.org/rdl/RDS2220092 AC POWER SUPPLY
http://data.15926.org/rdl/RDS222500 ACCELERATION MEASURING ELEMENT
http://data.15926.org/rdl/RDS2227120 ACCELLERATION MEASURING INSTRUMENT