# loki.batch.sgraph module

class SGraph(graph=None)

  Bases: `object`

  The dependency graph underpinning the `Scheduler`

  It is built upon a `networkx.DiGraph` to expose the dependencies between `Item` nodes. It is typically created from one or multiple seed items via `from_seed()` by recursively chasing dependencies.

  Cyclic dependencies are broken for procedures that are marked as `RECURSIVE`, which would otherwise constitute a dependency on itself. See `_break_cycles()`.

  Parameters:

    graph (`networkx.DiGraph`, optional) – Optionally, directed graph as instance of `networkx.DiGraph` as initial graph.

  classmethod from_seed(seed, item_factory, config=None)

    Create a new `SGraph` using `seed` as starting point.

    Parameters:

      - seed ((list of) str) – The names of the root nodes

      - item_factory (`ItemFactory`) – The item factory to use when creating graph nodes

      - config (`SchedulerConfig`, optional) – The config object to use when creating items

  classmethod from_dict(graph_dict)

    Create a new `SGraph` using `graph_dict` as starting point.

    Parameters:

      graph_dict (dict) – Representation of the underlying graph represented as dict.

  as_filegraph(item_factory, config=None, item_filter=None, exclude_ignored=False)

    Convert the `SGraph` to a dependency graph that only contains `FileItem` nodes.

    Parameters:

      - item_factory (`ItemFactory`) – The item factory to use when creating graph nodes

      - config (`SchedulerConfig`, optional) – The config object to use when creating items

      - item_filter (list of `Item` subclasses, optional) – Only include files that include at least one dependency item of the given type. By default, all items are included.

      - exclude_ignored (bool, optional) – Exclude `Item`s that have the ``is_ignored`` property

    Returns:

      A new graph object

    Return type:

      `SGraph`

  property items

    Return all `Item` nodes in the dependency graph

  property dependencies

    Return all dependencies, i.e., edges of the dependency graph

  successors(item, item_filter=None)

    Return the list of successor nodes in the dependency tree below `Item`

    This returns all immediate successors (but can be filtered accordingly using the item’s `targets` property) of the item in the dependency graph

    The list of successors is provided to transformations during processing with the `Scheduler`.

    Parameters:

      - item (`Item`) – The item node in the dependency graph for which to determine the successors

      - item_filter (list of `Item` subclasses, optional) – Filter successor items to only include items of the provided type. By default, all items are considered. Note that including `ProcedureItem` in the `item_filter` automatically adds `ProcedureBindingItem` and `InterfaceItem` as well, since these are intermediate nodes. Their dependencies will also be included until they eventually resolve to a `ProcedureItem`.

  get_sub_sgraph(item, item_filter=None)

    Return the subgraph of `self._graph` from source `item` as a new instance of `SGraph`.

    Parameters:

      - item (`Item`) – The item node in the dependency graph for which to determine the successors

      - item_filter (list of `Item` subclasses, optional) – Filter successor items to only include items of the provided type. By default, all items are considered. Note that including `ProcedureItem` in the `item_filter` automatically adds `ProcedureBindingItem` and `InterfaceItem` as well, since these are intermediate nodes. Their dependencies will also be included until they eventually resolve to a `ProcedureItem`.

  property depths

    Return a mapping of `Item` nodes to their depth (topological generation) in the dependency graph

  add_node(item)

    Add `item` as a node to the dependency graph

  add_nodes(items)

    Add the given `items` as nodes to the dependency graph

  add_edge(edge)

    Add a dependency `edge` to the dependency graph

  add_edges(edges)

    Add the dependency `edges` to the dependency graph

  export_to_file(dotfile_path)

    Generate a dotfile from the current graph

    Parameters:

      dotfile_path (str or pathlib.Path) – Path to write the dotfile to. A corresponding graphical representation will be created with an additional `.pdf` appendix.

  descendants(item, item_factory, include_scope_items=False)

    Get all descendants of a given `item`.

    Parameters:

      - item (`Item`) – The item node in the dependency graph for which to determine the successors

      - item_factory (`ItemFactory`) – The item factory to use.

      - include_scope_items (bool, optional) – Whether to include scope items like module and file items.