o
    ¹iý.  ã                   @  sÌ   d dl mZ 	 d dlZd dlmZmZmZmZ erd dlm	Z	 e 
e¡Zdd„ Zeefd!dd„Zdd„ fd"dd„Zddd„ fd!dd„Zddd„ fd!dd„Zedƒgedƒgdd„ dd„ dd„ fd#dd „ZdS )$é    )ÚannotationsN)ÚTYPE_CHECKINGÚAnyÚDictÚList)ÚGraphc                 C  s   | S )N© )Úxr   r   ú\/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/rdflib/extras/external_graph_libs.pyÚ	_identity   s   r   Úgraphr   Úcalc_weightsÚboolc                 C  sÚ   t |ƒsJ ‚t |ƒsJ ‚t |ƒsJ ‚ddl}| D ]R\}}}	||ƒ||	ƒ}
}| |
|¡}|du s6t||jƒrM||||	ƒ}|rBd|d< |j|
|fi |¤Ž q|rW|d  d7  < d|v rj||||	ƒ}|d  |d ¡ qdS )aè  Helper method for multidigraph, digraph and graph.

    Modifies nxgraph in-place!

    Arguments:
        graph: an rdflib.Graph.
        nxgraph: a networkx.Graph/DiGraph/MultiDigraph.
        calc_weights: If True adds a 'weight' attribute to each edge according
            to the count of s,p,o triples between s and o, which is meaningful
            for Graph/DiGraph.
        edge_attrs: Callable to construct edge data from s, p, o.
           'triples' attribute is handled specially to be merged.
           'weight' should not be generated if calc_weights==True.
           (see invokers below!)
        transform_s: Callable to transform node generated from s.
        transform_o: Callable to transform node generated from o.
    r   Né   ÚweightÚtriples)ÚcallableÚnetworkxÚget_edge_dataÚ
