<!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>Deep Integration of Python with Web Ontology Language</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Marian Babik</string-name>
          <email>Marian.Babik@saske.sk</email>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Ladislav Hluchy ?</string-name>
          <email>Ladislav.Hluchy@savba.sk</email>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>Intelligent and Knowledge-based Technologies Group, Department of Parallel and Distributed Computing, Institute of Informatics, Slovak Academy of Sciences</institution>
        </aff>
      </contrib-group>
      <abstract>
        <p>The Semantic Web is a vision for the future of the Web in which information is given explicit meaning, making it easier for machines to automatically process and integrate information available on the Web. Semantic Web will build on the well known Semantic Web language stack, part of which is the Web Ontology Language (OWL). Python is an interpreted, object-oriented, extensible programming language, which provides an excellent combination of clarity and versatility. The deep integration of both languages is one of the novel approaches for enabling free and interoperable data [1]. In this article we present a metaclass-based implementation of the deep integration ideas. The implementation is an early Python prototype supporting in-line class and properties declaration, instance creation and simple triple-based queries. The implementation is backed up by a well known OWL-DL reasoner Pellet [3]. The integration of the Python and OWL-DL through meta-class programming provides a unique approach, which can be implemented with any metaclass enabled scripting language.</p>
      </abstract>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>Introduction</title>
      <p>
        The deep integration of scripting languages and Semantic Web has introduced
an idea of importing the ontologies directly into the programming context so
that its classes are usable alongside classes de ned normally. This can provide a
more natural mapping of OWL-DL than classic APIs, re ecting the set-theoretic
semantics of OWL-DL, while preserving the access to the classic Python objects.
Such integration also encourages separation of concerns among declarative and
procedural and encourages a new wave of programming, where problems can be
? Acknowledgments: The research reported in this paper has been partially nanced by
the EU within the project IST-2004-511385 K-WfGrid and Slovak national projects,
Research and development of a knowledge based system to support work ow
management in organizations with administrative processes, APVT-51-024604; Tools for
acquisition, organization and maintenance of knowledge in an environment of
heterogeneous information resources, SPVV 1025/04; E cient tools and mechanisms
for grid computing (2006-2008) VEGA 2/6103/6.
de ned by using description logics [
        <xref ref-type="bibr" rid="ref10">10</xref>
        ] and manipulated by dynamic scripting
languages [
        <xref ref-type="bibr" rid="ref1">1</xref>
        ]. The approach represents a uni cation, that allows both languages
to be conveniently used for di erent subproblems in the software-engineering
environment.
      </p>
      <p>
        In this article we would like to introduce an early prototype, which
implements some of the ideas of the deep integration in Python language [
        <xref ref-type="bibr" rid="ref8">8</xref>
        ]. It
supports in-line declaration of OWL classes and properties, instance creation and
simple triple-based queries [
        <xref ref-type="bibr" rid="ref2">2</xref>
        ]. We will emphasize the notion of modeling
intensional sets (i-sets) through metaclasses. We will also discuss the possible
drawbacks of the approach and the current implementation.
2
      </p>
    </sec>
    <sec id="sec-2">
      <title>Intensional Sets and Metaclasses</title>
      <p>
        Intensional sets as introduced in [
        <xref ref-type="bibr" rid="ref1">1</xref>
        ] are sets that are described with OWL DL's
construct and according to this description, encompass all tting instances. A
sample intensional set can be de ned by using Notation3 (N3) [
        <xref ref-type="bibr" rid="ref12">12</xref>
        ] as, e.g.
":Person a owl:Class; rdfs:subClassOf :Mortal". This simply states that Person is also
a Mortal. Assuming we introduce two instances, e.g. ":John a :Person and :Jane
a :Mortal", the instances of Mortal are both John and Jane. Please note, that
N3 is used only for demonstration purposes, the current implementation can also
support NTriples and RDF/XML.
      </p>
      <p>Terminology-wise, a metaclass is simply "the class of a class". Any class
