/**
 * @file tests/pdevs/graph_manager.cpp
 * @author The PARADEVS Development Team
 * See the AUTHORS or Authors.txt file
 */

/*
 * PARADEVS - the multimodeling and simulation environment
 * This file is a part of the PARADEVS environment
 *
 * Copyright (C) 2013-2015 ULCO http://www.univ-litoral.fr
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef TESTS_PDEVS_GRAPH_MANAGER_HPP
#define TESTS_PDEVS_GRAPH_MANAGER_HPP 1

#include <tests/pdevs/models.hpp>

#include <paradevs/kernel/pdevs/Coordinator.hpp>
#include <paradevs/kernel/pdevs/GraphManager.hpp>
#include <paradevs/kernel/pdevs/Simulator.hpp>

namespace paradevs { namespace tests { namespace pdevs {

class S1GraphManager :
        public paradevs::pdevs::GraphManager < common::DoubleTime >
{
public:
    S1GraphManager(common::Coordinator < common::DoubleTime >* coordinator,
                   const paradevs::common::NoParameters& parameters) :
        paradevs::pdevs::GraphManager < common::DoubleTime >(coordinator,
                                                          parameters),
        a("a1", common::NoParameters()), b("b1", common::NoParameters())
    {
        add_child(&a);
        add_child(&b);

        a.add_out_port("out");
        b.add_in_port("in");
        b.add_out_port("out");
        coordinator->add_out_port("out");

        add_link(&a, "out", &b, "in");
        add_link(&b, "out", coordinator, "out");
    }

    virtual ~S1GraphManager()
    { }

private:
    paradevs::pdevs::Simulator < common::DoubleTime, A > a;
    paradevs::pdevs::Simulator < common::DoubleTime, B > b;
};

class S2GraphManager :
        public paradevs::pdevs::GraphManager < common::DoubleTime >
{
public:
    S2GraphManager(common::Coordinator < common::DoubleTime >* coordinator,
                   const paradevs::common::NoParameters& parameters) :
        paradevs::pdevs::GraphManager < common::DoubleTime >(coordinator,
                                                          parameters),
        a("a2", common::NoParameters()), b("b2", common::NoParameters())
    {
        add_child(&a);
        add_child(&b);

        a.add_in_port("in");
        a.add_out_port("out");
        b.add_in_port("in");
        coordinator->add_in_port("in");

        add_link(&a, "out", &b, "in");
        add_link(coordinator, "in", &a, "in");
    }

    virtual ~S2GraphManager()
    { }

private:
    paradevs::pdevs::Simulator < common::DoubleTime, A > a;
    paradevs::pdevs::Simulator < common::DoubleTime, B > b;
};

class RootGraphManager :
        public paradevs::pdevs::GraphManager < common::DoubleTime >
{
public:
    RootGraphManager(
        common::Coordinator < common::DoubleTime >* coordinator,
        const paradevs::common::NoParameters& parameters) :
        paradevs::pdevs::GraphManager < common::DoubleTime >(
                                            coordinator, parameters),
        S1("S1", paradevs::common::NoParameters(),
           paradevs::common::NoParameters()),
        S2("S2", paradevs::common::NoParameters(),
           paradevs::common::NoParameters())
    {
        add_child(&S1);
        add_child(&S2);

        add_link(&S1, "out", &S2, "in");
    }

    virtual ~RootGraphManager()
    { }

private:
    paradevs::pdevs::Coordinator <
        common::DoubleTime,
        S1GraphManager > S1;
    paradevs::pdevs::Coordinator <
        common::DoubleTime,
        S2GraphManager > S2;
};

template < typename M >
class OnlyOneGraphManager :
        public paradevs::pdevs::GraphManager < common::DoubleTime >
{
public:
    OnlyOneGraphManager(common::Coordinator < common::DoubleTime >* coordinator,
                        const paradevs::common::NoParameters& parameters) :
        paradevs::pdevs::GraphManager < common::DoubleTime >(coordinator,
                                                          parameters),
        model("m", common::NoParameters())
    {
        add_child(&model);
    }

    virtual ~OnlyOneGraphManager()
    { }

private:
    paradevs::pdevs::Simulator < common::DoubleTime, M > model;
};

class FlatGraphManager :
        public paradevs::pdevs::GraphManager < common::DoubleTime >
{
public:
    FlatGraphManager(common::Coordinator < common::DoubleTime >* coordinator,
                     const paradevs::common::NoParameters& parameters) :
        paradevs::pdevs::GraphManager < common::DoubleTime >(coordinator,
                                                          parameters),
        a1("a1", common::NoParameters()), b1("b1", common::NoParameters()),
        a2("a2", common::NoParameters()), b2("b2", common::NoParameters())
    {
        add_child(&a1);
        add_child(&b1);
        add_child(&a2);
        add_child(&b2);

        a1.add_out_port("out");
        b1.add_in_port("in");
        b1.add_out_port("out");
        a2.add_in_port("in");
        a2.add_out_port("out");
        b2.add_in_port("in");

        add_link(&a1, "out", &b1, "in");
        add_link(&b1, "out", &a2, "in");
        add_link(&a2, "out", &b2, "in");
    }

    virtual ~FlatGraphManager()
    { }

private:
    paradevs::pdevs::Simulator < common::DoubleTime, A > a1;
    paradevs::pdevs::Simulator < common::DoubleTime, B > b1;
    paradevs::pdevs::Simulator < common::DoubleTime, A > a2;
    paradevs::pdevs::Simulator < common::DoubleTime, B > b2;
};

} } } // namespace paradevs tests pdevs

#endif
