Tulip  4.6.1
Better Visualization Through Research
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Groups Pages
AbstractProperty.h
1 /*
2  *
3  * This file is part of Tulip (www.tulip-software.org)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 
20 #ifndef ABSTRACT_PROPERTY_H
21 #define ABSTRACT_PROPERTY_H
22 
23 #include <typeinfo>
24 #include <string>
25 #include <cstdlib>
26 #include <tulip/tulipconf.h>
27 #include <tulip/StoredType.h>
28 #include <tulip/MutableContainer.h>
29 #include <tulip/PropertyInterface.h>
30 #include <tulip/Iterator.h>
31 #include <tulip/PropertyAlgorithm.h>
32 #include <tulip/DataSet.h>
33 #include <tulip/Graph.h>
34 
35 namespace tlp {
36 
37 class GraphView;
38 
39 //==============================================================
40 
41 /**
42  * @ingroup Graph
43  * @brief This class extends upon PropertyInterface, and adds type-safe methods to
44  * get and set the node and edge values, through the magic of template programming.
45  *
46  * Nodes and Edges can have different types (e.g. tlp::LayoutProperty has tlp::PointType as node type and tlp::LineType as edge type),
47  * but most of the time they have the same type (e.g. tlp::DoubleProperty, tlp::IntegerProperty).
48  *
49  * Some of the pure virtual functions of PropertyInterface are implemented in this class (e.g. erase()).
50  *
51  * The actual data is stored in this class, and it manages the default values.
52  */
53 template <class Tnode, class Tedge, class Tprop=PropertyInterface>
54 class TLP_SCOPE AbstractProperty : public Tprop {
55  friend class Graph;
56  friend class GraphView;
57 
58 public:
59  AbstractProperty(Graph *, const std::string& n = "");
60 
61  /**
62  * @brief Gets the default node value of the property.
63  * @return The default value of nodes.
64  */
65  virtual typename Tnode::RealType getNodeDefaultValue() const;
66 
67  /**
68  * @brief Gets the default edge value of the property.
69  * @return The default value of edges.
70  **/
71  virtual typename Tedge::RealType getEdgeDefaultValue() const;
72 
73  /**
74  * @brief Returns the value associated with the node n in this property.
75  * If there is no value, it returns the default node value.
76  *
77  * @param n The node for which we want to get the value of the property.
78  * @return :StoredType< Tnode::RealType >::ReturnedConstValue The value of the property for this node.
79  **/
80  virtual typename tlp::StoredType<typename Tnode::RealType>::ReturnedConstValue getNodeValue(const node n ) const;
81 
82  /**
83  * @brief Returns the value associated to the edge e in this property.
84  * If there is no value, it returns the default edge value.
85  *
86  * @param e The edge for which we want to get the value of the property.
87  * @return :StoredType< Tedge::RealType >::ReturnedConstValue The value of the property for this edge.
88  **/
89  virtual typename tlp::StoredType<typename Tedge::RealType>::ReturnedConstValue getEdgeValue(const edge e) const;
90 
91  /**
92  * @brief Sets the value of a node and notify the observers of a modification.
93  *
94  * @param n The node to set the value of.
95  * @param v The value to affect for this node.
96  **/
97  virtual void setNodeValue(const node n, const typename Tnode::RealType &v);
98 
99  /**
100  * @brief Set the value of an edge and notify the observers of a modification.
101  *
102  * @param e The edge to set the value of.
103  * @param v The value to affect for this edge.
104  **/
105  virtual void setEdgeValue(const edge e, const typename Tedge::RealType &v);
106 
107  /**
108  * @brief Sets the value of all nodes and notify the observers.
109  *
110  * @param v The value to set to all nodes.
111  *
112  **/
113  virtual void setAllNodeValue(const typename Tnode::RealType &v);
114 
115  /**
116  * @brief Sets the value of all edges and notify the observers.
117  *
118  * @param v The value to set to all edges.
119  *
120  **/
121  virtual void setAllEdgeValue(const typename Tedge::RealType &v);
122  //=================================================================================
123 
124  /**
125  * @brief Resets the value of a node to the default value.
126  *
127  * @param n The node to reset the value of.
128  *
129  **/
130  virtual void erase(const node n) {
131  setNodeValue(n, nodeDefaultValue);
132  }
133  //=================================================================================
134 
135  /**
136  * @brief Resets the value of an edge to the default value.
137  *
138  * @param e The edge to reset the value of.
139  *
140  **/
141  virtual void erase(const edge e) {
142  setEdgeValue(e, edgeDefaultValue);
143  }
144  //=================================================================================
145  /**
146  * @brief This operator overload allows to copy a property using the following syntax :
147  *
148  * @code
149  * IntegerProperty* shape = graph->getProperty<IntegerProperty>("viewShape");
150  * IntegerProperty* backup = graph->getProperty<IntegerProperty>("shapeBackup");
151  * *backup = *shape; // all the values from 'shape' will be copied into 'backup'.
152  * @endcode
153  * @param prop The property nto copy the values from.
154  * @return This property with the values copied.
155  */
157  if (this!= &prop) {
158  if (Tprop::graph == NULL) Tprop::graph = prop.Tprop::graph;
159 
160  if (Tprop::graph == prop.Tprop::graph) {
161  setAllNodeValue(prop.getNodeDefaultValue());
162  setAllEdgeValue(prop.getEdgeDefaultValue());
163  Iterator<node> *itN = prop.getNonDefaultValuatedNodes();
164 
165  while (itN->hasNext()) {
166  node itn = itN->next();
167  setNodeValue(itn, prop.getNodeValue(itn));
168  }
169 
170  delete itN;
171  Iterator<edge> *itE = prop.getNonDefaultValuatedEdges();
172 
173  while (itE->hasNext()) {
174  edge ite = itE->next();
175  setEdgeValue(ite, prop.getEdgeValue(ite));
176  }
177 
178  delete itE;
179  }
180  else {
181  //==============================================================*
182  Iterator<node>* itN = Tprop::graph->getNodes();
183 
184  while (itN->hasNext()) {
185  node itn=itN->next();
186 
187  if (prop.Tprop::graph->isElement(itn))
188  setNodeValue(itn, prop.getNodeValue(itn));
189  }
190 
191  delete itN;
192  Iterator<edge>*itE = Tprop::graph->getEdges();
193 
194  while (itE->hasNext()) {
195  edge ite=itE->next();
196 
197  if (prop.Tprop::graph->isElement(ite))
198  setEdgeValue(ite, prop.getEdgeValue(ite));
199  }
200 
201  delete itE;
202  }
203 
204  clone_handler(prop);
205  }
206 
207  return *this;
208  }
209  //=================================================================================
210  // Untyped accessors inherited from PropertyInterface, documentation is inherited
211  virtual std::string getNodeDefaultStringValue() const {
212  typename Tnode::RealType v = getNodeDefaultValue();
213  return Tnode::toString( v );
214  }
215  virtual std::string getEdgeDefaultStringValue() const {
216  typename Tedge::RealType v = getEdgeDefaultValue();
217  return Tedge::toString( v );
218  }
219  virtual std::string getNodeStringValue( const node n ) const {
220  typename Tnode::RealType v = getNodeValue( n );
221  return Tnode::toString( v );
222  }
223  virtual std::string getEdgeStringValue( const edge e ) const {
224  typename Tedge::RealType v = getEdgeValue( e );
225  return Tedge::toString( v );
226  }
227  virtual bool setNodeStringValue( const node inN, const std::string & inV ) {
228  typename Tnode::RealType v;
229 
230  if( !Tnode::fromString(v,inV) )
231  return false;
232 
233  setNodeValue( inN, v );
234  return true;
235  }
236  virtual bool setEdgeStringValue( const edge inE, const std::string & inV ) {
237  typename Tedge::RealType v;
238 
239  if( !Tedge::fromString(v,inV) )
240  return false;
241 
242  setEdgeValue( inE, v );
243  return true;
244  }
245  virtual bool setAllNodeStringValue( const std::string & inV ) {
246  typename Tnode::RealType v;
247 
248  if( !Tnode::fromString(v,inV) )
249  return false;
250 
251  setAllNodeValue( v );
252  return true;
253  }
254  virtual bool setAllEdgeStringValue( const std::string & inV ) {
255  typename Tedge::RealType v;
256 
257  if( !Tedge::fromString(v,inV) )
258  return false;
259 
260  setAllEdgeValue( v );
261  return true;
262  }
263  virtual tlp::Iterator<node>* getNonDefaultValuatedNodes(const Graph* g = NULL) const;
264  virtual unsigned int numberOfNonDefaultValuatedNodes() const;
265  virtual unsigned int nodeValueSize() const;
266  virtual void writeNodeDefaultValue(std::ostream&) const;
267  virtual void writeNodeValue(std::ostream&, node) const;
268  virtual bool readNodeDefaultValue(std::istream&);
269  virtual bool readNodeValue(std::istream&, node);
270  virtual tlp::Iterator<edge>* getNonDefaultValuatedEdges(const Graph* g = NULL) const;
271  virtual unsigned int numberOfNonDefaultValuatedEdges() const;
272  virtual unsigned int edgeValueSize() const;
273  virtual void writeEdgeDefaultValue(std::ostream&) const;
274  virtual void writeEdgeValue(std::ostream&, edge) const;
275  virtual bool readEdgeDefaultValue(std::istream&);
276  virtual bool readEdgeValue(std::istream&, edge);
277  virtual bool copy(const node destination, const node source, PropertyInterface *property,
278  bool ifNotDefault = false) {
279  if (property == NULL)
280  return false;
281 
283  dynamic_cast<tlp::AbstractProperty<Tnode,Tedge,Tprop>*>(property);
284  assert(tp);
285  bool notDefault;
286  typename StoredType<typename Tnode::RealType>::ReturnedValue value =
287  tp->nodeProperties.get(source.id, notDefault);
288 
289  if (ifNotDefault && !notDefault)
290  return false;
291 
292  setNodeValue(destination, value);
293  return true;
294  }
295  virtual bool copy(const edge destination, const edge source, PropertyInterface *property,
296  bool ifNotDefault = false) {
297  if (property == NULL)
298  return false;
299 
301  dynamic_cast<tlp::AbstractProperty<Tnode,Tedge,Tprop>*>(property);
302  assert(tp);
303  bool notDefault;
304  typename StoredType<typename Tedge::RealType>::ReturnedValue value =
305  tp->edgeProperties.get(source.id, notDefault);
306 
307  if (ifNotDefault && !notDefault)
308  return false;
309 
310  setEdgeValue(destination, value);
311  return true;
312  }
313  virtual void copy(PropertyInterface* property) {
315  dynamic_cast<typename tlp::AbstractProperty<Tnode,Tedge,Tprop>*>(property);
316  assert(prop != NULL);
317  *this = *prop;
318  }
319  // for performance reason and use in GraphUpdatesRecorder
320  virtual DataMem* getNodeDefaultDataMemValue() const {
321  return new TypedValueContainer<typename Tnode::RealType>(getNodeDefaultValue());
322  }
323  virtual DataMem* getEdgeDefaultDataMemValue() const {
324  return new TypedValueContainer<typename Tedge::RealType>(getEdgeDefaultValue());
325  }
326  virtual DataMem* getNodeDataMemValue(const node n) const {
327  return new TypedValueContainer<typename Tnode::RealType>(getNodeValue(n));
328  }
329  virtual DataMem* getEdgeDataMemValue(const edge e) const {
330  return new TypedValueContainer<typename Tedge::RealType>(getEdgeValue(e));
331  }
332  virtual DataMem* getNonDefaultDataMemValue( const node n ) const {
333  bool notDefault;
334  typename StoredType<typename Tnode::RealType>::ReturnedValue value = nodeProperties.get(n.id, notDefault);
335 
336  if (notDefault)
337  return new TypedValueContainer<typename Tnode::RealType>(value);
338 
339  return NULL;
340  }
341  virtual DataMem* getNonDefaultDataMemValue( const edge e ) const {
342  bool notDefault;
343  typename StoredType<typename Tedge::RealType>::ReturnedValue value = edgeProperties.get(e.id, notDefault);
344 
345  if (notDefault)
346  return new TypedValueContainer<typename Tedge::RealType>(value);
347 
348  return NULL;
349  }
350  virtual void setNodeDataMemValue( const node n, const DataMem* v) {
351  setNodeValue(n, ((TypedValueContainer<typename Tnode::RealType> *) v)->value);
352  }
353  virtual void setEdgeDataMemValue( const edge e, const DataMem* v) {
354  setEdgeValue(e, ((TypedValueContainer<typename Tedge::RealType> *) v)->value);
355  }
356  virtual void setAllNodeDataMemValue(const DataMem* v) {
357  setAllNodeValue(((TypedValueContainer<typename Tnode::RealType> *) v)->value);
358  }
359  virtual void setAllEdgeDataMemValue(const DataMem* v) {
360  setAllEdgeValue(((TypedValueContainer<typename Tedge::RealType> *) v)->value);
361  }
362 
363  // PropertyInterface methods
364  // mN is the meta node, sg is the corresponding subgraph
365  // and mg is the graph owning mN
366  virtual void computeMetaValue(node n, Graph* sg, Graph* mg) {
367  if (Tprop::metaValueCalculator)
369  Tprop::metaValueCalculator)->computeMetaValue(this, n, sg, mg);
370  }
371  // mE is the meta edge, itE is an iterator on the underlying edges
372  // mg is the graph owning mE
373  virtual void computeMetaValue(edge e, tlp::Iterator<edge>* itE, Graph* mg) {
374  if (Tprop::metaValueCalculator)
375  ((typename tlp::AbstractProperty<Tnode,Tedge,Tprop>::MetaValueCalculator *) Tprop::metaValueCalculator)->computeMetaValue(this, e, itE, mg);
376  }
377  virtual void setMetaValueCalculator(PropertyInterface::MetaValueCalculator *mvCalc) {
378  if (mvCalc && !dynamic_cast<typename tlp::AbstractProperty<Tnode,Tedge,Tprop>::MetaValueCalculator *>(mvCalc)) {
379  tlp::warning() << "Warning : " << __PRETTY_FUNCTION__ << " ... invalid conversion of " << typeid(mvCalc).name() << "into " << typeid(typename tlp::AbstractProperty<Tnode,Tedge,Tprop>::MetaValueCalculator *).name() << std::endl;
380  abort();
381  }
382 
383  Tprop::metaValueCalculator = mvCalc;
384  }
385 
386  int compare(const node n1, const node n2) const;
387  int compare(const edge e1, const edge e2) const;
388 
389 
390  /**
391  * @brief This class is used to delegate the computation of the values associated to meta nodes or edges.
392  **/
394  public:
395  // computes the value of the meta node mN of the graph mg
396  // for the property prop, according to the values associated
397  // to the underlying nodes i.e the nodes of the subgraph sg.
398  virtual void computeMetaValue(AbstractProperty<Tnode,Tedge,Tprop>*,
399  node,
400  Graph*, Graph*) {}
401  // computes the value of the meta node mE of the graph mg
402  // for the property prop, according to the values associated
403  // to the underlying edges given by the iterator itE.
404  // The method do not have to delete the iterator
405  virtual void computeMetaValue(AbstractProperty<Tnode,Tedge,Tprop>*,
406  edge, tlp::Iterator<edge>*,
407  Graph*) {}
408  };
409 
410 protected:
411  //=================================================================================
412  ///Enable to clone part of sub_class
414 
415  MutableContainer<typename Tnode::RealType> nodeProperties;
416  MutableContainer<typename Tedge::RealType> edgeProperties;
417  typename Tnode::RealType nodeDefaultValue;
418  typename Tedge::RealType edgeDefaultValue;
419 };
420 
421 template <typename vectType,typename eltType,typename propType=VectorPropertyInterface>
422 class TLP_SCOPE AbstractVectorProperty : public AbstractProperty<vectType, vectType, propType> {
423 public:
424  AbstractVectorProperty(Graph *, const std::string& name = "");
425 
426  // 2 methods inherited from VectorPropertyInterface
427  bool setNodeStringValueAsVector(const node, const std::string&,
428  char, char, char);
429 
430  bool setEdgeStringValueAsVector(const edge, const std::string&,
431  char, char, char);
432 
433  /**
434  * @brief Sets the value for node n, at index i, to v, and notify the observers of a modification.
435  *
436  * @param n The node to set a value of.
437  * @param i The index at which the value should be set.
438  * @param v The value to set.
439  *
440  **/
441  void setNodeEltValue(const node n, unsigned int i, typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v);
442  /**
443  * @brief Gets the value associated to node n, at index i.
444  *
445  * @param n The node to set a value of.
446  * @param i The index at which to set the value.
447  * @return const eltType& The value at index i in the vector for node n.
448  **/
449  typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue getNodeEltValue(const node n, unsigned int i) const;
450  /**
451  * @brief Appends a new value at the end of the vector associated to node n, and notify the observers of a modification.
452  *
453  * @param n The node to add a value to.
454  * @param v The value to append at the end of the vector.
455  *
456  **/
457  void pushBackNodeEltValue(const node n, typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v);
458  /**
459  * @brief Removes the value at the end of the vector associated to node n, and notify the observers of a modification.
460  *
461  * @param n The node to remove a value from.
462  *
463  **/
464  void popBackNodeEltValue(const node n);
465  /**
466  * @brief Resizes the vector associated to node n, and notify the observers of a modification.
467  *
468  * @param n The node associated to the vector to resize.
469  * @param size The new size of the vector.
470  * @param elt The default value to set at indices where there was no value before. Defaults to eltType().
471  *
472  **/
473  void resizeNodeValue(const node n, size_t size, typename eltType::RealType elt = eltType::defaultValue());
474  /**
475  * @brief Sets the value for edge e, at index i, to v, and notify the observers of a modification.
476  *
477  * @param e The edge to set the value of.
478  * @param i The index at which the value should be set.
479  * @param v The value to set.
480  *
481  **/
482  void setEdgeEltValue(const edge e, unsigned int i, typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v);
483  /**
484  * @brief Gets the value associated to edge e, at index i.
485  *
486  * @param e The edge to set a value of.
487  * @param i The index at which to set the value.
488  * @return const eltType& The value at index i in the vector for node n.
489  **/
490  typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue getEdgeEltValue(const edge n, unsigned int i) const;
491  /**
492  * @brief Appends a new value at the end of the vector associated to edge e, and notify the observers of a modification.
493  *
494  * @param e The node to add a value to.
495  * @param v The value to append at the end of the vector.
496  *
497  **/
498  void pushBackEdgeEltValue(const edge e, typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v);
499  /**
500  * @brief Removes the value at the end of the vector associated to edge e, and notify the observers of a modification.
501  *
502  * @param e The edge to remove a value from.
503  *
504  **/
505  void popBackEdgeEltValue(const edge e);
506  /**
507  * @brief Resizes the vector associated to edge e, and notify the observers of a modification.
508  *
509  * @param e The edge associated to the vector to resize.
510  * @param size The new size of the vector.
511  * @param elt The default value to set at indices where there was no value before. Defaults to eltType().
512  *
513  **/
514  void resizeEdgeValue(const edge e, size_t size, typename eltType::RealType elt = eltType::defaultValue());
515 };
516 
517 
518 }
519 #if !defined(_MSC_VER) || defined(DLL_TULIP) //When using VC++, we only want to include this when we are in the TULIP dll. With any other compiler, include it all the time
520 # include "cxx/AbstractProperty.cxx"
521 #endif
522 #endif
This class extends upon PropertyInterface, and adds type-safe methods to get and set the node and edg...
virtual tlp::StoredType< typename Tnode::RealType >::ReturnedConstValue getNodeValue(const node n) const
Returns the value associated with the node n in this property. If there is no value, it returns the default node value.
virtual void erase(const edge e)
Resets the value of an edge to the default value.
virtual Tnode::RealType getNodeDefaultValue() const
Gets the default node value of the property.
The edge struct represents an edge in a Graph object.
Definition: Edge.h:39
The node struct represents a node in a Graph object.
Definition: Node.h:39
virtual void erase(const node n)
Resets the value of a node to the default value.
This class is used to delegate the computation of the values associated to meta nodes or edges...
virtual tlp::StoredType< typename Tedge::RealType >::ReturnedConstValue getEdgeValue(const edge e) const
Returns the value associated to the edge e in this property. If there is no value, it returns the default edge value.
Base class for computing values on meta nodes and edges.
virtual Tedge::RealType getEdgeDefaultValue() const
Gets the default edge value of the property.
virtual void clone_handler(AbstractProperty< Tnode, Tedge, Tprop > &)
Enable to clone part of sub_class.