/**
 * @file tests/mpi/cluster/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_MPI_CLUSTER_GRAPH_MANAGER_HPP
#define TESTS_PDEVS_MPI_CLUSTER_GRAPH_MANAGER_HPP 1

#include <tests/pdevs/graph_manager.hpp>
#include <tests/mpi/cluster/models.hpp>

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

#include <sstream>

namespace paradevs { namespace tests { namespace mpi { namespace cluster {

struct SubGraphManagerParameters
{
  std::vector < int > indexes;
  std::vector < std::pair < int, int > > internals;
  std::vector < std::pair < int, int > > inputs;
  std::vector < std::pair < int, int > > outputs;  
};

class SubGraphManager :
    public paradevs::pdevs::mpi::GraphManager < common::DoubleTime,
						SubGraphManagerParameters >
{
public:
  SubGraphManager(common::Coordinator < common::DoubleTime >* coordinator,
		  const SubGraphManagerParameters& parameters) :
    paradevs::pdevs::mpi::GraphManager <
       common::DoubleTime,
       SubGraphManagerParameters >(coordinator, parameters)
  {
    for (std::vector < int >::const_iterator it = parameters.indexes.begin();
	 it != parameters.indexes.end(); ++it) {
      std::stringstream ss;
      
      ss << "m_" << *it;
      Simulator* m = new Simulator(ss.str(), paradevs::common::NoParameters());
      
      models[*it] = m;
      add_child(m);
      m->add_in_port("in");
      m->add_out_port("out");
    }
    for (std::vector < std::pair < int, int > >::const_iterator it =
	   parameters.inputs.begin(); it != parameters.inputs.end(); ++it) {
      std::stringstream ss;
      
      ss << "in_" << it->first;
      if (not coordinator->exist_in_port(ss.str())) {
	coordinator->add_in_port(ss.str());
      }
      if (not exist_link(coordinator, ss.str(), models[it->second], "in")) {
	add_link(coordinator, ss.str(), models[it->second], "in");
      }
    }
    for (std::vector < std::pair < int, int > >::const_iterator it =
	   parameters.outputs.begin(); it != parameters.outputs.end(); ++it) {
      std::stringstream ss;
      
      ss << "out_" << it->first;
      if (not coordinator->exist_out_port(ss.str())) {
	coordinator->add_out_port(ss.str());
      }
      if (not exist_link(models[it->first], "out", coordinator, ss.str())) {
	add_link(models[it->first], "out", coordinator, ss.str());
      }
    }
    for (std::vector < std::pair < int, int > >::const_iterator it =
	   parameters.internals.begin(); it != parameters.internals.end();
	 ++it) {
      if (not exist_link(models[it->first], "out", models[it->second], "in")) {
	add_link(models[it->first], "out", models[it->second], "in");
      }
    }
  }
  
  void init()
  { }
  
  void start(common::DoubleTime::type /* t */)
  { }
  
  void transition(const common::Models < common::DoubleTime >& /* receivers */,
		  common::DoubleTime::type /* t */)
  { }

  virtual ~SubGraphManager()
  {
    for (Simulators::iterator it = models.begin(); it != models.end(); ++it) {
      delete it->second;
    }
  }

private:
  typedef paradevs::pdevs::Simulator < common::DoubleTime,
				       ThreeStateModel > Simulator;
  typedef std::map < int, Simulator* > Simulators;
  
  Simulators models;
};

struct RootGraphManagerParameters
{
  std::vector <
    std::vector <
      std::pair <
	std::pair < int, int >,
	std::pair < int, int > > > > parents;
};

class RootGraphManager :
        public paradevs::pdevs::GraphManager < common::DoubleTime,
                                               RootGraphManagerParameters >
{
public:
    RootGraphManager(
	common::Coordinator < common::DoubleTime >* coordinator,
        const RootGraphManagerParameters& parameters) :
        paradevs::pdevs::GraphManager < common::DoubleTime,
                                        RootGraphManagerParameters >(
                                            coordinator, parameters)
    {
      int index = 0;
      
      for (std::vector < std::vector <
	     std::pair < std::pair < int, int >,
	     std::pair < int, int > > > >::const_iterator it =
	     parameters.parents.begin(); it != parameters.parents.end();
	   ++it) {
	std::stringstream ss;
	ModelProxy* model = 0;
	
	ss << "S_" << index;
	model = new ModelProxy(ss.str(), index + 1, false);
	models.push_back(model);
	add_child(model);
	++index;
      }

      for (std::vector < std::vector <
	     std::pair < std::pair < int, int >,
	     std::pair < int, int > > > >::const_iterator it =
	     parameters.parents.begin(); it != parameters.parents.end();
	   ++it) {
	for (std::vector <
	       std::pair < std::pair < int, int >,
	       std::pair < int, int > > >::const_iterator it2 =
	       it->begin(); it2 != it->end(); ++it2) {
	  std::stringstream out_ss;
	  std::stringstream in_ss;

	  out_ss << "out_" << it2->first.second;
	  if (not models[it2->first.first]->exist_out_port(out_ss.str())) {
	    models[it2->first.first]->add_out_port(out_ss.str());
	  }
	  in_ss << "in_" << it2->first.second;
	  if (not models[it2->second.first]->exist_in_port(in_ss.str())) {
	    models[it2->second.first]->add_in_port(in_ss.str());
	  }
	  if (not exist_link(models[it2->first.first], out_ss.str(),
			     models[it2->second.first], in_ss.str())) {
	    add_link(models[it2->first.first], out_ss.str(),
		     models[it2->second.first], in_ss.str());
	  }
	}
      }
    }
  
    virtual ~RootGraphManager()
    {
        std::for_each(models.begin(), models.end(),
                      std::default_delete < ModelProxy >());
    }

private:
    typedef paradevs::pdevs::mpi::ModelProxy < common::DoubleTime > ModelProxy;
    typedef std::vector < ModelProxy* > ModelProxies;

    ModelProxies models;
};

} } } } // namespace paradevs tests mpi cluster

#endif
