o
    i%/                    @   s  d Z ddlZddlZddlmZ ddlmZ ddlmZm	Z	m
Z
mZmZmZ ddlmZmZmZmZmZ ddlmZ eeZ	 g dZG d	d
 d
Zde	e
eedddZdd Zdd Zdd Zdd Z	dRddZ dd Z!G dd dZ"G dd dZ#ed Z$G d!d" d"e#Z%G d#d$ d$e%Z&d%d& Z'd'd( Z(G d)d* d*eZ)e*ed++ej,ej-ej.ej/ej0ej1ej2ej3ej4ej5ej6ej7gZ8d,d- Z9d.d/ Z:G d0d1 d1e;Z<G d2d3 d3e<Z=dSd4d5Z>G d6d7 d7e%Z?G d8d9 d9Z@G d:d; d;e@e?ZAej6ej5gZBG d<d= d=ZCG d>d? d?ZDG d@dA dAe@e?ZEdBdC ZFG dDdE dEe?ZGedFdG ZHedHdG ZIedIdG ZJedJdG ZKedKdG ZLedLdG ZMdMZNG dNdO dOe%ZOdSdPdQZPdS )Tuz  RDFLib Python binding for OWL Abstract Syntax

OWL Constructor     DL Syntax       Manchester OWL Syntax   Example
====================================================================================
intersectionOf      C ∩ D              C AND D             Human AND Male
unionOf             C ∪ D              C OR D              Man OR Woman
complementOf         ¬ C               NOT C               NOT Male
oneOf             {a} ∪ {b}...        {a b ...}            {England Italy Spain}
someValuesFrom      ∃ R C              R SOME C            hasColleague SOME Professor
allValuesFrom       ∀ R C              R ONLY C            hasColleague ONLY Professor
minCardinality      ≥ N R              R MIN 3             hasColleague MIN 3
maxCardinality      ≤ N R              R MAX 3             hasColleague MAX 3
cardinality         = N R              R EXACTLY 3         hasColleague EXACTLY 3
hasValue             ∃ R               {a} R VALUE a       hasColleague VALUE Matthew

see: http://www.w3.org/TR/owl-semantics/syntax.html
     http://owl-workshop.man.ac.uk/acceptedLong/submission_9.pdf

3.2.3 Axioms for complete classes without using owl:equivalentClass

Named class description of type 2 (with owl:oneOf) or type 4-6
(with owl:intersectionOf, owl:unionOf or owl:complementOf

Uses Manchester Syntax for __repr__

>>> exNs = Namespace("http://example.com/")
>>> g = Graph()
>>> g.bind("ex", exNs, override=False)

Now we have an empty graph, we can construct OWL classes in it
using the Python classes defined in this module

>>> a = Class(exNs.Opera, graph=g)

Now we can assert rdfs:subClassOf and owl:equivalentClass relationships
(in the underlying graph) with other classes using the 'subClassOf'
and 'equivalentClass' descriptors which can be set to a list
of objects for the corresponding predicates.

>>> a.subClassOf = [exNs.MusicalWork]

We can then access the rdfs:subClassOf relationships

>>> print(list(a.subClassOf))
[Class: ex:MusicalWork ]

This can also be used against already populated graphs:

>>> owlGraph = Graph().parse(str(OWL))
>>> list(Class(OWL.Class, graph=owlGraph).subClassOf)
[Class: rdfs:Class ]

Operators are also available. For instance we can add ex:Opera to the extension
of the ex:CreativeWork class via the '+=' operator

>>> a
Class: ex:Opera SubClassOf: ex:MusicalWork
>>> b = Class(exNs.CreativeWork, graph=g)
>>> b += a
>>> print(sorted(a.subClassOf, key=lambda c:c.identifier))
[Class: ex:CreativeWork , Class: ex:MusicalWork ]

And we can then remove it from the extension as well

>>> b -= a
>>> a
Class: ex:Opera SubClassOf: ex:MusicalWork

Boolean class constructions can also  be created with Python operators.
For example, The | operator can be used to construct a class consisting of a
owl:unionOf the operands:

>>> c =  a | b | Class(exNs.Work, graph=g)
>>> c
( ex:Opera OR ex:CreativeWork OR ex:Work )

Boolean class expressions can also be operated as lists (using python list
operators)

>>> del c[c.index(Class(exNs.Work, graph=g))]
>>> c
( ex:Opera OR ex:CreativeWork )

The '&' operator can be used to construct class intersection:

>>> woman = Class(exNs.Female, graph=g) & Class(exNs.Human, graph=g)
>>> woman.identifier = exNs.Woman
>>> woman
( ex:Female AND ex:Human )
>>> len(woman)
2

Enumerated classes can also be manipulated

>>> contList = [Class(exNs.Africa, graph=g), Class(exNs.NorthAmerica, graph=g)]
>>> EnumeratedClass(members=contList, graph=g)
{ ex:Africa ex:NorthAmerica }

owl:Restrictions can also be instantiated:

>>> Restriction(exNs.hasParent, graph=g, allValuesFrom=exNs.Human)
( ex:hasParent ONLY ex:Human )

Restrictions can also be created using Manchester OWL syntax in 'colloquial'
Python
>>> exNs.hasParent @ some @ Class(exNs.Physician, graph=g)
( ex:hasParent SOME ex:Physician )

>>> Property(exNs.hasParent, graph=g) @ max @ Literal(1)
( ex:hasParent MAX 1 )

>>> print(g.serialize(format='pretty-xml'))  # doctest: +SKIP

    N)
Collection)Graph)OWLRDFRDFSXSD	NamespaceNamespaceManager)BNode
IdentifierLiteralURIRefVariable)first)%ACE_NS
AllClassesAllDifferentAllPropertiesAnnotatableTermsBooleanClassCLASS_RELATIONSCallable	CastClassClassClassNamespaceFactoryCommonNSBindingsComponentTermsDeepClassClearEnumeratedClassGetIdentifiedClasses
IndividualInfixMalformedClassMalformedClassErrorOWLRDFListProxyOntologyPropertyPropertyAbstractSyntaxRestrictionclassOrIdentifierclassOrTermexactlygenerateQNamemanchesterSyntaxmaxminnsBindsonlypropertyOrIdentifiersomevaluec                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )r!   c                 C   
   || _ d S Nfunction)selfr8    r:   Q/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/rdflib/extras/infixowl.py__init__      
zInfix.__init__c                 C      t | |fddS )Nc                 S      | || S r6   r7   xr9   otherr:   r:   r;   <lambda>       z#Infix.__rlshift__.<locals>.<lambda>r!   r9   rB   r:   r:   r;   __rlshift__      zInfix.__rlshift__c                 C   
   |  |S r6   r7   rF   r:   r:   r;   
__rshift__   r=   zInfix.__rshift__c                 C   r>   )Nc                 S   r?   r6   r7   r@   r:   r:   r;   rC      rD   z#Infix.__rmatmul__.<locals>.<lambda>rE   rF   r:   r:   r;   __rmatmul__   rH   zInfix.__rmatmul__c                 C   rI   r6   r7   rF   r:   r:   r;   
__matmul__   r=   zInfix.__matmul__c                 C   s   |  ||S r6   r7   )r9   value1value2r:   r:   r;   __call__      zInfix.__call__N)	__name__
__module____qualname__r<   rG   rJ   rK   rL   rO   r:   r:   r:   r;   r!      s    r!   z$http://www.w3.org/2004/02/skos/core#z$http://www.w3.org/2000/10/swap/list#z http://purl.org/dc/elements/1.1/)skosrdfrdfsowllistdcc                 C   s"   |  t|\}}}d||gS N:)compute_qnamer)   join)graphuriprefix	localnamer:   r:   r;   r,      s   r,   c                 C   s(   t | tr| jS t | tttfsJ | S r6   )
isinstancer   
identifierr   r
   r   thingr:   r:   r;   r*      s   
r*   c                 C   s2   t | ttfr
| jS t | ttfsJ d|  | S )Nz8Expecting a Class, Property, URIRef, or BNode.. not a %s)rb   r&   r   rc   r   r
   rd   r:   r:   r;   r)      s   r)   c                 C   s"   t | tr| jS t | tsJ | S r6   )rb   r&   rc   r   rd   r:   r:   r;   r2      s   
r2   Fc                    s  | dusJ |r|rt | } fdd| D }nt t | } fddt | D }|tjkrg }g }|D ]}t|trC|| q6|| q6|r fdd}	t|dkrddd	t	|	| d
 }
