<!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>on2ts - Typescript Generation from OWL Ontologies and SHACL</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Jesse Wright</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Sergio J. Rodr guez M</string-name>
          <email>Sergio.RodriguezMendez@anu.edu.au</email>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Armin H</string-name>
        </contrib>
        <contrib contrib-type="author">
          <string-name>rry T</string-name>
        </contrib>
        <contrib contrib-type="author">
          <string-name>G. Omr</string-name>
          <email>P.G.Omran@anu.edu.au</email>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>Australian National University</institution>
          ,
          <addr-line>Canberra ACT 2601, AU</addr-line>
        </aff>
      </contrib-group>
      <abstract>
        <p>Ontologies expressed in OWL and their associated SHACL [4] constraints contain detailed metadata and assumptions on Knowledge Graphs (KGs). With this information comes the possibility of developing front-end applications that interact with these KGs. These opportunities are often unrealised as Web developers are required to interpret OWL and SHACL statements in order to write front-end code that conforms with the back-end data model. This paper introduces on2ts, a developer tool that automatically converts OWL de nitions and SHACL constraints into Typescript [1] classes and interfaces. This enables developers to import these de nitions directly into their application. The authors have developed this tool with the goal of reducing development time of linked-data applications, improving type-safety of linked-data applications and providing an appropriate level of abstraction for front-end development environments.</p>
      </abstract>
      <kwd-group>
        <kwd>Typescript</kwd>
        <kwd>Javascript</kwd>
        <kwd>SHACL</kwd>
        <kwd>OWL</kwd>
        <kwd>Ontology</kwd>
        <kwd>Code</kwd>
        <kwd>Generation</kwd>
        <kwd>linked-data</kwd>
        <kwd>Developer Tool</kwd>
      </kwd-group>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>-</title>
      <p>
        Despite rapid growth in the development of back-end technologies for the
Semantic Web, front-end tools remain limited. on2ts aims to address several issues
hindering development of scalable linked-data applications and libraries:
1. Developers have been slow to adopt Semantic Web technologies, and
historically, the developer experience in linked-data applications has been poor.
2. Applications and libraries which are designed for the same ontology are often
incompatible due to di erent internal methods for handling data structures.
3. There are few linked-data libraries which make use of Typescript (or similar
typed languages that are used by developers of large scale web applications).
This is unsurprising given the di culty of working with types under the
openworld assumption made by Semantic Web languages. The use of types in
linked data applications is now becoming easier with the advent of constraint
languages such as ShEx [
        <xref ref-type="bibr" rid="ref7">7</xref>
        ] and SHACL [
        <xref ref-type="bibr" rid="ref4">4</xref>
        ].
4. Open world assumptions in linked-data environments force developers to
handle cases that are not encountered in traditional databases.
      </p>
      <p>
        The rst issue has recently come to light in Semantic web communities
focused on front-end development. The authors of LD ex note that: \use cases
indicate that designing a linked-data developer experience|analogous to a user
experience|is crucial for adoption" [
        <xref ref-type="bibr" rid="ref12">12</xref>
        ] and have begun to address this issue
with APIs which abstract away complexities of RDF and SPARQL. Whilst such
tooling is su cient for small scale applications, our experience with the
development of larger applications such as Sch matos5 [
        <xref ref-type="bibr" rid="ref10 ref13">10, 13</xref>
        ] indicates that these
libraries require the support of ontology-based objects if they are to be used in
scalable applications. In addition, we are not aware of any tooling that speci
cally aids Typescript/Javascript developers in handling the complexities of open
world assumptions arising from the RDF data model. In this paper, we discuss
how on2ts can be used to resolve these issues.
      </p>
      <p>
        The authors are aware of similar code generators which have been developed
for other languages such as Protege's code generator [
        <xref ref-type="bibr" rid="ref11">11</xref>
        ], the OWL-API [
        <xref ref-type="bibr" rid="ref2">2</xref>
        ] for
Java and owl2perl [
        <xref ref-type="bibr" rid="ref3">3</xref>
        ] for Perl. Additionally there are some
Javascript/Typescript libraries [
        <xref ref-type="bibr" rid="ref5">5</xref>
        ] that extract some low level information such as the IRIs and
