RDFClosure.CombinedClosure

Package RDFClosure :: Module CombinedClosure |
[hide private]
[frames] | no frames]

# Source Code for Module RDFClosure.CombinedClosure

      1  #!/d/Bin/Python/python.exe
      2  # -*- coding: utf-8 -*-
      3  #
      4  """
      5  The combined closure: performing I{both} the OWL 2 RL and RDFS closures.
      6  The two are very close but there are some rules in RDFS that are not in OWL 2 RL (eg, the axiomatic
      7  triples concerning the container membership properties). Using this closure class the
      8  OWL 2 RL implementation becomes a full extension of RDFS.
      9
     10  @requires: U{RDFLib<https://github.com/RDFLib/rdflib>}, 4.0.0 and higher
     11  @license: This software is available for use under the U{W3C Software License<http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231>}
     12  @organization: U{World Wide Web Consortium<http://www.w3.org>}
     13  @author: U{Ivan Herman<a href="http://www.w3.org/People/Ivan/">}
     14
     15  """
     16
     17  __author__  = 'Ivan Herman'
     18  __contact__ = 'Ivan Herman, ivan@w3.org'
     19  __license__ = u'W3C® SOFTWARE NOTICE AND LICENSE, http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231'
     20
     21  from RDFClosure.RDFS import Resource, Class, Datatype
     22  from RDFClosure.OWL  import OWLClass, Thing, equivalentClass, DataRange
     23
     24  from RDFClosure.RDFSClosure import RDFS_Semantics
     25  from RDFClosure.OWLRL       import OWLRL_Semantics
     26
     27  ######################################################################################################
     28
     29
     30  # noinspection PyPep8Naming
     31 -class RDFS_OWLRL_Semantics(RDFS_Semantics, OWLRL_Semantics):
     32          """Common subclass of the RDFS and OWL 2 RL semantic classes. All methods simply call back
     33          to the functions in the superclasses. This may lead to some unnecessary duplication of terms
     34          and rules, but it it not so bad. Also, the additional identification defined for OWL Full,
     35          ie, Resource being the same as Thing and OWL and RDFS classes being identical are added to the
     36          triple store.
     37
     38          Note that this class is also a possible user extension point: subclasses can be created that
     39          extend the standard functionality by extending this class. This class I{always} performs RDFS inferences.
     40          Subclasses have to set the C{self.rdfs} flag explicitly to the requested value if that is to be controlled.
     41
     42          @ivar full_binding_triples: additional axiom type triples that are added to the combined semantics; these 'bind' the RDFS and the OWL worlds together
     43          @ivar rdfs: whether RDFS inference is to be performed or not. In this class instance the value is I{always} C{True}, subclasses may explicitly change it at initialization time.
     44          @type rdfs: boolean
     45          """
     46          full_binding_triples = [
     47                  (Thing, equivalentClass, Resource),
     48                  (Class, equivalentClass, OWLClass),
     49                  (DataRange, equivalentClass, Datatype)
     50          ]
     51
     52 -        def __init__(self, graph, axioms, daxioms, rdfs=True):
     53                  """
     54                  @param graph: the RDF graph to be extended
     55                  @type graph: rdflib.Graph
     56                  @param axioms: whether (non-datatype) axiomatic triples should be added or not
     57                  @type axioms: bool
     58                  @param daxioms: whether datatype axiomatic triples should be added or not
     59                  @type daxioms: bool
     60                  @param rdfs: placeholder flag (used in subclassed only, it is always defaulted to True in this class)
     61                  @type rdfs: boolean
     62                  """
     63                  OWLRL_Semantics.__init__(self, graph, axioms, daxioms, rdfs)
     64                  RDFS_Semantics.__init__(self, graph, axioms, daxioms, rdfs)
     65                  self.rdfs = True
     66
     67          # noinspection PyMethodMayBeStatic
     68          @staticmethod
     69 -        def add_new_datatype(uri, conversion_function, datatype_list, subsumption_dict=None, subsumption_key=None, subsumption_list=None):
     70                  """If an extension wants to add new datatypes, this method should be invoked at initialization time.
     71
     72                  @param uri : URI for the new datatypes, like owl_ns["Rational"]
     73                  @param conversion_function : a function converting the lexical representation of the datatype to a Python value,
     74                  possibly raising an exception in case of unsuitable lexical form
     75                  @param datatype_list : list of datatypes already in use that has to be checked
     76                  @param subsumption_dict : dictionary of subsumption hierarchies (indexed by the datatype URI-s)
     77                  @param subsumption_key  : key in the dictionary, if None, the uri parameter is used
     78                  @param subsumption_list : list of subsumptions associated to a subsumption key (ie, all datatypes that are superclasses of the new datatype)
     79                  """
     80                  from DatatypeHandling import AltXSDToPYTHON, use_Alt_lexical_conversions
     81
     82                  if datatype_list:
     83                          datatype_list.append(uri)
     84
     85                  if subsumption_dict and subsumption_list:
     86                          if subsumption_key:
     87                                  subsumption_dict[subsumption_key] = subsumption_list
     88                          else :
     89                                  subsumption_dict[uri] = subsumption_list
     90
     91                  AltXSDToPYTHON[uri] = conversion_function
     92                  use_Alt_lexical_conversions()
     93
     94 -        def post_process(self):
     95                  """Do some post-processing step. This method when all processing is done, but before handling possible
     96                  errors (ie, the method can add its own error messages). By default, this method is empty, subclasses
     97                  can add content to it by overriding it.
     98                  """
     99                  OWLRL_Semantics.post_process(self)
    100
    101 -        def rules(self, t, cycle_num):
    102                  """
    103                  @param t: a triple (in the form of a tuple)
    104                  @param cycle_num: which cycle are we in, starting with 1. This value is forwarded to all local rules; it is also used
    105                  locally to collect the bnodes in the graph.
    106                  """
    107                  OWLRL_Semantics.rules(self, t, cycle_num)
    108                  if self.rdfs:
    109                          RDFS_Semantics.rules(self, t, cycle_num)
    110
    111 -        def add_axioms(self):
    112                  if self.rdfs:
    113                          RDFS_Semantics.add_axioms(self)
    114                  OWLRL_Semantics.add_axioms(self)
    115
    116 -        def add_d_axioms(self):
    117                  if self.rdfs:
    118                          RDFS_Semantics.add_d_axioms(self)
    119                  OWLRL_Semantics.add_d_axioms(self)
    120
    121 -        def one_time_rules(self):
    122                  """Adds some extra axioms and calls for the d_axiom part of the OWL Semantics."""
    123                  for t in self.full_binding_triples:
    124                          self.store_triple(t)
    125
    126                  # Note that the RL one time rules include the management of datatype which is a true superset
    127                  # of the rules in RDFS. It is therefore unnecessary to add those even self.rdfs is True.
    128                  OWLRL_Semantics.one_time_rules(self)
    129