]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/ProcessObject.cxx
Moved to version 1.0
[cpPlugins.git] / lib / cpPlugins / ProcessObject.cxx
1 // =========================================================================
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // =========================================================================
4
5 #include <cpPlugins/ProcessObject.h>
6 #include <cpPlugins/DataObject.h>
7 #include <cpPlugins/Pipeline.h>
8 #include <boost/property_tree/ptree.hpp>
9
10 // -------------------------------------------------------------------------
11 const std::string& cpPlugins::ProcessObject::
12 GetName( ) const
13 {
14   return( this->m_Name );
15 }
16
17 // -------------------------------------------------------------------------
18 void cpPlugins::ProcessObject::
19 SetName( const std::string& name )
20 {
21   if( this->m_Name != name )
22   {
23     this->m_Name = name;
24     this->Modified( );
25   } // end if
26 }
27
28 // -------------------------------------------------------------------------
29 void cpPlugins::ProcessObject::
30 Print( std::ostream& o ) const
31 {
32   this->Superclass::Print( o );
33   this->TParameters::Print( o );
34 }
35
36 // -------------------------------------------------------------------------
37 void cpPlugins::ProcessObject::
38 SetPipeline( cpPlugins::Pipeline* p )
39 {
40   if( p != NULL )
41     this->m_Pipeline = p->CastWeakPtr< cpPlugins::Pipeline >( );
42   else
43     this->m_Pipeline.reset( );
44 }
45
46 // -------------------------------------------------------------------------
47 void cpPlugins::ProcessObject::
48 SetInput( const std::string& name, cpPlugins::DataObject* data )
49 {
50   TInputs::iterator i = this->m_Inputs.find( name );
51   if( i != this->m_Inputs.end( ) )
52   {
53     if( !( i->second.Set( data ) ) )
54       cpPluginsErrorMacro(
55         this, << "Input \"" << name << "\" does not match to input data."
56         );
57   }
58   else
59     cpPluginsErrorMacro(
60       this, << "Input \"" << name << "\" does not belong to this filter."
61       );
62 }
63
64 // -------------------------------------------------------------------------
65 cpPlugins::DataObject* cpPlugins::ProcessObject::
66 GetInput( const std::string& name, const unsigned int id )
67 {
68   TInputs::iterator i = this->m_Inputs.find( name );
69   if( i != this->m_Inputs.end( ) )
70   {
71     if( id < i->second.Count( ) )
72       return( i->second.Get( id ) );
73     else
74       cpPluginsErrorMacro(
75         this, << "Input \"" << name << "\" does have enough inputs."
76         );
77   }
78   else
79     cpPluginsErrorMacro(
80       this, << "Input \"" << name << "\" does not belong to this filter."
81       );
82 }
83
84 // -------------------------------------------------------------------------
85 const cpPlugins::DataObject* cpPlugins::ProcessObject::
86 GetInput( const std::string& name, const unsigned int id ) const
87 {
88   TInputs::const_iterator i = this->m_Inputs.find( name );
89   if( i != this->m_Inputs.end( ) )
90   {
91     if( id < i->second.Count( ) )
92       return( i->second.Get( id ) );
93     else
94       cpPluginsErrorMacro(
95         this, << "Input \"" << name << "\" does have enough inputs."
96         );
97   }
98   else
99     cpPluginsErrorMacro(
100       this, << "Input \"" << name << "\" does not belong to this filter."
101       );
102 }
103
104 // -------------------------------------------------------------------------
105 bool cpPlugins::ProcessObject::
106 HasInput( const std::string& name ) const
107 {
108   TInputs::const_iterator i = this->m_Inputs.find( name );
109   return( i != this->m_Inputs.end( ) );
110 }
111
112 // -------------------------------------------------------------------------
113 std::set< std::string > cpPlugins::ProcessObject::
114 GetInputsNames( ) const
115 {
116   std::set< std::string > n;
117   TInputs::const_iterator i = this->m_Inputs.begin( );
118   for( ; i != this->m_Inputs.end( ); ++i )
119     n.insert( i->first );
120   return( n );
121 }
122
123 // -------------------------------------------------------------------------
124 unsigned int cpPlugins::ProcessObject::
125 GetNumberOfInputs( ) const
126 {
127   return( this->m_Inputs.size( ) );
128 }
129
130 // -------------------------------------------------------------------------
131 unsigned int cpPlugins::ProcessObject::
132 GetNumberOfInputs( const std::string& name ) const
133 {
134   TInputs::const_iterator i = this->m_Inputs.find( name );
135   if( i != this->m_Inputs.end( ) )
136     return( i->second.Count( ) );
137   else
138     return( 0 );
139 }
140
141 // -------------------------------------------------------------------------
142 std::set< std::string > cpPlugins::ProcessObject::
143 GetAllInputsNames( ) const
144 {
145   std::set< std::string > r = this->GetInputsNames( );
146   std::set< std::string > iScalar = this->GetInValueNames( );
147   std::set< std::string > iSet = this->GetInSetNames( );
148   std::set< std::string > iSequence = this->GetInSequenceNames( );
149   for( const std::string& s: iScalar )
150     r.insert( s );
151   for( const std::string& s: iSet )
152     r.insert( s );
153   for( const std::string& s: iSequence )
154     r.insert( s );
155
156   return( r );
157 }
158
159 // -------------------------------------------------------------------------
160 std::set< std::string > cpPlugins::ProcessObject::
161 GetAllOutputsNames( ) const
162 {
163   std::set< std::string > r = this->GetOutValueNames( );
164   std::set< std::string > oSet = this->GetOutSetNames( );
165   std::set< std::string > oSequence = this->GetOutSequenceNames( );
166   for( const std::string& s: oSet )
167     r.insert( s );
168   for( const std::string& s: oSequence )
169     r.insert( s );
170
171   return( r );
172 }
173
174 // -------------------------------------------------------------------------
175 bool cpPlugins::ProcessObject::
176 GetExecutionDebug( ) const
177 {
178   return( this->m_ExecutionDebug );
179 }
180
181 // -------------------------------------------------------------------------
182 void cpPlugins::ProcessObject::
183 SetExecutionDebug( bool d )
184 {
185   this->m_ExecutionDebug = d;
186 }
187
188 // -------------------------------------------------------------------------
189 void cpPlugins::ProcessObject::
190 ExecutionDebugOn( )
191 {
192   this->SetExecutionDebug( true );
193 }
194
195 // -------------------------------------------------------------------------
196 void cpPlugins::ProcessObject::
197 ExecutionDebugOff( )
198 {
199   this->SetExecutionDebug( false );
200 }
201
202 // -------------------------------------------------------------------------
203 const double& cpPlugins::ProcessObject::
204 GetLastDuration( ) const
205 {
206   return( this->m_LastDuration );
207 }
208
209 // -------------------------------------------------------------------------
210 bool cpPlugins::ProcessObject::
211 IsUpdated( const TTimeStamp& d ) const
212 {
213   bool ok = this->Superclass::IsUpdated( d );
214   if( ok )
215   {
216     for( const TInputs::value_type& i: this->m_Inputs )
217     {
218       for( unsigned long j = 0; j < i.second.Count( ); ++j )
219       {
220         const cpPlugins::DataObject* dObj = i.second.Get( j );
221         if( dObj != NULL )
222           ok &= dObj->IsUpdated( d );
223
224       } // end for
225     } // end for
226   } // end if
227   return( ok );
228 }
229
230 // -------------------------------------------------------------------------
231 void cpPlugins::ProcessObject::
232 Modified( )
233 {
234   this->m_LastDuration = -1;
235   this->Superclass::Modified( );
236 }
237
238 // -------------------------------------------------------------------------
239 void cpPlugins::ProcessObject::
240 ModifiedInValue( const std::string& name )
241 {
242   // WARNING: Synch parameters in cpPlugins::Pipeline
243   this->Modified( );
244 }
245
246 // -------------------------------------------------------------------------
247 void cpPlugins::ProcessObject::
248 ModifiedInSet( const std::string& name )
249 {
250   // WARNING: Synch parameters in cpPlugins::Pipeline
251   this->Modified( );
252 }
253
254 // -------------------------------------------------------------------------
255 void cpPlugins::ProcessObject::
256 ModifiedInSequence( const std::string& name )
257 {
258   // WARNING: Synch parameters in cpPlugins::Pipeline
259   this->Modified( );
260 }
261
262 // -------------------------------------------------------------------------
263 void cpPlugins::ProcessObject::
264 ModifiedOutValue( const std::string& name )
265 {
266   // WARNING: Synch parameters in cpPlugins::Pipeline
267   this->Modified( );
268 }
269
270 // -------------------------------------------------------------------------
271 void cpPlugins::ProcessObject::
272 ModifiedOutSet( const std::string& name )
273 {
274   // WARNING: Synch parameters in cpPlugins::Pipeline
275   this->Modified( );
276 }
277
278 // -------------------------------------------------------------------------
279 void cpPlugins::ProcessObject::
280 ModifiedOutSequence( const std::string& name )
281 {
282   // WARNING: Synch parameters in cpPlugins::Pipeline
283   this->Modified( );
284 }
285
286 // -------------------------------------------------------------------------
287 void cpPlugins::ProcessObject::
288 ModifiedChoice( const std::string& name )
289 {
290   // WARNING: Synch parameters in cpPlugins::Pipeline
291   this->Modified( );
292 }
293
294 // -------------------------------------------------------------------------
295 void cpPlugins::ProcessObject::
296 Update( )
297 {
298   bool is_updated = this->IsUpdated( );
299   is_updated &= this->TParameters::Update( );
300
301   // Check inputs
302   for( TInputs::value_type i: this->m_Inputs )
303   {
304     if( i.second.IsValid( ) )
305     {
306       for( unsigned long j = 0; j < i.second.Count( ); ++j )
307       {
308         cpPlugins::DataObject* dObj = i.second.Get( j );
309         if( dObj )
310         {
311           if( !( dObj->IsUpdated( ) ) )
312           {
313             dObj->Update( );
314             is_updated = false;
315
316           } // end if
317         } // end if
318       } // end for
319     } // end if
320   } // end for
321
322   // Allow some updates in particular cases (i.e. Functors)
323   is_updated &= this->_BeforeUpdate( );
324
325   // Update outputs
326   if( !is_updated )
327   {
328     if( this->m_ExecutionDebug )
329     {
330       *( this->m_OutStream )
331         << "Executing node \"" << this->GetName( ) << "\"... "
332         << std::ends;
333       this->m_OutStream->flush( );
334     } // end if
335     std::chrono::steady_clock::time_point start =
336       std::chrono::steady_clock::now( );
337     this->_GenerateData( );
338     std::chrono::steady_clock::time_point end =
339       std::chrono::steady_clock::now( );
340     this->m_LastDuration =
341       double(
342         std::chrono::duration_cast< std::chrono::microseconds >(
343         end - start
344           ).count( )
345         ) * 1e-6;
346     if( this->m_ExecutionDebug )
347     {
348       *( this->m_OutStream )
349         << "done in "
350         << this->m_LastDuration << " s"
351         << std::endl;
352       this->m_OutStream->flush( );
353     } // end if
354     this->_SynchTime( );
355   } // end if
356 }
357
358 // -------------------------------------------------------------------------
359 template< >
360 void cpPlugins::ProcessObject::
361 SaveXML( boost::property_tree::ptree* node ) const
362 {
363   boost::property_tree::ptree xml;
364   xml.put( "<xmlattr>.name", this->GetName( ) );
365   xml.put( "<xmlattr>.class", this->GetClassName( ) );
366   this->TParameters::SaveXML( &xml );
367   node->add_child( "pipeline.node", xml );
368 }
369
370 // -------------------------------------------------------------------------
371 cpPlugins::ProcessObject::
372 ProcessObject( )
373   : Superclass( ),
374     TParameters( ),
375     m_OutStream( &std::cout ),
376     m_ExecutionDebug( false ),
377     m_LastDuration( -1 )
378 {
379 }
380
381 // -------------------------------------------------------------------------
382 cpPlugins::ProcessObject::
383 ~ProcessObject( )
384 {
385 }
386
387 // -------------------------------------------------------------------------
388 bool cpPlugins::ProcessObject::
389 _BeforeUpdate( )
390 {
391   return( true );
392 }
393
394 // eof - $RCSfile$