Home > Hello Sparnatural

You can also read the PDF version of this guide

 

“Hello Sparnatural”

How-to adapt the “hello Sparnatural” page to your own Knowledge Graph

 

Version : 1.0

Last modified on : ju 2023

Authors : thomas.francart@sparna.fr, marie.muller@sparna.fr 

License : this document is placed under the LGPL 3.0 license, like Sparnatural

 

Introduction 2

Objectives 2

Prerequisites 2

Structure of this guide 3

Setup the tutorial page 3

Get the tutorial page 3

Content of the tutorial folder 3

Setup your local working environment 4

Allow loading of local files in your browser 4

Ensure your SPARQL endpoint is CORS-enabled, or use a proxy 6

Why do we need CORS ? 6

Check CORS 6

Allow CORS on your triplestore 6

…or use Sparnatural SPARQL proxy 7

Point the tutorial page to your SPARQL service 7

Create your first Sparnatural configuration 8

Setup your configuration ontology in Protégé 8

Import Sparnatural ontologies 8

Specify your configuration ontology URI 9

Create 2 classes 10

Create a property 12

Save your ontology 14

Point demo page to your config file 15

Test and enjoy 15

Next steps 16

Annex : adjust the example queries 17

 

 

Introduction

Objectives

Welcome to this guide on how to setup the “Hello Sparnatural” page ! “Hello Sparnatural” is the tutorial page released with Sparnatural in each of the releases. This documentation will guide you on the necessary steps to work with this tutorial page, and start adapting it to your own knowledge graph. Once customized you can keep the tutorial page in your local machine or publish it online, provided that your SPARQL endpoint is also publicly available.

At the end of this tutorial, you will have a working HTML page demonstrating the feasibility of plugin Sparnatural on your own data. You can then explore further possibilities of configuring Sparnatural in the other guide “How to configure Sparnatural”, and adapt the look-and-feel of the page to match your website.

Prerequisites

In order to follow this guide, you need the following prerequisites:

  1. You need to have your own SPARQL-accessible dataset. If you don’t have any SPARQL-accessible dataset, you can use the DBPedia dataset, accessible at https://dbpedia.org/sparql. GraphDB users can find their active repository SPARQL URL in the home page, in the “link” icon in the “Active repository” section:

  1. You need to have a basic understanding of the structure of your dataset. More specifically you need to know the URI identifiers of 2 classes in your ontology, and one property linking these two classes. If you don’t know this information, try to find the documentation of your dataset, in the form of UML diagrams or documentation tables.

  2. You need to have the Protégé OWL editor installed on your machine. To install Protégé, go to https://protege.stanford.edu/.

  3. You need to have a basic understanding of HTML, and a basic text editor to edit an HTML page.

Structure of this guide

This guide will explain:

  1. How to setup your local work environment

  2. How to adjust the tutorial page to use your configuration file and your own SPARQL endpoint URL

  3. How to setup a minimal configuration demonstrating how Sparnatural can work on your data

  4. What are the next steps once you completed this tutorial

Setup the tutorial page

Get the tutorial page

The “Hello Sparnatural” tutorial page is included with each release of Sparnatural starting from 8.4.0. To get this tutorial page:

  1. go to the latest release of Sparnatural

  2. download the “hello-sparnatural.zip” file from the assets list

  3. unzip it in a local folder on your computer

Content of the tutorial folder

The tutorial folder is based on a very simple Bootstrap HTML template, in which Sparnatural is inserted and integrated with the YasGUI SPARQL editor and query result viewer. While using YasGUI is an obvious choice, YasGUI is *not* a requirement of Sparnatural. You can choose to present query results differently than in YasGUI, and you may not want to display the raw SPARQL query to the user.

The tutorial folder is composed of the following files:

Files you will edit in this tutorial:

  • index.html: the main HTML page that you can open in a browser and that includes the Sparnatural component.

  • config.ttl: a Sparnatural configuration file. In this tutorial you will create your own new config file.

  • example-queries.js: sample queries that you can change after your configuration has been adapted.

Files you can further customize after this tutorial if you want to enhance your demo:

  • main.js: the Javascript code executed by index.html, containing Sparnatural event listeners as well as YasGUI initialization code.

  • styles.css: the specific CSS rules for the index.html page.

 

