<!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>Type-safe programming with OWL in Semantics4J</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Carsten Hartenfels</string-name>
          <xref ref-type="aff" rid="aff1">1</xref>
          <xref ref-type="aff" rid="aff2">2</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Martin Leinberger</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
          <xref ref-type="aff" rid="aff1">1</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Ralf Lämmel</string-name>
          <xref ref-type="aff" rid="aff1">1</xref>
          <xref ref-type="aff" rid="aff2">2</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Steffen Staab</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
          <xref ref-type="aff" rid="aff1">1</xref>
          <xref ref-type="aff" rid="aff3">3</xref>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>Institute for Web Science and Technologies, University of Koblenz-Landau</institution>
          ,
          <country country="DE">Germany</country>
        </aff>
        <aff id="aff1">
          <label>1</label>
          <institution>Listing 2. Example data</institution>
        </aff>
        <aff id="aff2">
          <label>2</label>
          <institution>The Software Languages Team, University of Koblenz-Landau</institution>
          ,
          <country country="DE">Germany</country>
        </aff>
        <aff id="aff3">
          <label>3</label>
          <institution>Web and Internet Science Research Group, University of Southampton</institution>
          ,
          <addr-line>England</addr-line>
        </aff>
      </contrib-group>
      <abstract>
        <p>Programming with OWL is error-prone due to the lack of type-safe integration into programming languages. While generic types such as Resource can represent anything the data can model, they make it impossible to errorcheck programs. Mapping ontological concepts into types of the programming language on the other hand often cannot capture the ontology completely and duplicates knowledge that a semantic reasoner already has. Semantics4J, an extended Java compiler, allows for type-safe programming with OWL by integrating DL expressions as types and values into the programming language. This paper presents a prototype that supports our theoretical concept [4] by relying on an extended type-checking process building upon a reasoner as well as class expression queries that are being used as types themselves.</p>
      </abstract>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>1 Introduction</title>
      <p>
        While OWL allows for capturing knowledge in a natural manner, programming with
such a data source is error-prone. There are two common approaches to program with
OWL. Using generic types (1) such as Resource as exemplified by [
        <xref ref-type="bibr" rid="ref1">1</xref>
        ] can represent
anything the data can model, but it ignores structural properties or ontological
knowledge and therefore makes it impossible for type systems to detect errors in the program
based on semantic reasoning with ontological concepts. To allow such an error
detection, mapping approaches (2) map ontological concepts into types of the programming
language. However, such mappings are limited by the expressiveness of the type system
used in the target language and they duplicate knowledge that a reasoner working over
such data already has. As an example, consider data about musicians, actors and mutual
influences represented in description logics as shown in Listings 1 and 2.
      </p>
      <p>In the ontological part (Listing 1), everyone who has recorded a song is defined to
be a Musician (line 1). ActingMusicians are entities who are both Actor and
Musician (lines 2–3). A RockMusician is a special form of a Musician (lines 4).</p>
      <p>In the data part (Listing 2), hendrix has recorded the song machineGun (lines 1–2).
1
2
3
4
5
6
7
8
9
10
11
12</p>
      <p>The entity elvis, who is both Actor and RockMusician is said to have influenced
hendrix (lines 3–5). A mapping that converts such data into types of a programming
language faces difficulties because of implicit knowledge, e.g. the fact that hendrix
is a Musican or the lack of multiple inheritance in common programming languages.
With regard to the latter, consider ActingMusician, which is subsumed by both
Actor and Musican. But it must also handle the intricacies of the influencedBy
role, which is used very broadly. As a result, many mappings cannot fully capture the
ontology for the purpose of type checking the program.</p>
      <p>
        Our system relies on a type system extension. It uses DL expressions as types and
extends the type-checking process to benefit from a semantic reasoner. This has been
explored from a theoretical point of view in [
        <xref ref-type="bibr" rid="ref4">4</xref>
        ] where we showed that this approach
does indeed yield a type-safe language. In this paper, we present an backward
compatible extension of the Java programming language called Semantics4J that implements
the methodology in the Java programming language.
2
      </p>
    </sec>
    <sec id="sec-2">
      <title>Example use case</title>
      <p>As an example, consider an application written for the data as defined in Listings 1
