From 499fa143187cae0b67438279190f6bdd88b2b6fa Mon Sep 17 00:00:00 2001
From: Christoph Kampmeyer <christoph.kampmeyer@desy.de>
Date: Wed, 29 Apr 2020 15:58:29 +0200
Subject: [PATCH] wip #107: Detection of underlying instances:

Correctly navigate to parent module via virtual path.
---
 Modules/src/StatusAggregator.cc | 103 +++++++++++++++++---------------
 1 file changed, 55 insertions(+), 48 deletions(-)

diff --git a/Modules/src/StatusAggregator.cc b/Modules/src/StatusAggregator.cc
index e28a5922..a8710fbd 100644
--- a/Modules/src/StatusAggregator.cc
+++ b/Modules/src/StatusAggregator.cc
@@ -3,82 +3,89 @@
 #include <list>
 #include <regex>
 
-namespace ChimeraTK{
+namespace ChimeraTK {
 
-  void StatusAggregator::populateStatusInput(){
-
-    std::cout << "Populating aggregator " << getName() << ", fully qualified name is: "
-              <<  getQualifiedName() <<std::endl;
+  void StatusAggregator::populateStatusInput() {
+    std::cout << "Populating aggregator " << getName() << ", fully qualified name is: " << getQualifiedName()
+              << std::endl;
 
     // This does not work, we need to operate on the virtual hierarchy
     //std::list<Module*> subModuleList{getOwner()->getSubmoduleList()};
 
     // This approach crashes
-//    auto subModuleList = getOwner()->findTag(".*").getSubmoduleList();
-
-//    for(auto module : subModuleList){
+    //    auto subModuleList = getOwner()->findTag(".*").getSubmoduleList();
 
-//      std::cout << "Testing Module " << module->getName() << std::endl;
-//      auto accList{module->getAccessorList()};
-//    }
+    //    for(auto module : subModuleList){
 
+    //      std::cout << "Testing Module " << module->getName() << std::endl;
+    //      auto accList{module->getAccessorList()};
+    //    }
 
     // Another try, virtualise the entire Application
-    auto virtualApplicationHierarchy = Application::getInstance().findTag(".*");
+    auto virtualisedApplication = Application::getInstance().findTag(".*");
     auto virtualPathToThis = getVirtualQualifiedName();
     std::cout << "  ** Fully qualified name in vitual hierarchy: "
-              //<< virtualApplicationHierarchy.getQualifiedName() << std::endl;
               << getVirtualQualifiedName() << std::endl;
 
-    // Split the name into individual modules
-    std::regex pathsepRegex{"/"};
-    std::sregex_token_iterator name{virtualPathToThis.begin(), virtualPathToThis.end(),
-        pathsepRegex, -1}, end;
 
+    // Remove application name and name of leaf node from the path
+    auto pathBegin = virtualPathToThis.find_first_not_of("/" + Application::getInstance().getName() + "/");
+    auto pathEnd = virtualPathToThis.find_last_of("/") - 1;
+
+    auto pathWithoutAppAndLeafNode{virtualPathToThis.substr(pathBegin, pathEnd - pathBegin + 1)};
 
-    ++name; // Leave out first, empty match. FIXME: Should be avoided by regex
-    while(name != end){
-      std::cout << "Current path elem: " << *(name++) << std::endl;
+    std::list<VariableNetworkNode> allAccessors;
+    std::list<Module*> allSubmodules;
+    if(pathWithoutAppAndLeafNode == getName()) {
+      // Path to this module, the parent is the Application
+      allAccessors = virtualisedApplication.getAccessorListRecursive();
+      allSubmodules = virtualisedApplication.getSubmoduleListRecursive();
+
+      std::cout << "Parent is the Application" << std::endl;
     }
+    else {
+      std::cout << "SubPath to this aggregator's parent : " << pathWithoutAppAndLeafNode << std::endl;
+
+      Module& virtualParent =
+          virtualisedApplication.submodule(virtualPathToThis.substr(pathBegin, pathEnd - pathBegin + 1));
 
-    // I can just call submodule instead of the above procedure
-    //Module& thisModule = virtualApplicationHierarchy.submodule(virtualPathToThis);
+      virtualParent.dump();
 
+      allAccessors = virtualParent.getAccessorListRecursive();
+      allSubmodules = virtualParent.getSubmoduleListRecursive();
+    }
 
     // Works, as long as we do not use HIerarchyModifiers other than "none" in the test app
     // Update: This still has problems, because getOwner gives us the "real" C++ Module
     //          We need to do everything on the virtual plane
-    //    auto allAccessors{getOwner()->findTag(".*").getAccessorListRecursive()};
+    //auto allAccessors{virtualParent.getAccessorListRecursive()};
+    //auto allSubmodules{virtualParent.getSubmoduleListRecursive()};
 
-    //    std::cout <<  "  Size of allAccessors: " << allAccessors.size() << std::endl;
+    std::cout << "  Size of allAccessors: " << allAccessors.size() << std::endl;
+    std::cout << "  Size of allSubmodules: " << allSubmodules.size() << std::endl;
 
-    //    for(auto acc : allAccessors){
+    for(auto acc : allAccessors) {
+      if(acc.getDirection().dir == VariableDirection::feeding) {
+        std::cout << "      -- Accessor: " << acc.getName() << " of module: " << acc.getOwningModule()->getName()
+                  << std::endl;
+      }
+    }
 
-    //      if(acc.getDirection().dir == VariableDirection::feeding){
-    //        std::cout << "      -- Accessor: " << acc.getName()
-    //                  << " of module: " << acc.getOwningModule()->getName()
-    //                  << std::endl;
+    //      if(dynamic_cast<StatusMonitor*>(module)){
+    //        std::cout << "  Found Monitor " << module->getName() << std::endl;
     //      }
+    //      else if(dynamic_cast<StatusAggregator*>(module)){
+    //          std::string moduleName = module->getName();
 
-    //    }
-
-
-
-//      if(dynamic_cast<StatusMonitor*>(module)){
-//        std::cout << "  Found Monitor " << module->getName() << std::endl;
-//      }
-//      else if(dynamic_cast<StatusAggregator*>(module)){
-//          std::string moduleName = module->getName();
+    //          std::cout << "Found Aggregator " << moduleName << std::endl;
 
-//          std::cout << "Found Aggregator " << moduleName << std::endl;
-
-//          statusInput.emplace_back(this, moduleName, "", "");
-//      }
-//      else if(dynamic_cast<ModuleGroup*>(module)){
-//        std::cout << "Found ModuleGroup " << module->getName() << std::endl;
-//      }
-//      else{}
-//    }
+    //          statusInput.emplace_back(this, moduleName, "", "");
+    //      }
+    //      else if(dynamic_cast<ModuleGroup*>(module)){
+    //        std::cout << "Found ModuleGroup " << module->getName() << std::endl;
+    //      }
+    //      else{}
+    //    }
     std::cout << std::endl << std::endl;
   } // poplateStatusInput()*/
-}
+} // namespace ChimeraTK
-- 
GitLab