spinetoolbox.dag_handler

Contains classes for handling DAGs.

author
  1. Savolainen (VTT)

date

8.4.2019

Module Contents

Classes

DirectedGraphHandler

Class for manipulating graphs according to user’s actions.

class spinetoolbox.dag_handler.DirectedGraphHandler[source]

Bases: PySide2.QtCore.QObject

Class for manipulating graphs according to user’s actions.

dags(self)[source]

Returns a list of graphs (DiGraph) in the project.

add_dag(self, dag)[source]

Add graph to list.

Parameters

dag (DiGraph) – Graph to add

remove_dag(self, dag)[source]

Remove graph from instance variable list.

Parameters

dag (DiGraph) – Graph to remove

add_dag_node(self, node_name)[source]

Create directed graph with one node and add it to list.

Parameters

node_name (str) – Project item name to add as a node

add_graph_edge(self, src_node, dst_node)[source]

Adds an edge between the src and dst nodes. If nodes are in different graphs, the reference to union graph is saved and the references to the original graphs are removed. If src and dst nodes are already in the same graph, the edge is added to the graph. If src and dst are the same node, a self-loop (feedback) edge is added.

Parameters
  • src_node (str) – Source project item node name

  • dst_node (str) – Destination project item node name

Returns

True if edge established, False if not (e.g. any of the nodes doesn’t really exist)

Return type

bool

remove_graph_edge(self, src_node, dst_node)[source]

Removes edge from a directed graph.

Parameters
  • src_node (str) – Source project item node name

  • dst_node (str) – Destination project item node name

Returns

One or two DAGs containing source and destination nodes.

Return type

list of DiGraph

remove_node_from_graph(self, node_name)[source]

Removes node from a graph that contains it. Called when project item is removed from project.

Parameters

node_name (str) – Project item name

rename_node(self, old_name, new_name)[source]

Handles renaming the node and edges in a graph when a project item is renamed.

Parameters
  • old_name (str) – Old project item name

  • new_name (str) – New project item name

Returns

True if successful, False if renaming failed

Return type

bool

dag_with_node(self, node_name)[source]

Returns directed graph that contains given node.

Parameters

node_name (str) – Node to look for

Returns

Directed graph that contains node or None if not found.

Return type

(DiGraph)

dag_with_edge(self, src_node, dst_node)[source]

Returns directed graph that contains given edge.

Parameters
  • src_node (str) – Source node name

  • dst_node (str) – Destination node name

Returns

Directed graph that contains edge or None if not found.

Return type

(DiGraph)

static node_successors(g)[source]

Returns a dict mapping nodes in the given graph to a list of its direct successors. The nodes are in topological sort order. Topological sort in the words of networkx: “a nonunique permutation of the nodes, such that an edge from u to v implies that u appears before v in the topological sort order.”

Parameters

g (DiGraph) – Directed graph to process

Returns

key is the node name, value is list of successor names Empty dict if given graph is not a DAG.

Return type

dict

successors_til_node(self, g, node)[source]

Like node_successors but only until the given node, and ignoring all nodes that are not its ancestors.

node_is_isolated(self, node, allow_self_loop=False)[source]

Checks if the project item with the given name has any connections.

Parameters
  • node (str) – Project item name

  • allow_self_loop (bool) – If default (False), Self-loops are considered as an in-neighbor or an out-neighbor so the method returns False. If True, single node with a self-loop is considered isolated.

Returns

True if project item has no in-neighbors nor out-neighbors, False if it does.

Single node with a self-loop is NOT isolated (returns False).

Return type

bool

static source_nodes(g)[source]

Returns a list of source nodes in given graph. A source node has no incoming edges. This is determined by calculating the in-degree of each node in the graph. If nodes in-degree == 0, it is a source node

Parameters

g (DiGraph) – Graph to examine

Returns

List of source node names or an empty list is there are none.

Return type

list

static edges_causing_loops(g)[source]

Returns a list of edges whose removal from g results in it becoming acyclic.

static export_to_graphml(g, path)[source]

Export given graph to a path in GraphML format.

Parameters
  • g (DiGraph) – Graph to export

  • path (str) – Full output path for GraphML file

Returns

Operation success status

Return type

bool