def test_add_nodes(self):
     """Tests adding two different nodes to the graph."""
     node1 = self.test_graph.add_node_if_new(self.UNIQUE_KEY_1)
     node2 = self.test_graph.add_node_if_new(self.UNIQUE_KEY_2)
     self.assertEqual(self.test_graph.num_nodes, 2)
     self.assertEqual(graph.sorted_nodes_by_name(self.test_graph.nodes),
                      graph.sorted_nodes_by_name([node1, node2]))
Example #2
0
def print_class_dependencies_for_key(class_graph, key):
    """Prints dependencies for a valid key into the class graph."""
    node = class_graph.get_node_by_key(key)

    print(f'{len(node.inbound)} inbound dependency(ies) for {node.name}:')
    for inbound_dep in graph.sorted_nodes_by_name(node.inbound):
        print(f'\t{inbound_dep.name}')

    print(f'{len(node.outbound)} outbound dependency(ies) for {node.name}:')
    for outbound_dep in graph.sorted_nodes_by_name(node.outbound):
        print(f'\t{outbound_dep.name}')
Example #3
0
def print_class_dependencies_for_key(
        class_graph: class_dependency.JavaClassDependencyGraph, key: str,
        print_mode: PrintMode):
    """Prints dependencies for a valid key into the class graph."""
    node: class_dependency.JavaClass = class_graph.get_node_by_key(key)

    if print_mode.inbound:
        print(f'{len(node.inbound)} inbound dependency(ies) into {node.name}:')
        print_class_nodes(graph.sorted_nodes_by_name(node.inbound), print_mode)

    if print_mode.outbound:
        print(
            f'{len(node.outbound)} outbound dependency(ies) from {node.name}:')
        print_class_nodes(graph.sorted_nodes_by_name(node.outbound),
                          print_mode)
Example #4
0
def print_class_dependencies_for_key(
        class_graph: class_dependency.JavaClassDependencyGraph, key: str,
        print_mode: PrintMode) -> TargetDependencies:
    """Prints dependencies for a valid key into the class graph."""
    target_dependencies = TargetDependencies()
    node: class_dependency.JavaClass = class_graph.get_node_by_key(key)

    if print_mode.inbound:
        print_class_dependencies(graph.sorted_nodes_by_name(node.inbound),
                                 print_mode, node, INBOUND)

    if print_mode.outbound:
        target_dependencies = print_class_dependencies(
            graph.sorted_nodes_by_name(node.outbound), print_mode, node,
            OUTBOUND)
    return target_dependencies
def print_package_dependencies_for_key(package_graph, key, ignore_subpackages):
    """Prints dependencies for a valid key into the package graph.

    Since we store self-edges for the package graph
    but they aren't relevant in this case, we skip them.
    """
    node = package_graph.get_node_by_key(key)

    inbound_without_self = [other for other in node.inbound if other != node]
    print(f'{len(inbound_without_self)} inbound dependency(ies) '
          f'for {node.name}:')
    for inbound_dep in graph.sorted_nodes_by_name(inbound_without_self):
        if ignore_subpackages and inbound_dep.name.startswith(node.name):
            continue
        print_package_dependencies_for_edge(inbound_dep, node)

    outbound_without_self = [other for other in node.outbound if other != node]
    print(f'{len(outbound_without_self)} outbound dependency(ies) '
          f'for {node.name}:')
    for outbound_dep in graph.sorted_nodes_by_name(outbound_without_self):
        if ignore_subpackages and outbound_dep.name.startswith(node.name):
            continue
        print_package_dependencies_for_edge(node, outbound_dep)
Example #6
0
def create_json_obj_from_graph(graph_obj: graph.Graph) -> Dict:
    """Generates a JSON representation of the current graph.

    The list of nodes and edges is sorted in order to help with testing.
    Structure:
    {
        'nodes': [
            { see create_json_obj_from_node }, ...
        ],
        'edges': [
            {
                'begin': str,
                'end': str,
                'meta': { see Graph.get_edge_metadata },
            }, ...
        ],
    }
    """
    sorted_nodes = graph.sorted_nodes_by_name(graph_obj.nodes)
    json_nodes = [create_json_obj_from_node(node) for node in sorted_nodes]

    json_edges = []
    for begin_node, end_node in graph.sorted_edges_by_name(graph_obj.edges):
        edge_json_obj = {
            json_consts.BEGIN: begin_node.name,
            json_consts.END: end_node.name,
        }
        edge_meta = graph_obj.get_edge_metadata(begin_node, end_node)
        if edge_meta is not None:
            edge_json_obj[json_consts.META] = edge_meta
        json_edges.append(edge_json_obj)

    return {
        json_consts.NODES: json_nodes,
        json_consts.EDGES: json_edges,
    }