<!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>CodeOntology: Querying Source Code in a Semantic Framework</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Mattia Atzeni</string-name>
          <email>ma.atzeni12@studenti.unica.it</email>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Maurizio Atzori</string-name>
          <email>atzori@unica.it</email>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>University of Cagliari Math/CS Department Via Ospedale 72</institution>
          ,
          <addr-line>09124 Cagliari (CA)</addr-line>
          ,
          <country country="IT">Italy</country>
        </aff>
      </contrib-group>
      <abstract>
        <p>Code reuse, code querying and computer aided programming are some of the main research challenges in software engineering. Therefore, we have introduced CodeOntology as an approach to leverage recent advances in the Semantic Web area and the impressive amount of open source code freely available online, to provide a semantic view of software systems by extracting structured information from source code and by performing named entity disambiguation on the comments provided within the code, in order to link the corresponding entities to pertinent DBpedia resources. In this paper, we focus on the expressiveness of this framework by showing how CodeOntology can be used for static code analysis, semantic component search and code reuse.</p>
      </abstract>
      <kwd-group>
        <kwd>SPARQL</kwd>
        <kwd>RDF</kwd>
        <kwd>OWL</kwd>
        <kwd>Ontology</kwd>
        <kwd>Programming Languages</kwd>
      </kwd-group>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>-</title>
      <p>
        Recent research in software engineering is focusing towards graph-based
approaches to model software architecture and software process. For instance, in
[
        <xref ref-type="bibr" rid="ref1">1</xref>
        ] and [
        <xref ref-type="bibr" rid="ref2">2</xref>
        ], software is modeled as a directed multigraph, thereby enabling the
collection and maintenance of the architectural knowledge in respect to both
software and software process. However, the Semantic Web technology stack already
provides exible and expressive standards to represent structured information
in a format that is easy to query and automatically process. Therefore, in [
        <xref ref-type="bibr" rid="ref3">3</xref>
        ] we
have introduced CodeOntology, as an attempt to provide a semantic view of
software systems, by leveraging the Semantic Web technology stack. CodeOntology
includes two main contributions: (i) an OWL 2 ontology modeling
object-oriented code constructs and (ii) a parser which relies on Spoon [
        <xref ref-type="bibr" rid="ref4">4</xref>
        ] to serialize
Java source code or bytecode into RDF triples, thereby creating a queryable
RDF representation of source code. Furthermore, CodeOntology makes use of
TagMe [
        <xref ref-type="bibr" rid="ref5">5</xref>
        ] to automatically add links to DBpedia [
        <xref ref-type="bibr" rid="ref6">6</xref>
        ] by disambiguating named
entities found within the comments available in the source code.
      </p>
      <p>
        Details about the implementation of the parser and the design of the ontology
have been provided in [
        <xref ref-type="bibr" rid="ref3">3</xref>
        ]. In this paper, we focus on a practical demonstration
about the level of expressiveness that can be achieved using CodeOntology. We
dig into more details about the structure of the data sets generated by the
parser and we present extended results and experiments by providing some
simple SPARQL queries showing how CodeOntology can be used for static code
analysis and component search and reuse.
2
      </p>
    </sec>
    <sec id="sec-2">
      <title>Data Set from OpenJDK</title>
      <p>The parser provided by CodeOntology is capable of analyzing the structure of
Java projects to generate RDF triples. The input of this process can either be
(i) a text le containing Java source code, (ii) the root directory of a Java project
or (iii) a JAR le aggregating many Java class les. The output mainly contains
triples about structural information common to all object-oriented programming
languages, like class hierarchy or the RDF serialization of the underlying
structure of each class. The parser has been successfully applied to the OpenJDK
8 source code1, generating a data set which consists of about 2 million RDF
triples. Figure 1 shows some information about the structure of this data set.</p>
      <p>As we can see, actual source code as literals and literal comments are less than
10% of the total number of extracted RDF triples, while structural information
about source code covers almost the 80% of the data set. This knowledge base
is available at https://doi.org/10.5281/zenodo.818116 and can be queried
at http://codeontology.org/sparql.</p>
      <p>The parser supports both Maven and Gradle projects. This allows to
download the dependencies of the input project in the form of JAR les that can
be optionally analyzed and serialized into RDF triples. The parser, along with
a detailed tutorial about how to apply it on di erent kinds of Java projects, is
available on GitHub2.
1 http://openjdk.java.net/
2 https://github.com/codeontology/parser</p>
    </sec>
    <sec id="sec-3">
      <title>Queries over Source Code</title>
      <p>CodeOntology allows to leverage a powerful language like SPARQL to run highly
expressive queries over source code. When a method m references another
resource r, then the parser is able to serialize this information into a triple of the
form: m woc:references r. We can use this property to easily select recursive
methods by means of the following simple query.</p>
      <p>SELECT ? method
WHERE {
? method a woc : Method ;</p>
      <p>woc : references + ? method .
}
A more interesting example which makes use of the same property is given by the
following query, which selects the classes that turn out to be the most referenced
ones by the methods of the other classes.</p>
      <p>SELECT ? class ( COUNT ( DISTINCT ? anotherClass ) as ? count )
WHERE {
? class a woc : Class .
? method a woc : Method ;
woc : isDeclaredBy ? anotherClass ;
woc : references ? class .</p>
      <p>FILTER (? class != ? anotherClass )
}
GROUP BY ? class
ORDER BY DESC (? count )
Unsurprisingly, the most referenced class in OpenJDK is the java.lang.String
class, followed by the classes java.lang.Object and java.io.IOException.</p>
      <p>Another important use case of CodeOntology is undoubtedly the semantic
