Querying Product Data

SPARQL is a query language for RDF data.

From inside one or several RDF graph(s), SPARQL can perform the following:

  • CONSTRUCT a new RDF graph;
  • DELETE existing data, INSERT new data;
  • SELECT data for extraction into tables.

Only the SELECT queries will be considered in this guide.

 

Introduction to querying product data

Querying product data can be achieved using the SELECT clause. If we want to select all the contracts in a given LSA database, we can do the following:

SELECT ?contract ?contractIdentifier WHERE { 
    ?contract a s3kl:Contract . 
    ?contract s3kl:contractIdentifier ?contractIdentifier .
}

The SELECT clause contains the columns we want to extract. Here, two columns, entitled "contract" and "contractIdentifier" respectively, will be in the output table.

The WHERE clause contains zero, one or several graph patterns, each consisting of triple patterns. Each triple pattern is executed and results in changes in the search space. A triple pattern that yields nothing results in the termination of the search. Otherwise, the search goes on and the next triple pattern in the same graph pattern is executed, and so on. Here, there is one graph pattern with two triple patterns.

First triple pattern

The first triple pattern ?contract a s3kl:Contract yields one result from the bicycle example because the data contains the following triple:

<urn:uuid:14cb22ac-0c36-4f12-9d7b-828ef37f895d> a s3kl:Contract.

Past this point, the ?contract variable becomes bound (to the sole result) and any subsequent triple pattern that uses that variable leads to a narrower search space.

Second triple pattern

The second triple pattern is then executed and searches for any of the matched ?contract with s3kl:contractIdentifier as property. Fortunately, there is one triple that matches this search:

<urn:uuid:14cb22ac-0c36-4f12-9d7b-828ef37f895d> s3kl:contractIdentifier "BICYCLE-ASD-CONTRACT01" .

This results in the binding of the variable ?contractIdentifier to "BICYCLE-ASD-CONTRACT01".

The search is over and the result is displayed in a table.

Results of the query
contract contractIdentifier
urn:uuid:14cb22ac-0c36-4f12-9d7b-828ef37f895d BICYCLE-ASD-CONTRACT01

 

Querying product data: printing the list of product variants

This example is based on the S3KL bicycle example. It consists in printing the list of product variants for a given contract. We want to show the list of ProductVariant objects, their relating Product and, finally, the involved Contract objects.

SELECT ?productIdentifier ?productVariantIdentifier ?contractIdentifier ?numberOfContractedItems WHERE {
   ?product a s3kl:Product .
   ?productVariant s3kl:productVariantOwnedBy ?product .
   ?contractedProductVariants s3kl:contractedProductVariantRelated ?productVariant ;
      s3kl:contractedProductVariantRelating ?contract .

   ?product s3kl:productIdentifier ?productIdentifier .
   ?productVariant s3kl:productVariantIdentifier ?productVariantIdentifier .
   ?contract s3kl:contractIdentifier ?contractIdentifier .

   OPTIONAL { ?contractedProductVariants s3kl:quantityOfContractedProductVariant ?numberOfContractedItems }
}

The OPTIONAL block enables the search to proceed even if the triple pattern doesn't yield any result. (Remember that normally, the search is halted when there are unresolved triple patterns; here, we escape out of OPTIONAL blocks as soon as this happens and proceed as usual.) This allows us to print the results even if the (optional) S3000L attribute quantityOfContractedProductVariant has no value set. This leads to the following result set:

Results of the query
productIdentifier productVariantIdentifier contractIdentifier numberOfContractedItems
MOUNTAIN BIKE BIKE BICYCLE-ASD-CONTRACT01