o
    mi=                    @  s  d Z ddlmZ ddlZddlZddlmZmZ ddlm	Z	 ddl
mZ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%	dTd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/e0ed-1ej2ej3ej4ej5ej6ej7ej8ej9ej:ej;ej<ej=gZ>d.d/ Z?d0d1 Z@G d2d3 d3eAZBG d4d5 d5eBZCdUd6d7ZDG d8d9 d9e+ZEG d:d; d;ZFG d<d= d=eFeEZGej<ej;gZHG d>d? d?ZIG d@dA dAZJG dBdC dCeFeEZKdDdE ZLG dFdG dGeEZMe dHdI ZNe dJdI ZOe dKdI ZPe dLdI ZQe dMdI ZRe dNdI ZSdOZTG dPdQ dQe+ZUdUdRdSZVdS )Vu  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__`

```python
>>> 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

```python
>>> 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.

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

```

We can then access the rdfs:subClassOf relationships

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

```

This can also be used against already populated graphs:

```python
>>> 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

```python
>>> 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

```python
>>> 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:

```python
>>> 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)

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

```

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

```python
>>> 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

```python
>>> 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:

```python
>>> 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

```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
```
    )annotationsN)IterableUnion)
Collection)Graph_ObjectType)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)selfr<    r>   P/home/kim/smarthome/.venv/lib/python3.10/site-packages/rdflib/extras/infixowl.py__init__      
zInfix.__init__c                 C     t | |fddS )Nc                 S     | || S r:   r;   xr=   otherr>   r>   r?   <lambda>       z#Infix.__rlshift__.<locals>.<lambda>r%   r=   rF   r>   r>   r?   __rlshift__      zInfix.__rlshift__c                 C  
   |  |S r:   r;   rJ   r>   r>   r?   
__rshift__   rA   zInfix.__rshift__c                 C  rB   )Nc                 S  rC   r:   r;   rD   r>   r>   r?   rG      rH   z#Infix.__rmatmul__.<locals>.<lambda>rI   rJ   r>   r>   r?   __rmatmul__   rL   zInfix.__rmatmul__c                 C  rM   r:   r;   rJ   r>   r>   r?   
__matmul__   rA   zInfix.__matmul__c                 C  s   |  ||S r:   r;   )r=   Zvalue1Zvalue2r>   r>   r?   __call__      zInfix.__call__N)	__name__
__module____qualname__r@   rK   rN   rO   rP   rQ   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/)Zskosrdfrdfsowllistdcc                 C  s"   |  t|\}}}d||gS N:)compute_qnamer-   join)graphuriprefix	localnamer>   r>   r?   r0      s   r0   c                 C  s(   t | tr| jS t | tttfsJ | S r:   )
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)rc   r*   r   rd   r   r   re   r>   r>   r?   r-     s   r-   c                 C  s"   t | tr| jS t | tsJ | S r:   )rc   r*   rd   r   re   r>   r>   r?   r6     s   
r6   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>   r1   .0childstorer>   r?   
<listcomp>&  s    z$manchesterSyntax.<locals>.<listcomp>c                   rg   r>   rh   ri   rl   r>   r?   rn   )  s    
c                   s     | \}}}d||gS r[   )r]   r^   )rE   ra   r`   rb   rl   r>   r?   castToQName6  s   z%manchesterSyntax.<locals>.castToQName   z( z AND z )r   z THAT c                   s   g | ]	}t t| qS r>   )strr1   )rj   rE   rl   r>   r?   rn   C  s    c                 S     g | ]}t |qS r>   rq   rj   cr>   r>   r?   rn   I      z OR c                 S  rr   r>   rs   rt   r>   r>   r?   rn   K  rv   z{  c                 S  rr   r>   rs   rt   r>   r>   r?   rn   M  rv   z }subject	predicater\   z'%s'z( %s ONLY %s )z( %s VALUE %s )z( %s SOME %s )MAXZMINZEQUALSz( %s %s %s )z
( NOT %s )
c                 S  s   g | ]
}d |t | f qS )zPREFIX %s: <%s>)r4   )rj   kr>   r>   r?   rn   i      z6
SELECT ?p ?bool WHERE {?class a owl:Class; ?p ?bool .z?bool rdf:first ?foo }z?classsparql)	processorZinitBindingsbooleanr_   ))iterr   r   intersectionOfrc   r   appendlenr^   mapr1   rq   unionOfoneOfcomplementOfr,   objectsr	   typerY   
onPropertyr]   r   r
   labelallValuesFromhasValuesomeValuesFrommaxCardinalityminCardinalitycardinalitytriples_choiceskeysr4   r   query	Exceptionr   Zn3rd   r   )rf   rm   r   ZtransientListZlivechildrenchildrenZ	childlistnamedrk   ro   ra   propr`   rb   Z
propstringr   Z	onlyclassvalZ	someclassZ
cardlookup_spoZcomplprologZqstrZinitbZboolpropcolqnamer>   rl   r?   r1     s   




"r1   c                 c  s2    | j tjtjdD ]}t|trt|V  q
d S N)rz   object)subjectsr	   r   r   r   rc   r   r_   ru   r>   r>   r?   r#     s   

r#   c                   @     e Zd Zdd Zdd ZdS )TermDeletionHelperc                 C  r9   r:   )r   )r=   r   r>   r>   r?   r@     rA   zTermDeletionHelper.__init__c                       fdd}|S )Nc                   s   | j | j jd f d S r:   )r_   removerd   r   )instr=   r>   r?   _remover     z-TermDeletionHelper.__call__.<locals>._removerr>   )r=   fr   r>   r   r?   rQ     s   zTermDeletionHelper.__call__NrS   rT   rU   r@   rQ   r>   r>   r>   r?   r         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dZd(ddZeejdd ZeeeeZd)ddZd*ddZeeeZd'dd Zd+d"d#Zeejd$d% ZeeeeZdS ),r$   zE
    A typed individual, the base class of the InfixOWL classes.
    c                 C  s(   | j | jd d fD ]}|| q
d S r:   )factoryGraphtriplesrd   addr=   r_   factr>   r>   r?   	serialize  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 r[   )
r   _Individual__identifierr   r_   r   rc   rd   r]   r^   r   )r=   rd   r_   ra   r`   rb   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   rd   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.

        ```python
        >>> 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   rd   r   r-   r   )r=   rF   sr   _or>   r>   r?   replace  s   zIndividual.replacereturnIterable[_ObjectType]c                 c  &    | j j| jtjdD ]}|V  qd S Nrx   )r_   r   rd   r	   r   r=   Z_tr>   r>   r?   	_get_type     zIndividual._get_typekind4Union[Individual, Identifier, Iterable[_ObjectType]]c                 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 r:   )	rc   r$   r   r_   r   rd   r	   r   r-   )r=   r   ru   r>   r>   r?   	_set_type  s   zIndividual._set_typec                 C     dS )z
        ```python
        >>> 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_typer   c                 C     | j S r:   )r   r   r>   r>   r?   _get_identifier     zIndividual._get_identifieric           
        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>   rj   r   r   r   r>   r>   r?   rn         z.Individual._set_identifier.<locals>.<listcomp>c                 S  s   g | ]	\}}}||fqS r>   r>   r   r>   r>   r?   rn     r   c                   s   g | ]\}} ||j fqS r>   r   )rj   p1o1r   r=   r>   r?   rn         c                   s   g | ]\}}|| j fqS r>   r   )rj   s1r   r   r>   r?   rn     r   r\   )r   r_   r   r   ZaddNrc   r   r]   r^   r   r   )
