From 76ccc349901c91a3203060109aaffbc26c011cd4 Mon Sep 17 00:00:00 2001
From: cmoineau <cyril.moineau@cea.fr>
Date: Tue, 18 Feb 2025 16:28:37 +0000
Subject: [PATCH] Fix aidge_onnx#51, name uniqueness was not checked in the
 graph that was added.

---
 python_binding/graph/pybind_GraphView.cpp |  1 +
 src/graph/GraphView.cpp                   | 13 +++++++------
 src/graph/Node.cpp                        |  1 +
 3 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/python_binding/graph/pybind_GraphView.cpp b/python_binding/graph/pybind_GraphView.cpp
index d1eec2907..31e3a0099 100644
--- a/python_binding/graph/pybind_GraphView.cpp
+++ b/python_binding/graph/pybind_GraphView.cpp
@@ -30,6 +30,7 @@ void init_GraphView(py::module& m) {
           :param path: save location
           :type path: str
           )mydelimiter")
+          .def("set_name", &GraphView::setName, py::arg("name"))
           .def("inputs", (std::vector<std::pair<NodePtr, IOIndex_t>> (GraphView::*)() const) &GraphView::inputs)
           .def("outputs", (std::vector<std::vector<std::pair<NodePtr, IOIndex_t>>> (GraphView::*)() const) &GraphView::outputs)
           .def("in_view", (bool (GraphView::*)(const NodePtr&) const) &GraphView::inView)
diff --git a/src/graph/GraphView.cpp b/src/graph/GraphView.cpp
index d0b539182..37c4e5742 100644
--- a/src/graph/GraphView.cpp
+++ b/src/graph/GraphView.cpp
@@ -808,18 +808,19 @@ bool Aidge::GraphView::add(std::set<std::shared_ptr<Node>> otherNodes, bool incl
   }
 
   bool orderUnicity = true;
-
   // List only the nodes that are not already present in current graph
   std::set<NodePtr> nodesToAdd;
   std::set_difference(otherNodes.begin(), otherNodes.end(), mNodes.begin(), mNodes.end(), std::inserter(nodesToAdd, nodesToAdd.begin()));
 
   // Check no name is common with the name in the current Graph
   for (auto node : nodesToAdd) {
-    if (mNodeRegistry.find(node->name()) != mNodeRegistry.end()){
-      std::string newName = node->createUniqueName(node->name());
-      fmt::print("Warning: node name \"{}\" is a duplicate, renaming to {}.\n", node->name(), newName);
-      node->setName(newName);
-
+    if (mNodeRegistry.find(node->name()) != mNodeRegistry.end()) {
+        std::string newName = node->createUniqueName(node->name());
+        while (mNodeRegistry.find(newName) != mNodeRegistry.end()) {
+            newName = node->createUniqueName(newName + "_1"); 
+        }
+        Log::notice("node name \"{}\" is a duplicate, renaming to {}.\n", node->name(), newName);
+        node->setName(newName);
     }
   }
   // List the nodes to rank, initially all the nodes in the GraphView
diff --git a/src/graph/Node.cpp b/src/graph/Node.cpp
index 0dec30c2f..f791ab31c 100644
--- a/src/graph/Node.cpp
+++ b/src/graph/Node.cpp
@@ -93,6 +93,7 @@ std::string Aidge::Node::createUniqueName(std::string baseName)
         nameAlreadyUsed = false;
         for (auto graphView : views()) {
             if (graphView->inView(newName)) {
+                Log::info("Node::createUniqueName(): name '{}' already used in graph '{}'", newName, graphView->name());
                 nameAlreadyUsed = true;
                 break;
             }
-- 
GitLab