isinstanceÚMultiDiGraphÚadd_edgeÚextend)r   Únxgraphr   Ú
edge_attrsÚtransform_sÚtransform_oÚnxÚsÚpÚoÚtsÚtoÚdataÚdr   r   r
   Ú_rdflib_to_networkx_graph   s&   €ñr%   c                 C  s   d|iS )NÚkeyr   ©r   r   r    r   r   r
   Ú<lambda>N   s    r(   c                 K  s*   ddl }| ¡ }t| |d|fi |¤Ž |S )a±  Converts the given graph into a networkx.MultiDiGraph.

    The subjects and objects are the later nodes of the MultiDiGraph.
    The predicates are used as edge keys (to identify multi-edges).

    :Parameters:

        - graph: a rdflib.Graph.
        - edge_attrs: Callable to construct later edge_attributes. It receives
            3 variables (s, p, o) and should construct a dictionary that is
            passed to networkx's add_edge(s, o, \*\*attrs) function.

            By default this will include setting the MultiDiGraph key=p here.
            If you don't want to be able to re-identify the edge later on, you
            can set this to ``lambda s, p, o: {}``. In this case MultiDiGraph's
            default (increasing ints) will be used.

    Returns:
        networkx.MultiDiGraph

    >>> from rdflib import Graph, URIRef, Literal
    >>> g = Graph()
    >>> a, b, l = URIRef('a'), URIRef('b'), Literal('l')
    >>> p, q = URIRef('p'), URIRef('q')
    >>> edges = [(a, p, b), (a, q, b), (b, p, a), (b, p, l)]
    >>> for t in edges:
    ...     g.add(t)
    ...
    >>> mdg = rdflib_to_networkx_multidigraph(g)
    >>> len(mdg.edges())
    4
    >>> mdg.has_edge(a, b)
    True
    >>> mdg.has_edge(a, b, key=p)
    True
    >>> mdg.has_edge(a, b, key=q)
    True

    >>> mdg = rdflib_to_networkx_multidigraph(g, edge_attrs=lambda s,p,o: {})
    >>> mdg.has_edge(a, b, key=0)
    True
    >>> mdg.has_edge(a, b, key=1)
    True
    r   NF)r   r   r%   )r   r   Úkwdsr   Úmdgr   r   r
   Úrdflib_to_networkx_multidigraphM   s   /r+   Tc                 C  ó   d| ||fgiS ©Nr   r   r'   r   r   r
   r(   †   ó    c                 K  ó*   ddl }| ¡ }t| |||fi |¤Ž |S )aÕ  Converts the given graph into a networkx.DiGraph.

    As an rdflib.Graph() can contain multiple edges between nodes, by default
    adds the a 'triples' attribute to the single DiGraph edge with a list of
    all triples between s and o.
    Also by default calculates the edge weight as the length of triples.

    :Parameters:

        - ``graph``: a rdflib.Graph.
        - ``calc_weights``: If true calculate multi-graph edge-count as edge 'weight'
        - ``edge_attrs``: Callable to construct later edge_attributes. It receives
            3 variables (s, p, o) and should construct a dictionary that is passed to
            networkx's add_edge(s, o, \*\*attrs) function.

            By default this will include setting the 'triples' attribute here,
            which is treated specially by us to be merged. Other attributes of
            multi-edges will only contain the attributes of the first edge.
            If you don't want the 'triples' attribute for tracking, set this to
            ``lambda s, p, o: {}``.

    Returns: networkx.DiGraph

    >>> from rdflib import Graph, URIRef, Literal
    >>> g = Graph()
    >>> a, b, l = URIRef('a'), URIRef('b'), Literal('l')
    >>> p, q = URIRef('p'), URIRef('q')
    >>> edges = [(a, p, b), (a, q, b), (b, p, a), (b, p, l)]
    >>> for t in edges:
    ...     g.add(t)
    ...
    >>> dg = rdflib_to_networkx_digraph(g)
    >>> dg[a][b]['weight']
    2
    >>> sorted(dg[a][b]['triples']) == [(a, p, b), (a, q, b)]
    True
    >>> len(dg.edges())
    3
    >>> dg.size()
    3
    >>> dg.size(weight='weight')
    4.0

    >>> dg = rdflib_to_networkx_graph(g, False, edge_attrs=lambda s,p,o:{})
    >>> 'weight' in dg[a][b]
    False
    >>> 'triples' in dg[a][b]
    False

    r   N)r   ÚDiGraphr%   )r   r   r   r)   r   Údgr   r   r
   Úrdflib_to_networkx_digraphƒ   ó   8r2   c                 C  r,   r-   r   r'   r   r   r
   r(   Å   r.   c                 K  r/   )a  Converts the given graph into a networkx.Graph.

    As an rdflib.Graph() can contain multiple directed edges between nodes, by
    default adds the a 'triples' attribute to the single DiGraph edge with a
    list of triples between s and o in graph.
    Also by default calculates the edge weight as the len(triples).

    :Parameters:

        - graph: a rdflib.Graph.
        - calc_weights: If true calculate multi-graph edge-count as edge 'weight'
        - edge_attrs: Callable to construct later edge_attributes. It receives
                    3 variables (s, p, o) and should construct a dictionary that is
                    passed to networkx's add_edge(s, o, \*\*attrs) function.

                    By default this will include setting the 'triples' attribute here,
                    which is treated specially by us to be merged. Other attributes of
                    multi-edges will only contain the attributes of the first edge.
                    If you don't want the 'triples' attribute for tracking, set this to
                    ``lambda s, p, o: {}``.

    Returns:
        networkx.Graph

    >>> from rdflib import Graph, URIRef, Literal
    >>> g = Graph()
    >>> a, b, l = URIRef('a'), URIRef('b'), Literal('l')
    >>> p, q = URIRef('p'), URIRef('q')
    >>> edges = [(a, p, b), (a, q, b), (b, p, a), (b, p, l)]
    >>> for t in edges:
    ...     g.add(t)
    ...
    >>> ug = rdflib_to_networkx_graph(g)
    >>> ug[a][b]['weight']
    3
    >>> sorted(ug[a][b]['triples']) == [(a, p, b), (a, q, b), (b, p, a)]
    True
    >>> len(ug.edges())
    2
    >>> ug.size()
    2
    >>> ug.size(weight='weight')
    4.0

    >>> ug = rdflib_to_networkx_graph(g, False, edge_attrs=lambda s,p,o:{})
    >>> 'weight' in ug[a][b]
    False
    >>> 'triples' in ug[a][b]
    False
    r   N)r   r   r%   )r   r   r   r)   r   Úgr   r   r
   Úrdflib_to_networkx_graphÂ   r3   r5   Útermc                 C  s   t dƒ| iS ©Nr6   ©Ústrr'   r   r   r
   r(     ó    c                 C  s   t dƒ|iS r7   r8   r'   r   r   r
   r(     r:   c                 C  s   t dƒ|iS r7   r8   r'   r   r   r
   r(     r:   Úv_prop_namesú	List[str]Úe_prop_namesc                   sF  ddl }| ¡ ‰ ‡ fdd„|D ƒ}|D ]	\}}	|	ˆ j|< q‡ fdd„|D ƒ}