r=   r   Zoldstatements_outZoldstatements_inr   r   r   ra   r`   rb   r>   r   r?   _set_identifier  s.   

zIndividual._set_identifierc                 c  r   r   )r_   r   rd   r   sameAsr   r>   r>   r?   _get_sameAs  r   zIndividual._get_sameAstermc                 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 r:   )	rc   r$   r   r_   r   rd   r   r   r-   )r=   r   ru   r>   r>   r?   _set_sameAs  s   zIndividual._set_sameAsc                 C     d S r:   r>   r   r>   r>   r?   _delete_sameAs*     zIndividual._delete_sameAsNN)r   r   )r   r   )r   r   )r   r   )r   r   )rS   rT   rU   __doc__r   r   r   r@   r   r   r   r   r   r   r   r	   r   r   propertyr   r   rd   r   r   r   r   r   r>   r>   r>   r?   r$     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:

    ```
    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.

    ```xml
    <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 r:   )	superr   r@   setupACEAnnotationsrd   handleAnnotation	PN_sgpropextentr   )r=   rd   r_   nameAnnotationnameIsLabel	__class__r>   r?   r@   {  s   zAnnotatableTerms.__init__c                 C  s   t |tr|S t|S r:   )rc   r   )r=   r   r>   r>   r?   r     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 )NZaceFoverride)baseTyper_   )r_   bindr   r*   ZPN_sgr   AnnotationPropertyr   ZCN_sg	CN_sgpropZCN_pl	CN_plpropZTV_sg	tv_sgpropZTV_pl	tv_plpropZTV_vbg
tv_vbgpropr   r>   r>   r?   r     s&   z$AnnotatableTerms.setupACEAnnotationsc                 c  r   r   )r_   r   rd   r
   comment)r=   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 r:   )rc   r   r_   r   rd   r
   r   )r=   r   ru   r>   r>   r?   _set_comment     
zAnnotatableTerms._set_commentc                 C  r   r:   r>   r   r>   r>   r?   _del_comment  r   zAnnotatableTerms._del_commentc                 c  r   r   )r_   r   rd   r
   seeAlso)r=   Zseealsor>   r>   r?   _get_seealso  r   zAnnotatableTerms._get_seealsoc                 C  s,   |sd S |D ]}| j | jtj|f qd S r:   )r_   r   rd   r
   r   )r=   Zseealsosr   r>   r>   r?   _set_seealso  s
   zAnnotatableTerms._set_seealsoc                 C  r   r:   r>   r   r>   r>   r?   _del_seealso  r   zAnnotatableTerms._del_seealsoc                 c  r   r   )r_   r   rd   r
   r   )r=   r   r>   r>   r?   
_get_label  r   zAnnotatableTerms._get_labelc                 C  r   r:   )rc   r   r_   r   rd   r
   r   )r=   r   Zl_r>   r>   r?   
_set_label  r   zAnnotatableTerms._set_labelc                 C  r   )z
        ```python
        >>> 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)rS   rT   rU   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   4  s.    I!	

	
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 r:   )r   r)   r@   importsr   rd   r	   r   r   r_   r   )r=   rd   r  r   r_   r   r>   r?   r@     s   zOntology.__init__c                 C  s   | j | jtj|f d S r:   )r_   setrd   r   versionInfo)r=   versionr>   r>   r?   
setVersion  r   zOntology.setVersionc                 c  s(    | j j| jtd dD ]}|V  qd S )Nr  rx   )r_   r   rd   r   )r=   rX   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   rd   r   )r=   rF   r   r>   r>   r?   _set_imports
  s
   zOntology._set_importsr  c                 C  r   r:   r>   r   r>   r>   r?   _del_imports  r   zOntology._del_imports)NNNN)rS   rT   rU   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TransitivePropertyZDatatypePropertyObjectPropertyr   r   r*   )r_   Z	prevpropsr   _pr   Z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 r:   )r   r   r=   namer>   r>   r?   r   <     zClassNamespaceFactory.termNc                 C  rM   r:   )r   )r=   keydefaultr>   r>   r?   __getitem__?  rA   z!ClassNamespaceFactory.__getitem__c                 C  s   | drt| |S )N__)
