<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD v1.0 20120330//EN" "JATS-archivearticle1.dtd">
<article xmlns:xlink="http://www.w3.org/1999/xlink">
  <front>
    <journal-meta />
    <article-meta>
      <title-group>
        <article-title>R4R: Template-based REST API Framework for RDF Knowledge Graphs</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Ontology Engineering Group</string-name>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Spain</string-name>
        </contrib>
        <contrib contrib-type="author">
          <string-name>cbadenes</string-name>
        </contrib>
        <contrib contrib-type="author">
          <string-name>pespinoza</string-name>
        </contrib>
        <contrib contrib-type="author">
          <string-name>ocorchog@fi.upm.es</string-name>
        </contrib>
      </contrib-group>
      <abstract>
        <p>Knowledge graphs (KGs) are increasingly being used to make structured information available on the Web, by means of REST APIs and/or SPARQL endpoints. In many cases, these REST APIs are generated on top of the SPARQL endpoints, using existing technology approaches that are based on proprietary con guration les or ontologies to create the APIs. These approaches may impose content-based or structural constraints when composing Web resources. To relax these constraints we propose R4R, a more exible solution based on Web standards and REST principles that creates and publishes customizable APIs exposing Web resources from SPARQL queries organized in le system directories. R4R features include individual and nested resources, paginated queries, optional elds, web authentication, query parameters and sorting lists. Resource type: Software License: Apache License 2.0 DOI: https://doi.org/10.5281/zenodo.3543320</p>
      </abstract>
      <kwd-group>
        <kwd>API</kwd>
        <kwd>Knowledge Graph</kwd>
        <kwd>REST</kwd>
        <kwd>SPARQL</kwd>
      </kwd-group>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>-</title>
      <p>
        Knowledge graphs (KGs) are drawing increasing attention from both academia
and industry for representing, sharing and using knowledge in applications [
        <xref ref-type="bibr" rid="ref3 ref7">7, 3</xref>
        ].
They may be made available as RDF-based datasets, including a SPARQL
endpoint (e.g., DBpedia), and/or via REST APIs (e.g., Google Knowledge Graph).
In both cases, KGs share many commonalities from the data representation point
of view (both use triples to represent facts), but they are radically di erent in
terms of query capabilities: SPARQL provides a more expressive query language
than what can be normally done with a REST API, but it can be a barrier for
non-expert users. Web APIs usually present data according to REpresentative
State Transfer (REST) architecture principles mapping HTTP verbs (POST,
GET, PUT, DELETE) to CRUD operations (Create, Read, Update, Delete).
      </p>
      <p>Copyright © 2021 for this paper by its authors. Use permitted under Creative
Commons License Attribution 4.0 International (CC BY 4.0)
However, API resources do not necessarily match to KG resources. A public
procurement-focused API and a technology-focused API may present, in a
different way, the information retrieved from the same KG about companies. One
more focused on merit and the other on innovations.</p>
      <p>In this demo we present R4R, an open source framework that facilitates the
publication of a KG via a REST API over HTTP. Our approach proposes a
fully customizable de nition of resources, both naming and content (and even
nesting), through a hierarchical organization of data (Figure 1). It deploys a web
service based on SPARQL queries to retrieve the information and provides
templates to compose resources that are organized in folders in a system directory.
Finally, we describe a motivating example where R4R is used to enhance KG
access.
2</p>
      <p>
        KG data consumption via Web API
Several approaches are available to provide Web developers with mechanisms
to ease KG data consumption without dealing with the complexity of Semantic
Web standards and technologies, namely SPARQL. Some of these approaches
have been focused on the provision of Web APIs that allow developers to
interact with KG data. There are tools [
        <xref ref-type="bibr" rid="ref1">1</xref>
        ][
        <xref ref-type="bibr" rid="ref5">5</xref>
        ] that generate Web APIs from set of
SPARQL queries but require setting up programming environments or use xed
structures for resources. Others [
        <xref ref-type="bibr" rid="ref4">4</xref>
        ] proposed a SPARQL Transformer to provide
speci c JSON structures from the SPARQL queries but this prevents validating
SPARQL queries from any SPARQL endpoint, or [
        <xref ref-type="bibr" rid="ref2">2</xref>
        ] creates a Web service from