|
D ]	\}}|ˆ j|< q(i }| D ]j\}}}| |¡}|du raˆ  ¡ }|||< ||||ƒ}|D ]
\}}	|| |	|< qT|}| |¡}|du r‡ˆ  ¡ }|||< ||||ƒ}|D ]
\}}	|| |	|< qz|}ˆ  ||¡}||||ƒ}|
D ]
\}}|| ||< q•q6ˆ S )a¾  Converts the given graph into a graph_tool.Graph().

    The subjects and objects are the later vertices of the Graph.
    The predicates become edges.

    :Parameters:
        - graph: a rdflib.Graph.
        - v_prop_names: a list of names for the vertex properties. The default is set
          to ['term'] (see transform_s, transform_o below).
        - e_prop_names: a list of names for the edge properties.
        - transform_s: callable with s, p, o input. Should return a dictionary
          containing a value for each name in v_prop_names. By default is set
          to {'term': s} which in combination with v_prop_names = ['term']
          adds s as 'term' property to the generated vertex for s.
        - transform_p: similar to transform_s, but wrt. e_prop_names. By default
          returns {'term': p} which adds p as a property to the generated
          edge between the vertex for s and the vertex for o.
        - transform_o: similar to transform_s.

    Returns: graph_tool.Graph()

    >>> from rdflib import Graph, URIRef, Literal
    >>> g = Graph()
    >>> a, b, l = URIRef('a'), URIRef('b'), Literal('l')
    >>> p, q = URIRef('p'), URIRef('q')
    >>> edges = [(a, p, b), (a, q, b), (b, p, a), (b, p, l)]
    >>> for t in edges:
    ...     g.add(t)
    ...
    >>> mdg = rdflib_to_graphtool(g)
    >>> len(list(mdg.edges()))
    4
    >>> from graph_tool import util as gt_util
    >>> vpterm = mdg.vertex_properties['term']
    >>> va = gt_util.find_vertex(mdg, vpterm, a)[0]
    >>> vb = gt_util.find_vertex(mdg, vpterm, b)[0]
    >>> vl = gt_util.find_vertex(mdg, vpterm, l)[0]
    >>> (va, vb) in [(e.source(), e.target()) for e in list(mdg.edges())]
    True
    >>> epterm = mdg.edge_properties['term']
    >>> len(list(gt_util.find_edge(mdg, epterm, p))) == 3
    True
    >>> len(list(gt_util.find_edge(mdg, epterm, q))) == 1
    True

    >>> mdg = rdflib_to_graphtool(
    ...     g,
    ...     e_prop_names=[str('name')],
    ...     transform_p=lambda s, p, o: {str('name'): unicode(p)})
    >>> epterm = mdg.edge_properties['name']
    >>> len(list(gt_util.find_edge(mdg, epterm, unicode(p)))) == 3
    True
    >>> len(list(gt_util.find_edge(mdg, epterm, unicode(q)))) == 1
    True

    r   Nc                   ó   g | ]	}|ˆ   d ¡f‘qS ©Úobject)Únew_vertex_property)Ú.0Úvpn©r4   r   r
   Ú
<listcomp>F  ó    z'rdflib_to_graphtool.<locals>.<listcomp>c                   r>   r?   )Únew_edge_property)rB   ÚepnrD   r   r
   rE   I  rF   )Ú
graph_toolr   Úvertex_propertiesÚedge_propertiesÚgetÚ
add_vertexr   )r   r;   r=   r   Útransform_pr   ÚgtÚvpropsrC   ÚvpropÚepropsrH   ÚepropÚnode_to_vertexr   r   r    ÚsvÚvÚ	tmp_propsÚovÚer   rD   r
   Úrdflib_to_graphtool  s@   A

ÿrZ   )r   r   r   r   )r   r   )r   r   r;   r<   r=   r<   )Ú
__future__r   ÚloggingÚtypingr   r   r   r   Úrdflib.graphr   Ú	getLoggerÚ__name__Úloggerr   r%   r+   r2   r5   r9   rZ   r   r   r   r
   Ú<module>   s2   
	ú1ÿ8ýAýAú