startswithAttributeErrorr   r  r>   r>   r?   __getattr__B  s   

z!ClassNamespaceFactory.__getattr__r:   )rS   rT   rU   r   r  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   rd   r   r   r   rc   r   r    r   r   
subClassOfr-   r   )clsr   r  Zinner_class_idZinner_classZ_c_clsr   r>   r>   r?   r    ]  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

    ```python
    >>> 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 r:   )rc   r-   r   r!   )_classr>   r>   r?   deepClearIfBNode  s   z(DeepClassClear.<locals>.deepClearIfBNodeN)r   r$   r   r!  r_   r   rd   r
   equivalentClassr   r   rc   r   clear	_operator)Zclass_to_pruner%  ru   Zinverse_classr>   r>   r?   r!     s(   .





r!   c                   @  s   e Zd ZdZdS )r&   zY
    !!! warning "Deprecated"
        This class will be removed in version `7.0.0`.
    N)rS   rT   rU   r   r>   r>   r>   r?   r&     s    r&   c                   @  r   )r'   c                 C  r9   r:   msg)r=   r*  r>   r>   r?   r@     rA   zMalformedClassError.__init__c                 C  r   r:   r)  r   r>   r>   r?   __repr__  r   zMalformedClassError.__repr__N)rS   rT   rU   r@   r+  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 )Nrx   )rd   r_   r   c                 S  s"   g | ]}t |t td  qS )r,  )rq   splitr   )rj   r   r>   r>   r?   rn     s   " zCastClass.<locals>.<listcomp>zMalformed owl:Restrictionr   )operatorr_   Tr_   r   r>   )r   r   r-   r	   r   r   r,   r   r   restrictionKindsrq   r-  r  intersectionr'   r   r   r   r   r"   r   r   )ru   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													dK fd
d	ZdLd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 Z2dFdG Z3dMdIdJZ4  Z5S )Nr   uv  '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 r:   )r!  r   r_   r   r&  disjointWithr   )r=   r_   clr>   r>   r?   
_serialize"  s   


zClass._serializec                 C  s2   | j | jd d fD ]}|| q
| | d S r:   )r_   r   rd   r   r5  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 r:   )rc   tuplerd   r   r   r   r   )r=   Znoun_annotationsZ	cn_sgpropZ	cn_plpropr>   r>   r?   setupNounAnnotations1  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 r:   )r   r   r@   r7  rd   r	   r   r   r_   r,   r   r!  r&  r3  r   r   )r=   rd   r!  r&  r3  r   r_   r   r   ZnounAnnotationsr   r   r   r>   r?   r@   A  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   rd   )r=   r_   memberr>   r>   r?   _get_extenta  s   
zClass._get_extentc                 C  s0   |sd S |D ]}| j t|tj| jf qd S r:   )r_   r   r-   r	   r   rd   )r=   rF   mr>   r>   r?   _set_extentg  
   zClass._set_extentc                 C  r   r:   r>   r   r>   r>   r?   	_del_typem  r   zClass._del_typec                 c  s$    | j j| j|dD ]}|V  q
d S r   )r_   r   rd   )r=   r   
annotationr>   r>   r?   _get_annotations  s   zClass._get_annotationc                 C  s   | S r:   r>   )rE   r>   r>   r?   rG   w  s    zClass.<lambda>c                 C  s   t dtj| jfS )NZCLASS)r   r	   r   rd   r   r>   r>   r?   _get_extentqueryy  rL   zClass._get_extentqueryc                 C  r   r:   r>   rJ   r>   r>   r?   _set_extentquery|     zClass._set_extentqueryc                 C  
   t | jS )z}
        >>> b = Class(OWL.Restriction)
        >>> c = Class(OWL.Restriction)
        >>> len(set([b,c]))
        1
        )hashrd   r   r>   r>   r?   __hash__  s   
zClass.__hash__c                 C  s"   t |tsJ t|| j|jkS r:   )rc   r   reprrd   rJ   r>   r>   r?   __eq__  s   zClass.__eq__c                 C  s   t |tsJ | g|_| S r:   )rc   r   r!  rJ   r>   r>   r?   __iadd__  s   zClass.__iadd__c                 C  s,   t |tsJ | jt|tj| jf | S r:   )rc   r   r_   r   r-   r
   r!  rd   rJ   r>   r>   r?   __isub__  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__  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
        r.  membersr_   )r   r   r   r_   rJ   r>   r>   r?   __or__  s   zClass.__or__c                 C  rK  )a  
        Construct an anonymous class description consisting of the
        intersection of this class and 'other' and return it

        Chaining 3 intersections

        ```python
        >>> 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

        ```
        rL  )r   r   r   r_   rJ   r>   r>   r?   __and__  s   zClass.__and__c                 c  s2    | j j| jtjdD ]}t|| j ddV  qd S )Nrx   Tr/  )r_   r   rd   r
   r!  r   r=   Zancr>   r>   r?   _get_subclassof     
zClass._get_subclassofc                 C  0   |sd S |D ]}| j | jtjt|f qd S r:   )r_   r   rd   r
   r!  r-   r=   rF   scr>   r>   r?   _set_subclassof  r<  zClass._set_subclassofc                 C  r   r:   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 Nrx   r   )r_   r   rd   r   r&  r   )r=   ecr>   r>   r?   _get_equivalentclass     
zClass._get_equivalentclassc                 C  rS  r:   )r_   r   rd   r   r&  r-   rT  r>   r>   r?   _set_equivalentclass     zClass._set_equivalentclassc                 C  r   r:   r>   r   r>   r>   r?   _del_equivalentclass  r   zClass._del_equivalentclassc                 c  rX  rY  )r_   r   rd   r   r3  r   )r=   rZ   r>   r>   r?   _get_disjointwith  r\  zClass._get_disjointwithc                 C  rS  r:   )r_   r   rd   r   r3  r-   )r=   rF   ru   r>   r>   r?   _set_disjointwith  r<  zClass._set_disjointwithc                 C  r   r:   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|)Nrx   rp   r   r   )	rY   r_   r   rd   r   r   r   r   r   )r=   compr>   r>   r?   _get_complementof  s   zClass._get_complementofc                 C  &   |sd S | j | jtjt|f d S r:   )r_   r   rd   r   r   r-   rJ   r>   r>   r?   _set_complementof     zClass._set_complementofc                 C  r   r:   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

        ```python
        >>> 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 ]

        ```
        r,  Tr  r   N)	itertoolschainr!  r&  r   r   r   r	   rd   rY   Ztransitive_subjectsrestr   r   rc   r   r   r   r   r(   )r=   parentlinkZsiblingslistZcollectionheadZdisjointclassrdf_listr8  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)rd   r	   r   r   r,   r_   rY   r&  r   r   r   r   r1   r   )r=   rZ  
_boolclassr   rn  _er>   r>   r?   isPrimitiveK  s   
zClass.isPrimitivec                 c  s&    | j jtj| jdD ]}|V  qd S r   )r_   r   r
   r!  rd   )r=   r   r>   r>   r?   subSumpteeIds[  r   zClass.subSumpteeIdsc                 C  s   | j dddS )NFT)fullnormalization)manchesterClassr   r>   r>   r?   r+  d     zClass.__repr__Tc                   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>   )	rc   r   rd   r   rF  r   r_   r1   r-   rj   r   r   r>   r?   rn     s    	

z)Class.manchesterClass.<locals>.<listcomp>zPrimitive Type %szSubClassOf: %sc                 S  rr   r>   rs   )rj   nr>   r>   r?   rn     rv   z
    r,  c                   s*   g | ]}t |tr|ptt| jqS r>   )rc   rq   r1   r-   r_   r|  r   r>   r?   rn     s    
zA Defined Class %szEquivalentTo: %szDisjointWith %s
z
                 c                   s   g | ]
}t t| jqS r>   )r1   r-   r_   r|  r   r>   r?   rn     r~   z
    ## %s ##z
    %sz . zSome Class z
Class: %s )rY   r!  r&  r_   r   rd   r   r   r   r   r1   r3  r   r   r
   r   r^   r   rc   r   r   )r=   rt  ru  ZexprsrU  rZ  rp  r   rn  rZ   ru   Z	klasskindr   ZscjoinZnec_statementsZnec_suff_statementsdescrZ
klassdescrr>   r   r?   rv  g  s   




	
	zClass.manchesterClass)NNNNNNFNNNFr:   )FT)6rS   rT   rU   r   r5  r   r7  r@   r9  r;  r   r	   r   r=  r   r   r
   r   r?  r>  r@  rA  ZextentQueryrE  rG  rH  rI  rJ  rN  rO  rQ  rV  r!  rW  r[  r]  r   r&  r_  r`  ra  r3  rb  rd  rf  r   rh  ro  parentsrr  rs  r+  rv  r  r>   r>   r   r?   r     s    

 

		