the API paths, methods and SPARQL queries provided in a con guration le
based on a key-value structure in proprietary format. In Table 1 we summarized
the main features of the aforementioned approaches, and it also includes the
features of R4R which will be described in the following sections.
      </p>
    </sec>
    <sec id="sec-2">
      <title>Characteristic</title>
      <sec id="sec-2-1">
        <title>Resources</title>
      </sec>
      <sec id="sec-2-2">
        <title>Methods</title>
      </sec>
      <sec id="sec-2-3">
        <title>Security Documentation</title>
      </sec>
      <sec id="sec-2-4">
        <title>Parameters</title>
      </sec>
      <sec id="sec-2-5">
        <title>Serialization</title>
      </sec>
      <sec id="sec-2-6">
        <title>Content</title>
      </sec>
      <sec id="sec-2-7">
        <title>Deployment</title>
        <p>3
R4R</p>
      </sec>
    </sec>
    <sec id="sec-3">
      <title>Proposal</title>
      <p>single
multiple
nested
GET
POST</p>
      <p>PUT</p>
      <p>DELETE
basic authentication
Swagger-compliant
static HTML</p>
      <p>ltering
ordering
pagination</p>
      <p>XML
CSV
JSON</p>
      <p>RDF</p>
      <p>JSON-oriented
SPARQL compatible
remote
local
isolated</p>
      <p>X
X
x
X
X
x
x
X
X
x
x
x
x
X
X
X
X
x
x
X
X
x</p>
      <p>X
X
x
X
X
x
x
X
X
x
x
x
X
x
X
X
X
X
x
X
X
X</p>
      <p>X
X
x
X
X
x
x
x
x
X
X
X
x
x
X
X
x
X
x
X
X
x</p>
      <p>X
X
X
X
x
x
x
X
X
X
X
X
X
x
x
X
x
X
X
X
X
X
The R4R1 open source framework aims to bring it closer to non-expert Web
service developers: (1) customizable resource abstraction and an (2) intuitive
REST-based interface. The purpose of this tool is to facilitate access to KGs
guided by use cases via a REST API. R4R supports isolated deployment
without the need to con gure programming environments. Unlike
existing approaches, R4R provides nested resources that let us reference
complex objects. Thanks to this feature, users can, for example, get the
characters of a movie through an API call like /movies/fidg/characters where the
resulting characters will depend on the particular movie resource identi ed with
the \fidg" value. It also maintains SPARQL compatibility by avoiding any
non-SPARQL variables in queries, so that queries can be externally
validated from any SPARQL endpoint. Our approach allows users to generate API
documentation from a YAML le with a Swagger speci cation as well as with a
static HTML le, for those users not familiar with the Swagger
speci cation. In regard to parameters, our approach provides ltering, ordering,
and pagination options which allows full exibility when users have to deal
with resources.
4</p>
      <p>Motivating Example