nt
|d  }
|rt|
d d	 fdd|D  S |
S dd	dd |D  d
 S |tjkrdddd |D  d
 S |tjkrdddd |D  d S |tjksJ ntj j| tjdv rZt j| tjdd } |\}
}}d|
|g}t j|tjd}|rd| } j| tjdD ]}d|t
| f   S  j| tjdD ]}d|t
| f   S  j| tjdD ]}d|t
| f   S tjdtjdtjdi} | t|  dfD ]\}}}d||| |f   S t j| tjd}|rpdt
|d   S d d!d t!D }|d" d# }t"d$| i} j#|d%|d&D ]\}}t| tst
| |d'  S qz | \}
}}d|
|g}W n# t$y   t| t%r| &  Y S t| ts| j' Y S |  Y S w tt(|  d(j}|r|S |S ))z
    Core serialization
    thing is a Class and is processed as a subject
    store is an RDFLib Graph to be queried about thing
    Nc                       g | ]}t | qS r:   r-   .0childstorer:   r;   
<listcomp>  s    z$manchesterSyntax.<locals>.<listcomp>c                    rf   r:   rg   rh   rk   r:   r;   rm     s    
c                    s     | \}}}d||gS rZ   )r\   r]   )rA   r`   r_   ra   rk   r:   r;   castToQName  s   z%manchesterSyntax.<locals>.castToQName   z( z AND z )r   z THAT c                    s   g | ]	}t t| qS r:   )strr-   )ri   rA   rk   r:   r;   rm     s    c                 S      g | ]}t |qS r:   rp   ri   cr:   r:   r;   rm   %      z OR c                 S   rq   r:   rr   rs   r:   r:   r;   rm   '  ru   z{  c                 S   rq   r:   rr   rs   r:   r:   r;   rm   )  ru   z }subject	predicater[   z'%s'z( %s ONLY %s )z( %s VALUE %s )z( %s SOME %s )MAXMINEQUALSz( %s %s %s )z
( NOT %s )
c                 S   s   g | ]
}d |t | f qS )zPREFIX %s: <%s>)r0   )ri   kr:   r:   r;   rm   E      z6
SELECT ?p ?bool WHERE {?class a owl:Class; ?p ?bool .z?bool rdf:first ?foo }z?classsparql)	processorinitBindingsbooleanr^   ))iterr   r   intersectionOfrb   r   appendlenr]   mapr-   rp   unionOfoneOfcomplementOfr(   objectsr   typerX   
onPropertyr\   r   r   labelallValuesFromhasValuesomeValuesFrommaxCardinalityminCardinalitycardinalitytriples_choiceskeysr0   r   query	Exceptionr
   n3rc   r   )re   rl   r   transientListlivechildrenchildren	childlistnamedrj   rn   r`   propr_   ra   
propstringr   	onlyclassval	someclass
cardlookup_spocomplprologqstrinitbboolpropcolqnamer:   rk   r;   r-      s   




"r-   c                 c   s2    | j tjtjdD ]}t|trt|V  q
d S N)ry   object)subjectsr   r   r   r   rb   r   r^   rt   r:   r:   r;   r   ^  s   

r   c                   @      e Zd Zdd Zdd ZdS )TermDeletionHelperc                 C   r5   r6   )r   )r9   r   r:   r:   r;   r<   e  r=   zTermDeletionHelper.__init__c                        fdd}|S )Nc                    s   | j | j jd f d S r6   )r^   removerc   r   )instr9   r:   r;   _removeri     z-TermDeletionHelper.__call__.<locals>._removerr:   )r9   fr   r:   r   r;   rO   h  s   zTermDeletionHelper.__call__NrQ   rR   rS   r<   rO   r:   r:   r:   r;   r   d      r   c                   @   s   e Zd ZdZe Zdd ZdddZdd Zd	d
 Z	dd Z
dd Zdd Zdd Zeejdd ZeeeeZdd Zdd ZeeeZdd Zdd Zeejdd ZeeeeZdS ) r    zF
    A typed individual, the base class of the InfixOWL classes.

    c                 C   s(   | j | jd d fD ]}|| q
d S r6   )factoryGraphtriplesrc   addr9   r^   factr:   r:   r;   	serializew  s   zIndividual.serializeNc                 C   s   |d ur|pt  | _|d u r| j| _n|| _d | _t| jt s?z| j| j\}}}d||g| _W d S  t	y>   Y d S w d S rZ   )
r
   _Individual__identifierr   r^   r   rb   rc   r\   r]   r   )r9   rc   r^   r`   r_   ra   r:   r:   r;   r<   {  s   
zIndividual.__init__c                 C   s   | j dd| jf dS )za
        Remove references to this individual as an object in the
        backing store.
        Nr^   r   rc   r   r:   r:   r;   clearInDegree  s   zIndividual.clearInDegreec                 C   s   | j | jddf dS )a&  
        Remove all statements to this individual as a subject in the
        backing store. Note that this only removes the statements
        themselves, not the blank node closure so there is a chance
        that this will cause orphaned blank nodes to remain in the
        graph.
        Nr   r   r:   r:   r;   clearOutDegree  s   zIndividual.clearOutDegreec                 C   s   |    |   dS )z`
        Delete the individual from the graph, clearing the in and
        out degrees.
        N)r   r   r   r:   r:   r;   delete  s   zIndividual.deletec                 C   sB   | j dd| jfD ]\}}}| j ||t|f q
|   dS )a  
        Replace the individual in the graph with the given other,
        causing all triples that refer to it to be changed and then
        delete the individual.

        >>> g = Graph()
        >>> b = Individual(OWL.Restriction, g)
        >>> b.type = RDFS.Resource
        >>> len(list(b.type))
        1
        >>> del b.type
        >>> len(list(b.type))
        0
        N)r^   r   rc   r   r)   r   )r9   rB   sr   _or:   r:   r;   replace  s   zIndividual.replacec                 c   &    | j j| jtjdD ]}|V  qd S Nrw   )r^   r   rc   r   r   r9   _tr:   r:   r;   	_get_type     zIndividual._get_typec                 C   sn   |sd S t |ttfr| j| jtjt|f d S |D ]}t |ttfs'J | j| jtjt|f qd S r6   )	rb   r    r   r^   r   rc   r   r   r)   )r9   kindrt   r:   r:   r;   	_set_type  s   zIndividual._set_typec                 C      dS )z
        >>> g = Graph()
        >>> b = Individual(OWL.Restriction, g)
        >>> b.type = RDFS.Resource
        >>> len(list(b.type))
        1
        >>> del b.type
        >>> len(list(b.type))
        0
        Nr:   r   r:   r:   r;   _delete_type     zIndividual._delete_typec                 C      | j S r6   )r   r   r:   r:   r;   _get_identifier     zIndividual._get_identifierc           
         s   sJ  j krhdd jj d d fD }dd jd d j fD }|D ]\}}jj ||f q)|D ]\}}j||j f q: _ j fdd|D  j fdd|D  t tszj \}}}	d||	g_	W d S  t
y   Y d S w d S )Nc                 S   s   g | ]	\}}}||fqS r:   r:   ri   r   r   r   r:   r:   r;   rm         z.Individual._set_identifier.<locals>.<listcomp>c                 S   s   g | ]	\}}}||fqS r:   r:   r   r:   r:   r;   rm     r   c                    s   g | ]\}} ||j fqS r:   r   )ri   p1o1ir9   r:   r;   rm         c                    s   g | ]\}}|| j fqS r:   r   )ri   s1r   r   r:   r;   rm     r   r[   )r   r^   r   r   addNrb   r
   r\   r]   r   r   )