TermTypes from ontologies. Crucially, we note that none of these frameworks
make use of SHACL to provide additional or customised validation behavior.
      </p>
      <p>
        The Shapes Constraint Language (SHACL) [
        <xref ref-type="bibr" rid="ref4">4</xref>
        ] is a W3C recommendation
developed to express conditions, as shape graphs, for validating RDF-based KGs.
Thus domain-relevant structure can be enforced. Whilst similar languages such
as ShEx [
        <xref ref-type="bibr" rid="ref7">7</xref>
        ] exist, the authors have chosen to use SHACL as it is a W3C
recommendation. We additionally note that tools including RDFShape [
        <xref ref-type="bibr" rid="ref8">8</xref>
        ] enable the
conversion from ShEx to SHACL.
      </p>
      <p>
        Typescript is an open-source language developed by Microsoft which forms
a superset of Javascript. It introduces static type de nitions that \describe the
shape of an object, providing better documentation, and allowing Typescript
to validate that your code is working correctly"6. Reasons for choosing to
develop on2ts in Typescript include; the language's increasing popularity7 and
the enhanced developer experience that Typescript provides when working in
most IDEs8. Furthermore, static typing can help developers use patterns that
accurately match de nitions of OWL ontologies and adhere to the constraints
5 on2ts was initially created to support Sch matos
6 https://www.typescriptlang.org/
7 A recent survey of 65,000 developers by StackOver ow indicates that
developers are using Typescript in their projects [
        <xref ref-type="bibr" rid="ref6">6</xref>
        ].
8 https://code.visualstudio.com/docs/languages/typescript
67% of web
of associated SHACL shapes. Javascript applications may also import libraries
written in Typescript and bene t from tooltips derived from Typescript de
nitions.
2
      </p>
      <p>
        From ontologies and SHACL to Typescript
on2ts uses LD ex [
        <xref ref-type="bibr" rid="ref12">12</xref>
        ] to read OWL axioms and SHACL constraints. ts-morph [
        <xref ref-type="bibr" rid="ref9">9</xref>
        ]