0	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  rr   r>   )r-   )rj   r:  r>   r>   r?   rn     rv   z,OWLRDFListProxy.__init__.<locals>.<listcomp>)
r_   r   _rdfListr   r-   r   r   rd   r(  r`   )r=   rn  rM  r_   r8  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)rc   r   rF  r   r   r   rangerd   )r=   rF   lengthidxr>   r>   r?   rG    s   "
zOWLRDFListProxy.__eq__c                 C  rC  r:   )r   r  r   r>   r>   r?   __len__  rA   zOWLRDFListProxy.__len__c                 C  s   | j t|S r:   )r  indexr-   r=   itemr>   r>   r?   r    r  zOWLRDFListProxy.indexc                 C  s
   | j | S r:   r  r=   r  r>   r>   r?   r    rA   zOWLRDFListProxy.__getitem__c                 C  s   t || j|< d S r:   )r-   r  )r=   r  r8   r>   r>   r?   __setitem__  rL   zOWLRDFListProxy.__setitem__c                 C  s   | j |= d S r:   r  r  r>   r>   r?   __delitem__  rR   zOWLRDFListProxy.__delitem__c                 C  s   | j   d S r:   )r  r'  r   r>   r>   r?   r'    rw  zOWLRDFListProxy.clearc                 c  s    | j D ]}|V  qd S r:   r  r  r>   r>   r?   __iter__  s   
zOWLRDFListProxy.__iter__c                 C  s"   | j D ]}|t|kr dS qdS )Nrp   r   )r  r-   )r=   r  r   r>   r>   r?   __contains__  s
   
zOWLRDFListProxy.__contains__c                 C  s   | j | d S r:   )r  r   r  r>   r>   r?   r      r  zOWLRDFListProxy.appendc                 C  s   | j t| | S r:   )r  r   r-   rJ   r>   r>   r?   rH    s   zOWLRDFListProxy.__iadd__r   )rS   rT   rU   r@   rG  r  r  r  r  r  r'  r  r  r   rH  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 } ')'

    ```python
    >>> 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?   rr  .  rB  zEnumeratedClass.isPrimitiveNc                 C  sJ   t j| ||d |d u rg n|}t| jjtj| jd}t| || d S )Nr   rz   ry   )	r   r@   rY   r_   r   r   r   rd   r(   )r=   rd   rM  r_   ZrdfListr>   r>   r?   r@   1  s   zEnumeratedClass.__init__c                 C  s   t | jj| j| jdS rx  r   )r1   r  r`   r_   r(  r   r>   r>   r?   r+  9  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 r:   r   r   r  r   r   r_   r   r   rd   r(  r`   r   r5  r=   r_   Z