r9   r   oldstatements_outoldstatements_inr   r   r   r`   r_   ra   r:   r   r;   _set_identifier  s.   

zIndividual._set_identifierc                 c   r   r   )r^   r   rc   r   sameAsr   r:   r:   r;   _get_sameAs  r   zIndividual._get_sameAsc                 C   sf   t |ttfr| j| jtjt|f d S |D ]}t |ttfs#J | j| jtjt|f qd S r6   )	rb   r    r   r^   r   rc   r   r   r)   )r9   termrt   r:   r:   r;   _set_sameAs  s   zIndividual._set_sameAsc                 C      d S r6   r:   r   r:   r:   r;   _delete_sameAs     zIndividual._delete_sameAsNN)rQ   rR   rS   __doc__r   r   r   r<   r   r   r   r   r   r   r   r   r   r   propertyr   r   rc   r   r   r   r   r   r:   r:   r:   r;   r    o  s,    






r    z'http://attempto.ifi.uzh.ch/ace_lexicon#c                       s   e Zd ZdZ			d fdd	Zdd Zdd	 Zd
d Zdd Ze	e
jdd ZeeeeZdd Zdd Ze	e
jdd ZeeeeZdd Zdd Ze	e
jdd ZeeeeZ  ZS )r   a
  
    Terms in an OWL ontology with rdfs:label and rdfs:comment


    ## Interface with ATTEMPTO (http://attempto.ifi.uzh.ch/site)

    ### Verbalisation of OWL entity IRIS

    #### How are OWL entity IRIs verbalized?

    The OWL verbalizer maps OWL entity IRIs to ACE content words such
    that

    - OWL individuals map to ACE proper names (PN)
    - OWL classes map to ACE common nouns (CN)
    - OWL properties map to ACE transitive verbs (TV)

    There are 6 morphological categories that determine the surface form
    of an IRI:

    - singular form of a proper name (e.g. John)
    - singular form of a common noun (e.g. man)
    - plural form of a common noun (e.g. men)
    - singular form of a transitive verb (e.g. mans)
    - plural form of a transitive verb (e.g. man)
    - past participle form a transitive verb (e.g. manned)

    The user has full control over the eventual surface forms of the IRIs
    but has to choose them in terms of the above categories.
    Furthermore,

    - the surface forms must be legal ACE content words (e.g. they
      should not contain punctuation symbols);
    - the mapping of IRIs to surface forms must be bidirectional
      within the same word class, in order to be able to (if needed)
      parse the verbalization back into OWL in a semantics preserving
      way.

    ### Using the lexicon

    It is possible to specify the mapping of IRIs to surface forms using
    the following annotation properties:

    .. code-block:: none

        http://attempto.ifi.uzh.ch/ace_lexicon#PN_sg
        http://attempto.ifi.uzh.ch/ace_lexicon#CN_sg
        http://attempto.ifi.uzh.ch/ace_lexicon#CN_pl
        http://attempto.ifi.uzh.ch/ace_lexicon#TV_sg
        http://attempto.ifi.uzh.ch/ace_lexicon#TV_pl
        http://attempto.ifi.uzh.ch/ace_lexicon#TV_vbg

    For example, the following axioms state that if the IRI "#man" is used
    as a plural common noun, then the wordform men must be used by the
    verbalizer. If, however, it is used as a singular transitive verb,
    then mans must be used.

    .. code-block:: none

        <AnnotationAssertion>
            <AnnotationProperty IRI="http://attempto.ifi.uzh.ch/ace_lexicon#CN_pl"/>
            <IRI>#man</IRI>
            <Literal datatypeIRI="&xsd;string">men</Literal>
        </AnnotationAssertion>

        <AnnotationAssertion>
            <AnnotationProperty IRI="http://attempto.ifi.uzh.ch/ace_lexicon#TV_sg"/>
            <IRI>#man</IRI>
            <Literal datatypeIRI="&xsd;string">mans</Literal>
        </AnnotationAssertion>

    NFc                    sL   t t| || |r"|   | j| |fg| j_|r$|g| _d S d S d S r6   )	superr   r<   setupACEAnnotationsrc   handleAnnotation	PN_sgpropextentr   )r9   rc   r^   nameAnnotationnameIsLabel	__class__r:   r;   r<   S  s   zAnnotatableTerms.__init__c                 C   s   t |tr|S t|S r6   )rb   r   )r9   r   r:   r:   r;   r   c  s   z!AnnotatableTerms.handleAnnotationc                 C   s   | j jdtdd ttjtj| j d| _ttjtj| j d| _	ttj
tj| j d| _ttjtj| j d| _ttjtj| j d| _ttjtj| j d| _d S )NaceFoverride)baseTyper^   )r^   bindr   r&   PN_sgr   AnnotationPropertyr   CN_sg	CN_sgpropCN_pl	CN_plpropTV_sg	tv_sgpropTV_pl	tv_plpropTV_vbg
tv_vbgpropr   r:   r:   r;   r   f  s&   z$AnnotatableTerms.setupACEAnnotationsc                 c   r   r   )r^   r   rc   r   comment)r9   r  r:   r:   r;   _get_comment     
zAnnotatableTerms._get_commentc                 C   P   |sd S t |tr| j| jtj|f d S |D ]}| j| jtj|f qd S r6   )rb   r   r^   r   rc   r   r  )r9   r  rt   r:   r:   r;   _set_comment     
zAnnotatableTerms._set_commentc                 C   r   r6   r:   r   r:   r:   r;   _del_comment  r   zAnnotatableTerms._del_commentc                 c   r   r   )r^   r   rc   r   seeAlso)r9   seealsor:   r:   r;   _get_seealso  r  zAnnotatableTerms._get_seealsoc                 C   s,   |sd S |D ]}| j | jtj|f qd S r6   )r^   r   rc   r   r  )r9   seealsosr   r:   r:   r;   _set_seealso  s
   zAnnotatableTerms._set_seealsoc                 C   r   r6   r:   r   r:   r:   r;   _del_seealso  r   zAnnotatableTerms._del_seealsoc                 c   r   r   )r^   r   rc   r   r   )r9   r   r:   r:   r;   
_get_label  r   zAnnotatableTerms._get_labelc                 C   r  r6   )rb   r   r^   r   rc   r   r   )r9   r   l_r:   r:   r;   
_set_label  r  zAnnotatableTerms._set_labelc                 C   r   )z
        >>> g = Graph()
        >>> b = Individual(OWL.Restriction,g)
        >>> b.label = Literal('boo')
        >>> len(list(b.label))
        1
        >>> del b.label
        >>> len(list(b.label))
        0
        Nr:   r   r:   r:   r;   _delete_label  r   zAnnotatableTerms._delete_label)NNF)rQ   rR   rS   r   r<   r   r   r  r  r   r   r  r  r   r  r  r  r  r  r  r   r  __classcell__r:   r:   r   r;   r   	  s.    L!	

	
r   c                       sZ   e Zd ZdZd fdd	Zdd Zdd Zd	d
 Zee	d dd Z
eeee
Z  ZS )r%   zThe owl ontology metadataNc                    sl   t t| || |d u rg n|| _|d u rg n|| _| jtjtjf| j	vr4| j	
| jtjtjf d S d S r6   )r   r%   r<   importsr  rc   r   r   r   r^   r   )r9   rc   r  r  r^   r   r:   r;   r<     s   zOntology.__init__c                 C   s   | j | jtj|f d S r6   )r^   setrc   r   versionInfo)r9   versionr:   r:   r;   
setVersion  r   zOntology.setVersionc                 c   s(    | j j| jtd dD ]}|V  qd S )Nr  rw   )r^   r   rc   r   )r9   rW   r:   r:   r;   _get_imports  s   

zOntology._get_importsc                 C   s.   |sd S |D ]}| j | jtd |f qd S )Nr  )r^   r   rc   r   )r9   rB   r   r:   r:   r;   _set_imports  s
   zOntology._set_importsr  c                 C   r   r6   r:   r   r:   r:   r;   _del_imports  r   zOntology._del_imports)NNNN)rQ   rR   rS   r   r<   r#  r$  r%  r   r   r&  r   r  r  r:   r:   r   r;   r%     s    

r%   c                 c   s,    t | jtjtjdD ]}t|V  qd S r   )r   r   r   r   r   r   r   r:   r:   r;   r     s   r   c                 c   s    t  }| d tjtjtjtjtjtj	tj
tjgfD ])\}}}|tjtjtjtj
fv r0tj
}ntj	}||vrD|| t|| |dV  qd S )Nr^   r   )r   r   r   r   r   SymmetricPropertyFunctionalPropertyInverseFunctionalPropertyTransitivePropertyDatatypePropertyObjectPropertyr  r   r&   )r^   	prevpropsr   _pr   bTyper:   r:   r;   r     s8   
r   c                   @   s&   e Zd Zdd ZdddZdd ZdS )	r   c                 C   s   t t| | S r6   )r   r   r9   namer:   r:   r;   r        zClassNamespaceFactory.termNc                 C   rI   r6   )r   )r9   keydefaultr:   r:   r;   __getitem__  r=   z!ClassNamespaceFactory.__getitem__c                 C   s   | drt| |S )N__)
startswithAttributeErrorr   r1  r:   r:   r;   __getattr__  s   

z!ClassNamespaceFactory.__getattr__r6   )rQ   rR   rS   r   r6  r:  r:   r:   r:   r;   r     s    
r   z0http://www.w3.org/2002/07/owl#resourcePropertiesc                 c   sp   t j| jv rHz6t| tj} | j| jt jt j	gdfD ]\}}}t
|dd}t|tr7t|D ]}|V  q0q|V  qW dS  tyG   Y dS w t| tj} t| trt| D ]}t
|dd}t|jtrnt|D ]}|V  qgqU|V  qUdS | jD ]}t|jtrt|D ]}|V  qqw|V  qw| jt| tdfD ]\}}}t|trtt|tjD ]}|V  qq|V  qdS )z
    Takes a Class instance and returns a generator over the classes that
    are involved in its definition, ignoring unnamed classes
    NTskipOWLClassMembership)r   r(   r   r   r    r   r   rc   r   r   r   rb   r
   r   r   r   
subClassOfr)   r   )clsr   r/  inner_class_idinner_class_c_clsr   r:   r:   r;   r   2  sT   
	


r   c                 C   s   dd }t | tj} | jD ]}|| q| j| jtjdf | jD ]}|| q"| j| jt	jdf | j
}|rH| j| jt	j
df || t| trg| D ]}|| qO|   | j| j| jdf dS dS )a  
    Recursively clear the given class, continuing
    where any related class is an anonymous class

    >>> EX = Namespace("http://example.com/")
    >>> g = Graph()
    >>> g.bind("ex", EX, override=False)
    >>> Individual.factoryGraph = g
    >>> classB = Class(EX.B)
    >>> classC = Class(EX.C)
    >>> classD = Class(EX.D)
    >>> classE = Class(EX.E)
    >>> classF = Class(EX.F)
    >>> anonClass = EX.someProp @ some @ classD
    >>> classF += anonClass
    >>> list(anonClass.subClassOf)
    [Class: ex:F ]
    >>> classA = classE | classF | anonClass
    >>> classB += classA
    >>> classA.equivalentClass = [Class()]
    >>> classB.subClassOf = [EX.someProp @ some @ classC]
    >>> classA
    ( ex:E OR ex:F OR ( ex:someProp SOME ex:D ) )
    >>> DeepClassClear(classA)
    >>> classA
    (  )
    >>> list(anonClass.subClassOf)
    []
    >>> classB
    Class: ex:B SubClassOf: ( ex:someProp SOME ex:C )

    >>> otherClass = classD | anonClass
    >>> otherClass
    ( ex:D OR ( ex:someProp SOME ex:D ) )
    >>> DeepClassClear(otherClass)
    >>> otherClass
    (  )
    >>> otherClass.delete()
    >>> list(g.triples((otherClass.identifier, None, None)))
    []
    c                 S   s   t t| trt|  d S d S r6   )rb   r)   r
   r   )_classr:   r:   r;   deepClearIfBNode  s   z(DeepClassClear.<locals>.deepClearIfBNodeN)r   r    r   r=  r^   r   rc   r   equivalentClassr   r   rb   r   clear	_operator)class_to_prunerD  rt   inverse_classr:   r:   r;   r   `  s(   +





r   c                   @   s   e Zd ZdZdS )r"   zc
    .. deprecated:: TODO-NEXT-VERSION
       This class will be removed in version ``7.0.0``.
    N)rQ   rR   rS   r   r:   r:   r:   r;   r"     s    r"   c                   @   r   )r#   c                 C   r5   r6   msg)r9   rK  r:   r:   r;   r<     r=   zMalformedClassError.__init__c                 C   r   r6   rJ  r   r:   r:   r;   __repr__  r   zMalformedClassError.__repr__N)rQ   rR   rS   r<   rL  r:   r:   r:   r;   r#     r   r#   c              	   C   sZ  |d u r| j p|}|jt| tjdD ]}|tjkrmt| |d}|t| d d fD ]'\}}}|tjkrR|tjkr?||d< q+|tj	vrEq+||t