is then used to generate the subsequent Typescript les. In this process, the
ontology le is used to generate the base structure of classes, which includes
encoding domain, range, subclass and type declarations. In contrast the SHACL
les are used to generate validators that are applied when instantiating or loading
an instance of a given class.
2.1 Exotic Objects and Proxies [Data from ontologies]
on2ts makes use of exotic objects and proxies to allow Javascript/Typescript
developers to interact with ontologies using familiar patterns of Object Oriented
Programming. For instance the Symbol.hasInstance method is overwritten on
the classes generated by on2ts. This enables the instanceof keyword to have
the same behavior as a/rdf:type in a SPARQL query (including inferencing
using subclass/subproperty relations). This means that if we have const John
= new foaf.Person(`ex:John') then John instanceof foaf.Person, John
instanceof foaf.Agent and foaf.Agent instanceof owl.Class all return
true. A simpli ed implementation of this is given as follows:
static [Symbol.hasInstance](obj: any): boolean {
return obj.constructor === this // John instanceof foaf.Person
// foaf.Agent instanceof owl.Class
|| obj.rdfType === this
// John instanceof Base
|| obj.constructor.__proto__ === this
// John instanceof foaf.Agent
|| obj.constructor.getExtends?.()
      </p>
      <p>.some((extend: typeof Base) =&gt; extend instanceof this)
}
similarly we have overwritten the in operator to check if an instance has a given
property within the data model. For instance as below
function handlePerson(person: Person): void {
if (ex.SupervisorOf in person) {...}
else {...}}
on2ts similarly encodes information related to domain/range and datatype
properties. Importantly, note that as the ontology de nitions are encoded within
the Typescript les, all information is self contained in the program and hence
these operations require no external API calls beyond the initial loading of
ex:John.</p>
      <p>In some cases we need to add Javascript/Typescript methods to
supplement the ontology/SHACL de nitions. For instance, we need to add custom
validator methods to each class of SHACL validators. These methods are
integrated into the objects using ts-morph during the code generation process.</p>
      <p>This tool does not yet process the complete OWL2 speci cation. The current
focus of development is on ease of usage rather than complete coverage of the
speci cation.
2.2</p>
      <p>Data from SHACL
Traditional applications that interact with SQL databases are programmed against
a strict set of assumptions that are encoded into the model of the database.</p>
      <p>The open world assumption of RDF data models forces developers to work
many cases that would traditionally not have to be considered. SHACL
constraints allow developers or information architects to de ne a set of conditions
that data must satisfy in order for applications to reasonably perform. By
encoding this information into an applications data model, validation of data entering
the application can be automated and programmers can work under assumptions
that they are traditionally used to.</p>
      <p>
        Consider a linked-data application for playing a game of chess. A reasonable
assumption is that each piece in play lies in a valid position on the 8 8 board.
This constraint is given as follows:
ex:inPlayShape a sh:nodeShape ;
sh:property [
sh:path ex:position ;
sh:datatype xsd:string ;
sh:pattern "^[A-H][
        <xref ref-type="bibr" rid="ref1 ref2 ref3 ref4 ref5 ref6 ref7 ref8">1-8</xref>
        ]\$" ; ] .
      </p>
      <p>] ;
sh:property [
sh:path ex:inPlay ;
sh:value true ;</p>
      <p>
        If this SHACL constraint is applied (at code generation time) to the class
ex:inPositionPiece it becomes safe to assume that if the code const piece
= new inPositionPiece(loadedPiece) runs without error, then the piece will
be in a valid position on the playing board. Developers could customise the error
handling for invalid data using a try/catch statement. In other applications,
they may choose to call a SHACL form generation tool such as Sch matos [
        <xref ref-type="bibr" rid="ref10 ref13">10, 13</xref>
        ]
so that users can correct missing or invalid data before the application continues.
An example use case for this is generating a form for users to enter their banking
details before an application loads the payment details for making a purchase.
      </p>
      <p>
        The base class structure of the SHACL validator is generated using on2ts.
We have then provided a function for each validation constraint. Consequently
full coverage of the SHACL speci cation is obtained.
This tool is under active development, at https://github.com/jeswr/on2ts
along with sample Typescript les generated from several ontologies. In the
repository we are also building the plugin required to use LD ex [
        <xref ref-type="bibr" rid="ref12">12</xref>
        ] in
conjunction with these les. Demonstration videos and further use cases will be
added during development. Some on2ts features discussed in this paper remain
in development at the time of writing.
      </p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          1.
          <string-name>
            <surname>Bierman</surname>
            ,
            <given-names>G.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Abadi</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Torgersen</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          :
          <article-title>Understanding typescript</article-title>
          . In: Jones,
          <string-name>
            <surname>R</surname>
          </string-name>
          . (ed.)
          <source>ECOOP</source>
          <year>2014</year>
          {
          <article-title>Object-Oriented Programming</article-title>
          . pp.
          <volume>257</volume>
          {
          <fpage>281</fpage>
          . Springer Berlin Heidelberg, Berlin, Heidelberg (
          <year>2014</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          2.
          <string-name>
            <surname>Horridge</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Bechhofer</surname>
            ,
            <given-names>S.:</given-names>
          </string-name>
          <article-title>The owl api: A java api for owl ontologies</article-title>
          .
          <source>Semantic Web</source>
          <volume>2</volume>
          (
          <issue>1</issue>
          ),
          <volume>11</volume>
          {
          <fpage>21</fpage>
          (
          <year>2011</year>
          ). https://doi.org/10.3233/SW-2011-0025
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          3.
          <string-name>
            <surname>Kawas</surname>
            ,
            <given-names>E.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Wilkinson</surname>
          </string-name>
          , M.D.:
          <article-title>OWL2Perl: creating Perl modules from OWL class de nitions</article-title>
          .
          <source>Bioinformatics</source>
          <volume>26</volume>
          (
          <issue>18</issue>
          ),
          <volume>2357</volume>
          {
          <volume>2358</volume>
          (07
          <year>2010</year>
          ). https://doi.org/10.1093/bioinformatics/btq416, https://doi.org/10.1093/ bioinformatics/btq416
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          4.
          <string-name>
            <surname>Knublauch</surname>
            ,
            <given-names>H.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Kontokostas</surname>
            ,
            <given-names>D.</given-names>
          </string-name>
          :
          <article-title>Shapes constraint language (SHACL)</article-title>
          .
          <source>W3C Recommendation, w3c (July</source>
          <year>2017</year>
          ), https://www.w3.org/TR/shacl
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          5. ontola:
          <source>ontola/ontologies: v2.0.1 (Aug</source>
          <year>2020</year>
          ), https://github.com/ontola/ ontologies
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          6.
          <article-title>Over ow"</article-title>
          ,
          <source>S.: Stack over ow developer survey</source>
          <year>2020</year>
          (
          <year>2020</year>
          ), https://insights. stackoverflow.com/survey/2020
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          7.
          <string-name>
            <surname>Prud'hommeaux</surname>
          </string-name>
          , E.,
          <string-name>
            <surname>Boneva</surname>
            ,
            <given-names>I.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Gayo</surname>
            ,
            <given-names>J.E.L.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Kellogg</surname>
          </string-name>
          , G.:
          <article-title>Shape expressions language 2.1</article-title>
          .
          <source>Final community group report 8 october</source>
          <year>2019</year>
          , W3C Community Group (
          <year>2019</year>
          ), http://shex.io/shex-semantics/
        </mixed-citation>
      </ref>
      <ref id="ref8">
        <mixed-citation>8. RDFShape: Parse and convert schema, http://rdfshape.herokuapp.com/ schemaConversions</mixed-citation>
      </ref>
      <ref id="ref9">
        <mixed-citation>
          9.
          <string-name>
            <surname>Sherret</surname>
          </string-name>
          , D.: dsherret/ts-morph
          <source>: v7.3.0 (Aug</source>
          <year>2020</year>
          ), https://github.com/ dsherret/ts-morph
        </mixed-citation>
      </ref>
      <ref id="ref10">
        <mixed-citation>
          10. University, A.N.: schimatos/schimatos.
          <source>org: v0.0</source>
          .2-alpha (
          <year>Aug 2020</year>
          ). https://doi.org/10.5281/zenodo.3988748, https://doi.org/10.5281/zenodo. 3988748
        </mixed-citation>
      </ref>
      <ref id="ref11">
        <mixed-citation>
          11. University, S.: protegeproject/code-generator
          <source>: v2.0.0 (Aug</source>
          <year>2020</year>
          ), https:// github.com/protegeproject/code-generator
        </mixed-citation>
      </ref>
      <ref id="ref12">
        <mixed-citation>
          12.
          <string-name>
            <surname>Verborgh</surname>
            ,
            <given-names>R.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Taelman</surname>
            ,
            <given-names>R.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Herwegen</surname>
            ,
            <given-names>J.V.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Lemee</surname>
            ,
            <given-names>J.B.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Willems</surname>
            ,
            <given-names>J.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Shurmer</surname>
          </string-name>
          , J., de Jong, M.: Ld ex/ld ex:
          <source>v2.11.1 (Jun</source>
          <year>2020</year>
          ). https://doi.org/10.5281/zenodo.3894538, https://doi.org/10.5281/zenodo. 3894538
        </mixed-citation>
      </ref>
      <ref id="ref13">
        <mixed-citation>
          13.
          <string-name>
            <surname>Wright</surname>
            ,
            <given-names>J.</given-names>
          </string-name>
          , Rodr guez Mendez,
          <string-name>
            <given-names>S.</given-names>
            ,
            <surname>Haller</surname>
          </string-name>
          ,
          <string-name>
            <given-names>A.</given-names>
            ,
            <surname>Taylor</surname>
          </string-name>
          , K.,
          <string-name>
            <surname>Omran</surname>
            ,
            <given-names>P.</given-names>
          </string-name>
          :
          <article-title>Schimatos: a SHACL-based web-form generator for knowledge graph editing</article-title>
          .
          <source>In: Proc. International Semantic Web Conference, ISWC2020</source>
          . p.
          <volume>16</volume>
          pages.
          <source>Springer LNCS (November</source>
          <year>2020</year>
          ), To appear.
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>