clonedlistr4  r   r   r   r>   r>   r?   r   ?  s   


zEnumeratedClass.serialize)NNN)rS   rT   rU   r   r   r   r(  rr  r@   r+  r   r>   r>   r>   r?   r"     s    #
r"   c                   @  s    e Zd ZdZdd Zdd ZdS )BooleanClassExtentHelperaw  
    ```python
    >>> 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  r9   r:   r.  )r=   r.  r>   r>   r?   r@   e  rA   z!BooleanClassExtentHelper.__init__c                   r   )Nc                  3  s*    t j jD ]
} t|  jdV  qd S )Nr  )r$   r   r   r.  r   )ru   r   r>   r?   
_getExtenti  s   z5BooleanClassExtentHelper.__call__.<locals>._getExtentr>   )r=   r   r  r>   r   r?   rQ   h  s   z!BooleanClassExtentHelper.__call__N)rS   rT   rU   r   r@   rQ   r>   r>   r>   r?   r  O  s    r  c                   @  r   )r   c                 C  r9   r:   Z_callfn)r=   Zanycallabler>   r>   r?   r@   q  rA   zCallable.__init__c                 O  s   | j |i |S r:   r  )r=   argsr2  r>   r>   r?   rQ   t  r  zCallable.__call__Nr   r>   r>   r>   r?   r   p  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   zl
    See: http://www.w3.org/TR/owl-ref/#Boolean

    owl:complementOf is an attribute of Class, however
    c                   C  r   r:   r>   r>   r>   r>   r?   getIntersections     zBooleanClass.getIntersectionsc                   C  r   r:   r>   r>   r>   r>   r?   	getUnions  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 )Nrp   r   r  z-This is a previous boolean class description.)r   r   r   r   r   r   rF  r   r@   rq   r(  rY   r_   r   rd   r(   )
r=   rd   r.  rM  r_   propsr   r   r   rn  r>   r>   r?   r@     s&   
zBooleanClass.__init__c                 C  s   t | jt| | jd}|S )z-
        Create a copy of this class
        rL  )r   r(  rY   r_   )r=   Zcopy_of_classr>   r>   r?   copy  s   zBooleanClass.copyc                 C  r  r:   r  r  r>   r>   r?   r     s   


zBooleanClass.serializec                 C  r   r  r>   r   r>   r>   r?   rr    rB  zBooleanClass.isPrimitivec                 C  sN   || j ks	J d| j| j| j | jjf | j| j|| jjf || _ dS )a8  
        Converts a unionOf / intersectionOf class expression into one
        that instead uses the given operator

        ```python
        >>> 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)r(  r_   r   rd   r  r`   r   )r=   ZnewOperatorr>   r>   r?   changeOperator  s   
zBooleanClass.changeOperatorc                 C  s(   t t| jtr| jjnt | j| jdS r  )r1   rc   r  r   r`   r   r_   r(  r   r>   r>   r?   r+    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
        )r(  r   r   r  r   r-   rJ   r>   r>   r?   rN    s   zBooleanClass.__or__)rS   rT   rU   r   r  r   r   r   r  r   r  r@   r  r   rr  r  r+  rN  r>   r>   r>   r?   r   x  s$    

	
r   c                 C  r   )zj
    TODO: implement this function

    DisjointClasses(' description description { description } ')'
    Nr>   )rM  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,   a  
    ```
    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 )NTr/  c                 S  s    g | ]\}}|d ur||fqS r:   r>   )rj   r   Zotermr>   r>   r?   rn   +  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@   rd   r   r   r6   r   r   r   r   r   r   r   r   
ValueErrorpopZrestrictionTyperc   r   restrictionRanger   r-   r   r_   r   r	   r   r   )r=   r   r_   r   r   r8   r   r   r   rd   Zrestr_typesZvalid_restr_propsrestriction_rangeZ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 )aK  
        ```python
        >>> 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   rd   r   r   r   r   r   )r=   r_   r   r   r   r>   r>   r?   r   I  s   zRestriction.serializec                 C  r   r  r>   r   r>   r>   r?   rr  h  rB  zRestriction.isPrimitivec                 C  s   t | j| jfS r:   )rD  r   r  r   r>   r>   r?   rE  k  r  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)rc   r   rF  r   r,   r   r  r  rJ   r>   r>   r?   rG  n  s   "

