From b2766ecd34e96d64bb11cc94efdd2f081ebd607f Mon Sep 17 00:00:00 2001
From: Martin Hierholzer <martin.hierholzer@desy.de>
Date: Wed, 20 Sep 2017 11:14:19 +0200
Subject: [PATCH] allow creation of a dot graph containing the variable model

---
 include/EntityOwner.h |  6 ++++++
 src/EntityOwner.cc    | 46 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+)

diff --git a/include/EntityOwner.h b/include/EntityOwner.h
index ab55724f..7bee80dc 100644
--- a/include/EntityOwner.h
+++ b/include/EntityOwner.h
@@ -109,6 +109,12 @@ namespace ChimeraTK {
       
       /** Print the full hierarchy to stdout. */
       void dump(const std::string &prefix="") const;
+      
+      /** Create Graphviz dot graph write to file */
+      void dumpGraph(const std::string &fileName="graph.dot") const;
+      
+      /** Create Graphviz dot graph write to stream, excluding the surrounding digraph command */
+      void dumpGraphInternal(std::ostream &stream) const;
 
   protected:
     
diff --git a/src/EntityOwner.cc b/src/EntityOwner.cc
index 3d159581..2dc70103 100644
--- a/src/EntityOwner.cc
+++ b/src/EntityOwner.cc
@@ -146,6 +146,52 @@ namespace ChimeraTK {
 
   /*********************************************************************************************************************/
 
+  void EntityOwner::dumpGraph(const std::string& fileName) const {
+    std::fstream file(fileName, std::ios_base::out);
+    file << "digraph G {" << std::endl;
+    dumpGraphInternal(file);
+    file << "}" << std::endl;
+    file.close();
+    std::cout << "HIER " << fileName << std::endl;
+  }
+
+  /*********************************************************************************************************************/
+
+  void EntityOwner::dumpGraphInternal(std::ostream &stream) const {
+    
+    std::string myDotNode = getQualifiedName();
+    std::replace(myDotNode.begin(), myDotNode.end(), '/', '_');
+    stream << myDotNode << "[label=\"" << getName() << "\"]" << std::endl;
+    
+    for(auto &node : getAccessorList()) {
+      std::string dotNode = node.getQualifiedName();
+      std::replace(dotNode.begin(), dotNode.end(), '/', '_');
+      stream << dotNode << "[label=\"{" << node.getName() << "| {";
+      bool first = true;
+      for(auto tag : node.getTags()) {
+        if(!first) {
+          stream << "|";
+        }
+        else {
+          first = false;
+        }
+        stream << tag;
+      }
+      stream << "}}\", shape=record]" << std::endl;
+      stream << "  " << myDotNode << " -> " << dotNode  << std::endl;
+    }
+
+    for(auto &submodule : getSubmoduleList()) {
+      std::string dotNode = submodule->getQualifiedName();
+      std::replace(dotNode.begin(), dotNode.end(), '/', '_');
+      stream << "  " << myDotNode << " -> " << dotNode << std::endl;
+      submodule->dumpGraphInternal(stream);
+    }
+
+  }
+
+  /*********************************************************************************************************************/
+
   void EntityOwner::addTag(const std::string &tag) {
     for(auto &node : getAccessorList()) node.addTag(tag);
     for(auto &submodule : getSubmoduleList()) submodule->addTag(tag);
-- 
GitLab