From 47a418ab162bf7ecab5c94ec9a10efbc4f72a4c2 Mon Sep 17 00:00:00 2001
From: Florent Lamiraux <florent@laas.fr>
Date: Tue, 21 Apr 2015 11:50:30 +0200
Subject: [PATCH] Add name in constructors of GraphComponent.

  - name setter is now deprecated.
---
 include/hpp/manipulation/graph/edge.hh        | 22 +++++++---
 .../hpp/manipulation/graph/graph-component.hh |  4 +-
 include/hpp/manipulation/graph/graph.hh       |  6 +--
 .../hpp/manipulation/graph/node-selector.hh   |  6 +--
 include/hpp/manipulation/graph/node.hh        | 16 ++++---
 src/graph/edge.cc                             | 43 +++++++++++--------
 src/graph/graph.cc                            |  8 ++--
 src/graph/node-selector.cc                    |  8 ++--
 src/graph/node.cc                             | 14 +++---
 tests/test-constraintgraph.cc                 | 17 ++++----
 10 files changed, 86 insertions(+), 58 deletions(-)

diff --git a/include/hpp/manipulation/graph/edge.hh b/include/hpp/manipulation/graph/edge.hh
index 2e64c206..cfa46322 100644
--- a/include/hpp/manipulation/graph/edge.hh
+++ b/include/hpp/manipulation/graph/edge.hh
@@ -63,7 +63,10 @@ namespace hpp {
           ~Edge ();
 
           /// Create a new empty Edge.
-          static EdgePtr_t create (const GraphWkPtr_t& graph, const NodeWkPtr_t& from, const NodeWkPtr_t& to);
+          static EdgePtr_t create (const std::string& name,
+				   const GraphWkPtr_t& graph,
+				   const NodeWkPtr_t& from,
+				   const NodeWkPtr_t& to);
 
           virtual bool applyConstraints (core::NodePtr_t nnear, ConfigurationOut_t q) const;
 
@@ -99,7 +102,7 @@ namespace hpp {
               const NodeWkPtr_t& to);
 
           /// Constructor
-          Edge();
+          Edge (const std::string& name);
 
           /// Constraint to project onto the same leaf as config.
           /// \return The initialized projector.
@@ -168,7 +171,10 @@ namespace hpp {
       {
         public:
           /// Create a new WaypointEdge.
-          static WaypointEdgePtr_t create (const GraphWkPtr_t& graph, const NodeWkPtr_t& from, const NodeWkPtr_t& to);
+	static WaypointEdgePtr_t create (const std::string& name,
+					 const GraphWkPtr_t& graph,
+					 const NodeWkPtr_t& from,
+					 const NodeWkPtr_t& to);
 
           virtual bool build (core::PathPtr_t& path, ConfigurationIn_t q1, ConfigurationIn_t q2, const core::WeighedDistance& d) const;
 
@@ -194,6 +200,9 @@ namespace hpp {
           NodePtr_t node () const;
 
         protected:
+	  WaypointEdge (const std::string& name) : Edge (name)
+	    {
+	    }
           /// Initialization of the object.
           void init (const EdgeWkPtr_t& weak, const GraphWkPtr_t& graph, const NodeWkPtr_t& from,
               const NodeWkPtr_t& to);
@@ -215,7 +224,10 @@ namespace hpp {
           ~LevelSetEdge ();
 
           /// Create a new LevelSetEdge.
-          static LevelSetEdgePtr_t create (const GraphWkPtr_t& graph, const NodeWkPtr_t& from, const NodeWkPtr_t& to);
+          static LevelSetEdgePtr_t create (const std::string& name,
+					   const GraphWkPtr_t& graph,
+					   const NodeWkPtr_t& from,
+					   const NodeWkPtr_t& to);
 
           virtual bool applyConstraints (ConfigurationIn_t qoffset, ConfigurationOut_t q) const;
 
@@ -240,7 +252,7 @@ namespace hpp {
           void init (const EdgeWkPtr_t& weak, const GraphWkPtr_t& graph, const NodeWkPtr_t& from,
               const NodeWkPtr_t& to);
 
-          LevelSetEdge ();
+	  LevelSetEdge (const std::string& name);
 
           /// Print the object in a stream.
           virtual std::ostream& print (std::ostream& os) const;
diff --git a/include/hpp/manipulation/graph/graph-component.hh b/include/hpp/manipulation/graph/graph-component.hh
index 86b48bce..264d6572 100644
--- a/include/hpp/manipulation/graph/graph-component.hh
+++ b/include/hpp/manipulation/graph/graph-component.hh
@@ -45,7 +45,7 @@ namespace hpp {
           const std::string& name() const;
 
           /// Set the component name.
-          void name(const std::string& name);
+          void name(const std::string& name) HPP_MANIPULATION_DEPRECATED;
 
           /// Get the component by its ID. The validity of the GraphComponent
           /// is not checked.
@@ -94,7 +94,7 @@ namespace hpp {
           /// Initialize the component
           void init (const GraphComponentWkPtr_t& weak);
 
-          GraphComponent() : id_(-1)
+          GraphComponent(const std::string& name) : name_ (name), id_(-1)
           {}
 
           /// Stores the numerical constraints.
diff --git a/include/hpp/manipulation/graph/graph.hh b/include/hpp/manipulation/graph/graph.hh
index 4b1c8bf5..cb09de29 100644
--- a/include/hpp/manipulation/graph/graph.hh
+++ b/include/hpp/manipulation/graph/graph.hh
@@ -43,10 +43,10 @@ namespace hpp {
       {
         public:
           /// Create a new Graph.
-          static GraphPtr_t create(DevicePtr_t robot);
+	static GraphPtr_t create(const std::string& name, DevicePtr_t robot);
 
           /// Create and insert a NodeSelector inside the graph.
-          NodeSelectorPtr_t createNodeSelector ();
+          NodeSelectorPtr_t createNodeSelector (const std::string& name);
 
           /// Returns the states of a configuration.
           NodePtr_t getNode (ConfigurationIn_t config) const;
@@ -107,7 +107,7 @@ namespace hpp {
           void init (const GraphWkPtr_t& weak, DevicePtr_t robot);
 
           /// Constructor
-          Graph ()
+          Graph (const std::string& name) : GraphComponent (name)
           {}
 
           /// Print the object in a stream.
diff --git a/include/hpp/manipulation/graph/node-selector.hh b/include/hpp/manipulation/graph/node-selector.hh
index bcb02d8c..4ae71187 100644
--- a/include/hpp/manipulation/graph/node-selector.hh
+++ b/include/hpp/manipulation/graph/node-selector.hh
@@ -31,10 +31,10 @@ namespace hpp {
       {
         public:
           /// Create a new NodeSelector.
-          static NodeSelectorPtr_t create();
+          static NodeSelectorPtr_t create(const std::string& name);
 
           /// Create an empty node
-          NodePtr_t createNode ();
+          NodePtr_t createNode (const std::string& name);
 
           /// Returns the state of a configuration.
           NodePtr_t getNode(ConfigurationIn_t config) const;
@@ -65,7 +65,7 @@ namespace hpp {
           void init (const NodeSelectorPtr_t& weak);
 
           /// Constructor
-          NodeSelector ()
+          NodeSelector (const std::string& name) : GraphComponent (name)
           {}
 
           /// Print the object in a stream.
diff --git a/include/hpp/manipulation/graph/node.hh b/include/hpp/manipulation/graph/node.hh
index e7644c3d..4213e6a9 100644
--- a/include/hpp/manipulation/graph/node.hh
+++ b/include/hpp/manipulation/graph/node.hh
@@ -43,17 +43,21 @@ namespace hpp {
       class HPP_MANIPULATION_DLLAPI Node : public GraphComponent
       {
         public:
+	typedef boost::function < EdgePtr_t
+				  (const std::string&, const GraphWkPtr_t&,
+				   const NodeWkPtr_t&, const NodeWkPtr_t&) >
+	EdgeFactory;
           /// Destructor
           ~Node ();
 
           /// Create a new node.
-          static NodePtr_t create ();
+          static NodePtr_t create (const std::string& name);
 
           /// Create a link from this node to the given node.
-          EdgePtr_t linkTo(const NodePtr_t& to, const Weight_t& w = 1,
-              const bool& isInNodeFrom = false,
-              boost::function < EdgePtr_t (const GraphWkPtr_t&, const NodeWkPtr_t&, const NodeWkPtr_t&) > create
-              = Edge::create);
+          EdgePtr_t linkTo (const std::string& name, const NodePtr_t& to,
+			    const Weight_t& w = 1,
+			    const bool& isInNodeFrom = false,
+			    EdgeFactory create = Edge::create);
 
           /// Check whether the configuration is in this state.
           /// \code
@@ -130,7 +134,7 @@ namespace hpp {
           void init (const NodeWkPtr_t& self);
 
           /// Constructor
-          Node();
+          Node(const std::string& name);
 
           /// Print the object in a stream.
           std::ostream& print (std::ostream& os) const;
diff --git a/src/graph/edge.cc b/src/graph/edge.cc
index 2230710f..e1e1fb92 100644
--- a/src/graph/edge.cc
+++ b/src/graph/edge.cc
@@ -31,8 +31,9 @@
 namespace hpp {
   namespace manipulation {
     namespace graph {
-      Edge::Edge () : pathConstraints_ (new Constraint_t()),
-      configConstraints_ (new Constraint_t())
+      Edge::Edge (const std::string& name) :
+	GraphComponent (name), pathConstraints_ (new Constraint_t()),
+	configConstraints_ (new Constraint_t())
       {}
 
       Edge::~Edge ()
@@ -57,9 +58,11 @@ namespace hpp {
         else return to ();
       }
 
-      EdgePtr_t Edge::create (const GraphWkPtr_t& graph, const NodeWkPtr_t& from, const NodeWkPtr_t& to)
+      EdgePtr_t Edge::create (const std::string& name,
+			      const GraphWkPtr_t& graph,
+			      const NodeWkPtr_t& from, const NodeWkPtr_t& to)
       {
-        Edge* ptr = new Edge;
+        Edge* ptr = new Edge (name);
         EdgePtr_t shPtr (ptr);
         ptr->init(shPtr, graph, from, to);
         return shPtr;
@@ -188,9 +191,11 @@ namespace hpp {
         return false;
       }
 
-      WaypointEdgePtr_t WaypointEdge::create (const GraphWkPtr_t& graph, const NodeWkPtr_t& from, const NodeWkPtr_t& to)
+      WaypointEdgePtr_t WaypointEdge::create
+      (const std::string& name, const GraphWkPtr_t& graph,
+       const NodeWkPtr_t& from, const NodeWkPtr_t& to)
       {
-        WaypointEdge* ptr = new WaypointEdge;
+        WaypointEdge* ptr = new WaypointEdge (name);
         WaypointEdgePtr_t shPtr (ptr);
         ptr->init(shPtr, graph, from, to);
         return shPtr;
@@ -242,24 +247,23 @@ namespace hpp {
 
       void WaypointEdge::createWaypoint (const unsigned d, const std::string& bname)
       {
-        NodePtr_t node = Node::create ();
-        node->parentGraph(graph_);
         std::ostringstream ss;
         ss << bname << "_n" << d;
-        node->name (ss.str());
+        NodePtr_t node = Node::create (ss.str());
+        node->parentGraph(graph_);
         EdgePtr_t edge;
+        ss.str (std::string ()); ss.clear ();
+        ss << bname << "_e" << d;
         if (d == 0) {
-          edge = Edge::create (graph_, from (), node);
+          edge = Edge::create (ss.str (), graph_, from (), node);
           edge->isInNodeFrom (isInNodeFrom ());
         } else {
-          WaypointEdgePtr_t we = WaypointEdge::create (graph_, from (), node);
+          WaypointEdgePtr_t we = WaypointEdge::create (ss.str (), graph_,
+						       from (), node);
           we->createWaypoint (d-1, bname);
           edge = we;
           edge->isInNodeFrom (isInNodeFrom ());
         }
-        ss.str (std::string ()); ss.clear ();
-        ss << bname << "_e" << d;
-        edge->name (ss.str ());
         waypoint_ = Waypoint (edge, node);
         config_ = Configuration_t(graph_.lock ()->robot ()->configSize ());
         result_ = Configuration_t(graph_.lock ()->robot ()->configSize ());
@@ -388,9 +392,11 @@ namespace hpp {
         Edge::init (weak, graph, from, to);
       }
 
-      LevelSetEdgePtr_t LevelSetEdge::create (const GraphWkPtr_t& graph, const NodeWkPtr_t& from, const NodeWkPtr_t& to)
+      LevelSetEdgePtr_t LevelSetEdge::create
+      (const std::string& name, const GraphWkPtr_t& graph,
+       const NodeWkPtr_t& from, const NodeWkPtr_t& to)
       {
-        LevelSetEdge* ptr = new LevelSetEdge;
+        LevelSetEdge* ptr = new LevelSetEdge (name);
         LevelSetEdgePtr_t shPtr (ptr);
         ptr->init(shPtr, graph, from, to);
         return shPtr;
@@ -475,7 +481,10 @@ namespace hpp {
         extraLockedJoints_.push_back (lockedJoint);
       }
 
-      LevelSetEdge::LevelSetEdge (): extraConstraints_ (new Constraint_t()) {}
+      LevelSetEdge::LevelSetEdge (const std::string& name) :
+	Edge (name), extraConstraints_ (new Constraint_t())
+      {
+      }
 
       LevelSetEdge::~LevelSetEdge ()
       {
diff --git a/src/graph/graph.cc b/src/graph/graph.cc
index ef48c210..927b89e7 100644
--- a/src/graph/graph.cc
+++ b/src/graph/graph.cc
@@ -25,9 +25,9 @@
 namespace hpp {
   namespace manipulation {
     namespace graph {
-      GraphPtr_t Graph::create(DevicePtr_t robot)
+      GraphPtr_t Graph::create(const std::string& name, DevicePtr_t robot)
       {
-        Graph* ptr = new Graph;
+        Graph* ptr = new Graph (name);
         GraphPtr_t shPtr (ptr);
         ptr->init (shPtr, robot);
         return shPtr;
@@ -40,9 +40,9 @@ namespace hpp {
         wkPtr_ = weak;
       }
 
-      NodeSelectorPtr_t Graph::createNodeSelector()
+      NodeSelectorPtr_t Graph::createNodeSelector (const std::string& name)
       {
-        nodeSelector_ = NodeSelector::create();
+        nodeSelector_ = NodeSelector::create (name);
         nodeSelector_->parentGraph (wkPtr_);
         return nodeSelector_;
       }
diff --git a/src/graph/node-selector.cc b/src/graph/node-selector.cc
index 81bbd15b..2b157117 100644
--- a/src/graph/node-selector.cc
+++ b/src/graph/node-selector.cc
@@ -22,9 +22,9 @@
 namespace hpp {
   namespace manipulation {
     namespace graph {
-      NodeSelectorPtr_t NodeSelector::create()
+      NodeSelectorPtr_t NodeSelector::create(const std::string& name)
       {
-        NodeSelector* ptr = new NodeSelector();
+        NodeSelector* ptr = new NodeSelector (name);
         NodeSelectorPtr_t shPtr (ptr);
         ptr->init (shPtr);
         return shPtr;
@@ -36,9 +36,9 @@ namespace hpp {
         wkPtr_ = weak;
       }
 
-      NodePtr_t NodeSelector::createNode ()
+      NodePtr_t NodeSelector::createNode (const std::string& name)
       {
-        NodePtr_t newNode = Node::create();
+        NodePtr_t newNode = Node::create (name);
         newNode->nodeSelector(wkPtr_);
         newNode->parentGraph(graph_);
         orderedStates_.push_back(newNode);
diff --git a/src/graph/node.cc b/src/graph/node.cc
index d0450c4b..37be2aa1 100644
--- a/src/graph/node.cc
+++ b/src/graph/node.cc
@@ -25,7 +25,8 @@
 namespace hpp {
   namespace manipulation {
     namespace graph {
-      Node::Node () : configConstraints_ (new Constraint_t())
+      Node::Node (const std::string& name) :
+	GraphComponent (name), configConstraints_ (new Constraint_t())
       {}
 
       Node::~Node ()
@@ -33,9 +34,9 @@ namespace hpp {
         if (configConstraints_) delete configConstraints_;
       }
 
-      NodePtr_t Node::create ()
+      NodePtr_t Node::create (const std::string& name)
       {
-        Node* node = new Node;
+        Node* node = new Node (name);
         NodePtr_t shPtr(node);
         shPtr->init(shPtr);
         return shPtr;
@@ -47,10 +48,11 @@ namespace hpp {
         wkPtr_ = weak;
       }
 
-      EdgePtr_t Node::linkTo(const NodePtr_t& to, const Weight_t& w, const bool& isInNodeFrom,
-          boost::function < EdgePtr_t (const GraphWkPtr_t&, const NodeWkPtr_t&, const NodeWkPtr_t&) > create)
+      EdgePtr_t Node::linkTo(const std::string& name, const NodePtr_t& to,
+			     const Weight_t& w, const bool& isInNodeFrom,
+			     EdgeFactory create)
       {
-        EdgePtr_t newEdge = create(graph_, wkPtr_, to);
+        EdgePtr_t newEdge = create(name, graph_, wkPtr_, to);
         neighbors_.insert (newEdge, w);
         newEdge->isInNodeFrom (isInNodeFrom);
         return newEdge;
diff --git a/tests/test-constraintgraph.cc b/tests/test-constraintgraph.cc
index 1a039bb3..3f8e5c85 100644
--- a/tests/test-constraintgraph.cc
+++ b/tests/test-constraintgraph.cc
@@ -62,16 +62,17 @@ namespace hpp_test {
     } else {
       robot = Device::create ("test-robot");
     }
-    graph_ = Graph::create (robot); components.push_back(graph_);
+    graph_ = Graph::create ("manpulation-graph", robot);
+    components.push_back(graph_);
     graph_->maxIterations (20);
     graph_->errorThreshold (1e-4);
-    ns = graph_->createNodeSelector(); components.push_back(ns);
-    n1 = ns->createNode (); components.push_back(n1);
-    n2 = ns->createNode (); components.push_back(n2);
-    e11 = n1->linkTo (n1); components.push_back(e11);
-    e21 = n2->linkTo (n1); components.push_back(e21);
-    e12 = n1->linkTo (n2); components.push_back(e12);
-    e22 = n2->linkTo (n2); components.push_back(e22);
+    ns = graph_->createNodeSelector("node-selector"); components.push_back(ns);
+    n1 = ns->createNode ("node 1"); components.push_back(n1);
+    n2 = ns->createNode ("node 2"); components.push_back(n2);
+    e11 = n1->linkTo ("edge 11", n1); components.push_back(e11);
+    e21 = n2->linkTo ("edge 21", n1); components.push_back(e21);
+    e12 = n1->linkTo ("edge 12", n2); components.push_back(e12);
+    e22 = n2->linkTo ("edge 22", n2); components.push_back(e22);
 
     q1 = Configuration_t::Zero(6);
     q2 = Configuration_t::Zero(6);
-- 
GitLab