123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394 |
- /**
- * @file tests/boost_graph/graph_manager.hpp
- * @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 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_BOOST_GRAPH_GRAPH_MANAGER_HPP
- #define __TESTS_BOOST_GRAPH_GRAPH_MANAGER_HPP 1
- #include <common/scheduler/HeapScheduler.hpp>
- #include <common/scheduler/VectorScheduler.hpp>
- #include <kernel/pdevs/Coordinator.hpp>
- #include <kernel/pdevs/GraphManager.hpp>
- #include <kernel/pdevs/Simulator.hpp>
- #include <tests/boost_graph/models.hpp>
- namespace paradevs { namespace tests { namespace boost_graph {
- struct SchedulerHandle;
- typedef typename paradevs::common::scheduler::HeapScheduler <
- common::DoubleTime, SchedulerHandle >::type SchedulerType;
- struct SchedulerHandle
- {
- SchedulerHandle()
- { }
- SchedulerHandle(const SchedulerType::handle_type& handle)
- : _handle(handle)
- { }
- const SchedulerHandle& handle() const
- { return *this; }
- void handle(const SchedulerHandle& handle)
- { _handle = handle._handle; }
- SchedulerType::handle_type _handle;
- };
- struct GraphParameters
- {
- OrientedGraph _graph;
- InputEdges _input_edges;
- OutputEdges _output_edges;
- GraphParameters(const OrientedGraph& graph,
- const InputEdges& input_edges,
- const OutputEdges& output_edges) :
- _graph(graph), _input_edges(input_edges), _output_edges(output_edges)
- { }
- };
- template < class SchedulerHandle, class Parameters >
- class FlatGraphManager :
- public paradevs::pdevs::GraphManager < common::DoubleTime,
- SchedulerHandle,
- Parameters >
- {
- public:
- FlatGraphManager(common::Coordinator < common::DoubleTime,
- SchedulerHandle >* coordinator,
- const Parameters& parameters) :
- paradevs::pdevs::GraphManager < common::DoubleTime, SchedulerHandle,
- Parameters >(
- coordinator, parameters)
- { }
- virtual ~FlatGraphManager()
- {
- for (typename TopSimulators::const_iterator it =
- _top_simulators.begin(); it != _top_simulators.end();
- ++it) {
- delete it->second;
- }
- for (typename NormalSimulators::const_iterator it =
- _normal_simulators.begin(); it != _normal_simulators.end();
- ++it) {
- delete it->second;
- }
- }
- void build_flat_graph(const OrientedGraph& g, const InputEdges& inputs)
- {
- OrientedGraph::vertex_iterator vertexIt, vertexEnd;
- boost::tie(vertexIt, vertexEnd) = boost::vertices(g);
- for (; vertexIt != vertexEnd; ++vertexIt)
- {
- std::ostringstream ss;
- ss << "a" << g[*vertexIt]._index;
- switch (g[*vertexIt]._type) {
- case TOP_PIXEL:
- _top_simulators[g[*vertexIt]._index] =
- new pdevs::Simulator <
- common::DoubleTime, TopPixel < SchedulerHandle >,
- SchedulerHandle,
- TopPixelParameters >(ss.str(), TopPixelParameters());
- _top_simulators[g[*vertexIt]._index]->add_out_port("out");
- FlatGraphManager < SchedulerHandle, Parameters >::add_child(
- _top_simulators[g[*vertexIt]._index]);
- break;
- case NORMAL_PIXEL:
- unsigned int n = 0;
- OrientedGraph::adjacency_iterator neighbourIt, neighbourEnd;
- boost::tie(neighbourIt, neighbourEnd) =
- boost::adjacent_vertices(*vertexIt, g);
- for (; neighbourIt != neighbourEnd; ++neighbourIt) {
- ++n;
- }
- for (InputEdges::const_iterator it = inputs.begin();
- it != inputs.end(); ++it) {
- if (g[*vertexIt]._index == it->second) {
- ++n;
- }
- }
- _normal_simulators[g[*vertexIt]._index] =
- new pdevs::Simulator <
- common::DoubleTime, NormalPixel < SchedulerHandle >,
- SchedulerHandle, NormalPixelParameters >(
- ss.str(), NormalPixelParameters(n));
- _normal_simulators[g[*vertexIt]._index]->add_in_port("in");
- _normal_simulators[g[*vertexIt]._index]->add_out_port("out");
- FlatGraphManager < SchedulerHandle, Parameters >::add_child(
- _normal_simulators[g[*vertexIt]._index]);
- break;
- };
- }
- std::cout << FlatGraphManager <
- SchedulerHandle,
- Parameters >::get_coordinator()->get_name() << ":" << std::endl;
- boost::tie(vertexIt, vertexEnd) = boost::vertices(g);
- for (; vertexIt != vertexEnd; ++vertexIt)
- {
- OrientedGraph::adjacency_iterator neighbourIt, neighbourEnd;
- boost::tie(neighbourIt, neighbourEnd) =
- boost::adjacent_vertices(*vertexIt, g);
- for (; neighbourIt != neighbourEnd; ++neighbourIt) {
- paradevs::common::Model < common::DoubleTime,
- SchedulerHandle >* a = 0;
- paradevs::common::Model < common::DoubleTime,
- SchedulerHandle >* b = 0;
- if (g[*vertexIt]._type == TOP_PIXEL) {
- a = _top_simulators[g[*vertexIt]._index];
- } else {
- a = _normal_simulators[g[*vertexIt]._index];
- }
- if (g[*neighbourIt]._type == TOP_PIXEL) {
- b = _top_simulators[g[*neighbourIt]._index];
- } else {
- b = _normal_simulators[g[*neighbourIt]._index];
- }
- FlatGraphManager < SchedulerHandle,
- Parameters >::add_link(a, "out",
- b, "in");
- std::cout << " " << a->get_name() << "::out -> "
- << b->get_name() << "::in" << std::endl;
- }
- }
- }
- protected:
- typedef std::map < int, pdevs::Simulator <
- common::DoubleTime,
- TopPixel < SchedulerHandle >,
- SchedulerHandle,
- TopPixelParameters >* > TopSimulators;
- typedef std::map < int, pdevs::Simulator <
- common::DoubleTime,
- NormalPixel < SchedulerHandle >,
- SchedulerHandle,
- NormalPixelParameters >* > NormalSimulators;
- TopSimulators _top_simulators;
- NormalSimulators _normal_simulators;
- };
- template < class SchedulerHandle >
- class BuiltFlatGraphManager :
- public FlatGraphManager < SchedulerHandle, GraphParameters >
- {
- public:
- BuiltFlatGraphManager(
- common::Coordinator < common::DoubleTime,
- SchedulerHandle >* coordinator,
- const GraphParameters& parameters) :
- FlatGraphManager < SchedulerHandle, GraphParameters >(
- coordinator, parameters)
- {
- BuiltFlatGraphManager < SchedulerHandle >::build_flat_graph(
- parameters._graph, parameters._input_edges);
- // input
- for (Edges::const_iterator it = parameters._input_edges.begin();
- it != parameters._input_edges.end(); ++it) {
- std::ostringstream ss_in;
- ss_in << "in_" << it->first;
- if (not coordinator->exist_in_port(ss_in.str())) {
- coordinator->add_in_port(ss_in.str());
- }
- BuiltFlatGraphManager < SchedulerHandle>::add_link(
- coordinator, ss_in.str(),
- BuiltFlatGraphManager <
- SchedulerHandle >::_normal_simulators[it->second], "in");
- std::cout << coordinator->get_name() << "::" << ss_in.str()
- << " -> "
- << BuiltFlatGraphManager <
- SchedulerHandle >::_normal_simulators[
- it->second]->get_name() << "::in" << std::endl;
- }
- // output
- for (Edges::const_iterator it = parameters._output_edges.begin();
- it != parameters._output_edges.end(); ++it) {
- std::ostringstream ss_out;
- ss_out << "out_" << it->first;
- if (not coordinator->exist_out_port(ss_out.str())) {
- coordinator->add_out_port(ss_out.str());
- }
- BuiltFlatGraphManager < SchedulerHandle>::add_link(
- BuiltFlatGraphManager <
- SchedulerHandle >::_normal_simulators[it->first], "out",
- coordinator, ss_out.str());
- std::cout << BuiltFlatGraphManager <
- SchedulerHandle >::_normal_simulators[it->first]->get_name()
- << "::out -> " << coordinator->get_name()
- << "::" << ss_out.str() << std::endl;
- }
- }
- virtual ~BuiltFlatGraphManager()
- { }
- };
- template < class SchedulerHandle, class GraphBuilder >
- class InBuildFlatGraphManager :
- public FlatGraphManager < SchedulerHandle,
- paradevs::common::NoParameters >
- {
- public:
- InBuildFlatGraphManager(
- common::Coordinator < common::DoubleTime,
- SchedulerHandle >* coordinator,
- const paradevs::common::NoParameters& parameters) :
- FlatGraphManager < SchedulerHandle, paradevs::common::NoParameters >(
- coordinator, parameters)
- {
- GraphBuilder builder;
- OrientedGraphs graphs;
- InputEdgeList input_edges;
- OutputEdgeList output_edges;
- Connections parent_connections;
- builder.build(graphs, input_edges, output_edges, parent_connections);
- InBuildFlatGraphManager < SchedulerHandle,
- GraphBuilder >::build_flat_graph(
- graphs.front(), InputEdges());
- }
- virtual ~InBuildFlatGraphManager()
- { }
- };
- template < class SchedulerHandle, class GraphBuilder >
- class HierarchicalGraphManager :
- public paradevs::pdevs::GraphManager < common::DoubleTime,
- SchedulerHandle,
- paradevs::common::NoParameters >
- {
- public:
- HierarchicalGraphManager(
- common::Coordinator < common::DoubleTime,
- SchedulerHandle >* coordinator,
- const paradevs::common::NoParameters& parameters) :
- paradevs::pdevs::GraphManager < common::DoubleTime, SchedulerHandle,
- paradevs::common::NoParameters >(
- coordinator, parameters)
- {
- GraphBuilder graph_builder;
- OrientedGraphs graphs;
- InputEdgeList input_edges;
- OutputEdgeList output_edges;
- Connections parent_connections;
- graph_builder.build(graphs, input_edges, output_edges,
- parent_connections);
- // build coordinators (graphs)
- for (unsigned int i = 0; i < graphs.size(); ++i) {
- Coordinator* coordinator = 0;
- std::ostringstream ss;
- ss << "S" << (i + 1);
- coordinator =
- new Coordinator(ss.str(), paradevs::common::NoParameters(),
- GraphParameters(graphs[i], input_edges[i],
- output_edges[i]));
- _coordinators.push_back(coordinator);
- HierarchicalGraphManager < SchedulerHandle,
- GraphBuilder >::add_child(coordinator);
- }
- // builds internal connections (edges)
- std::cout << "parent connections:" << std::endl;
- for (unsigned int i = 0; i < parent_connections.size(); i++) {
- std::cout << " (" << parent_connections.at(i).first.first << ","
- << parent_connections.at(i).first.second << ") -> ("
- << parent_connections.at(i).second.first << ","
- << parent_connections.at(i).second.second << ")"
- << std::endl;
- }
- for (Connections::const_iterator it = parent_connections.begin();
- it != parent_connections.end(); ++it) {
- const Connection& connection = *it;
- std::ostringstream ss_out;
- std::ostringstream ss_in;
- ss_out << "out_" << connection.first.second;
- ss_in << "in_" << connection.first.second;
- HierarchicalGraphManager <
- SchedulerHandle, GraphBuilder >::add_link(
- _coordinators[connection.first.first - 1], ss_out.str(),
- _coordinators[connection.second.first - 1], ss_in.str());
- std::cout << _coordinators[connection.first.first - 1]->get_name()
- << "::" << ss_out.str() << " -> "
- << _coordinators[connection.second.first - 1]->get_name()
- << "::" << ss_in.str() << std::endl;
- }
- }
- virtual ~HierarchicalGraphManager()
- {
- for (typename Coordinators::const_iterator it = _coordinators.begin();
- it != _coordinators.end(); ++it) {
- delete *it;
- }
- }
- private:
- typedef paradevs::pdevs::Coordinator <
- common::DoubleTime,
- SchedulerType,
- SchedulerHandle,
- BuiltFlatGraphManager < SchedulerHandle >,
- common::NoParameters,
- GraphParameters > Coordinator;
- typedef std::vector < Coordinator* > Coordinators;
- Coordinators _coordinators;
- };
- } } } // namespace paradevs tests boost_graph
- #endif
|