whose instances are themselves classes, is a metaclass. A metaclass-based
implementation of the intensional sets is based on the core metaclass Thing, whose
constructor accepts two main attributes, i.e. default namespace and N3
description of the i-set. The instance of the metaclass is then a mapping of the OWL
class to the intensional set. Following the above example class Person can be
created with a Python construct:
Person = Thing('Person', (),
{defined_by: 'a owl:Class; rdfs:subClassOf :Mortal',\
namespace: 'http://samplens.org/test#'})
This creates a Python class representing the intensional set for Person and its
namespace. In the background it also updates the knowledge base with the new
assertion. The individual John can then be instantiated simply by calling J ohn =
P erson(). This statement calls the default constructor of the class Person, which
provides support for asserting new OWL individual into the knowledge base.
A similar metaclass is used for the OWL property except that it can not be
instantiated. The constructor is used here for di erent purpose, i.e. to create a
relation between classes or individuals. The notion of importing the ontology into
the Python's namespace is then a matter of decomposing the ontology into the
groups of intensional sets, generating Python classes for these sets and creating
the instances.</p>
      <p>This kind of programming is also called metaclass programming and can
provide a transparent way how to generate new OWL classes and properties. Since
metaclasses act like regular classes it is possible to extend their functionality by
inheriting from the base metaclass. It is also simple to hide the complex tasks
needed for accessing the knowledge base, reasoner and processing the mappings
between OWL-DL's concepts and their respective Python counterparts.</p>
    </sec>
    <sec id="sec-3">
      <title>3 Sample session</title>
      <p>A sample session shows an in-line declaration of a class Person. This is
implemented by calling a static method new of the metaclass Thing (the static method
new is used to hide the complex call introduced in Sec. 2). An instance is created
by calling the Person's constructor, which in fact creates an in-line declaration
of the OWL individual (John). The print statements show the Python's view of
the respective objects, i.e. Person as a class and John as an instance of the class
Person. We also show a more complex de nition of the PersonWithSingleSon,
which we will later use to demonstrate the reasoning about property assertions.
&gt;&gt;&gt; from Thing import Thing
&gt;&gt;&gt; Person = Thing.new('Person',' a owl:Class .')
&gt;&gt;&gt; John = Person()
&gt;&gt;&gt; print Person
&lt;class 'Thing.Person'&gt;
&gt;&gt;&gt; print John
&lt;Thing.Person object at 0xb7d0b50c&gt;
&gt;&gt;&gt; PersonWithSingleSon = Thing.new('PersonWithSingleSon', \
""" a owl:Class ; rdfs:subClassOf [ a owl:Restriction ;
owl:cardinality "1"^^&lt;http://www.w3.org/2001/XMLSchema#int&gt; ;
owl:onProperty :hasSon
] ;
rdfs:subClassOf [ a owl:Restriction ;
owl:cardinality "1"^^&lt;http://www.w3.org/2001/XMLSchema#int&gt; ;
owl:onProperty :hasChild ] .""")</p>
      <p>A similar way can be used for in-line declarations of OWL properties.