Files you will not have to edit:

  • sparnatural.js and sparnatural.css: the code of the Sparnatural component, with its CSS rules

  • sparnatural-yasgui-plugins.js: the code of the result table display plugin[1]

  • fa subfolder: folder containing the Fontawesome free icon set.

Setup your local working environment

Allow loading of local files in your browser

Open the file index.html page from the tutorial folder in your browser. You will see a blank page with Sparnatural not loading properly, and and error in your browser console[2]:

This is because browsers, for security reasons, disable by default the dynamic loading of other files from your local directory - in our case the Sparnatural configuration file. In order for this to work, we need to instruct the browser that it is safe to dynamically load local files. This is called “enabling CORS for local files”.

/!\ Don’t worry, changing these settings will not affect the other security restrictions in your browser, in particular related to CORS.

/!\ Of course, this is only in order to work with local files. Once deployed on a web server, this restriction is not applicable anymore, and normal users of your page will not have to set the same settings.

The procedure to enable CORS for local files depends on the browser:

Firefox

To make Firefox CORS-enabled for local files:

  1. Open Firefox

  2. Type “about:config” in the address bar

  3. Accept security warning

  4. Search for the config security.fileuri.strict_origin_policy

  5. Set this config to “false”

  6. Restart your browser to make sure this is taken into account

Chrome, Chromium or Edge

To make Chrome, Chromium or Edge CORS-enabled for local files :

  1. Close Chrome

  2. Open a command-line or a terminal

  3. Re-run chrome with the flag “--allow-file-access-from-files”, e.g. on Ubuntu Linux “chromium --allow-file-access-from-files”

  4. You can create a shortcut with this flag set, for example in Windows, create a shortcut to Chrome and modify the command being run in the shortcut properties:

 

Once you have configured this setting, try reopening the index.html page. You should see Sparnatural loaded correctly:

 

Ensure your SPARQL endpoint is CORS-enabled, or use a proxy

Why do we need CORS ?

CORS stands for Cross-Origin Resource Sharing. It is a mechanism by which a web client can retrieve resources from a different server from the one which it was originally loaded[3]. With CORS, servers can indicate that they allow clients loaded from another domain (or from certain other domains) to send them requests. Note that this restriction applies to client-server interactions, not to server-server interactions.

In the case of Sparnatural, it is the HTML page that sends the SPARQL query directly to a SPARQL service. This SPARQL service thus needs to allow clients/webpages to send queries to it even if they are loaded from another domain, otherwise the query will simply fail with a security warning, and you can see security errors in the requests of your browser console.

You thus need to make sure that the SPARQL endpoint you want to query with Sparnatural is CORS-enabled, unless in your target architecture the Sparnatural page will be served from the same domain as the SPARQL endpoint.

Check CORS

In case the SPARQL endpoint you want to query is public, the website https://www.test-cors.org/ can allow you to check if CORS is enabled. Most of the large SPARQL services already allow CORS, e.g. DBpedia, Wikidata, etc.

Allow CORS on your triplestore

The procedure to allow CORS from your triplestore varies depending on the tool.

GraphDB

To enable CORS on GraphDB, you need to set some specific runtime properties. Please refer to the GraphDB documentation at https://graphdb.ontotext.com/documentation/10.2/directories-and-config-properties.html?highlight=cors#workbench-properties

Virtuoso

To enable CORS on Virtuoso, follow the documentation at https://vos.openlinksw.com/owiki/wiki/VOS/VirtTipsAndTricksCORsEnableSPARQLURLs 

…or use Sparnatural SPARQL proxy

Sometimes you can’t modify the triplestore parameters, or your security policy would not allow it. In that case we provide a SPARQL proxy that is CORS-enabled and that will forward the request to a target SPARQL endpoint, just acting as a bridge between Sparnatural and the target SPARQL endpoint. This SPARQL proxy is deployed at https://proxy.sparnatural.eu and documented in the Sparnatural documentation.

/!\ Of course this is only a temporary workaround, and you must not use the online proxy server in a production environment. You should deploy your own SPARQL proxy in that case. The code of the SPARQL proxy server is open-source in its own Github repository, just in case.

 

Point the tutorial page to your SPARQL service

Now that you have 1. adjusted the security settings of your browser and 2. enabled CORS on your triplestore (or used a proxy), it is time to point the tutorial page to your endpoint !

