Skip to content
Snippets Groups Projects
Commit c916dde6 authored by Cyril Moineau's avatar Cyril Moineau Committed by Cyril Moineau
Browse files

Fix duplicate node name wen adding a GraphView to an other.

Extra:
- add Node::createUniqueName
- GraphView::inView with str arg
parent 2a2046a7
No related branches found
No related tags found
2 merge requests!152Update Aidge export to take a graph view has an argument instead of a...,!133Fix duplicate name issue when adding a GraphView to an other
......@@ -103,6 +103,13 @@ public:
*/
bool inView(const NodePtr& nodePtr) const;
/**
* Check that a node is in the current GraphView.
* @param nodeName Name of the node to test the existence of.
* @return true if the GraphView contains a Node with the name ``nodeName``.
*/
bool inView(const std::string& nodeName) const;
inline NodePtr rootNode() const noexcept {
return mRootNode;
}
......
......@@ -100,6 +100,17 @@ public:
*/
void setName(const std::string &name);
/**
* @brief Given the parameter name generate a new name which is unique
* in all the GraphView which contains this node.
* To generate the new name the method is called recursively and append
* the caracter ``_``.
* If no duplicate return name, this is the exit condition.
* @param name Base name to make unique.
* @return A unique name in all the GraphView which contains this one.
*/
std::string createUniqueName(std::string name);
/**
* @brief Type of the node.
* @return std::string
......
......@@ -74,6 +74,9 @@ bool Aidge::GraphView::inView(const std::shared_ptr<Aidge::Node>& nodePtr) const
return mNodes.find(nodePtr) != mNodes.cend();
}
bool Aidge::GraphView::inView(const std::string& nodeName) const {
return mNodeRegistry.find(nodeName) != mNodeRegistry.end();
}
void Aidge::GraphView::save(const std::string& path, bool verbose, bool showProducers) const {
auto fp = std::unique_ptr<FILE, decltype(&std::fclose)>(std::fopen((path + ".mmd").c_str(), "w"), &std::fclose);
......@@ -652,6 +655,14 @@ bool Aidge::GraphView::add(std::set<std::shared_ptr<Node>> otherNodes, bool incl
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());
node->setName(newName);
fmt::print("Warning: node name \"{}\" is a duplicate, renaming to {}.\n", node->name(), newName);
}
}
// List the nodes to rank, initially all the nodes in the GraphView
std::set<NodePtr> nodesToRank(mNodes);
nodesToRank.insert(nodesToAdd.begin(), nodesToAdd.end());
......@@ -749,7 +760,7 @@ bool Aidge::GraphView::add(std::pair<NodePtr, std::set<NodePtr>> nodes, bool inc
bool Aidge::GraphView::add(std::shared_ptr<GraphView> graph) {
// set the rootNode to the other graphView rootNode if no rootNode yet
mRootNode = mRootNode ? mRootNode : graph->rootNode();
return add(graph->getNodes(), false);
return add(graph->getNodes(), true);
}
void Aidge::GraphView::addChild(std::shared_ptr<Node> toOtherNode,
......
......@@ -61,6 +61,16 @@ void Aidge::Node::setName(const std::string& name) {
mName = name;
}
std::string Aidge::Node::createUniqueName(std::string name){
for (auto graphView : views()){
if (graphView->inView(name)){
return createUniqueName(name.append("_"));
}else{
return name;
}
}
}
///////////////////////////////////////////////////////
// OPERATORS
///////////////////////////////////////////////////////
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment