Example #1
0
def diff(A, B, context=False, mods=False):
    '''Given two graphs A and B, where it is generally assumed that B is a "newer" version of A,
    returns a new graph which captures information about which nodes and edges of A were
    removed, added, and remain the same in B.

    Specifically, it returns A ∪ B, such that:
    1. Nodes in A - B are given the "diffstatus" attribute "removed"
    2. Nodes in B - A are given the "diffstatus" attribute "added"
    3. Nodes in A ∩ B are given the "diffstatus" attribute "same"

    Notice that the union of 1 - 3 equals A ∪ B.

    The optional parameter context, when true, will prune the graph so that nodes/edges which are
    the same are only present in the diff graph if:
    1. An edge incident on/to/from it has been changed, or
    2. it is connected to a changed node.

    The optional parameter mods, when true, will check for attribute modifications on nodes and
    edges, in addition to new/removed nodes. Any nodes/edges that have had their attributes
    changed between A and B are marked with the "diffstatus" attribute as "modified."

    WARNING: Currently, this method only works if both A and B were generated with unique IDs in a
    deterministic fashion; i.e., two identical nodes are given the same ID at both points in time.
    This means that diff() will not work on graphs which were generated with automatic random UUIDs.
    '''
    # must take their union first, then mark appropriate nodes/edges
    AB = sn.union(A, B)

    # any edges incident on, to, or from the removed nodes will not be in the removed graph,
    # since we cannot have edges incident on, to, or from non-existent nodes
    removed = sn.difference(A, B)
    _mark_nodes_edges_as(AB, removed, 'removed')
    _mark_incident_edges_as(AB, removed, 'removed')

    added = sn.difference(B, A)
    _mark_nodes_edges_as(AB, added, 'added')
    _mark_incident_edges_as(AB, added, 'added')

    same = sn.intersection(B, A)
    _mark_nodes_edges_as(AB, same, 'same')
    _check_changed_edges(A, B, AB, same)

    if mods:
        _check_mods(A, B, AB, same)

    if context:
        _clear_clutter(AB)

    return AB
Example #2
0
def test_intersection(populated_digraph):
    another_digraph = sn.DiGraph()
    a = another_digraph.add_node({"type": "A"}, '3caaa8c09148493dbdf02c574b95526c')
    b = another_digraph.add_node({"type": "B"}, '2cdfebf3bf9547f19f0412ccdfbe03b7')
    d = another_digraph.add_node({"type": "D"}, 'da30015efe3c44dbb0b3b3862cef704a')
    another_digraph.add_edge(a, b, {"type": "normal"}, '5f5f44ec7c0144e29c5b7d513f92d9ab')
    another_digraph.add_edge(b, a, {"type": "normal"}, 'f3674fcc691848ebbd478b1bfb3e84c3')
    another_digraph.add_edge(a, d, {"type": "normal"}, 'f3674fcc691848ebbd478b1bfb3e84c3')
    another_digraph.add_edge(d, b, {"type": "irregular"}, 'f3674fcc691848ebbd478b1bfb3e84c3')

    I = sn.intersection(populated_digraph, another_digraph)

    correct_nodes = {
        uuid.UUID('3caaa8c09148493dbdf02c574b95526c'): {
            "type": "A",
            "id": uuid.UUID('3caaa8c09148493dbdf02c574b95526c')
        },
        uuid.UUID('2cdfebf3bf9547f19f0412ccdfbe03b7'): {
            "type": "B",
            "id": uuid.UUID('2cdfebf3bf9547f19f0412ccdfbe03b7')
        }
    }
    assert I.get_nodes() == correct_nodes

    correct_edges = {
        # a,b
        uuid.UUID('5f5f44ec7c0144e29c5b7d513f92d9ab'): {
            "type": "normal",
            "src": uuid.UUID('3caaa8c09148493dbdf02c574b95526c'),
            "dst": uuid.UUID('2cdfebf3bf9547f19f0412ccdfbe03b7'),
            "id": uuid.UUID('5f5f44ec7c0144e29c5b7d513f92d9ab')
        },
        # b,a
        uuid.UUID('f3674fcc691848ebbd478b1bfb3e84c3'): {
            "type": "normal",
            "src": uuid.UUID('2cdfebf3bf9547f19f0412ccdfbe03b7'),
            "dst": uuid.UUID('3caaa8c09148493dbdf02c574b95526c'),
            "id": uuid.UUID('f3674fcc691848ebbd478b1bfb3e84c3')
        }
    }
    assert I.get_edges() == correct_edges