We have prepared a short tutorial1 to create a REST API over DBpedia that
browses movies. Following an intuitive approach to the REST architectural style,
1 https://github.com/oeg-upm/r4r
the characters of a movie, for example, are available thanks to a
SPARQLquery (Listing 1.1) and a template (Listing 1.2) les located in a
resources/movies/characters folder. As a result, a JSON message with the list of characters
of the movie is obtained by requesting, for example,
/movies/WarGames/characters (Listing 1.3).</p>
      <p>Listing 1.1. SPARQL query to retrieve characters of a movie
}
,
}
,
{
}
,
{
}
PREFIX dbo: &lt;http://dbpedia.org/ontology/&gt;
PREFIX dbp: &lt;http://dbpedia.org/property/&gt;
PREFIX res: &lt;http://dbpedia.org/resource/&gt;
PREFIX dbr: &lt;http://dbpedia.org/resource/&gt;
PREFIX rdf: &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt;
PREFIX foaf: &lt;http://xmlns.com/foaf/0.1/&gt;
PREFIX rdfs: &lt;http://www.w3.org/2000/01/rdf-schema#&gt;
SELECT ?name ?birthDate ( ?starring AS ?uri )
WHERE {
?id dbo:starring ?starring .
?starring foaf:name ?name .
?starring dbo:birthDate ?birthDate .</p>
      <p>OPTIONAL {?name rdfs:label ?string . FILTER (lang(?string) = 'en') }</p>
      <p>Listing 1.2. Template to return characters of a movie
#foreach( $person in $results )
{
"uri" : "$person.uri",
"name" : "$person.name",
"birthDate" : "$person.birthDate"
}
#if ( $velocityCount &lt; ${results.size()} )</p>
      <p>,
#end
#end</p>
      <p>Listing 1.3. Main characters of the movie WarGames
"uri" : "http://dbpedia.org/resource/John_Wood_(English_actor)",
"name" : "John Wood",
"birthDate" : "1930-07-05"</p>
      <p>
        {
"uri" : "http://dbpedia.org/resource/Ally_Sheedy",
"name" : "Ally Sheedy",
"birthDate" : "1962-06-13"
"uri" : "http://dbpedia.org/resource/Matthew_Broderick",
"name" : "Matthew Broderick",
"birthDate" : "1962-03-21"
"uri" : "http://dbpedia.org/resource/Dabney_Coleman",
"name" : "Dabney Coleman",
"birthDate" : "1932-01-03"
R4R facilitates the creation and publication of Web resources following REST
principles through lesystem directories. Resource paths are constrained to
follow a tree structure by automatically creating them from directories and
avoiding manual editing. In addition, R4R eases KG consumption by providing a
developer-friendly serialization of the SPARQL results without imposing custom
con guration rules but using standard SPARQL and Velocity data structures.
It has been adopted by TheyBuyForYou (TBFY) project [
        <xref ref-type="bibr" rid="ref6">6</xref>
        ] to facilitate access
to its public procurement KG2 with more than 150 million triples.
      </p>
      <p>R4R has to be seen as a fully operational rst step of building REST API over
KG based on templates to create resources, SPARQL queries to retrieve data
and directories to de ne resource paths. We plan to extend R4R in the future
to enable write operations (POST, PUT, DELETE) to fully interact with the
KG data and implement additional content negotiation capabilities and formats
(JSON-LD, Turtle, HTML).</p>
      <p>Acknowledgments
Work supported by KnowledgeSpaces, PID2020-118274RB-I00.
2 https://github.com/TBFY/knowledge-graph-API</p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          1.
          <string-name>
            <surname>Daga</surname>
            ,
            <given-names>E.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Panziera</surname>
            ,
            <given-names>L.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Pedrinaci</surname>
            ,
            <given-names>C.</given-names>
          </string-name>
          :
          <article-title>A BASILar approach for building web APIs on top of SPARQL endpoints</article-title>
          .
          <source>In: CEUR Workshop Proceedings</source>
          . vol.
          <volume>1359</volume>
          , pp.
          <volume>22</volume>
          {
          <issue>32</issue>
          (
          <year>2015</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          2.
          <string-name>
            <surname>Daquino</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Heibi</surname>
            ,
            <given-names>I.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Peroni</surname>
            ,
            <given-names>S.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Shotton</surname>
            ,
            <given-names>D.</given-names>
          </string-name>
          :
          <article-title>Creating Restful APIs over SPARQL endpoints with RAMOSE</article-title>
          . arXiv preprint arXiv:
          <year>2007</year>
          .
          <volume>16079</volume>
          (
          <year>2020</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          3.
          <string-name>
            <surname>Hogan</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Blomqvist</surname>
            ,
            <given-names>E.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Cochez</surname>
          </string-name>
          , M.,
          <string-name>
            <surname>d'Amato</surname>
            ,
            <given-names>C.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>de Melo</surname>
          </string-name>
          , G.,
          <string-name>
            <surname>Gutierrez</surname>
            ,
            <given-names>C.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Gayo</surname>
            ,
            <given-names>J.E.L.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Kirrane</surname>
            ,
            <given-names>S.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Neumaier</surname>
            ,
            <given-names>S.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Polleres</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Navigli</surname>
            ,
            <given-names>R.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Ngomo</surname>
            ,
            <given-names>A.C.N.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Rashid</surname>
            ,
            <given-names>S.M.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Rula</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Schmelzeisen</surname>
            ,
            <given-names>L.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Sequeda</surname>
            ,
            <given-names>J.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Staab</surname>
            ,
            <given-names>S.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Zimmermann</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          :
          <article-title>Knowledge Graphs (</article-title>
          <year>2021</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          4.
          <string-name>
            <surname>Lisena</surname>
            ,
            <given-names>P.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Meron~</surname>
            o-Pen~uela,
            <given-names>A.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Kuhn</surname>
            ,
            <given-names>T.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Troncy</surname>
          </string-name>
          , R.:
          <article-title>Easy Web API Development with SPARQL Transformer</article-title>
          . In: Ghidini,
          <string-name>
            <given-names>C.</given-names>
            ,
            <surname>Hartig</surname>
          </string-name>
          ,
          <string-name>
            <given-names>O.</given-names>
            ,
            <surname>Maleshkova</surname>
          </string-name>
          ,
          <string-name>
            <given-names>M.</given-names>
            ,
            <surname>Svatek</surname>
          </string-name>
          ,
          <string-name>
            <given-names>V.</given-names>
            ,
            <surname>Cruz</surname>
          </string-name>
          ,
          <string-name>
            <given-names>I.</given-names>
            ,
            <surname>Hogan</surname>
          </string-name>
          ,
          <string-name>
            <given-names>A.</given-names>
            ,
            <surname>Song</surname>
          </string-name>
          ,
          <string-name>
            <given-names>J.</given-names>
            ,
            <surname>Lefrancois</surname>
          </string-name>
          ,
          <string-name>
            <given-names>M.</given-names>
            ,
            <surname>Gandon</surname>
          </string-name>
          ,
          <string-name>
            <surname>F</surname>
          </string-name>
          . (eds.)
          <source>The Semantic Web { ISWC 2019</source>
          . pp.
          <volume>454</volume>
          {
          <fpage>470</fpage>
          . Springer International Publishing,
          <string-name>
            <surname>Cham</surname>
          </string-name>
          (
          <year>2019</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          5.
          <string-name>
            <surname>Meron</surname>
          </string-name>
          <article-title>~o-Pen~uela,</article-title>
          <string-name>
            <given-names>A.</given-names>
            ,
            <surname>Hoekstra</surname>
          </string-name>
          , R.:
          <article-title>grlc makes GitHub taste like linked data APIs</article-title>
          .
          <source>In: European Semantic Web Conference</source>
          . pp.
          <volume>342</volume>
          {
          <fpage>353</fpage>
          . Springer (
          <year>2016</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          6.
          <string-name>
            <surname>Soylu</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Corcho</surname>
            ,
            <given-names>O.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Elves</surname>
            <given-names>ter</given-names>
          </string-name>
          ,
          <string-name>
            <given-names>B.</given-names>
            ,
            <surname>Badenes-Olmedo</surname>
          </string-name>
          ,
          <string-name>
            <given-names>C.</given-names>
            ,
            <surname>Mart nez</surname>
          </string-name>
          , F.
          <string-name>
            <given-names>Y.</given-names>
            ,
            <surname>Kovacic</surname>
          </string-name>
          ,
          <string-name>
            <given-names>M.</given-names>
            ,
            <surname>Posinkovic</surname>
          </string-name>
          ,
          <string-name>
            <given-names>M.</given-names>
            ,
            <surname>Makgill</surname>
          </string-name>
          ,
          <string-name>
            <given-names>I.</given-names>
            ,
            <surname>Taggart</surname>
          </string-name>
          ,
          <string-name>
            <given-names>C.</given-names>
            ,
            <surname>Simperl</surname>
          </string-name>
          ,
          <string-name>
            <given-names>E.</given-names>
            ,
            <surname>Lech</surname>
          </string-name>
          ,
          <string-name>
            <given-names>T.C.</given-names>
            ,
            <surname>Roman</surname>
          </string-name>
          ,
          <string-name>
            <surname>D.</surname>
          </string-name>
          :
          <article-title>Enhancing public procurement in the european union through constructing and exploiting an integrated knowledge graph</article-title>
          .
          <source>In: The Semantic Web { ISWC 2020</source>
          . pp.
          <volume>430</volume>
          {
          <fpage>446</fpage>
          . Springer International Publishing (
          <year>2020</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          7.
          <string-name>
            <surname>Wang</surname>
            ,
            <given-names>Q.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Mao</surname>
            ,
            <given-names>Z.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Wang</surname>
            ,
            <given-names>B.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Guo</surname>
            ,
            <given-names>L.</given-names>
          </string-name>
          :
          <article-title>Knowledge Graph Embedding: A Survey of Approaches and Applications</article-title>
          .
          <source>IEEE Transactions on Knowledge and Data Engineering</source>
          <volume>29</volume>
          (
          <issue>12</issue>
          ),
          <volume>2724</volume>
          {
          <fpage>2743</fpage>
          (
          <year>2017</year>
          ). https://doi.org/10.1109/TKDE.
          <year>2017</year>
          .2754499
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>