Virtuoso Open-Source Wiki
Virtuoso Open-Source, OpenLink Data Spaces, and OpenLink Ajax Toolkit
Advanced Search
Help?
Location: / Dashboard / Main / VirtTipsAndTricksGuide / VirtTipsAndTricksSPARQL11FeaturesExamplesCollection / VirtTipsAndTricksSPARQL11RefDataINSTCONST

Using SPARQL 1.1 INSERT patterns and CONSTRUCT to Enhance Existing Data

What?

Using SPARQL 1.1 to provide new insights into existing data.

Why?

There are times when existing data doesn't provide the required insights for a given audience or data analytics pursuit.

How?

Use SPARQL 1.1 INSERT patterns to generate new data from existing data. Basically, this is like make a CONSTRUCT query where the output is persisted to a specific Named Graph.

The steps are as follows:

  1. Assume the following Raw Data Representation in Turtle:

    <#rojer> <#contactType> "100 - Sport" . <#alice> <#contactType> "98 - School" . <#bob> <#contactType> "99 - University" . <#john> <#contactType> "101 - Other" . <#carol> <#contactType> "100 - Friends" . <#karine> <#contactType> "102 - Colleagues" . <#ivan> <#contactType> "99 - University" . <#gloria> <#contactType> "101 - Other" . <#kate> <#contactType> "101 - Other" . <#jordan> <#contactType> "99 - University" . <#brigitte> <#contactType> "100 - Sport" .

  2. Load the sample data using SPARQL 1.1. INSERT Syntax:

    INSERT { GRAPH <urn:sparql:tests:people> { <#rojer> <#contactType> "100 - Sport" . <#alice> <#contactType> "98 - School" . <#bob> <#contactType> "99 - University" . <#john> <#contactType> "101 - Other" . <#carol> <#contactType> "100 - Friends" . <#karine> <#contactType> "102 - Colleagues" . <#ivan> <#contactType> "99 - University" . <#gloria> <#contactType> "101 - Other" . <#kate> <#contactType> "101 - Other" . <#jordan> <#contactType> "99 - University" . <#brigitte> <#contactType> "100 - Sport" . } }

  3. Let's get all Contact types:

    SELECT DISTINCT ?s2 as ?contact_type FROM <urn:sparql:tests:people> WHERE { ?s1 <#contactType> ?s2 . }

  4. View the SPARQL Query Definition via SPARQL Protocol URL;
  5. View the SPARQL Query Results via SPARQL Protocol URL
  6. CONSTRUCT Others: Purpose -- For every entity of the types from above to set to type: <#Friend> [a foaf:knows] when code does not start with '101 -':

    CONSTRUCT { ?s1 a <#Friend> } FROM <urn:sparql:tests:people> WHERE { ?s1 <#contactType> ?s2 . FILTER ( !strStarts(?s2, "101 -") ). } # Query result: @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix ns1: <#> . ns1:rojer rdf:type ns1:Friend . ns1:karine rdf:type ns1:Friend . ns1:ivan rdf:type ns1:Friend . ns1:jordan rdf:type ns1:Friend . ns1:brigitte rdf:type ns1:Friend . ns1:alice rdf:type ns1:Friend . ns1:bob rdf:type ns1:Friend . ns1:carol rdf:type ns1:Friend .

  7. Generate Friends list:

    INSERT { GRAPH <urn:sparql:tests:friends> { ?s1 a <#Friend> } } FROM <urn:sparql:tests:people> WHERE { ?s1 <#contactType> ?s2 . FILTER ( !strStarts(?s2, "101 -") ). }

  8. Let's get all Friends:

    SELECT * FROM <urn:sparql:tests:friends> WHERE { ?s ?p ?o }

  9. View the SPARQL Query Definition via SPARQL Protocol URL;
  10. View the SPARQL Query Results via SPARQL Protocol URL

Related

Powered By Virtuoso