|t
td < q+tdd tj	D |sdtdtdi |  S |t| tjtjtjgd fD ]"\}}}|tjkrtt| |d    S tt| ||d	    S tt| |d
d  S d S )Nrw   )rc   r^   r   c                 S   s"   g | ]}t |t td  qS )rM  )rp   splitr   )ri   r   r:   r:   r;   rm     s   " zCastClass.<locals>.<listcomp>zMalformed owl:Restrictionr   )operatorr^   Tr^   r<  r:   )r   r   r)   r   r   r   r(   r   r   restrictionKindsrp   rN  r   intersectionr#   r   r   r   r   r   r   r   )rt   r^   r   kwargsr   r   r   r   r:   r:   r;   r     s<   





r   c                       s  e Zd ZdZdd Zdd Zdd Z													dI fd
d	ZdJddZdd Z	e
ejdd Zeee	eZejfddZeedd Zdd Zdd ZeeeZdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Ze
ej d,d- Z!eeee!Z d.d/ Z"d0d1 Z#e
e$j%d2d3 Z&ee"e#e&Z%d4d5 Z'd6d7 Z(e
e$j)d8d9 Z*ee'e(e*Z)d:d; Z+d<d= Z,e
e$j-d>d? Z.ee+e,e.Z-d@dA Z/ee/Z0dBdC Z1dDdE Z2dKdGdHZ3  Z4S )Lr   ul  
    'General form' for classes:

    The Manchester Syntax (supported in Protege) is used as the basis for the
    form of this class

    See: http://owl-workshop.man.ac.uk/acceptedLong/submission_9.pdf:

    [Annotation]
    ‘Class:’ classID {Annotation
    ( (‘SubClassOf:’ ClassExpression)
    | (‘EquivalentTo’ ClassExpression)
    | (’DisjointWith’ ClassExpression)) }

    Appropriate excerpts from OWL Reference:

    ".. Subclass axioms provide us with partial definitions: they represent
     necessary but not sufficient conditions for establishing class
     membership of an individual."

    ".. A class axiom may contain (multiple) owl:equivalentClass statements"

    "..A class axiom may also contain (multiple) owl:disjointWith statements.."

    "..An owl:complementOf property links a class to precisely one class
      description."

    c                 C   s|   | j D ]}t|| j| q| jD ]}t|| j| q| jD ]}t|| j| q!| jr<t| j| j| d S d S r6   )r=  r   r^   r   rE  disjointWithr   )r9   r^   clr:   r:   r;   
_serialize  s   


zClass._serializec                 C   s2   | j | jd d fD ]}|| q
| | d S r6   )r^   r   rc   r   rV  r   r:   r:   r;   r     s   zClass.serializec                 C   sX   t |tr
|\}}n|}|}|r| j| |fg| j_|r*| j| |fg| j_d S d S r6   )rb   tuplerc   r   r  r   r  )r9   noun_annotations	cn_sgprop	cn_plpropr:   r:   r;   setupNounAnnotations  s   

zClass.setupNounAnnotationsNFc                    s   t t| |||
| |	r| |	 |s6| jtjtjf| jvr6| jtjtj	f| jvr6| j
| jtjtjf |d u r<g n|| _|d u rEg n|| _|d u rNg n|| _|rV|| _|d u r_g | _d S || _d S r6   )r   r   r<   r[  rc   r   r   r   r^   r(   r   r=  rE  rT  r   r  )r9   rc   r=  rE  rT  r   r^   r<  r  nounAnnotationsr   r   r   r:   r;   r<     s   
zClass.__init__c                 c   s2    |d u r| j p	|jtj| jdD ]}|V  qd S r   )r^   r   r   r   rc   )r9   r^   memberr:   r:   r;   _get_extent3  s   
zClass._get_extentc                 C   s0   |sd S |D ]}| j t|tj| jf qd S r6   )r^   r   r)   r   r   rc   )r9   rB   mr:   r:   r;   _set_extent9  
   zClass._set_extentc                 C   r   r6   r:   r   r:   r:   r;   	_del_type?  r   zClass._del_typec                 c   s$    | j j| j|dD ]}|V  q
d S r   )r^   r   rc   )r9   r   
annotationr:   r:   r;   _get_annotationE  s   zClass._get_annotationc                 C   s   | S r6   r:   )rA   r:   r:   r;   rC   I  s    zClass.<lambda>c                 C   s   t dtj| jfS )NCLASS)r   r   r   rc   r   r:   r:   r;   _get_extentqueryK  rH   zClass._get_extentqueryc                 C   r   r6   r:   rF   r:   r:   r;   _set_extentqueryN     zClass._set_extentqueryc                 C   
   t | jS )z}
        >>> b = Class(OWL.Restriction)
        >>> c = Class(OWL.Restriction)
        >>> len(set([b,c]))
        1
        )hashrc   r   r:   r:   r;   __hash__S  s   
zClass.__hash__c                 C   s"   t |tsJ t|| j|jkS r6   )rb   r   reprrc   rF   r:   r:   r;   __eq__\  s   zClass.__eq__c                 C   s   t |tsJ | g|_| S r6   )rb   r   r=  rF   r:   r:   r;   __iadd__`  s   zClass.__iadd__c                 C   s,   t |tsJ | jt|tj| jf | S r6   )rb   r   r^   r   r)   r   r=  rc   rF   r:   r:   r;   __isub__e  s   zClass.__isub__c                 C   s
   t | dS )z@
        Shorthand for Manchester syntax's not operator
        )r   )r   r   r:   r:   r;   
