bpp-phyl3  3.0.0
PhyloNode.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_PHYL_TREE_PHYLONODE_H
6 #define BPP_PHYL_TREE_PHYLONODE_H
7 
8 #include <Bpp/Clonable.h>
9 #include <Bpp/Utils/MapTools.h>
10 #include <memory>
11 
12 #include "PhyloTreeExceptions.h"
13 
14 namespace bpp
15 {
16 /***********
17  * For each PhyloNode of the tree, a description of the event at the node.
18  *
19  * It will determine the choice of the computing operator on this node.
20  *
21  */
22 
23 class NodeEvent : public Clonable
24 {
25  enum class NodeType
26  {
27  Speciation = 0,
28  Mixture = 1
29  };
30 
32 
33 public:
34  NodeEvent(NodeType type) { nodeType_ = type;}
35 
36  // NodeEvent(const NodeEvent& event) :
37  // nodeType_(event.nodeType_) {}
38 
39  NodeEvent* clone() const { return new NodeEvent(*this);}
40 
41  bool isSpeciation() const { return nodeType_ == NodeType::Speciation;}
42  bool isMixture() const {return nodeType_ == NodeType::Mixture;}
43 
44 public:
45  static const NodeEvent speciationEvent;
46  static const NodeEvent mixtureEvent;
47 
48  std::string toString() const
49  {
50  if (isSpeciation())
51  return "speciation";
52  else
53  return "mixture";
54  }
55 };
56 
57 
58 class PhyloNode
59 {
60 private:
61  // a name, if specified
62  std::string name_;
63 
64  // Node properties
65  mutable std::map<std::string, Clonable*> properties_;
66 
67 public:
72  name_(""),
73  properties_()
74  {}
75 
81  PhyloNode(const std::string& name) :
82  name_(name),
83  properties_()
84  {}
85 
91  PhyloNode(const PhyloNode& node);
92 
100  PhyloNode& operator=(const PhyloNode& node);
101 
102  PhyloNode* clone() const { return new PhyloNode(*this); }
103 
108  virtual ~PhyloNode()
109  {
111  }
112 
113 public:
126  std::string getName() const
127  {
128  if (!hasName()) throw PhyloNodePException("Node::getName: no name associated to this node.", this);
129  return name_;
130  }
131 
137  void setName(const std::string& name)
138  {
139  name_ = name;
140  }
141 
145  void deleteName()
146  {
147  name_ = "";
148  }
149 
155  bool hasName() const { return name_ != ""; }
156 
175  void setProperty(const std::string& name, const Clonable& property)
176  {
177  if (hasProperty(name))
178  delete properties_[name];
179  properties_[name] = property.clone();
180  }
181 
182  Clonable* getProperty(const std::string& name)
183  {
184  if (hasProperty(name))
185  return properties_[name];
186  else
187  throw PhyloNodePropertyNotFoundException("", name, this);
188  }
189 
190  const Clonable* getProperty(const std::string& name) const
191  {
192  if (hasProperty(name))
193  return const_cast<const Clonable*>(properties_[name]);
194  else
195  throw PhyloNodePropertyNotFoundException("", name, this);
196  }
197 
198  Clonable* removeProperty(const std::string& name)
199  {
200  if (hasProperty(name))
201  {
202  Clonable* removed = properties_[name];
203  properties_.erase(name);
204  return removed;
205  }
206  else
207  throw PhyloNodePropertyNotFoundException("", name, this);
208  }
209 
210  void deleteProperty(const std::string& name)
211  {
212  if (hasProperty(name))
213  {
214  delete properties_[name];
215  properties_.erase(name);
216  }
217  else
218  throw PhyloNodePropertyNotFoundException("", name, this);
219  }
220 
227  {
228  properties_.clear();
229  }
230 
235  {
236  for (std::map<std::string, Clonable*>::iterator i = properties_.begin(); i != properties_.end(); i++)
237  {
238  delete i->second;
239  }
240  properties_.clear();
241  }
242 
243  bool hasProperty(const std::string& name) const { return properties_.find(name) != properties_.end(); }
244 
245  std::vector<std::string> getPropertyNames() const { return MapTools::getKeys(properties_); }
247 }; // end of class node
248 } // end of namespace bpp.
249 
250 #else
251 namespace bpp { class PhyloNode; }
252 #endif // BPP_PHYL_TREE_PHYLONODE_H
static std::vector< Key > getKeys(const std::map< Key, T, Cmp > &myMap)
NodeEvent(NodeType type)
Definition: PhyloNode.h:34
std::string toString() const
Definition: PhyloNode.h:48
bool isSpeciation() const
Definition: PhyloNode.h:41
static const NodeEvent speciationEvent
Definition: PhyloNode.h:45
NodeType nodeType_
Definition: PhyloNode.h:31
static const NodeEvent mixtureEvent
Definition: PhyloNode.h:46
NodeEvent * clone() const
Definition: PhyloNode.h:39
bool isMixture() const
Definition: PhyloNode.h:42
General exception thrown when something is wrong with a particular node.
General exception thrown if a property could not be found.
void setName(const std::string &name)
Give a name or update the name associated to the node.
Definition: PhyloNode.h:137
bool hasName() const
Tell is this node has a name.
Definition: PhyloNode.h:155
Clonable * removeProperty(const std::string &name)
Definition: PhyloNode.h:198
PhyloNode()
Build a new void Node object.
Definition: PhyloNode.h:71
void deleteProperty(const std::string &name)
Definition: PhyloNode.h:210
Clonable * getProperty(const std::string &name)
Definition: PhyloNode.h:182
void deleteProperties()
Delete all node properties.
Definition: PhyloNode.h:234
std::string name_
Definition: PhyloNode.h:62
void deleteName()
Delete the name associated to this node (do nothing if there is no name).
Definition: PhyloNode.h:145
void setProperty(const std::string &name, const Clonable &property)
Set/add a node property.
Definition: PhyloNode.h:175
PhyloNode * clone() const
Definition: PhyloNode.h:102
std::string getName() const
Get the name associated to this node, if there is one, otherwise throw a NodeException.
Definition: PhyloNode.h:126
std::map< std::string, Clonable * > properties_
Definition: PhyloNode.h:65
PhyloNode(const std::string &name)
Build a new Node with specified name.
Definition: PhyloNode.h:81
PhyloNode & operator=(const PhyloNode &node)
Assignation operator.
Definition: PhyloNode.cpp:29
bool hasProperty(const std::string &name) const
Definition: PhyloNode.h:243
const Clonable * getProperty(const std::string &name) const
Definition: PhyloNode.h:190
virtual ~PhyloNode()
destructor.
Definition: PhyloNode.h:108
void removeProperties()
Remove all node properties.
Definition: PhyloNode.h:226
std::vector< std::string > getPropertyNames() const
Definition: PhyloNode.h:245
Defines the basic types of data flow nodes.