浏览代码

Add to_string method to convert hierarchical model to string

Eric Ramat 10 年之前
父节点
当前提交
5da560ce81

+ 10 - 1
src/common/Coordinator.hpp

@@ -33,7 +33,7 @@
 #include <common/Model.hpp>
 #include <common/Node.hpp>
 
-#include <iostream>
+#include <sstream>
 
 namespace paradevs { namespace common {
 
@@ -53,6 +53,15 @@ public :
     virtual bool is_atomic() const
     { return false; }
 
+    virtual std::string to_string(int /* level */) const
+    {
+        std::ostringstream ss;
+
+        ss << "Coordinator "
+           << Coordinator < Time, SchedulerHandle >::get_name();
+        return ss.str();
+    }
+
 // DEVS methods
     virtual void observation(std::ostream& file) const =0;
     virtual void output(typename Time::type t) =0;

+ 13 - 12
src/common/Links.hpp

@@ -28,6 +28,7 @@
 #define COMMON_LINKS 1
 
 #include <common/Node.hpp>
+#include <common/utils/String.hpp>
 
 #include <map>
 #include <sstream>
@@ -44,8 +45,9 @@ class Links : public std::multimap < Node < Time, SchedulerHandle >,
 public:
 
     typedef std::pair <
-    typename Links < Time, SchedulerHandle >::const_iterator,
-    typename Links < Time, SchedulerHandle >::const_iterator > Result;
+        typename Links < Time, SchedulerHandle >::const_iterator,
+        typename Links < Time, SchedulerHandle >::const_iterator
+    > Result;
 
     Links()
     { }
@@ -96,22 +98,21 @@ public:
                                        out_model, out_port_name));
     }
 
-    std::string to_string() const
+    std::string to_string(int level = 0) const
     {
         std::stringstream ss;
 
-        ss << "Graph = { ";
-        for (typename Node < Time, SchedulerHandle >::const_iterator it =
-                 Node < Time, SchedulerHandle >::begin();
-             it != Node < Time, SchedulerHandle >::end(); ++it) {
-            ss << "(" << it->first.get_model()->get_name() << ":"
+        ss << common::spaces(level * 2) << "Links:" << std::endl;
+        for (typename Links < Time, SchedulerHandle >::const_iterator it =
+                 Links < Time, SchedulerHandle >::begin();
+             it != Links < Time, SchedulerHandle >::end(); ++it) {
+            ss << common::spaces((level + 1) * 2)
+               << it->first.get_model()->get_name() << "::"
                << it->first.get_port_name()
                << " -> "
-               << it->second.get_model()->get_name() << ":"
-               << it->second.get_port_name()
-               << ") ";
+               << it->second.get_model()->get_name() << "::"
+               << it->second.get_port_name() << std::endl;
         }
-        ss << "}";
         return ss.str();
     }
 };

+ 2 - 0
src/common/Model.hpp

@@ -120,6 +120,8 @@ public:
     void set_parent(Model < Time, SchedulerHandle >* parent)
     { _parent = parent; }
 
+    virtual std::string to_string(int /* level */) const =0;
+
     // event
     void add_event(const common::ExternalEvent < Time, SchedulerHandle >&
                    message)

+ 1 - 1
src/common/Node.hpp

@@ -73,7 +73,7 @@ public :
 
 private :
     Model < Time, SchedulerHandle >* _model;
-    std::string     _port_name;
+    std::string                      _port_name;
 };
 
 } } // namespace paradevs common

+ 9 - 0
src/common/RootCoordinator.hpp

@@ -29,6 +29,7 @@
 
 #include <common/Parameters.hpp>
 
+#include <sstream>
 #include <string>
 
 namespace paradevs { namespace common {
@@ -74,6 +75,14 @@ public :
         }
     }
 
+    std::string to_string() const
+    {
+        std::ostringstream ss;
+
+        ss << _root.to_string(0);
+        return ss.str();
+    }
+
 private :
     Coordinator         _root;
     typename Time::type _t_max;

+ 10 - 0
src/common/Simulator.hpp

@@ -29,6 +29,8 @@
 
 #include <common/Model.hpp>
 
