fw4spl
fwAtomConversion/src/fwAtomConversion/mapper/Graph.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "fwAtomConversion/mapper/Graph.hpp"
8 
9 #include "fwAtomConversion/convert.hpp"
10 #include "fwAtomConversion/exception/ConversionNotManaged.hpp"
11 #include "fwAtomConversion/mapper/registry/macros.hpp"
12 
13 #include <fwAtoms/Map.hpp>
14 #include <fwAtoms/Sequence.hpp>
15 #include <fwAtoms/String.hpp>
16 
17 #include <fwData/Graph.hpp>
18 
19 #include <fwTools/UUID.hpp>
20 
21 namespace fwAtomConversion
22 {
23 namespace mapper
24 {
25 
26 //-----------------------------------------------------------------------------
27 
28 fwAtomConversionRegisterMacro( ::fwAtomConversion::mapper::Graph, ::fwData::Graph);
29 
30 //-----------------------------------------------------------------------------
31 
32 ::fwAtoms::Object::sptr Graph::convert( ::fwData::Object::sptr object,
33  DataVisitor::AtomCacheType& cache )
34 {
35  const camp::Class& metaclass = ::camp::classByName( object->getClassname() );
36  ::fwAtomConversion::DataVisitor visitor( object, cache );
37  metaclass.visit(visitor);
38  ::fwAtoms::Object::sptr atom = visitor.getAtomObject();
39 
40  ::fwData::Graph::sptr graph = ::fwData::Graph::dynamicCast(object);
41 
42  ::fwAtoms::Sequence::sptr sequence = ::fwAtoms::Sequence::New();
43 
44  typedef ::fwData::Graph::ConnectionContainer GraphConnections;
45  ::fwAtoms::Object::sptr value;
46  for( GraphConnections::value_type elem : graph->getConnections() )
47  {
48  value = ::fwAtoms::Object::New();
49 
50  value->setMetaInfo("ID_METAINFO", ::fwTools::UUID::generateUUID());
51 
52  value->setAttribute("edge", ::fwAtomConversion::convert(elem.first, cache));
53  value->setAttribute("source", ::fwAtomConversion::convert(elem.second.first, cache));
54  value->setAttribute("destination", ::fwAtomConversion::convert(elem.second.second, cache));
55 
56  sequence->push_back(value);
57  }
58 
59  atom->setAttribute("connections", sequence );
60 
61  return atom;
62 }
63 
64 //-----------------------------------------------------------------------------
65 
66 ::fwData::Object::sptr Graph::convert( ::fwAtoms::Object::sptr atom,
67  AtomVisitor::DataCacheType& cache,
68  const AtomVisitor::IReadPolicy& uuidPolicy
69  )
70 {
71  ::fwAtomConversion::AtomVisitor visitor( atom, cache, uuidPolicy );
72  visitor.visit();
73  ::fwData::Object::sptr data = visitor.getDataObject();
74  ::fwData::Graph::sptr graph = ::fwData::Graph::dynamicCast(data);
75 
76  ::fwAtoms::Sequence::sptr seqAtom = ::fwAtoms::Sequence::dynamicCast( atom->getAttribute("connections") );
77  for( ::fwAtoms::Base::sptr elemAtom : seqAtom->getValue() )
78  {
79  FW_RAISE_EXCEPTION_IF( exception::ConversionNotManaged(
80  "sub atoms stored in fwAtom::Sequence 'connections' must be atom objects"),
81  elemAtom->type() != ::fwAtoms::Base::OBJECT );
82 
83  ::fwAtoms::Object::sptr objectAtom = ::fwAtoms::Object::dynamicCast( elemAtom );
84  ::fwAtoms::Object::sptr edgeAtom = ::fwAtoms::Object::dynamicCast( objectAtom->getAttribute("edge") );
85  ::fwData::Edge::sptr edge =
86  ::fwData::Edge::dynamicCast( ::fwAtomConversion::convert( edgeAtom, cache, uuidPolicy ) );
87 
88  ::fwAtoms::Object::sptr srcAtom = ::fwAtoms::Object::dynamicCast( objectAtom->getAttribute("source") );
89  ::fwData::Node::sptr src =
90  ::fwData::Node::dynamicCast( ::fwAtomConversion::convert( srcAtom, cache, uuidPolicy ) );
91 
92  ::fwAtoms::Object::sptr destAtom = ::fwAtoms::Object::dynamicCast( objectAtom->getAttribute("destination") );
93  ::fwData::Node::sptr dest =
94  ::fwData::Node::dynamicCast( ::fwAtomConversion::convert( destAtom, cache, uuidPolicy ) );
95 
96  graph->addEdge( edge, src, dest );
97  }
98 
99  return graph;
100 }
101 
102 //-----------------------------------------------------------------------------
103 
104 } //namespace mapper
105 } //namespace fwAtomConversion
This class defines a graph object.
This namespace contains the necessary class for fwData <-> fwAtoms conversion.
FWATOMCONVERSION_API std::shared_ptr< ::fwAtoms::Object > convert(const std::shared_ptr< ::fwData::Object > &data)
Helper to convert a fwData::Object to a fwAtoms::Object.
Visitor used to convert a fwData to a fwAtoms. fwData camp property names are used like key to store ...
Definition: DataVisitor.hpp:38
virtual FWATOMCONVERSION_API std::shared_ptr< ::fwAtoms::Object > convert(std::shared_ptr< ::fwData::Object > object, DataVisitor::AtomCacheType &cache)
Convert a fwData::Object to a fwAtoms::Object.
This class is used to convert a fwAtoms to a fwData.
Definition: AtomVisitor.hpp:31
static FWTOOLS_API UUIDType generateUUID()
Return a new extended UUID;.
Definition: UUID.cpp:114