and 2. Such an application could query for all Musicians and then print their
influences. Listing 3 depicts the outline of such a program while leaving out the specifics of
a getInfluences method for mapping from a Musician to his influences.
p u b l i c c l a s s I n f l u e n c e s from " m u s i c . r d f " {
p u b l i c s t a t i c S e t &lt;9« : i n f l u e n c e d B y » &gt;&gt;</p>
      <p>g e t I n f l u e n c e s ( . . . a r t i s t ) { . . . }
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
f o r ( « : M u s i c i a n » a r t i s t : query f o r ( " : M u s i c i a n " )
S y s t e m . o u t . f o r m a t ( "%s was i n f l u e n c e d by %s " ,
a r t i s t . getName ( ) ,</p>
      <p>S t r i n g . j o i n ( " , " , names ( g e t I n f l u e n c e s ( a r t i s t ) ) )
}
}
) ;</p>
      <p>Listing 3. Program querying for musicians and listing their influences.</p>
      <p>Line 1 of the program specifies the class name and data source ”music.rdf“ the
program is written for. Lines 2–3 contain the signature of the getInfluences method. This
method returns a set of values where each value is an instance of the DL expression
9influencedBy :&gt;. In the main method of the program, Line 7 contains a query
for all Musicians over which the for-loop iterates. For each Musician, the
getInfluences method is called and its results are joined into a single string via the getName
method which returns the fragment identifier part of the entities IRI. An important
feature of Semantics4J is that DL expressions are both types and values. DL expressions as
types are used during the type-checking of the program. DL expressions as values, e.g.,
6
as a parameter to the query-for operator, are treated as strings and are implicitly
converted to roles and concepts. This allows them to be dynamic, enabling the construction
of a query on the fly. Assuming that a concept name, e.g., Actor, is being passed to
the program via command line, the query in line 6 could also query for the intersection
of the two concepts (see Listing 4).</p>
      <p>f o r ( « : M u s i c i a n » a r t i s t : query f o r ( " : M u s i c i a n " u a r g s [ 0 ] )</p>
      <p>Listing 4. Example for a dynamically constructed query.</p>
      <p>Typing of query-for is dependent on the given input values. However, only the static
parts of such queries can be used for typing as the dynamic part may change during
runtime. Therefore, the query displayed in Listing 4 is only typed with the concept
Musician. This is also true if the dynamic part is not given as a program parameter,
but rather as another variable holding a reference to some string.</p>
      <p>A simple approach to the getInfluences method may be to assume that all Musicians
have an influence. The method could therefore take an instance of the DL expression
9influencedBy:&gt; and query for influences directly (see Listing 5).
p u b l i c s t a t i c S e t &lt;9« : i n f l u e n c e d B y »</p>
      <p>g e t I n f l u e n c e s ( 9« : i n f l u e n c e d B y » &gt;
r e t u r n a r t i s t . ( " : i n f l u e n c e d B y " ) ;
&gt;&gt;
a r t i s t ) {
}</p>
      <p>Listing 5. Example for problematic code.</p>
      <p>However, this code contains a type error—since it unknown whether Musician is
a subconcept of 9influencedBy.&gt;, it is problematic to assume that this relation
holds. To enforce type safety, Semantics4J requires the programmer to do a type case
instead (see Listing 6). This also requires the presence of a default case which applies
if it is either unknown whether the Musician has an influence or if it is known that
the Musician has no influence.</p>
      <p>p u b l i c s t a t i c S e t &lt;9« : i n f l u e n c e d B y » &gt;&gt;
g e t I n f l u e n c e s ( « : M u s i c i a n » a r t i s t ) {
s w i t c h t y p e ( a r t i s t ) {
9« : i n f l u e n c e d B y » &gt; i n f l u e n c a b l e {</p>
      <p>r e t u r n i n f l u e n c a b l e . ( " : i n f l u e n c e d B y " ) ;
}
d e f a u l t { r e t u r n</p>
      <p>C o l l e c t i o n s . e m p t y S e t ( ) ; }
}
}</p>
      <p>Listing 6. Type-safe example code.</p>
    </sec>
    <sec id="sec-3">
      <title>3 Implementation</title>
      <p>
        Semantics4J builds upon ExtendJ, a JastAdd Extensible Java compiler [
        <xref ref-type="bibr" rid="ref2">2</xref>
        ]. It can parse
Java programs extended with the constructs used in the examples. Type-checking is
done by forwarding all decisions about the relation of ontological concepts to a
reasoning service. In case of the current implementation, we rely on HermiT [
        <xref ref-type="bibr" rid="ref3">3</xref>
        ].
Semantics4J transforms the extended program into standard JVM bytecode, which may query
the reasoning service during runtime to gain access to the data (see Fig. 1). The current
implementation can be found at https://github.com/hartenfels/Semantics4J.
      </p>
    </sec>
    <sec id="sec-4">
      <title>Summary</title>
      <p>In this paper, we present Semantics4J, an extended Java compiler for type-checking
programs working with OWL. It allows for using ontological concepts as types by
integrating the reasoner into type-checking. Furthermore, it provides typed queries. Typing
information allows the system to reject potentially problematic programs that may
produce runtime-errors. However, some restrictions have been put onto the system. A pure
extension that does not modify existing rules of a compiler requires a strict and
unambiguous syntactic separation between existing rules and newly integrated rules. The
distinction between types and values also creates some restrictions as values can be created
and modified during runtime. Typing of e.g., query-for can only consider values given
directly to the operator while variable parts are ignored. A deeper knowledge about the
dynamics of such variables would be desirable to allow for a more precise typing, but
requires an analysis of the flow of the program that is currently not implemented.</p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          1.
          <string-name>
            <given-names>J. J.</given-names>
            <surname>Carroll</surname>
          </string-name>
          , I. Dickinson,
          <string-name>
            <given-names>C.</given-names>
            <surname>Dollin</surname>
          </string-name>
          ,
          <string-name>
            <given-names>D.</given-names>
            <surname>Reynolds</surname>
          </string-name>
          ,
          <string-name>
            <given-names>A.</given-names>
            <surname>Seaborne</surname>
          </string-name>
          , and
          <string-name>
            <given-names>K.</given-names>
            <surname>Wilkinson</surname>
          </string-name>
          .
          <article-title>Jena: implementing the semantic web recommendations</article-title>
          .
          <source>In Proc. WWW - Alternate Track Papers &amp; Posters</source>
          , pages
          <fpage>74</fpage>
          -
          <lpage>83</lpage>
          . ACM,
          <year>2004</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          2.
          <string-name>
            <given-names>T.</given-names>
            <surname>Ekman</surname>
          </string-name>
          and
          <string-name>
            <given-names>G.</given-names>
            <surname>Hedin. The JastAdd Extensible Java</surname>
          </string-name>
          <article-title>Compiler</article-title>
          .
          <source>In Proc. OOPSLA '07</source>
          , pages
          <fpage>1</fpage>
          -
          <lpage>18</lpage>
          . ACM,
          <year>2007</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          3.
          <string-name>
            <given-names>B.</given-names>
            <surname>Glimm</surname>
          </string-name>
          ,
          <string-name>
            <given-names>I.</given-names>
            <surname>Horrocks</surname>
          </string-name>
          ,
          <string-name>
            <given-names>B.</given-names>
            <surname>Motik</surname>
          </string-name>
          , G. Stoilos, and
          <string-name>
            <given-names>Z.</given-names>
            <surname>Wang</surname>
          </string-name>
          .
          <source>HermiT: An OWL 2 Reasoner. J. Autom. Reason.</source>
          ,
          <volume>53</volume>
          (
          <issue>3</issue>
          ):
          <fpage>245</fpage>
          -
          <lpage>269</lpage>
          ,
          <year>2014</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          4.
          <string-name>
            <given-names>M.</given-names>
            <surname>Leinberger</surname>
          </string-name>
          ,
          <string-name>
            <given-names>R.</given-names>
            <surname>Lämmel</surname>
          </string-name>
          , and
          <string-name>
            <given-names>S.</given-names>
            <surname>Staab</surname>
          </string-name>
          .
          <article-title>The Essence of Functional Programming on Semantic Data</article-title>
          .
          <source>In Proc. ESOP</source>
          <year>2017</year>
          , LNCS, pages
          <fpage>750</fpage>
          -
          <lpage>776</lpage>
          . Springer,
          <year>2017</year>
          .
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>