__invert__j  s   
zClass.__invert__c                 C      t tj| |g| jdS )z
        Construct an anonymous class description consisting of the union of
        this class and 'other' and return it
        rO  membersr^   )r   r   r   r^   rF   r:   r:   r;   __or__p  s   zClass.__or__c                 C   rq  )a  
        Construct an anonymous class description consisting of the
        intersection of this class and 'other' and return it

        Chaining 3 intersections

        >>> exNs = Namespace("http://example.com/")
        >>> g = Graph()
        >>> g.bind("ex", exNs, override=False)
        >>> female = Class(exNs.Female, graph=g)
        >>> human = Class(exNs.Human, graph=g)
        >>> youngPerson = Class(exNs.YoungPerson, graph=g)
        >>> youngWoman = female & human & youngPerson
        >>> youngWoman  # doctest: +SKIP
        ex:YoungPerson THAT ( ex:Female AND ex:Human )
        >>> isinstance(youngWoman, BooleanClass)
        True
        >>> isinstance(youngWoman.identifier, BNode)
        True
        rr  )r   r   r   r^   rF   r:   r:   r;   __and__y  s   zClass.__and__c                 c   s2    | j j| jtjdD ]}t|| j ddV  qd S )Nrw   TrP  )r^   r   rc   r   r=  r   r9   ancr:   r:   r;   _get_subclassof     
zClass._get_subclassofc                 C   0   |sd S |D ]}| j | jtjt|f qd S r6   )r^   r   rc   r   r=  r)   r9   rB   scr:   r:   r;   _set_subclassof  ra  zClass._set_subclassofc                 C   r   r6   r:   r   r:   r:   r;   _del_subclassof  r   zClass._del_subclassofc                 c   0    | j j| jtjdD ]
}t|| j dV  qd S Nrw   r   )r^   r   rc   r   rE  r   )r9   ecr:   r:   r;   _get_equivalentclass     
zClass._get_equivalentclassc                 C   rz  r6   )r^   r   rc   r   rE  r)   r{  r:   r:   r;   _set_equivalentclass     zClass._set_equivalentclassc                 C   r   r6   r:   r   r:   r:   r;   _del_equivalentclass  r   zClass._del_equivalentclassc                 c   r  r  )r^   r   rc   r   rT  r   )r9   rY   r:   r:   r;   _get_disjointwith  r  zClass._get_disjointwithc                 C   rz  r6   )r^   r   rc   r   rT  r)   )r9   rB   rt   r:   r:   r;   _set_disjointwith  ra  zClass._set_disjointwithc                 C   r   r6   r:   r   r:   r:   r;   _del_disjointwith  r   zClass._del_disjointwithc                 C   sJ   t | jj| jtjd}|sd S t|dkrt|d | jdS tt|)Nrw   ro   r   r   )	rX   r^   r   rc   r   r   r   r   r   )r9   compr:   r:   r;   _get_complementof  s   zClass._get_complementofc                 C   &   |sd S | j | jtjt|f d S r6   )r^   r   rc   r   r   r)   rF   r:   r:   r;   _set_complementof     zClass._set_complementofc                 C   r   r6   r:   r   r:   r:   r;   _del_complementof  r   zClass._del_complementofc                 c   s    t | j| jD ]}|V  q	t| jtj| j}|rFt	| j
tj|}|r-|d }n|}| jtj|D ]}t|trEt|ddV  q7| j| jtjD ]}t|g| jdD ]}t|trgt|ddV  qYqOdS )a  
        computed attributes that returns a generator over taxonomic 'parents'
        by disjunction, conjunction, and subsumption

        >>> from rdflib.util import first
        >>> exNs = Namespace('http://example.com/')
        >>> g = Graph()
        >>> g.bind("ex", exNs, override=False)
        >>> Individual.factoryGraph = g
        >>> brother = Class(exNs.Brother)
        >>> sister = Class(exNs.Sister)
        >>> sibling = brother | sister
        >>> sibling.identifier = exNs.Sibling
        >>> sibling
        ( ex:Brother OR ex:Sister )
        >>> first(brother.parents)
        Class: ex:Sibling EquivalentTo: ( ex:Brother OR ex:Sister )
        >>> parent = Class(exNs.Parent)
        >>> male = Class(exNs.Male)
        >>> father = parent & male
        >>> father.identifier = exNs.Father
        >>> list(father.parents)
        [Class: ex:Parent , Class: ex:Male ]

        rM  Tr;  r   N)	itertoolschainr=  rE  r   r   r   r   rc   rX   transitive_subjectsrestr   r   rb   r   r   r   r   r$   )r9   parentlinksiblingslistcollectionheaddisjointclassrdf_listr]  r:   r:   r;   _get_parents  s,   


zClass._get_parentsc                 C   s~   | j tjtjf| jv rdS t| j}| j| j tj	tj
gd fD ]\}}}|t|| j|d q |D ]} dS | jr=dS dS )NFr   T)rc   r   r   r   r(   r^   rX   rE  r   r   r   r   r-   r   )r9   r  
_boolclassr   r  _er:   r:   r;   isPrimitive  s   
zClass.isPrimitivec                 c   s&    | j jtj| jdD ]}|V  qd S r   )r^   r   r   r=  rc   )r9   r   r:   r:   r;   subSumpteeIds'  r   zClass.subSumpteeIdsTc                    s*  g }t  j}t  j} j jtjtjgdfD ]\}}}|	t
| j|d qt  j}	 j}
|
r:|		|
 d}t  j jtj}|rQd|d  d pRd}|r|rZd}nd} fd	d
|D }|rkd| }|	d|dd
 |D   |rd|d  |d< |r fdd
|D }|rd| }|	dd|  |rd|d  |d< |	r|	dd fdd
|	D   |rd|d  |d< t  j jtj}|r|r|rd| |rd|d  pd d| pd|}n|r|rd|d  pdpdd| }t jtrdpd j | S )I
        Returns the Manchester Syntax equivalent for this class
        Nr    (r   )z
                z, c                    sB   g | ]}t |trt  jtrtt| jptt| jqS r:   )	rb   r   rc   r
   rl  r   r^   r-   r)   ri   r   r   r:   r;   rm   G  s    	

z"Class.__repr__.<locals>.<listcomp>zPrimitive Type %szSubClassOf: %sc                 S   rq   r:   rr   )ri   nr:   r:   r;   rm   U  ru   z
    rM  c                    s*   g | ]}t |tr|ptt| jqS r:   )rb   rp   r-   r)   r^   r  r   r:   r;   rm   Z  s    
zA Defined Class %szEquivalentTo: %szDisjointWith %s
z
                 c                    s   g | ]
}t t| jqS r:   )r-   r)   r^   r  r   r:   r;   rm   i  r   z
    ## %s ##z
    %sz . zSome Class z
Class: %s )rX   r=  rE  r^   r   rc   r   r   r   r   r-   rT  r   r   r   r   r]   r  rb   r
   r   )r9   fullnormalizationexprsr|  r  r  r   r  rY   rt   	klasskindr   scjoinnec_statementsnec_suff_statementsdescr
