#  Copyright 2020 Modelling, Simulation and Design Lab (MSDL) at
#  McGill University and the University of Antwerp (http://msdl.cs.mcgill.ca/)
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

"""
Within this package, you will be able to find a set of classes and functions
that allow for additional functionality that can be used in the building blocks.

This goes about file handing, mathematical helper functions, an implementation
for a `heap`, a set of packaging classes as required for the
:class:`pypdevsbbl.generic.transformers.Pack` and :class:`pypdevsbbl.generic.transformers.Unpack`
blocks and a set of inverse cumulative distribution functions that can
be used in the :class:`pypdevsbbl.generic.generators.RandomNumberGenerator`.
"""
from pypdevs.DEVS import CoupledDEVS

def toDot(model: CoupledDEVS, filename: str):
    """Simple function for creating a Graphviz file for a Coupled DEVS.

    The submodels become the nodes and the connections become the directed
    edges.

    Note:
        The graphical representation that will be created is not a valid
        visual concrete syntax and should therefore only be used as confirmation
        on the validity of models.

    Args:
        model (CoupledDEVS):    A :class:`CoupledDEVS` model.
        filename (str):         The filename to write the graph to.
    """
    res = ""
    # Create a node for each component
    for submodel in model.component_set:
        res += '\n\t"%s" [label="%s(%s)"];' % (submodel.name, type(submodel).__name__, submodel.name)

    res += "\n"

    for submodel in model.component_set:
        for out in submodel.OPorts:
            for inp in out.outline:
                res += '\n\t%s -> %s [label="%s / %s"];' % (out.host_DEVS.name, inp.host_DEVS.name, out.name, inp.name)

    res = "digraph Model {%s\n}" % res

    with open(filename, 'w') as file:
        file.write(res)