To do this, edit the index.html file, and search for the “<spar-natural” HTML element name (around line 68)  and set the “endpoint” configuration attribute to your SPARQL endpoint URL (or to the proxy URL):

    <spar-natural

        src="config.ttl"

        endpoint="https://localhost:7200/repositories/myRepo"

        lang="en"

        defaultLang="en"

        distinct="true"

        limit="1000"

        debug="true"

    ></spar-natural>

 

Now the tutorial page is configured to point to your endpoint. Reload the page in your browser and you should see the endpoint URL displayed:

Create your first Sparnatural configuration

Setup your configuration ontology in Protégé

Now that the Sparnatural tutorial page points to your SPARQL endpoint, it is time to configure the query builder following your data structure. All this can be done via the configuration ontology you need to construct in Protégé.

Open Protégé, and start a new config ontology of yours (keep the provided config.ttl from the tutorial folder apart).

 

Import Sparnatural ontologies

Sparnatural comes with 2 ontologies that need to be imported (through owl:imports) in your own configuration ontology :

Import the 2 Sparnatural configuration ontologies in your ontology ;

To import these ontologies in Protégé use section under “Imported Ontologies > Direct imports” and then in the dialog select “Import an ontology contained in a document located on the web”, and then enter the URI of an ontology above. Repeat the process for the other one.

 

Once both ontologies are loaded in Protégé, check in the Classes and Object Properties tab of Protégé that you see now you can see imported classes and properties from Sparnatural base ontologies.

 

 

Specify your configuration ontology URI