zRestriction.__eq__c                 C  s   t | jj| jtjdd S )Nrx   r   )rY   r_   r   rd   r   r   r   r>   r>   r?   _get_onproperty}  s
   zRestriction._get_onpropertyc                 C  8   |sd S | j tjt|f}|| jv rd S | j| d S r:   )rd   r   r   r6   r_   r  )r=   r   tripler>   r>   r?   _set_onproperty     
zRestriction._set_onpropertyc                 C  r   r:   r>   r   r>   r>   r?   _del_onproperty  r   zRestriction._del_onpropertyc                 C  .   | j j| jtjdD ]
}t|| j d  S d S rY  )r_   r   rd   r   r   r   r=   r   r>   r>   r?   _get_allvaluesfrom  
   
zRestriction._get_allvaluesfromc                 C  r  r:   )rd   r   r   r-   r_   r  r=   rF   r  r>   r>   r?   _set_allvaluesfrom  r  zRestriction._set_allvaluesfromc                 C  r   r:   r>   r   r>   r>   r?   _del_allvaluesfrom  r   zRestriction._del_allvaluesfromc                 C  r  rY  )r_   r   rd   r   r   r   r  r>   r>   r?   _get_somevaluesfrom  r  zRestriction._get_somevaluesfromc                 C  r  r:   )rd   r   r   r-   r_   r  r  r>   r>   r?   _set_somevaluesfrom  r  zRestriction._set_somevaluesfromc                 C  r   r:   r>   r   r>   r>   r?   _del_somevaluesfrom  r   zRestriction._del_somevaluesfromc                 C  r  rY  )r_   r   rd   r   r   r   r  r>   r>   r?   _get_hasvalue     zRestriction._get_hasvaluec                 C  r  r:   )rd   r   r   r-   r_   r  r  r>   r>   r?   _set_hasvalue  r  zRestriction._set_hasvaluec                 C  r   r:   r>   r   r>   r>   r?   _del_hasvalue  r   zRestriction._del_hasvaluec                 C  r  rY  )r_   r   rd   r   r   r   r  r>   r>   r?   _get_cardinality  r  zRestriction._get_cardinalityc                 C  r  r:   )rd   r   r   r.   r_   r  r  r>   r>   r?   _set_cardinality  r  zRestriction._set_cardinalityc                 C  r   r:   r>   r   r>   r>   r?   _del_cardinality  r   zRestriction._del_cardinalityc                 C  r  rY  )r_   r   rd   r   r   r   r  r>   r>   r?   _get_maxcardinality  r  zRestriction._get_maxcardinalityc                 C  r  r:   )rd   r   r   r.   r_   r  r  r>   r>   r?   _set_maxcardinality  r  zRestriction._set_maxcardinalityc                 C  r   r:   r>   r   r>   r>   r?   _del_maxcardinality  r   zRestriction._del_maxcardinalityc                 C  r  rY  )r_   r   rd   r   r   r   r  r>   r>   r?   _get_mincardinality  r  zRestriction._get_mincardinalityc                 C  r  r:   )rd   r   r   r-   r_   r  r  r>   r>   r?   _set_mincardinality
  r  zRestriction._set_mincardinalityc                 C  r   r:   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 )Nr,  )r_   r   rd   r0  r-  rq   r   )r=   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
        )r1   rd   r_   r   r>   r>   r?   r+  $  s   zRestriction.__repr__)NNNNNNNN),rS   rT   rU   r   r   r   r   r   r   r   r   r0  r@   r   rr  rE  rG  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  r+  r  r>   r>   r   r?   r,     s    ?	
	
	
	
	
	
	
	r,   c                 C     t | |j|dS )N)r_   r   r,   r_   r   r$  r>   r>   r?   rG   /      rG   c                 C  r  )N)r_   r   r  r  r>   r>   r?   rG   2  r  c                 C     t | | j|dS )N)r_   r   r  r  r>   r>   r?   rG   5  r  c                 C  r  )N)r_   r   r  r  r>   r>   r?   rG   8  r  c                 C  r  )N)r_   r   r  r  r>   r>   r?   rG   ;  r  c                 C  r  )N)r_   r8   r  r  r>   r>   r?   rG   =  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)rc   r6  rd   r   r   r   r   r   )r=   Zverb_annotationsr   r   Ztv_vbgr>   r>   r?   setupVerbAnnotationsY  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  rc   rd   r   r   r$   r_   r   Z	_baseTyper	   r   subPropertyOf	inverseOfdomainr  r   )r=   rd   r_   r   r  r  r  r  Z	otherTypeZequivalentPropertyr   ZverbAnnotationsr   r   r   r>   r?   r@   x  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 r:   )r_   r   rd   r   ri  rj  r  r  r   r  r  r   )r=   r_   r   r   ru   r>   r>   r?   r     s   zProperty.serializec                 c  s2    |d u r| j p	|d | jd fD ]}|V  qd S r:   )r_   r   rd   )r=   r_   r  r>   r>   r?   r9    s   