retrieval of software components. For instance, we can exploit DBpedia links to
select all methods computing the cube root of a parameter of type double.
SELECT ? method
WHERE {
? method a woc : Method ;
woc : hasParameter / woc : hasType woc : Double ;
dul : associatedWith dbpedia : Cube_root .</p>
      <p>The execution of this query against the data set extracted from OpenJDK yields
two methods, namely the method cbrt(double) declared by the class java.
lang.Math and the method cbrt(double), declared by the class java.lang.
StrictMath. Another example is given by the following query, which selects all
resources associated with public-key cryptography and, in particular, with RSA.
SELECT ?r
WHERE {
?r dul : associatedWith
dbpedia : Public - key_cryptography ,
dbpedia : RSA_ \( cryptosystem \) .</p>
      <p>
        CodeOntology allows also to run other interesting queries for di erent purposes,
such as detecting the implementation of a speci ed design pattern or computing
}
}
software metrics, like the well-known CK metrics [
        <xref ref-type="bibr" rid="ref7">7</xref>
        ]. Table 1 lists some more
examples, which are available at http://codeontology.org/examples.
      </p>
      <p>Sort classes by the number of subclasses
Select classes implementing the Singleton pattern
Select classes implementing the Builder pattern
Select classes implementing the Factory pattern
Select methods to read/write Zip les</p>
      <p>Select methods to read an image at a speci ed URL</p>
    </sec>
    <sec id="sec-4">
      <title>Conclusions and Demo Showcase</title>
      <p>CodeOntology is an open community-shared resource which aims at enabling
the RDF representation, from coarse to ne grain, of the structure and relations
found within source code. This way, it is possible to precisely search speci c
software components using expressive SPARQL queries, some of which will be
showcased during the demo. A video showing how to build and use the parser,
as well as the execution of some query examples, is available at: https://www.
youtube.com/watch?v=bd6pvUDy8kA.</p>
      <p>Acknowledgments. This work was supported in part by a 2015 Google Faculty
Research Award and Sardegna Ricerche (project OKgraph, CRP 120).</p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          1.
          <string-name>
            <surname>Dabrowski</surname>
          </string-name>
          , Robert and Stencel, Krzysztof and Timoszuk, Grzegorz.
          <source>In: Software Is a Directed Multigraph</source>
          . Springer Berlin Heidelberg, Berlin, Heidelberg (
          <year>2011</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          2.
          <string-name>
            <surname>Dabrowski</surname>
          </string-name>
          , Robert and Stencel, Krzysztof and Timoszuk, Grzegorz:
          <article-title>Software is a directed multigraph (and so is software process)</article-title>
          .
          <source>CoRR abs/1103</source>
          .4056 (
          <year>2011</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          3.
          <string-name>
            <surname>Atzeni</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Atzori</surname>
            ,
            <given-names>M.:</given-names>
          </string-name>
          <article-title>CodeOntology: RDF-ization of Source Code</article-title>
          .
          <source>In: The Semantic Web { ISWC</source>
          <year>2017</year>
          : 16th International Semantic Web Conference, Vienna, Austria,
          <source>October</source>
          <volume>21</volume>
          {
          <fpage>25</fpage>
          , Springer International Publishing (
          <year>2017</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          4.
          <string-name>
            <surname>Pawlak</surname>
            ,
            <given-names>R.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Monperrus</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Petitprez</surname>
            ,
            <given-names>N.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Noguera</surname>
            ,
            <given-names>C.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Seinturier</surname>
            ,
            <given-names>L.</given-names>
          </string-name>
          :
          <article-title>Spoon: A library for implementing analyses and transformations of java source code</article-title>
          .
          <source>Software: Practice and Experience</source>
          (
          <year>2015</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          5.
          <string-name>
            <surname>Ferragina</surname>
            ,
            <given-names>P.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Scaiella</surname>
            ,
            <given-names>U.</given-names>
          </string-name>
          : Tagme:
          <article-title>On-the- y annotation of short text fragments (by wikipedia entities)</article-title>
          .
          <source>In: Proceedings of the 19th ACM International Conference on Information and Knowledge Management. CIKM '10</source>
          ,
          <string-name>
            <surname>ACM</surname>
          </string-name>
          (
          <year>2010</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          6.
          <string-name>
            <surname>Lehmann</surname>
            ,
            <given-names>J.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Isele</surname>
            ,
            <given-names>R.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Jakob</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Jentzsch</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Kontokostas</surname>
            ,
            <given-names>D.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Mendes</surname>
            ,
            <given-names>P.N.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Hellmann</surname>
            ,
            <given-names>S.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Morsey</surname>
            , M., van Kleef,
            <given-names>P.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Auer</surname>
            ,
            <given-names>S.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Bizer</surname>
            ,
            <given-names>C.</given-names>
          </string-name>
          :
          <article-title>DBpedia - a largescale, multilingual knowledge base extracted from wikipedia</article-title>
          .
          <source>Semantic Web Journal</source>
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          7.
          <string-name>
            <surname>Chidamber</surname>
            ,
            <given-names>S.R.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Kemerer</surname>
            ,
            <given-names>C.F.</given-names>
          </string-name>
          :
          <article-title>Towards a metrics suite for object oriented design</article-title>
          .
          <source>SIGPLAN Not</source>
          .
          <volume>26</volume>
          (
          <issue>11</issue>
          ) (
          <year>November 1991</year>
          )
          <volume>197</volume>
          {
          <fpage>211</fpage>
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>