Back in the “Active Ontology” tab, in the Ontology IRI field above the screen, write the https address where your configuration ontology is theoretically to be published (ex : https://data.mydomain.com/ontologies/sparnatural-config)

 

 

You are now ready to populate the existing structure with the entities (« Classes ») and the relations (« Object properties ») of your knowledge graph.

 

Create 2 classes

We will suppose in this tutorial that the knowledge graph data model consists of FOAF Persons and Organizations, with respective URI foaf:Person and foaf:Organization, linked with a property foaf:member. You will need to adapt the identifiers with your own URI identifiers.

First you need to create a class for each kind of entity : Organization and Person. Switch to the “Classes” tab for this.

Selec the “SparnaturalClass” in the class tree, and click the button to add a subclass under it. In the dialog you can directly type in the « Name » field of the dialog the real URI of the FOAF class you want to add; we will start with foaf:Person http://xmlns.com/foaf/0.1/Person .

 

 

Once created, edit the attributes of the new class by clicking the “+” button next to the « Annotations » label:

  1. First add an « rdfs:label » annotation, in language “en”, with value “Person”.

  2. Note that you can create labels in as many languages as needed as the Sparnatural component can switch languages, but for the moment let’s stick to English.

 

  1. Second, add another annotation of type “tooltip” and enter a description that shall be displayed as a tooltip when the user hovers the corresponding entry in the query builder. You can enter the definition of the foaf:Person class “The foaf:Person class represents people. Something is a foaf:Person if it is a person. We don't nitpic about whether they're alive, dead, real, or imaginary.”

  2. Add a third annotation to set the icon. Select the annotation type “faIcon” that is under “icon”, and enter the value “fa-solid fa-user”.

  3. In general, to search for an icon code, go on https://fontawesome.com/ website, and search for a free License icon in the search bar, note the icon code (typically something like “fa-solid fa-user”), and enter this as a value.

 

 

Repeat the same process to create http://xmlns.com/foaf/0.1/Organization, with label “Organization” in language “en”, tooltip “An organisation represents a kind of Agent corresponding to social instititutions such as companies, societies etc. ” , and icon code “fa-solid fa-building”.

Create a property

We will now create the relationship “member” between an Organization and a Person, and we will indicate we want it to be created as a dropdown list.

Switch into the « Object properties » tab. This is where the different kind of relations between entities can be set up as List properties, Autocomplete properties, Tree, Search fields, Map properties, Time properties and so on.

 

Unfold the object properties tree, select ListProperty entry, and click on the button to add a subproperty under it. Use http://xmlns.com/foaf/0.1/member as its URI.

Do as for the classes to give it an rdfs:label annotation in English.

 

 

We will relate foaf:member property to foaf:Organization class as a Domain and foaf:Person class as a Range, this you can edit in the Description area of your Protégé :

 

 

Select “foaf:Organization” as a value in the “Domains” section, and “foaf:Person” as a value in the “Ranges” section.

2 classes, one property, domain and range defined, just enough to see what your ontology looks like in Sparnatural’s interface !

 

Save your ontology

Save your ontology in the “hello-sparnatural” tutorial folder with a name of your choice. Save it preferably in Turtle, or in RDF/XML, but NOT in an OWL-specific serialization (such as OWL/XML). Use the file name of your choice, like “myconfig.ttl”.

 

Point demo page to your config file

Edit the index.html page, look for the “<spar-natural>” tag and set the “src” attribute to point to your configuration file name, like “myconfig.ttl”, and save the file:

    <spar-natural

        src="myconfig.ttl"

        endpoint="https://localhost:7200/repositories/myRepo"

        lang="en"

        defaultLang="en"

        distinct="true"

        limit="1000"

        debug="true"

    ></spar-natural>

 

Test and enjoy

Open a CORS-enabled browser to load the index.html file in it : you should see your 2 classes linked with your properties, populated by the data from your SPARQL endpoint:

 

If everything is OK, you should see a list of Person URIs (or another class from your knowledge graph) listed in the dropdown. You can try to build a query and verify that it works !

If it does not work:

  1. check that the URIs you used in your configuration file for the 2 classes and the property do correspond to actual URI from your knowledge graph.

  2. debug the SPARQL query by opening your web browser console (F12 key) and monitor the SPARQL query that is sent to populate the dropdown box.

Congratulations, you have successfully completed this “Hello Sparnatural” tutorial !

Next steps

Look at the provided configuration ontology

The tutorial folder comes with an included ontology in the “config.ttl” file. You can try loading this ontology in Protégé and have a look at the various configuration features it contains, as a source of inspiration.

 

Read the configuration how-to

Sparnatural offers many nice features to explore your knowledge graph :

  • Autocomplete widgets, trees, maps, calendars and more.

  • Ability to map the configuration ontology to the underlying knowledge graph structure, if you want to display things with a different structure.

  • Ability to enable optional, negative or multilingual properties.

  • Ability to customize Sparnatural through a CSS theme file.

  • Ability to configure Sparnatural using a (Google or Excel) spreadsheet

 

All of these you can learn in the «     How-to configure Sparnatural » document. The Sparnatural documentation website offers comprehensive documentation on all the features. The Github repository of Sparnatural is where you can ask questions, report bugs, and reach us.

Tune the HTML and upload the demo page online

You can further tune the content of the index.html webpage, and customize it anyway you like. Once done, you can simply upload the entire folder on a (your) web server so that it is publicly accessible !

 

Annex : adjust the example queries

 

To give the users a few examples of queries they could write in the interface, you can save sample queries to display in the HTML page.

Save the sample queries only after your Sparnatural configuration ontology is stable. Otherwise there is a risk that you will need to rewrite them.

Start creating a query via the in-place query builder and open your browser console at the same time. Here you can see that each time you add a new parameter to your query, a new « Sparnatural JSON query structure » message appears.

Once your query written, then find the last « Object » in the Sparnatural JSON query structures listed here :

 

then right click on it and select « Copy object ».

Edit the example-querues.js javascript file :

 

  1. Select the value of “example_1” variable, from the first “{“ character to the last “}”, and Paste what you have copied from the console.

  2. Repeat the same process to save a second query in the “example_2” variable.

  3. Add more variables if needed.

  4. Save the file

Edit the index.html file and uncomment the example queries section around line 57 to 64, above the “<spar-natural>” tag:

 

    <!-- uncomment to enable links to load sample queries

    <p>

      Load example queries :

      <a href="#" onclick="document.querySelector('spar-natural').loadQuery(window['example_1']);return false;">example 1</a>

      |

      <a href="#" onclick="document.querySelector('spar-natural').loadQuery(window['example_2']);return false;">example 2</a>

    </p>

    -->

 

Adjust the title of the query accordingly (e.g. replace “example 1” with “My beautiful query”) :

 

 

Your query is saved and can be loaded in the query builder in one click !

 



[1] This plugin is part of another project at https://github.com/sparna-git/sparnatural-yasgui-plugins 

[2] The browser console can be opened with the F12 key

[3]  For more information we suggest to check https://enable-cors.org/, and the wikipedia page on Same Origin Policy at https://en.wikipedia.org/wiki/Same-origin_policy