+#include <sstream>
+
 namespace paradevs { namespace common {
 
 template < class Time, class SchedulerHandle >
@@ -43,6 +45,14 @@ public :
     virtual bool is_atomic() const
     { return true; }
 
+    virtual std::string to_string(int /* level */) const
+    {
+        std::ostringstream ss;
+
+        ss << "Simulator " << Simulator < Time, SchedulerHandle >::get_name();
+        return ss.str();
+    }
+
 // DEVS methods
     virtual void observation(std::ostream& file) const =0;
     virtual void output(typename Time::type t) =0;

+ 1 - 1
src/common/utils/CMakeLists.txt

@@ -10,7 +10,7 @@ LINK_DIRECTORIES(
   ${LIBXML_LIBRARY_DIR}
   ${Boost_LIBRARY_DIRS})
 
-SET(COMMON_UTILS_HPP Trace.hpp)
+SET(COMMON_UTILS_HPP String.hpp Trace.hpp)
 
 INSTALL(FILES ${COMMON_UTILS_HPP} DESTINATION
   ${PARADEVS_INCLUDE_DIRS}/common/utils)

+ 46 - 0
src/common/utils/String.hpp

@@ -0,0 +1,46 @@
+/**
+ * @file String.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 COMMON_STRING
+#define COMMON_STRING 1
+
+#include <string>
+
+namespace paradevs { namespace common {
+
+std::string spaces(unsigned int number)
+{
+    std::string str;
+
+    for (unsigned int i = 0; i < number; ++i) {
+        str += " ";
+    }
+    return str;
+}
+
+} }  // namespace paradevs common
+
+#endif

+ 11 - 0
src/kernel/pdevs/Coordinator.hpp

@@ -29,6 +29,7 @@
 
 #include <common/Coordinator.hpp>
 #include <common/Parameters.hpp>
+#include <common/utils/String.hpp>
 #include <common/utils/Trace.hpp>
 
 #include <cassert>
@@ -61,6 +62,16 @@ public:
     virtual ~Coordinator()
     { }
 
+    virtual std::string to_string(int level) const
+    {
+        std::ostringstream ss;
+
+        ss << common::spaces(level * 2) << "p-devs coordinator \""
+           << type::get_name() << "\":" << std::endl;
+        ss << _graph_manager.to_string(level + 1);
+        return ss.str();
+    }
+
     typename Time::type start(typename Time::type t)
     {
 

+ 15 - 0
src/kernel/pdevs/GraphManager.hpp

@@ -31,6 +31,9 @@
 #include <common/Links.hpp>
 #include <common/Model.hpp>
 #include <common/Parameters.hpp>
+#include <common/utils/String.hpp>
+
+#include <sstream>
 
 namespace paradevs { namespace pdevs {
 
@@ -140,6 +143,18 @@ public:
         }
     }
 
+    virtual std::string to_string(int level) const
+    {
+        std::ostringstream ss;
+
+        ss << common::spaces(level * 2) << "Childs:" << std::endl;
+        for (auto & child : _child_list) {
+            ss << child->to_string(level + 1);
+        }
+        ss << _link_list.to_string(level);
+        return ss.str();
+    }
+
 private:
     common::Links < Time, SchedulerHandle >        _link_list;
     common::Models < Time, SchedulerHandle >       _child_list;

+ 10 - 0
src/kernel/pdevs/Simulator.hpp

@@ -30,6 +30,7 @@
 #include <common/Coordinator.hpp>
 #include <common/Parameters.hpp>
 #include <common/Simulator.hpp>
+#include <common/utils/String.hpp>
 #include <common/utils/Trace.hpp>
 
 #include <cassert>
@@ -51,6 +52,15 @@ public :
     ~Simulator()
     { }
 
+    virtual std::string to_string(int level) const
+    {
+        std::ostringstream ss;
+
+        ss << common::spaces(level * 2) << "p-devs simulator \""
+           << type::get_name() << "\""<< std::endl;
+        return ss.str();
+    }
+
 /*************************************************
  * when i-message(t)
  *   tl = t - e

+ 2 - 0
src/tests/boost_graph/tests.cpp

@@ -89,6 +89,8 @@ void partitionning_test()
             paradevs::common::NoParameters >
         > rc(0, 100000, "root", NoParameters(), NoParameters());
 
+    std::cout << rc.to_string() << std::endl;
+
     rc.run();
 }