# AUTOGENERATED! DO NOT EDIT! File to edit: dbn.ipynb (unless otherwise specified).

__all__ = ['dbn_writer', 'has_positions', 'Axis']

# Cell
import gtsam

# Cell
from gtsam import GraphvizFormatting
Axis = GraphvizFormatting.Axis

def dbn_writer(writer=None, hints: dict = None, positions: dict = None,
               boxes: set = None, factor_positions: dict = None,
               binary_edges=False, **kwargs):
    """ Create a DotWriter depending on input arguments:
        If writer is supplied, we will add but not overwrite hints or positions.
    """
    if writer is None and hints is None and positions is None and boxes is None and factor_positions is None and binary_edges==False:
        return None
    writer = GraphvizFormatting() if writer is None else writer

    writer.paperHorizontalAxis = Axis.X
    writer.paperVerticalAxis = Axis.Y

    # Copy hints without overwriting
    if hints is not None:
        assert isinstance(hints, dict)
        ph: dict = writer.positionHints
        for key, y in hints.items():
            if key not in ph:
                ph[key] = y
        writer.positionHints = ph
    # Copy positions without overwriting
    if positions is not None:
        assert isinstance(positions, dict)
        kp: dict = writer.variablePositions
        for key, position in positions.items():
            if key not in kp:
                kp[key] = position
        writer.variablePositions = kp
    # Add boxes
    if boxes is not None:
        assert isinstance(boxes, set)
        bx: set = writer.boxes
        for key in boxes:
            bx.add(key)
        writer.boxes = bx
    # Copy factor positions without overwriting
    if factor_positions is not None:
        assert isinstance(factor_positions, dict)
        kp: dict = writer.factorPositions
        for i, position in factor_positions.items():
            if i not in kp:
                kp[i] = position
        writer.factorPositions = kp
    writer.binaryEdges = binary_edges
    return writer


def has_positions(writer):
    """Check if writer has positions for engine selection"""
    if writer is None:
        return False
    return len(writer.positionHints) > 0 or len(writer.variablePositions) > 0 or len(writer.factorPositions) > 0