zProperty._get_extentc                 C  s.   |sd S |D ]\}}| j || j|f qd S r:   )r_   r   rd   )r=   rF   Z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)ry  z  inverseOf( %s )%sz
 Symmetricr,  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 r:   )r-   rc   r   r  r   rq   r   r   r   r   r   rF  r   )r   gnormalized_namer>   r>   r?   canonicalName  s   


z(Property.__repr__.<locals>.canonicalNamerw   c                      g | ]
}d  |j  qS )z   super( %s )r   )rj   Zsuper_propertyr  r=   r>   r?   rn         z%Property.__repr__.<locals>.<listcomp>c                   r  )z   domain( %s )r   )rj   r  r  r>   r?   rn     r  c                   r  )z   range( %s )r   )rj   r  r  r>   r?   rn     r  r|   c                 S  s   g | ]}|r|qS r>   r>   )rj   exprr>   r>   r?   rn   	  rv   z
))r   r  r   r   r   r   r   r  rd   rF  r  r_   r   r	   r  r  r  rq   r-  r   r^   r  r  r  )r=   rtZtwo_link_inverseZinversereprr   r  Zroletyper>   r  r?   r+    s   
	zProperty.__repr__c                 c  2    | j j| jtjdD ]}t|| j d dV  qd S Nrx   r  )r_   r   rd   r
   r  r*   rP  r>   r>   r?   _get_subpropertyof
	  rR  zProperty._get_subpropertyofc                 C  rS  r:   )r_   r   rd   r
   r  r-   )r=   rF   Zsubpropertyr>   r>   r?   _set_subpropertyof	  r^  zProperty._set_subpropertyofc                 C  r   r:   r>   r   r>   r>   r?   _del_subpropertyof	  r   zProperty._del_subpropertyofc                 c  r  r  )r_   r   rd   r   r  r*   rP  r>   r>   r?   _get_inverseof 	  s   zProperty._get_inverseofc                 C  re  r:   )r_   r   rd   r   r  r-   rJ   r>   r>   r?   _set_inverseof$	  rg  zProperty._set_inverseofc                 C  r   r:   r>   r   r>   r>   r?   _del_inverseof)	  r   zProperty._del_inverseofc                 c  rX  rY  )r_   r   rd   r
   r  r   )r=   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 r:   )	rc   r$   r   r_   r   rd   r
   r  r-   )r=   rF   r  r>   r>   r?   _set_domain3	     zProperty._set_domainc                 C  r   r:   r>   r   r>   r>   r?   _del_domain<	  r   zProperty._del_domainc                 c  rX  rY  )r_   r   rd   r
   r  r   )r=   Zranr>   r>   r?   
