Query an Ontotext GraphDB repository with SPARQL from a FrameworX solution and show the result in a DataGrid that follows a tag.

How-to Guides → Solution Examples → Feature Examples → Data Foundation Examples → GraphDB SPARQL Query



Download the solution: StarWars.dbsln

Companion to the Reports WebData - SPARQL and Graph Databases reference page.

Prerequisites:

  • FrameworX 10.1.6a or later.
  • Ontotext GraphDB running on localhost:7200. With Docker: docker run -d --name graphdb -p 7200:7200 ontotext/graphdb:10.8.5
  • A GraphDB repository named starwars holding the Star Wars sample dataset from the GraphDB Workbench tutorial.




Summary

The solution sends a SPARQL query to a GraphDB repository, receives the answer as a DataTable and shows it in a DataGrid. The query asks for every relationship of one character and the name of the object at the other end, for example starship → X-wing. A Text tag holds the character name, so changing the tag changes the table on screen.

The same pattern answers plant questions: replace the character with an equipment name and the relationships with feedsInto, and the query returns what is downstream of that equipment.


Technical Information

WebData row

In Reports → WebData, the row StarWars:

ColumnValue
APIREST
EncodingText
DefaultURLhttp://localhost:7200/repositories/starwars
AuthorizationNoAuth

The row's Editor holds the query. {Tag.Character} is filled in with the tag value each time the query runs.

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX voc: <https://swapi.co/vocabulary/>
SELECT ?relation ?item
WHERE {
  ?person rdfs:label "{Tag.Character}" ;
          ?rel ?obj .
  ?obj rdfs:label ?item .
  FILTER(STRSTARTS(STR(?rel), STR(voc:)))
  BIND(STRAFTER(STR(?rel), STR(voc:)) AS ?relation)
}
ORDER BY ?relation ?item

Tags

TagTypeUse
CharacterTextThe name the query looks up. Start value Luke Skywalker.
ResultDataTableThe query result, shown by the DataGrid.
RowsIntegerNumber of rows returned, or -1 when the WebData row is disabled.
ErrTextThe error message when the call fails, empty otherwise.

Script Task

The Server task QueryGraph runs every 10 seconds:

try
{
    @Tag.Err = "";
    DataTable t = await @Report.WebData.StarWars.SparqlQueryTableAsync();
    @Tag.Result = t;
    @Tag.Rows = t == null ? -1 : t.Rows.Count;
}
catch (Exception ex)
{
    @Tag.Err = ex.GetType().Name + ": " + ex.Message;
    @Info.Trace("SPARQL query failed: " + ex.Message);
}

Display

The main Display has a DataGrid with Data Source set to Tag.Result. It shows two columns, relation and item.

Expected result

CharacterRows
Luke Skywalker11: five films, a friend, a homeworld, two starships and two vehicles
Leia Organa7
Han Solo7
Darth Vader6
Yoda6

Change Character while the solution runs, and the DataGrid shows the new character's relationships within 10 seconds.

Caveats

  • The name must match the label in the graph exactly, including capital letters.
  • {Tag.Character} is filled in only because the query is in the row's Editor. Query text passed as an argument to SparqlQueryTableAsync(query) is sent as written.
  • The 10-second period suits a demonstration. In a solution, run the query on a tag change or from a button.

In this section...