klassdescrr:   r   r;   rL  0  s   




	
	zClass.__repr__)NNNNNNFNNNFr6   )FT)5rQ   rR   rS   r   rV  r   r[  r<   r^  r`  r   r   r   rb  r   r   r   r   rd  rc  rf  rg  extentQueryrk  rm  rn  ro  rp  rt  ru  rx  r}  r=  r~  r  r  r   rE  r  r  r  rT  r  r  r  r   r  r  parentsr  r  rL  r  r:   r:   r   r;   r     s    

 

		



.	r   c                   @   sn   e Zd ZdddZdd Zdd Zdd	 Zd
d Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd ZdS )r$   Nc                 C   s   |r|| _ |d u rg n|}|r,t| j |d | _|D ]}|| jvr)| jt| qd S t| j t dd |D | _| j | j| j| jj	f d S )Nr   c                 S   rq   r:   )r)   )ri   r_  r:   r:   r;   rm     ru   z,OWLRDFListProxy.__init__.<locals>.<listcomp>)
r^   r   _rdfListr   r)   r
   r   rc   rG  r_   )r9   r  rs  r^   r]  r:   r:   r;   r<     s   
zOWLRDFListProxy.__init__c                 C   s|   t |tsJ t|tt| t |tr8t| }|t|kr"dS t|D ]}| | || kr3 dS  dS dS | j|jkS )zo
        Equivalence of boolean class constructors is determined by
        equivalence of its members
        FTN)rb   r   rl  r   r   r   rangerc   )r9   rB   lengthidxr:   r:   r;   rm    s   "
zOWLRDFListProxy.__eq__c                 C   ri  r6   )r   r  r   r:   r:   r;   __len__  r=   zOWLRDFListProxy.__len__c                 C   s   | j t|S r6   )r  indexr)   r9   itemr:   r:   r;   r    r3  zOWLRDFListProxy.indexc                 C   s
   | j | S r6   r  r9   r4  r:   r:   r;   r6    r=   zOWLRDFListProxy.__getitem__c                 C   s   t || j|< d S r6   )r)   r  )r9   r4  r4   r:   r:   r;   __setitem__  rH   zOWLRDFListProxy.__setitem__c                 C   s   | j |= d S r6   r  r  r:   r:   r;   __delitem__  rP   zOWLRDFListProxy.__delitem__c                 C   s   | j   d S r6   )r  rF  r   r:   r:   r;   rF    s   zOWLRDFListProxy.clearc                 c   s    | j D ]}|V  qd S r6   r  r  r:   r:   r;   __iter__  s   
zOWLRDFListProxy.__iter__c                 C   s"   | j D ]}|t|kr dS qdS )Nro   r   )r  r)   )r9   r  r   r:   r:   r;   __contains__  s
   
zOWLRDFListProxy.__contains__c                 C   s   | j | d S r6   )r  r   r  r:   r:   r;   r     r3  zOWLRDFListProxy.appendc                 C   s   | j t| | S r6   )r  r   r)   rF   r:   r:   r;   rn    s   zOWLRDFListProxy.__iadd__r   )rQ   rR   rS   r<   rm  r  r  r6  r  r  rF  r  r  r   rn  r:   r:   r:   r;   r$     s    
r$   c                   @   s8   e Zd ZdZejZdd ZdddZdd Z	d	d
 Z
dS )r   a  
    Class for owl:oneOf forms:

    OWL Abstract Syntax is used

    axiom ::= 'EnumeratedClass('
        classID ['Deprecated'] { annotation } { individualID } ')'

    >>> exNs = Namespace("http://example.com/")
    >>> g = Graph()
    >>> g.bind("ex", exNs, override=False)
    >>> Individual.factoryGraph = g
    >>> ogbujiBros = EnumeratedClass(exNs.ogbujicBros,
    ...                              members=[exNs.chime,
    ...                                       exNs.uche,
    ...                                       exNs.ejike])
    >>> ogbujiBros  # doctest: +SKIP
    { ex:chime ex:uche ex:ejike }
    >>> col = Collection(g, first(
    ...    g.objects(predicate=OWL.oneOf, subject=ogbujiBros.identifier)))
    >>> sorted([g.qname(item) for item in col])
    ['ex:chime', 'ex:ejike', 'ex:uche']
    >>> print(g.serialize(format='n3'))  # doctest: +SKIP
    @prefix ex: <http://example.com/> .
    @prefix owl: <http://www.w3.org/2002/07/owl#> .
    @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
    <BLANKLINE>
    ex:ogbujicBros a owl:Class;
        owl:oneOf ( ex:chime ex:uche ex:ejike ) .
    <BLANKLINE>
    <BLANKLINE>
    c                 C   r   NFr:   r   r:   r:   r;   r    rh  zEnumeratedClass.isPrimitiveNc                 C   sJ   t j| ||d |d u rg n|}t| jjtj| jd}t| || d S )Nr   ry   rx   )	r   r<   rX   r^   r   r   r   rc   r$   )r9   rc   rs  r^   rdfListr:   r:   r;   r<     s   zEnumeratedClass.__init__c                 C   s   t | jj| j| jdS r  r   )r-   r  r_   r^   rG  r   r:   r:   r;   rL    s   zEnumeratedClass.__repr__c                 C      t |t }| jD ]}|| t|| j| q	|| j| j	|j
f | j| jd d fD ]\}}}|| j	krA||||f q/| | d S r6   r   r
   r  r   r   r^   r   r   rc   rG  r_   r   rV  r9   r^   
clonedlistrU  r   r   r   r:   r:   r;   r      s   


zEnumeratedClass.serialize)NNN)rQ   rR   rS   r   r   r   rG  r  r<   rL  r   r:   r:   r:   r;   r     s    !
r   c                   @   s    e Zd ZdZdd Zdd ZdS )BooleanClassExtentHelpera`  
    >>> testGraph = Graph()
    >>> Individual.factoryGraph = testGraph
    >>> EX = Namespace("http://example.com/")
    >>> testGraph.bind("ex", EX, override=False)
    >>> fire = Class(EX.Fire)
    >>> water = Class(EX.Water)
    >>> testClass = BooleanClass(members=[fire, water])
    >>> testClass2 = BooleanClass(
    ...     operator=OWL.unionOf, members=[fire, water])
    >>> for c in BooleanClass.getIntersections():
    ...     print(c)  # doctest: +SKIP
    ( ex:Fire AND ex:Water )
    >>> for c in BooleanClass.getUnions():
    ...     print(c) #doctest: +SKIP
    ( ex:Fire OR ex:Water )
    c                 C   r5   r6   rO  )r9   rO  r:   r:   r;   r<   #  r=   z!BooleanClassExtentHelper.__init__c                    r   )Nc                  3   s*    t j jD ]
} t|  jdV  qd S )Nr  )r    r   r   rO  r   )rt   r   r:   r;   
_getExtent'  s   z5BooleanClassExtentHelper.__call__.<locals>._getExtentr:   )r9   r   r  r:   r   r;   rO   &  s   z!BooleanClassExtentHelper.__call__N)rQ   rR   rS   r   r<   rO   r:   r:   r:   r;   r    s    r  c                   @   r   )r   c                 C   r5   r6   _callfn)r9   anycallabler:   r:   r;   r<   /  r=   zCallable.__init__c                 O   s   | j |i |S r6   r  )r9   argsrS  r:   r:   r;   rO   2  r3  zCallable.__call__Nr   r:   r:   r:   r;   r   .  r   r   c                   @   s   e Zd ZdZeejedd ZeeZeej	edd Z
ee
Z
dejddfddZd	d
 Zdd Zdd Zdd Zdd Zdd ZdS )r   zm
    See: http://www.w3.org/TR/owl-ref/#Boolean

    owl:complementOf is an attribute of Class, however

    c                   C   r   r6   r:   r:   r:   r:   r;   getIntersections>     zBooleanClass.getIntersectionsc                   C   r   r6   r:   r:   r:   r:   r;   	getUnionsE  r  zBooleanClass.getUnionsNc           
      C   s   |d u r+g }| |tjtjgd fD ]\}}}|| |}qt|dks+J t|tj| ||d |tjtjfv sAJ t	||| _
