Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SPARQL functions in CONSTRUCT/WHERE

Tags:

rdf

sparql

fuseki

I mostly use SPARQL SELECT while working on a query for debugging purposes but in the end I want to use the final result it in a CONSTRUCT way; as I want to work with a graph and not key/value query results.

What I don't get yet (and can't seem to find with search engines/docs) is if I can use functions as well that way. As an example, I use a property path to concatenate titles I get into a "superstring", which I later use for building a Lucene index to increase plain text search quality:

PREFIX dc: <http://purl.org/dc/elements/1.1/>    

SELECT (group_concat(?title ; separator = " ") AS ?fancytitle) WHERE { 
  GRAPH ?graph {
    <http://data.staatsarchiv-bs.ch/id/archivalresource/CH-000027-1/pa-633c-a-312-fasc-163>  dc:relation+ ?relation .
    ?relation dc:title ?title .
  }
}

Now I would like to have the same ?fancytitleas a new triple like

<http://data.staatsarchiv-bs.ch/id/archivalresource/CH-000027-1/pa-633c-a-312-fasc-163> <fancytitle> ?fancytitle .

So I can store it directly in a new graph. Is this possible? I played with some queries but couldn't manage to get it accepted by the SPARQL processor. FYI I'm using Fuseki.

You can try it out at my SPARQL Endpoint

like image 384
Adrian Gschwend Avatar asked Feb 10 '15 08:02

Adrian Gschwend


People also ask

Where do we use SPARQL?

As a query language, SPARQL can be used to add, remove and retrieve data from RDF-style graph databases. SPARQL queries can not only match patterns of subject-predicate-object triples, but can also use mathematical operations and a wide range of utility functions to create filters and new variable bindings.

What is construct in SPARQL?

The CONSTRUCT query form returns an RDF graph. The graph is built based on a template which is used to generate RDF triples based on the results of matching the graph pattern of the query.

Why is SPARQL used?

SPARQL, short for “SPARQL Protocol and RDF Query Language”, enables users to query information from databases or any data source that can be mapped to RDF. The SPARQL standard is designed and endorsed by the W3C and helps users and developers focus on what they would like to know instead of how a database is organized.

What types of queries does SPARQL support?

SPARQL allows for a query to consist of triple patterns, conjunctions, disjunctions, and optional patterns. Implementations for multiple programming languages exist. There exist tools that allow one to connect and semi-automatically construct a SPARQL query for a SPARQL endpoint, for example ViziQuer.


2 Answers

Yes this is possible

You can't use expressions directly in a CONSTRUCT template but you can assign the variable in the WHERE clause either via a SELECT expression in a sub-query or using BIND.

In your case as GROUP_CONCAT is an aggregate it can only be a SELECT expression so you just need to put your entire SELECT as a sub-query e.g.

PREFIX dc: <http://purl.org/dc/elements/1.1/>    

CONSTRUCT
{
  <http://data.staatsarchiv-bs.ch/id/archivalresource/CH-000027-1/pa-633c-a-312-fasc-163> <http://fancyTitle> ?fancytitle
}
WHERE
{
  SELECT (group_concat(?title ; separator = " ") AS ?fancytitle) WHERE { 
    GRAPH ?graph {
      <http://data.staatsarchiv-bs.ch/id/archivalresource/CH-000027-1/pa-633c-a-312-fasc-163>  dc:relation+ ?relation .
      ?relation dc:title ?title .
    }
  }
}

The above works fine on your endpoint

like image 174
RobV Avatar answered Sep 17 '22 22:09

RobV


With the help of my colleague we got it to work, UNION and GROUP BY are essential. This query puts the string together for all locah:ArchivalResource in the graphs:

CONSTRUCT
{
  ?archresource skos:hiddenLabel ?supertitle
}
WHERE
{
  SELECT ?archresource  (group_concat(?title ; separator = ", ") AS ?supertitle) WHERE {
    GRAPH ?graph {
      {
        SELECT ?title ?archresource WHERE {
          GRAPH ?graph {
            {
              ?archresource a locah:ArchivalResource ;
              dc:title ?title .
            } UNION
            {
              ?archresource dc:relation+ ?relation .
              ?relation dc:title ?title .
            }
          }
        }
      }
    }
  } GROUP BY ?archresource
}
like image 29
Adrian Gschwend Avatar answered Sep 16 '22 22:09

Adrian Gschwend