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