Tulip  4.10.0
Better Visualization Through Research
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  * All previous values are lost and the given value is assigned as the default one to the future added nodes.
110  *
111  * @param v The value to set to all nodes.
112  * @param graph (since Tulip 4.10) An optional descendant graph from the one associated to that property (itself included).
113  * If provided, only the nodes from that descendant graph will have their value modified.
114  * In the case of the descendant graph is different from the one associated to that property, the default node value will not be modified.
115  * @warning If the provided graph is not a descendant of the one associated to that property, no node value will be modified in it.
116  *
117  **/
118  virtual void setAllNodeValue(const typename Tnode::RealType &v, const Graph *graph = NULL);
119 
120  /**
121  * @brief Sets the value of all edges and notify the observers.
122  * All previous values are lost and the given value is assigned as the default one to the future added edges.
123  * @param graph (since Tulip 4.10) An optional descendant graph from the one associated to that property (itself included).
124  * If provided, only the edges from that descendant graph will have their value modified.
125  * In the case of the descendant graph is different from the one associated to that property, the default edge value will not be modified.
126  * @warning If the provided graph is not a descendant of the one associated to that property, no edge value will be modified in it.
127  *
128  * @param v The value to set to all edges.
129  *
130  **/
131  virtual void setAllEdgeValue(const typename Tedge::RealType &v, const Graph *graph = NULL);
132  //=================================================================================
133 
134  /**
135  * @brief Resets the value of a node to the default value.
136  *
137  * @param n The node to reset the value of.
138  *
139  **/
140  virtual void erase(const node n) {
141  setNodeValue(n, nodeDefaultValue);
142  }
143  //=================================================================================
144 
145  /**
146  * @brief Resets the value of an edge to the default value.
147  *
148  * @param e The edge to reset the value of.
149  *
150  **/
151  virtual void erase(const edge e) {
152  setEdgeValue(e, edgeDefaultValue);
153  }
154  //=================================================================================
155  /**
156  * @brief This operator overload allows to copy a property using the following syntax :
157  *
158  * @code
159  * IntegerProperty* shape = graph->getProperty<IntegerProperty>("viewShape");
160  * IntegerProperty* backup = graph->getProperty<IntegerProperty>("shapeBackup");
161  * *backup = *shape; // all the values from 'shape' will be copied into 'backup'.
162  * @endcode
163  * @param prop The property nto copy the values from.
164  * @return This property with the values copied.
165  */
167  if (this!= &prop) {
168  if (Tprop::graph == NULL) Tprop::graph = prop.Tprop::graph;
169 
170  if (Tprop::graph == prop.Tprop::graph) {
171  setAllNodeValue(prop.getNodeDefaultValue());
172  setAllEdgeValue(prop.getEdgeDefaultValue());
173  Iterator<node> *itN = prop.getNonDefaultValuatedNodes();
174 
175  while (itN->hasNext()) {
176  node itn = itN->next();
177  setNodeValue(itn, prop.getNodeValue(itn));
178  }
179 
180  delete itN;
181  Iterator<edge> *itE = prop.getNonDefaultValuatedEdges();
182 
183  while (itE->hasNext()) {
184  edge ite = itE->next();
185  setEdgeValue(ite, prop.getEdgeValue(ite));
186  }
187 
188  delete itE;
189  }
190  else {
191  //==============================================================*
192  Iterator<node>* itN = Tprop::graph->getNodes();
193 
194  while (itN->hasNext()) {
195  node itn=itN->next();
196 
197  if (prop.Tprop::graph->isElement(itn))
198  setNodeValue(itn, prop.getNodeValue(itn));
199  }
200 
201  delete itN;
202  Iterator<edge>*itE = Tprop::graph->getEdges();
203 
204  while (itE->hasNext()) {
205  edge ite=itE->next();
206 
207  if (prop.Tprop::graph->isElement(ite))
208  setEdgeValue(ite, prop.getEdgeValue(ite));
209  }
210 
211  delete itE;
212  }
213 
214  clone_handler(prop);
215  }
216 
217  return *this;
218  }
219  //=================================================================================
220  // Untyped accessors inherited from PropertyInterface, documentation is inherited
221  virtual std::string getNodeDefaultStringValue() const {
222  typename Tnode::RealType v = getNodeDefaultValue();
223  return Tnode::toString( v );
224  }
225  virtual std::string getEdgeDefaultStringValue() const {
226  typename Tedge::RealType v = getEdgeDefaultValue();
227  return Tedge::toString( v );
228  }
229  virtual std::string getNodeStringValue( const node n ) const {
230  typename Tnode::RealType v = getNodeValue( n );
231  return Tnode::toString( v );
232  }
233  virtual std::string getEdgeStringValue( const edge e ) const {
234  typename Tedge::RealType v = getEdgeValue( e );
235  return Tedge::toString( v );
236  }
237  virtual bool setNodeStringValue( const node inN, const std::string & inV ) {
238  typename Tnode::RealType v;
239 
240  if( !Tnode::fromString(v,inV) )
241  return false;
242 
243  setNodeValue( inN, v );
244  return true;
245  }
246  virtual bool setEdgeStringValue( const edge inE, const std::string & inV ) {
247  typename Tedge::RealType v;
248 
249  if( !Tedge::fromString(v,inV) )
250  return false;
251 
252  setEdgeValue( inE, v );
253  return true;
254  }
255  virtual bool setAllNodeStringValue( const std::string & inV , const Graph *graph = NULL ) {
256  typename Tnode::RealType v;
257 
258  if( !Tnode::fromString(v,inV) )
259  return false;
260 
261  setAllNodeValue( v , graph );
262  return true;
263  }
264  virtual bool setAllEdgeStringValue( const std::string & inV , const Graph *graph = NULL ) {
265  typename Tedge::RealType v;
266 
267  if( !Tedge::fromString(v,inV) )
268  return false;
269 
270  setAllEdgeValue( v , graph );
271  return true;
272  }
273  virtual tlp::Iterator<node>* getNonDefaultValuatedNodes(const Graph* g = NULL) const;
274  virtual unsigned int numberOfNonDefaultValuatedNodes(const Graph* g = NULL) const;
275  virtual unsigned int nodeValueSize() const;
276  virtual void writeNodeDefaultValue(std::ostream&) const;
277  virtual void writeNodeValue(std::ostream&, node) const;
278  virtual bool readNodeDefaultValue(std::istream&);
279  virtual bool readNodeValue(std::istream&, node);
280  virtual tlp::Iterator<edge>* getNonDefaultValuatedEdges(const Graph* g = NULL) const;
281  virtual unsigned int numberOfNonDefaultValuatedEdges(const Graph* = NULL) const;
282  virtual unsigned int edgeValueSize() const;
283  virtual void writeEdgeDefaultValue(std::ostream&) const;
284  virtual void writeEdgeValue(std::ostream&, edge) const;
285  virtual bool readEdgeDefaultValue(std::istream&);
286  virtual bool readEdgeValue(std::istream&, edge);
287  virtual bool copy(const node destination, const node source, PropertyInterface *property,
288  bool ifNotDefault = false) {
289  if (property == NULL)
290  return false;
291 
293  dynamic_cast<tlp::AbstractProperty<Tnode,Tedge,Tprop>*>(property);
294  assert(tp);
295  bool notDefault;
296  typename StoredType<typename Tnode::RealType>::ReturnedValue value =
297  tp->nodeProperties.get(source.id, notDefault);
298 
299  if (ifNotDefault && !notDefault)
300  return false;
301 
302  setNodeValue(destination, value);
303  return true;
304  }
305  virtual bool copy(const edge destination, const edge source, PropertyInterface *property,
306  bool ifNotDefault = false) {
307  if (property == NULL)
308  return false;
309 
311  dynamic_cast<tlp::AbstractProperty<Tnode,Tedge,Tprop>*>(property);
312  assert(tp);
313  bool notDefault;
314  typename StoredType<typename Tedge::RealType>::ReturnedValue value =
315  tp->edgeProperties.get(source.id, notDefault);
316 
317  if (ifNotDefault && !notDefault)
318  return false;
319 
320  setEdgeValue(destination, value);
321  return true;
322  }
323  virtual void copy(PropertyInterface* property) {
325  dynamic_cast<typename tlp::AbstractProperty<Tnode,Tedge,Tprop>*>(property);
326  assert(prop != NULL);
327  *this = *prop;
328  }
329  // for performance reason and use in GraphUpdatesRecorder
330  virtual DataMem* getNodeDefaultDataMemValue() const {
331  return new TypedValueContainer<typename Tnode::RealType>(getNodeDefaultValue());
332  }
333  virtual DataMem* getEdgeDefaultDataMemValue() const {
334  return new TypedValueContainer<typename Tedge::RealType>(getEdgeDefaultValue());
335  }
336  virtual DataMem* getNodeDataMemValue(const node n) const {
337  return new TypedValueContainer<typename Tnode::RealType>(getNodeValue(n));
338  }
339  virtual DataMem* getEdgeDataMemValue(const edge e) const {
340  return new TypedValueContainer<typename Tedge::RealType>(getEdgeValue(e));
341  }
342  virtual DataMem* getNonDefaultDataMemValue( const node n ) const {
343  bool notDefault;
344  typename StoredType<typename Tnode::RealType>::ReturnedValue value = nodeProperties.get(n.id, notDefault);
345 
346  if (notDefault)
347  return new TypedValueContainer<typename Tnode::RealType>(value);
348 
349  return NULL;
350  }
351  virtual DataMem* getNonDefaultDataMemValue( const edge e ) const {
352  bool notDefault;
353  typename StoredType<typename Tedge::RealType>::ReturnedValue value = edgeProperties.get(e.id, notDefault);
354 
355  if (notDefault)
356  return new TypedValueContainer<typename Tedge::RealType>(value);
357 
358  return NULL;
359  }
360  virtual void setNodeDataMemValue( const node n, const DataMem* v) {
361  setNodeValue(n, ((TypedValueContainer<typename Tnode::RealType> *) v)->value);
362  }
363  virtual void setEdgeDataMemValue( const edge e, const DataMem* v) {
364  setEdgeValue(e, ((TypedValueContainer<typename Tedge::RealType> *) v)->value);
365  }
366  virtual void setAllNodeDataMemValue(const DataMem* v) {
367  setAllNodeValue(((TypedValueContainer<typename Tnode::RealType> *) v)->value);
368  }
369  virtual void setAllEdgeDataMemValue(const DataMem* v) {
370  setAllEdgeValue(((TypedValueContainer<typename Tedge::RealType> *) v)->value);
371  }
372 
373  // PropertyInterface methods
374  // mN is the meta node, sg is the corresponding subgraph
375  // and mg is the graph owning mN
376  virtual void computeMetaValue(node n, const Graph* sg, const Graph* mg) {
377  if (Tprop::metaValueCalculator)
379  Tprop::metaValueCalculator)->computeMetaValue(this, n, sg, mg);
380  }
381  // mE is the meta edge, itE is an iterator on the underlying edges
382  // mg is the graph owning mE
383  virtual void computeMetaValue(edge e, tlp::Iterator<edge>* itE, const Graph* mg) {
384  if (Tprop::metaValueCalculator)
385  ((typename tlp::AbstractProperty<Tnode,Tedge,Tprop>::MetaValueCalculator *) Tprop::metaValueCalculator)->computeMetaValue(this, e, itE, mg);
386  }
387  virtual void setMetaValueCalculator(PropertyInterface::MetaValueCalculator *mvCalc) {
388  if (mvCalc && !dynamic_cast<typename tlp::AbstractProperty<Tnode,Tedge,Tprop>::MetaValueCalculator *>(mvCalc)) {
389  tlp::warning() << "Warning : " << __PRETTY_FUNCTION__ << " ... invalid conversion of " << typeid(mvCalc).name() << "into " << typeid(typename tlp::AbstractProperty<Tnode,Tedge,Tprop>::MetaValueCalculator *).name() << std::endl;
390  abort();
391  }
392 
393  Tprop::metaValueCalculator = mvCalc;
394  }
395 
396  int compare(const node n1, const node n2) const;
397  int compare(const edge e1, const edge e2) const;
398 
399 
400  /**
401  * @brief This class is used to delegate the computation of the values associated to meta nodes or edges.
402  **/
404  public:
405  // computes the value of the meta node mN of the graph mg
406  // for the property prop, according to the values associated
407  // to the underlying nodes i.e the nodes of the subgraph sg.
408  virtual void computeMetaValue(AbstractProperty<Tnode,Tedge,Tprop>*,
409  node,
410  const Graph*, const Graph*) {}
411  // computes the value of the meta node mE of the graph mg
412  // for the property prop, according to the values associated
413  // to the underlying edges given by the iterator itE.
414  // The method do not have to delete the iterator
415  virtual void computeMetaValue(AbstractProperty<Tnode,Tedge,Tprop>*,
416  edge, tlp::Iterator<edge>*,
417  const Graph*) {}
418  };
419 
420 protected:
421  //=================================================================================
422  ///Enable to clone part of sub_class
424 
425  MutableContainer<typename Tnode::RealType> nodeProperties;
426  MutableContainer<typename Tedge::RealType> edgeProperties;
427  typename Tnode::RealType nodeDefaultValue;
428  typename Tedge::RealType edgeDefaultValue;
429 };
430 
431 template <typename vectType,typename eltType,typename propType=VectorPropertyInterface>
432 class TLP_SCOPE AbstractVectorProperty : public AbstractProperty<vectType, vectType, propType> {
433 public:
434  AbstractVectorProperty(Graph *, const std::string& name = "");
435 
436  // 2 methods inherited from VectorPropertyInterface
437  bool setNodeStringValueAsVector(const node, const std::string&,
438  char, char, char);
439 
440  bool setEdgeStringValueAsVector(const edge, const std::string&,
441  char, char, char);
442 
443  /**
444  * @brief Sets the value for node n, at index i, to v, and notify the observers of a modification.
445  *
446  * @param n The node to set a value of.
447  * @param i The index at which the value should be set.
448  * @param v The value to set.
449  *
450  **/
451  void setNodeEltValue(const node n, unsigned int i, typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v);
452  /**
453  * @brief Gets the value associated to node n, at index i.
454  *
455  * @param n The node to set a value of.
456  * @param i The index at which to set the value.
457  * @return const eltType& The value at index i in the vector for node n.
458  **/
459  typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue getNodeEltValue(const node n, unsigned int i) const;
460  /**
461  * @brief Appends a new value at the end of the vector associated to node n, and notify the observers of a modification.
462  *
463  * @param n The node to add a value to.
464  * @param v The value to append at the end of the vector.
465  *
466  **/
467  void pushBackNodeEltValue(const node n, typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v);
468  /**
469  * @brief Removes the value at the end of the vector associated to node n, and notify the observers of a modification.
470  *
471  * @param n The node to remove a value from.
472  *
473  **/
474  void popBackNodeEltValue(const node n);
475  /**
476  * @brief Resizes the vector associated to node n, and notify the observers of a modification.
477  *
478  * @param n The node associated to the vector to resize.
479  * @param size The new size of the vector.
480  * @param elt The default value to set at indices where there was no value before. Defaults to eltType().
481  *
482  **/
483  void resizeNodeValue(const node n, size_t size, typename eltType::RealType elt = eltType::defaultValue());
484  /**
485  * @brief Sets the value for edge e, at index i, to v, and notify the observers of a modification.
486  *
487  * @param e The edge to set the value of.
488  * @param i The index at which the value should be set.
489  * @param v The value to set.
490  *
491  **/
492  void setEdgeEltValue(const edge e, unsigned int i, typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v);
493  /**
494  * @brief Gets the value associated to edge e, at index i.
495  *
496  * @param e The edge to set a value of.
497  * @param i The index at which to set the value.
498  * @return const eltType& The value at index i in the vector for node n.
499  **/
500  typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue getEdgeEltValue(const edge n, unsigned int i) const;
501  /**
502  * @brief Appends a new value at the end of the vector associated to edge e, and notify the observers of a modification.
503  *
504  * @param e The node to add a value to.
505  * @param v The value to append at the end of the vector.
506  *
507  **/
508  void pushBackEdgeEltValue(const edge e, typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v);
509  /**
510  * @brief Removes the value at the end of the vector associated to edge e, and notify the observers of a modification.
511  *
512  * @param e The edge to remove a value from.
513  *
514  **/
515  void popBackEdgeEltValue(const edge e);
516  /**
517  * @brief Resizes the vector associated to edge e, and notify the observers of a modification.
518  *
519  * @param e The edge associated to the vector to resize.
520  * @param size The new size of the vector.
521  * @param elt The default value to set at indices where there was no value before. Defaults to eltType().
522  *
523  **/
524  void resizeEdgeValue(const edge e, size_t size, typename eltType::RealType elt = eltType::defaultValue());
525 };
526 
527 
528 }
529 #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
530 # include "cxx/AbstractProperty.cxx"
531 #endif
532 #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.
PropertyInterface describes the interface of a graph property.
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.
unsigned int id
id The identifier of the node.
Definition: Node.h:43
unsigned int id
id The identifier of the edge.
Definition: Edge.h:43