t| jj|| jd}	|rW|	rWJ dt| |	| d S )Nro   r   r  z-This is a previous boolean class description.)r   r   r   r   r   r   rl  r   r<   rp   rG  rX   r^   r   rc   r$   )
r9   rc   rO  rs  r^   propsr   r   r   r  r:   r:   r;   r<   L  s&   
zBooleanClass.__init__c                 C   s   t | jt| | jd}|S )z-
        Create a copy of this class
        rr  )r   rG  rX   r^   )r9   copy_of_classr:   r:   r;   copy`  s   zBooleanClass.copyc                 C   r  r6   r  r  r:   r:   r;   r   i  s   


zBooleanClass.serializec                 C   r   r  r:   r   r:   r:   r;   r  v  rh  zBooleanClass.isPrimitivec                 C   sN   || j ks	J d| j| j| j | jjf | j| j|| jjf || _ dS )a  
        Converts a unionOf / intersectionOf class expression into one
        that instead uses the given operator

        >>> testGraph = Graph()
        >>> Individual.factoryGraph = testGraph
        >>> EX = Namespace("http://example.com/")
        >>> testGraph.bind("ex", EX, override=False)
        >>> fire = Class(EX.Fire)
        >>> water = Class(EX.Water)
        >>> testClass = BooleanClass(members=[fire,water])
        >>> testClass
        ( ex:Fire AND ex:Water )
        >>> testClass.changeOperator(OWL.unionOf)
        >>> testClass
        ( ex:Fire OR ex:Water )
        >>> try:
        ...     testClass.changeOperator(OWL.unionOf)
        ... except Exception as e:
        ...     print(e)  # doctest: +SKIP
        The new operator is already being used!

        z'The new operator is already being used!N)rG  r^   r   rc   r  r_   r   )r9   newOperatorr:   r:   r;   changeOperatory  s   
zBooleanClass.changeOperatorc                 C   s(   t t| jtr| jjnt | j| jdS r  )r-   rb   r  r   r_   r
   r^   rG  r   r:   r:   r;   rL    s
   zBooleanClass.__repr__c                 C   s$   | j tjksJ | jt| | S )z9
        Adds other to the list and returns self
        )rG  r   r   r  r   r)   rF   r:   r:   r;   rt    s   zBooleanClass.__or__)rQ   rR   rS   r   r  r   r   r   r  r   r  r<   r  r   r  r  rL  rt  r:   r:   r:   r;   r   6  s$    

	
r   c                 C   r   )zk
    TODO: implement this function

    DisjointClasses(' description description { description } ')'

    Nr:   )rs  r:   r:   r;   r     s   r   c                       s  e Zd ZdZejejejejej	ej
gZ								d; fdd	Zdd Zdd Zd	d
 Zdd Zdd Zdd Zeejdd ZeeeeZdd Zdd Zeejdd ZeeeeZdd Zdd Zeejdd ZeeeeZdd  Zd!d" Zeejd#d$ ZeeeeZd%d& Z d'd( Z!eejd)d* Z"ee e!e"Zd+d, Z#d-d. Z$eej	d/d0 Z%ee#e$e%Z	d1d2 Z&d3d4 Z'eej
d5d6 Z(ee&e'e(Z
d7d8 Z)d9d: Z*  Z+S )<r(   z
    restriction ::= 'restriction('
    datavaluedPropertyID dataRestrictionComponent
    { dataRestrictionComponent } ')'
    | 'restriction(' individualvaluedPropertyID
    individualRestrictionComponent
    { individualRestrictionComponent } ')'

    Nc
                    s  |d u rt  n|}tt| j|	|dd | jtjt|f|vr+|| jtjt|f || _|tj	f|tj
f|tjf|tjf|tjf|tjfg}
dd |
D }t|sWtd| \}}|| _t|tri|| _nt|trtt|| _nt| j| j|| _| j|| jf| jvr| j| j|| jf | jd usJ t| j| jtjtjf| jvr| j| jtjtjf | j| jtjtjf d S d S )NTrP  c                 S   s    g | ]\}}|d ur||fqS r6   r:   )ri   r   otermr:   r:   r;   rm     s     z(Restriction.__init__.<locals>.<listcomp>z{Missing value. One of: allValuesFrom, someValuesFrom,value, cardinality, maxCardinality or minCardinalitymust have a value.)r   r   r(   r<   rc   r   r   r2   r   r   r   r   r   r   r   r   
ValueErrorpoprestrictionTyperb   r   restrictionRanger   r)   r   r^   r   r   r   r   )r9   r   r^   r   r   r4   r   r   r   rc   restr_typesvalid_restr_propsrestriction_rangerestriction_typer   r:   r;   r<     s^   


zRestriction.__init__c                 C   sn   t | j| jdd| | j| jddfD ]\}}}||||f |tjtj	fv r4t
|| j| qdS )a,  
        >>> g1 = Graph()
        >>> g2 = Graph()
        >>> EX = Namespace("http://example.com/")
        >>> g1.bind("ex", EX, override=False)
        >>> g2.bind("ex", EX, override=False)
        >>> Individual.factoryGraph = g1
        >>> prop = Property(EX.someProp, baseType=OWL.DatatypeProperty)
        >>> restr1 = (Property(
        ...    EX.someProp,
        ...    baseType=OWL.DatatypeProperty)) @ some @ (Class(EX.Foo))
        >>> restr1  # doctest: +SKIP
        ( ex:someProp SOME ex:Foo )
        >>> restr1.serialize(g2)
        >>> Individual.factoryGraph = g2
        >>> list(Property(
        ...     EX.someProp,baseType=None).type
        ... ) #doctest: +NORMALIZE_WHITESPACE +SKIP
        [rdflib.term.URIRef(
            'http://www.w3.org/2002/07/owl#DatatypeProperty')]
        Nr'  )r&   r   r^   r   r   rc   r   r   r   r   r   )r9   r^   r   r   r   r:   r:   r;   r     s   zRestriction.serializec                 C   r   r  r:   r   r:   r:   r;   r     rh  zRestriction.isPrimitivec                 C   s   t | j| jfS r6   )rj  r   r  r   r:   r:   r;   rk  #  r3  zRestriction.__hash__c                 C   sH   t |tsJ t|tt| t |tr"|j| jko!|j| jkS dS )z
        Equivalence of restrictions is determined by equivalence of the
        property in question and the restriction 'range'
        F)rb   r   rl  r   r(   r   r  r  rF   r:   r:   r;   rm  &  s   "

zRestriction.__eq__c                 C   s   t | jj| jtjdd S )Nrw   r   )rX   r^   r   rc   r   r   r   r:   r:   r;   _get_onproperty4  s
   zRestriction._get_onpropertyc                 C   8   |sd S | j tjt|f}|| jv rd S | j| d S r6   )rc   r   r   r2   r^   r   )r9   r   tripler:   r:   r;   _set_onproperty9     
zRestriction._set_onpropertyc                 C   r   r6   r:   r   r:   r:   r;   _del_onpropertyB  r   zRestriction._del_onpropertyc                 C   .   | j j| jtjdD ]
}t|| j d  S d S r  )r^   r   rc   r   r   r   r9   r   r:   r:   r;   _get_allvaluesfromJ  
   
zRestriction._get_allvaluesfromc                 C   r  r6   )rc   r   r   r)   r^   r   r9   rB   r  r:   r:   r;   _set_allvaluesfromQ  r  zRestriction._set_allvaluesfromc                 C   r   r6   r:   r   r:   r:   r;   _del_allvaluesfromZ  r   zRestriction._del_allvaluesfromc                 C   r  r  )r^   r   rc   r   r   r   r  r:   r:   r;   _get_somevaluesfromb  r  zRestriction._get_somevaluesfromc                 C   r  r6   )rc   r   r   r)   r^   r   r  r:   r:   r;   _set_somevaluesfromi  r  zRestriction._set_somevaluesfromc                 C   r   r6   r:   r   r:   r:   r;   _del_somevaluesfromr  r   zRestriction._del_somevaluesfromc                 C   r  r  )r^   r   rc   r   r   r   r  r:   r:   r;   _get_hasvaluez     zRestriction._get_hasvaluec                 C   r  r6   )rc   r   r   r)   r^   r   r  r:   r:   r;   _set_hasvalue  r  zRestriction._set_hasvaluec                 C   r   r6   r:   r   r:   r:   r;   _del_hasvalue  r   zRestriction._del_hasvaluec                 C   r  r  )r^   r   rc   r   r   r   r  r:   r:   r;   _get_cardinality  r  zRestriction._get_cardinalityc                 C   r  r6   )rc   r   r   r*   r^   r   r  r:   r:   r;   _set_cardinality  r  zRestriction._set_cardinalityc                 C   r   r6   r:   r   r:   r:   r;   _del_cardinality  r   zRestriction._del_cardinalityc                 C   r  r  )r^   r   rc   r   r   r   r  r:   r:   r;   _get_maxcardinality  r  zRestriction._get_maxcardinalityc                 C   r  r6   )rc   r   r   r*   r^   r   r  r:   r:   r;   _set_maxcardinality  r  zRestriction._set_maxcardinalityc                 C   r   r6   r:   r   r:   r:   r;   _del_maxcardinality  r   zRestriction._del_maxcardinalityc                 C   r  r  )r^   r   rc   r   r   r   r  r:   r:   r;   _get_mincardinality  r  zRestriction._get_mincardinalityc                 C   r  r6   )rc   r   r   r)   r^   r   r  r:   r:   r;   _set_mincardinality  r  zRestriction._set_mincardinalityc                 C   r   r6   r:   r   r:   r:   r;   _del_mincardinality  r   zRestriction._del_mincardinalityc                 C   s:   | j | j| jd fD ]\}}}|ttd   S d S )NrM  )r^   r   rc   rQ  rN  rp   r   )r9   r   r   r   r:   r:   r;   restrictionKind  s
   zRestriction.restrictionKindc                 C   s   t | j| jS )zO
        Returns the Manchester Syntax equivalent for this restriction
        )r-   rc   r^   r   r:   r:   r;   rL    s   zRestriction.__repr__)NNNNNNNN),rQ   rR   rS   r   r   r   r   r   r   r   r   rQ  r<   r   r  rk  rm  r  r  r   r   r  r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  rL  r  r:   r:   r   r;   r(     s    =	
	
	
	
	
	
	
r(   c                 C      t | |j|dS )N)r^   r   r(   r^   r   rC  r:   r:   r;   rC         rC   c                 C   r  )N)r^   r   r  r  r:   r:   r;   rC     r  c                 C      t | | j|dS )N)r^   r   r  r  r:   r:   r;   rC     r  c                 C   r   )N)r^   r   r  r  r:   r:   r;   rC     r  c                 C   r   )N)r^   r   r  r  r:   r:   r;   rC     r  c                 C   r   )N)r^   r4   r  r  r:   r:   r;   rC     r  zh
%s( %s { %s }
%s
{ 'super(' datavaluedPropertyID ')'} ['Functional']
{ domain( %s ) } { range( %s ) } )c                       s2  e Zd ZdZdd Zddejddddddddddf fdd	Zdd	 Zd*d
dZ	dd Z
ee	e
Zdd Zdd Zdd Zeejdd ZeeeeZdd Zdd Zeejdd ZeeeeZdd Zdd Zeejd d! ZeeeeZd"d# Zd$d% Zeejd&d' ZeeeeZd(d) Z   Z!S )+r&   a  
    axiom ::= 'DatatypeProperty(' datavaluedPropertyID ['Deprecated']
                { annotation }
                { 'super(' datavaluedPropertyID ')'} ['Functional']
                { 'domain(' description ')' } { 'range(' dataRange ')' } ')'
                | 'ObjectProperty(' individualvaluedPropertyID ['Deprecated']
                { annotation }
                { 'super(' individualvaluedPropertyID ')' }
                [ 'inverseOf(' individualvaluedPropertyID ')' ] [ 'Symmetric' ]
                [ 'Functional' | 'InverseFunctional' |
                'Functional' 'InverseFunctional' |
                'Transitive' ]
                { 'domain(' description ')' } { 'range(' description ')' } ')

    c                 C   sx   t |tr|\}}}n|}|}|}|r| j| |fg| j_|r+| j| |fg| j_|r:| j| |fg| j_dS dS )a  

        OWL properties map to ACE transitive verbs (TV)

        There are 6 morphological categories that determine the surface form
        of an IRI:

            singular form of a transitive verb (e.g. mans)
            plural form of a transitive verb (e.g. man)
            past participle form a transitive verb (e.g. manned)

            http://attempto.ifi.uzh.ch/ace_lexicon#TV_sg
            http://attempto.ifi.uzh.ch/ace_lexicon#TV_pl
            http://attempto.ifi.uzh.ch/ace_lexicon#TV_vbg

        N)rb   rW  rc   r   r  r   r
  r  )r9   verb_annotationsr  r
  tv_vbgr:   r:   r;   setupVerbAnnotations  s   
zProperty.setupVerbAnnotationsNFc                    s   t t| |||| |r| | t| jtrJ |d u r+tt| j| j	dj
| _n| jtj
|f| j	vr@| j	| jtj
|f || _|| _|| _|| _|| _|
d u rXg | _d S |
| _d S )Nr   )r   r&   r<   r  rb   rc   r
   r   r    r^   r   	_baseTyper   r   subPropertyOf	inverseOfdomainr  r  )r9   rc   r^   r   r  r  r  r  	otherTypeequivalentPropertyr  verbAnnotationsr   r   r   r:   r;   r<   0  s   
zProperty.__init__c                 C   sp   | j | jd d fD ]}|| q
t| j| jD ]}|| qt| j	| j
D ]}t|| j | q*d S r6   )r^   r   rc   r   r  r  r  r  r   r  r  r   )r9   r^   r   r   rt   r:   r:   r;   r   R  s   zProperty.serializec                 c   s2    |d u r| j p	|d | jd fD ]}|V  qd S r6   )r^   r   rc   )r9   r^   r  r:   r:   r;   r^  Z  s   
zProperty._get_extentc                 C   s.   |sd S |D ]\}}| j || j|f qd S r6   )r^   r   rc   )r9   rB   subjobjr:   r:   r;   r`  `  s
   zProperty._set_extentc                    s  g }t jjv rv|djtjrtjpdf  tjrPttjj}|r8|jjkr8tjj}nt	tj}|d|t j
jv rKdpLdf  jjtjt jt jt jgfD ]\}}}|t|tt d  qan+|djtjrtjpdf  jjtjt jfD ]
\}}}|d qdd	  |d
 fddjD  |d
 fddjD  |d
 fddjD  ddd |D }|d7 }|S )Nz!ObjectProperty( %s annotation(%s)r  z  inverseOf( %s )%sz
 SymmetricrM  zDatatypeProperty( %s %sz   Functionalc                 S   sV   t | }t|tr| S |trt| S t||tj	tj
gd fr&t| S t| jS r6   )r)   rb   r
   r8  r   rp   r   r   r   r   r   rl  r   )r   gnormalized_namer:   r:   r;   canonicalName  s   


z(Property.__repr__.<locals>.canonicalNamerv   c                       g | ]
}d  |j  qS )z   super( %s )r   )ri   super_propertyr  r9   r:   r;   rm         z%Property.__repr__.<locals>.<listcomp>c                    r  )z   domain( %s )r   )ri   r  r  r:   r;   rm     r  c                    r  )z   range( %s )r   )ri   r  r  r:   r;   rm     r  r}   c                 S   s   g | ]}|r|qS r:   r:   )ri   exprr:   r:   r;   rm     ru   z
))r   r-  r   r   r   r   r  r  rc   rl  r(  r^   r   r   r)  r*  r+  rp   rN  r   r]   r  r  r  )r9   rttwo_link_inverseinversereprr   r/  roletyper:   r  r;   rL  h  s   
zProperty.__repr__c                 c   2    | j j| jtjdD ]}t|| j d dV  qd S Nrw   r'  )r^   r   rc   r   r  r&   rv  r:   r:   r;   _get_subpropertyof  ry  zProperty._get_subpropertyofc                 C   rz  r6   )r^   r   rc   r   r  r)   )r9   rB   subpropertyr:   r:   r;   _set_subpropertyof  r  zProperty._set_subpropertyofc                 C   r   r6   r:   r   r:   r:   r;   _del_subpropertyof  r   zProperty._del_subpropertyofc                 c   r  r  )r^   r   rc   r   r  r&   rv  r:   r:   r;   _get_inverseof  s   zProperty._get_inverseofc                 C   r  r6   )r^   r   rc   r   r  r)   rF   r:   r:   r;   _set_inverseof  r  zProperty._set_inverseofc                 C   r   r6   r:   r   r:   r:   r;   _del_inverseof  r   zProperty._del_inverseofc                 c   r  r  )r^   r   rc   r   r  r   )r9   domr:   r:   r;   _get_domain     zProperty._get_domainc                 C   \   |sd S t |ttfr| j| jtjt|f d S |D ]}| j| jtjt|f qd S r6   )	rb   r    r   r^   r   rc   r   r  r)   )r9   rB   r"  r:   r:   r;   _set_domain     zProperty._set_domainc                 C   r   r6   r:   r   r:   r:   r;   _del_domain  r   zProperty._del_domainc                 c   r  r  )r^   r   rc   r   r  r   )r9   ranr:   r:   r;   
_get_range  r$  zProperty._get_rangec                 C   r%  r6   )	rb   r    r   r^   r   rc   r   r  r)   )r9   rangesr  r:   r:   r;   
_set_range  r'  zProperty._set_rangec                 C   r   r6   r:   r   r:   r:   r;   
_del_range	  r   zProperty._del_rangec                 C   s@   | j D ]\}}}| j|t||f q| jd | jd f d S r6   )r   r^   r   r2   r   rc   )r9   rB   r   r/  r   r:   r:   r;   r   	  s   zProperty.replacer6   )"rQ   rR   rS   r   r  r   r-  r<   r   r^  r`  r   r   rL  r  r  r   r   r  r  r  r   r  r!  r#  r&  r  r(  r*  r,  r  r-  r   r  r:   r:   r   r;   r&     sZ    %"

U

	
	
r&   c                 C   sl   |du ri n|}t | }|dt |dt |dt t| D ]\}}|j||dd q$|| _dS )zI
    Takes a graph and binds the common namespaces (rdf,rdfs, & owl)
    NrV   rU   rW   Fr   )r	   r   r   r   r   rX   itemsnamespace_manager)r^   additionalNSadditional_nsr/  r`   r_   r:   r:   r;   r   	  s   
r   r  r6   )Qr   r  loggingrdflib.collectionr   rdflib.graphr   rdflib.namespacer   r   r   r   r   r	   rdflib.termr
   r   r   r   r   rdflib.utilr   	getLoggerrQ   logger__all__r!   r0   r,   r*   r)   r2   r-   r   r   r    r   r   r%   r   r   r   r   
differencer   r   r   r   r  r  r!  backwardCompatibleWithincompatibleWithr   r   r   r   r   r   r  r"   r#   r   r   r$   r   BooleanPredicatesr  r   r   r   r(   r3   r1   r.   r/   r+   r4   r'   r&   r   r:   r:   r:   r;   <module>   s   s 
	.

	
h  D .C	
"   1GBs
  2  