Compared to a class declaration the returned Python class can not be instantiated
(i.e. returns None).
&gt;&gt;&gt; from PropertyThing import Property
&gt;&gt;&gt; hasChild = Property.new('hasChild',' a owl:ObjectProperty .')
&gt;&gt;&gt; print hasChild
&lt;class 'PropertyThing.hasChild'&gt;
&gt;&gt;&gt; hasSon = Property.new('hasSon', ' a owl:ObjectProperty ;
rdfs:subPropertyOf :hasChild .')</p>
      <p>Properties are naturally used to assign relationships between OWL classes
or individuals, which can be as simple as calling:
&gt;&gt;&gt; Bob = PersonWithSingleSon()
&gt;&gt;&gt; hasChild(Bob, John)</p>
      <p>Assuming we have declared several instances of the class Person we can
nd them by iterating over the class list. It is also possible to ask any triple
like queries (the query shown also demonstrates reasoning about the property
assertions).
&gt;&gt;&gt; for individual in Person.findInstances():
... print individual, individual.name
&lt;Thing.Man object at 0xb7d0b64c&gt; Peter
&lt;Thing.Person object at 0xb7d0b50c&gt; John
&lt;Thing.Person object at 0xb7d0b6ec&gt; Jane
&gt;&gt;&gt; for who in hasSon.query(Bob):
... who.name
'John'
&gt;&gt;&gt; print hasSon.query(Bob, John)
1
4</p>
    </sec>
    <sec id="sec-4">
      <title>Implementation and Drawbacks</title>
      <p>
        Apart from the metaclass-based implementation of the i-sets, it is necessary to
support query answering, i.e. provide an interface to the OWL-DL reasoner.
There are several choices for the OWL-DL reasoners including Racer, Pellet and
Kaon2 [
        <xref ref-type="bibr" rid="ref19 ref3 ref4">4, 3, 19</xref>
        ]. Although these reasoners provide su cient support for
OWLDL their integration with Python is not trivial since they are mostly based on
Java (except Racer) and although they can work in server-like mode Python's
support for standard protocols (like DIG) is missing. Other possibilities are to use
Python-based inference engines like CWM, Pychinko or Euler [13{15]. However,
due to the performance reasons, lack of documentation or too early prototypes we
have decided to use Java-based reasoners. We have managed to successfully use
JPype [
        <xref ref-type="bibr" rid="ref16">16</xref>
        ], which interfaces Java and Python at native level of virtual machines
(using JNI). This enables the possibility to access Java libraries from within
CPython. Having the ability to call Java and use all the capabilities of the
current version of CPython (e.g. metaclasses) is a big advantage over the other
approaches such as Jython or JPE [
        <xref ref-type="bibr" rid="ref11 ref9">11, 9</xref>
        ]. We have developed a wrapper class,
which can call Jena and Pellet APIs and perform the needed reasoning [
        <xref ref-type="bibr" rid="ref3 ref5">5, 3</xref>
        ].
The wrapper class is implemented as a singleton and interfaces the Python calls
to the reasoner and RDF/OWL API and forwards it to the JVM with the help
of the JPype.
      </p>
      <p>
        One of the main drawbacks of the current implementation is the fact, that
it doesn't support open world semantics (OWA). Although the reasoner in the
current implementation can perform OWA reasoning and thus it is possible to
correctly answer queries, the Python's semantics are based on the boolean values.
One of the possibilities is to use epistemic operator as suggested in [
        <xref ref-type="bibr" rid="ref1">1</xref>
        ], however
this is yet to be implemented. Another problem when dealing with ontologies
are namespaces. In the current prototype we have added a set of namespaces
that constitute the corresponding OWL class or property description as an
attribute of the Python's class. This attribute can then be used to generate the
headers for the N3 description. This approach needs further extension to support
management of di erent ontologies. One of the possibilities would be to re-use
Python's module namespace by introducing a core ontology class. This ontology
class would serve as a default namespace handler as well as a common importing
point for the ontology classes.
      </p>
      <p>The other drawback of the approach is the performance of the reasoner, which
is due to the nature of the JPype implementation (the conversions between
virtual machines imposes rather large performance bottlenecks). This can be
solved by extending the support for other Python to Java APIs and possible
implementation of specialized client-server protocols.
5</p>
    </sec>
    <sec id="sec-5">
      <title>Related Work</title>
      <p>
        To our best knowledge there is currently no Python implementation of the deep
integration ideas. There is a Ruby implementation done by Obie Fernandez,
however it is not known what is the current status of the project [
        <xref ref-type="bibr" rid="ref20">20</xref>
        ].
      </p>
      <p>
        The most popular existing OWL APIs, that provide programmatic access
to the OWL structures are based on Java [
        <xref ref-type="bibr" rid="ref5 ref7">5, 7</xref>
        ]. Since Java is a frame language
its notion of polymorphism is very di erent than in RDF/OWL. This is usually
solved by incorporating design patterns, which make the APIs quite complex
and sometimes di cult to use. The dynamic nature of the scripting languages
can support OWL/RDF level of polymorphism and thus it is possible to directly
expose the OWL structures as Python classes without any API interfaces. One of
the interesting Java projects, which tries to automatically map OWL ontologies
into Java through Java Beans is based on the ideas shown in [
        <xref ref-type="bibr" rid="ref17">17</xref>
        ]. This approach
tries to nd a way how to directly map the ontologies to the hierarchy of the
Java classes and interfaces.
      </p>
      <p>
        Among the existing Python libraries, which support RDF and OWL, the
most interesting in terms of partial integration are Sparta and Tramp, which
bind RDF graph nodes to Python objects and RDF arcs to attributes of such
objects [
        <xref ref-type="bibr" rid="ref21 ref6">21, 6</xref>
        ]. The projects however doesn't clearly address the OWL and are
mainly considered with RDF. It is thus di cult to evaluate what is the level of
support for the inferred OWL models.
6
      </p>
    </sec>
    <sec id="sec-6">
      <title>Conclusion</title>
      <p>
        We have described a metaclass-based prototype implementation of the deep
integration ideas. We have discussed the advantages and shortcomings of the current
implementation. We would like to note, that this a work in progress, which is
constantly changing and this is just a report of the current status. At the time
of writing authors are setting up an open source project, which will host the
implementation of the ideas presented. There are many other open questions,
that we haven't covered here including integration of query languages
(possibility to re-use ideas from native queries [
        <xref ref-type="bibr" rid="ref18">18</xref>
        ]); serialization of the ontologies;
representation of rules, concrete domains, etc. We hope that having an initial
implementation is a good start and that its continuation will contribute to the
success of the deep integration of the scripting and Semantic Web.
      </p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          1.
          <string-name>
            <surname>Vrandecic</surname>
            ,
            <given-names>D.</given-names>
          </string-name>
          ,
          <article-title>Deep Integration of Scripting Languages and Semantic Web Technologies</article-title>
          , In Soren Auer, Chris Bizer,
          <source>Libby Miller, 1st International Workshop on Scripting for the Semantic Web SFSW</source>
          <year>2005</year>
          , volume
          <volume>135</volume>
          <source>of CEUR Workshop Proceedings. CEUR-WS.org, Herakleion</source>
          , Greece, May
          <year>2005</year>
          . ISSN:
          <fpage>1613</fpage>
          -
          <lpage>0073</lpage>
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          2.
          <string-name>
            <given-names>Web</given-names>
            <surname>Ontology</surname>
          </string-name>
          <article-title>Language (OWL)</article-title>
          , see http://www.w3.org/TR/owl-features/
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          3.
          <string-name>
            <surname>Pellet</surname>
            <given-names>OWL</given-names>
          </string-name>
          Reasoner, see http://www.mindswap.org/2003/pellet/index.shtml
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          4.
          <string-name>
            <given-names>RacerPro</given-names>
            <surname>Reasoner</surname>
          </string-name>
          , see http://www.racer-systems.com
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          5.
          <string-name>
            <surname>Jena</surname>
          </string-name>
          :
          <article-title>A Semantic Web Framework for Java</article-title>
          , see http://www.hpl.hp.com/semweb/jena2.htm.
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          6. TRAMP:
          <article-title>Makes RDF look like Python data structures http://www</article-title>
          .aaronsw.com/2002/tramp, http://www.amk.ca/conceit/rdfinterface.html
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          7.
          <string-name>
            <surname>Bechhofer</surname>
            ,
            <given-names>S.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Lord</surname>
            ,
            <given-names>P.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Volz</surname>
          </string-name>
          ,R.:
          <article-title>Cooking the Semantic Web with the OWL API</article-title>
          . 2nd International Semantic Web Conference, ISWC,
          <string-name>
            <surname>Sanibel</surname>
            <given-names>Island</given-names>
          </string-name>
          , Florida,
          <year>October 2003</year>
        </mixed-citation>
      </ref>
      <ref id="ref8">
        <mixed-citation>
          8.
          <string-name>
            <surname>G. van Rossum</surname>
          </string-name>
          ,
          <article-title>Computer programming for everybody</article-title>
          .
          <source>Technical report, Corporation for National Research Initiatives</source>
          ,
          <year>1999</year>
        </mixed-citation>
      </ref>
      <ref id="ref9">
        <mixed-citation>
          9.
          <string-name>
            <surname>Java-Python</surname>
            <given-names>Extension</given-names>
          </string-name>
          , http://sourceforge.net/projects/jpe
        </mixed-citation>
      </ref>
      <ref id="ref10">
        <mixed-citation>
          10.
          <string-name>
            <surname>Baader</surname>
            ,
            <given-names>F.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Calvanese</surname>
            ,
            <given-names>D.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>McGuinness</surname>
            ,
            <given-names>D.</given-names>
          </string-name>
          ,
          <string-name>
            <given-names>L.</given-names>
            ,
            <surname>Nardi</surname>
          </string-name>
          ,
          <string-name>
            <given-names>D.</given-names>
            and
            <surname>Patel-Schneider</surname>
          </string-name>
          ,
          <string-name>
            <surname>P.</surname>
          </string-name>
          , F. editors.
          <article-title>The description logic handbook: theory, implementation, and applications</article-title>
          . Cambridge University Press, New York, NY, USA,
          <year>2003</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref11">
        <mixed-citation>
          11.
          <string-name>
            <surname>Jython</surname>
          </string-name>
          ,
          <article-title>Java implementation of the Python</article-title>
          , http://www.jython.org/
        </mixed-citation>
      </ref>
      <ref id="ref12">
        <mixed-citation>12. Notation 3, see http://www.w3.org/DesignIssues/Notation3.html</mixed-citation>
      </ref>
      <ref id="ref13">
        <mixed-citation>
          13. Closed World Machine, see http://www.w3.org/
          <year>2000</year>
          /10/swap/doc/cwm.html
        </mixed-citation>
      </ref>
      <ref id="ref14">
        <mixed-citation>
          14.
          <string-name>
            <surname>Katz</surname>
            ,
            <given-names>Y.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Clark</surname>
            ,
            <given-names>K.</given-names>
          </string-name>
          and
          <string-name>
            <surname>Parsia</surname>
            ,
            <given-names>B.</given-names>
          </string-name>
          ,
          <article-title>Pychinko: A native python rule engine</article-title>
          .
          <source>In International Python Conference 05</source>
          ,
          <year>2005</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref15">
        <mixed-citation>15. Euler proof mechanism, see http://www.agfa.com/w3c/euler/</mixed-citation>
      </ref>
      <ref id="ref16">
        <mixed-citation>16. JPype, Java to Python integration, see http://jpype.sourceforge.net/</mixed-citation>
      </ref>
      <ref id="ref17">
        <mixed-citation>
          17.
          <string-name>
            <surname>Kalyanpur</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Pastor</surname>
            ,
            <given-names>D.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Battle</surname>
            ,
            <given-names>S.</given-names>
          </string-name>
          and
          <string-name>
            <surname>Padget</surname>
            ,
            <given-names>J.</given-names>
          </string-name>
          ,
          <article-title>Automatic mapping of owl ontologies into java</article-title>
          .
          <source>In Proceedings of Software Engg. - Knowledge Engg. (SEKE)</source>
          <year>2004</year>
          , Ban , Canada,
          <year>June 2004</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref18">
        <mixed-citation>
          18.
          <string-name>
            <surname>Cook</surname>
            ,
            <given-names>W. R.</given-names>
          </string-name>
          and
          <string-name>
            <surname>Rosenberger</surname>
            ,
            <given-names>C.</given-names>
          </string-name>
          ,
          <article-title>Native Queries for Persistent Objects</article-title>
          , Dr.
          <source>Dobb's Journal</source>
          ,
          <year>February 2006</year>
        </mixed-citation>
      </ref>
      <ref id="ref19">
        <mixed-citation>
          19.
          <string-name>
            <surname>Hustadt</surname>
            ,
            <given-names>U.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Motik</surname>
            ,
            <given-names>B.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Sattler</surname>
          </string-name>
          , U..
          <article-title>Reducing SHIQ Description Logic to Disjunctive Datalog Programs</article-title>
          .
          <source>Proc. of the 9th International Conference on Knowledge Representation and Reasoning (KR2004)</source>
          ,
          <year>June 2004</year>
          , Whistler, Canada, pp.
          <fpage>152</fpage>
          -
          <lpage>16</lpage>
        </mixed-citation>
      </ref>
      <ref id="ref20">
        <mixed-citation>
          20.
          <string-name>
            <surname>Fernandez</surname>
            ,
            <given-names>O.</given-names>
          </string-name>
          ,
          <article-title>Deep Integration of Ruby with Semantic Web Ontologies, see gigaton</article-title>
          .thoughtworks.net/ ofernand1/DeepIntegration.pdf
        </mixed-citation>
      </ref>
      <ref id="ref21">
        <mixed-citation>
          21.
          <string-name>
            <surname>Sparta</surname>
          </string-name>
          , Python API for RDF, see http://www.mnot.net/sw/sparta/
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>