_get_rangeB	  r  zProperty._get_rangec                 C  r  r:   )	rc   r$   r   r_   r   rd   r
   r  r-   )r=   rangesr  r>   r>   r?   
_set_rangeF	  r  zProperty._set_rangec                 C  r   r:   r>   r   r>   r>   r?   
_del_rangeO	  r   zProperty._del_rangec                 C  s@   | j D ]\}}}| j|t||f q| jd | jd f d S r:   )r   r_   r   r6   r   rd   )r=   rF   r   r  r   r>   r>   r?   r   U	  s   zProperty.replacer:   )"rS   rT   rU   r   r  r   r  r@   r   r9  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>   r>   r   r?   r*   G  sZ    !"

Z

	
	
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)
    NrW   rV   rX   Fr   )r   r   r
   r	   r   rY   itemsnamespace_manager)r_   ZadditionalNSZadditional_nsr  ra   r`   r>   r>   r?   r   \	  s   
r   r  r:   )Wr   
__future__r   ri  loggingtypingr   r   Zrdflib.collectionr   Zrdflib.graphr   r   Zrdflib.namespacer   r	   r
   r   r   r   Zrdflib.termr   r   r   r   r   Zrdflib.utilr   	getLoggerrS   logger__all__r%   r4   r0   r.   r-   r6   r1   r#   r   r$   r   r   r)   r   r   r   r  
differencer   r   r   r   r  r  r  ZbackwardCompatibleWithZincompatibleWithr   r   r   r   r    r!   r  r&   r'   r   r   r(   r"   ZBooleanPredicatesr  r   r   r   r,   r7   r5   r2   r3   r/   r8   r+   r*   r   r>   r>   r>   r?   <module>   s      
.

	
h  D .F	
"   =JD!t	  ;  