]> Creatis software - cpPlugins.git/blobdiff - lib/cpPlugins/ProcessObject.cxx
Moved to version 1.0
[cpPlugins.git] / lib / cpPlugins / ProcessObject.cxx
diff --git a/lib/cpPlugins/ProcessObject.cxx b/lib/cpPlugins/ProcessObject.cxx
new file mode 100644 (file)
index 0000000..0c6b45c
--- /dev/null
@@ -0,0 +1,394 @@
+// =========================================================================
+// @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
+// =========================================================================
+
+#include <cpPlugins/ProcessObject.h>
+#include <cpPlugins/DataObject.h>
+#include <cpPlugins/Pipeline.h>
+#include <boost/property_tree/ptree.hpp>
+
+// -------------------------------------------------------------------------
+const std::string& cpPlugins::ProcessObject::
+GetName( ) const
+{
+  return( this->m_Name );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::ProcessObject::
+SetName( const std::string& name )
+{
+  if( this->m_Name != name )
+  {
+    this->m_Name = name;
+    this->Modified( );
+  } // end if
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::ProcessObject::
+Print( std::ostream& o ) const
+{
+  this->Superclass::Print( o );
+  this->TParameters::Print( o );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::ProcessObject::
+SetPipeline( cpPlugins::Pipeline* p )
+{
+  if( p != NULL )
+    this->m_Pipeline = p->CastWeakPtr< cpPlugins::Pipeline >( );
+  else
+    this->m_Pipeline.reset( );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::ProcessObject::
+SetInput( const std::string& name, cpPlugins::DataObject* data )
+{
+  TInputs::iterator i = this->m_Inputs.find( name );
+  if( i != this->m_Inputs.end( ) )
+  {
+    if( !( i->second.Set( data ) ) )
+      cpPluginsErrorMacro(
+        this, << "Input \"" << name << "\" does not match to input data."
+        );
+  }
+  else
+    cpPluginsErrorMacro(
+      this, << "Input \"" << name << "\" does not belong to this filter."
+      );
+}
+
+// -------------------------------------------------------------------------
+cpPlugins::DataObject* cpPlugins::ProcessObject::
+GetInput( const std::string& name, const unsigned int id )
+{
+  TInputs::iterator i = this->m_Inputs.find( name );
+  if( i != this->m_Inputs.end( ) )
+  {
+    if( id < i->second.Count( ) )
+      return( i->second.Get( id ) );
+    else
+      cpPluginsErrorMacro(
+        this, << "Input \"" << name << "\" does have enough inputs."
+        );
+  }
+  else
+    cpPluginsErrorMacro(
+      this, << "Input \"" << name << "\" does not belong to this filter."
+      );
+}
+
+// -------------------------------------------------------------------------
+const cpPlugins::DataObject* cpPlugins::ProcessObject::
+GetInput( const std::string& name, const unsigned int id ) const
+{
+  TInputs::const_iterator i = this->m_Inputs.find( name );
+  if( i != this->m_Inputs.end( ) )
+  {
+    if( id < i->second.Count( ) )
+      return( i->second.Get( id ) );
+    else
+      cpPluginsErrorMacro(
+        this, << "Input \"" << name << "\" does have enough inputs."
+        );
+  }
+  else
+    cpPluginsErrorMacro(
+      this, << "Input \"" << name << "\" does not belong to this filter."
+      );
+}
+
+// -------------------------------------------------------------------------
+bool cpPlugins::ProcessObject::
+HasInput( const std::string& name ) const
+{
+  TInputs::const_iterator i = this->m_Inputs.find( name );
+  return( i != this->m_Inputs.end( ) );
+}
+
+// -------------------------------------------------------------------------
+std::set< std::string > cpPlugins::ProcessObject::
+GetInputsNames( ) const
+{
+  std::set< std::string > n;
+  TInputs::const_iterator i = this->m_Inputs.begin( );
+  for( ; i != this->m_Inputs.end( ); ++i )
+    n.insert( i->first );
+  return( n );
+}
+
+// -------------------------------------------------------------------------
+unsigned int cpPlugins::ProcessObject::
+GetNumberOfInputs( ) const
+{
+  return( this->m_Inputs.size( ) );
+}
+
+// -------------------------------------------------------------------------
+unsigned int cpPlugins::ProcessObject::
+GetNumberOfInputs( const std::string& name ) const
+{
+  TInputs::const_iterator i = this->m_Inputs.find( name );
+  if( i != this->m_Inputs.end( ) )
+    return( i->second.Count( ) );
+  else
+    return( 0 );
+}
+
+// -------------------------------------------------------------------------
+std::set< std::string > cpPlugins::ProcessObject::
+GetAllInputsNames( ) const
+{
+  std::set< std::string > r = this->GetInputsNames( );
+  std::set< std::string > iScalar = this->GetInValueNames( );
+  std::set< std::string > iSet = this->GetInSetNames( );
+  std::set< std::string > iSequence = this->GetInSequenceNames( );
+  for( const std::string& s: iScalar )
+    r.insert( s );
+  for( const std::string& s: iSet )
+    r.insert( s );
+  for( const std::string& s: iSequence )
+    r.insert( s );
+
+  return( r );
+}
+
+// -------------------------------------------------------------------------
+std::set< std::string > cpPlugins::ProcessObject::
+GetAllOutputsNames( ) const
+{
+  std::set< std::string > r = this->GetOutValueNames( );
+  std::set< std::string > oSet = this->GetOutSetNames( );
+  std::set< std::string > oSequence = this->GetOutSequenceNames( );
+  for( const std::string& s: oSet )
+    r.insert( s );
+  for( const std::string& s: oSequence )
+    r.insert( s );
+
+  return( r );
+}
+
+// -------------------------------------------------------------------------
+bool cpPlugins::ProcessObject::
+GetExecutionDebug( ) const
+{
+  return( this->m_ExecutionDebug );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::ProcessObject::
+SetExecutionDebug( bool d )
+{
+  this->m_ExecutionDebug = d;
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::ProcessObject::
+ExecutionDebugOn( )
+{
+  this->SetExecutionDebug( true );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::ProcessObject::
+ExecutionDebugOff( )
+{
+  this->SetExecutionDebug( false );
+}
+
+// -------------------------------------------------------------------------
+const double& cpPlugins::ProcessObject::
+GetLastDuration( ) const
+{
+  return( this->m_LastDuration );
+}
+
+// -------------------------------------------------------------------------
+bool cpPlugins::ProcessObject::
+IsUpdated( const TTimeStamp& d ) const
+{
+  bool ok = this->Superclass::IsUpdated( d );
+  if( ok )
+  {
+    for( const TInputs::value_type& i: this->m_Inputs )
+    {
+      for( unsigned long j = 0; j < i.second.Count( ); ++j )
+      {
+        const cpPlugins::DataObject* dObj = i.second.Get( j );
+        if( dObj != NULL )
+          ok &= dObj->IsUpdated( d );
+
+      } // end for
+    } // end for
+  } // end if
+  return( ok );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::ProcessObject::
+Modified( )
+{
+  this->m_LastDuration = -1;
+  this->Superclass::Modified( );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::ProcessObject::
+ModifiedInValue( const std::string& name )
+{
+  // WARNING: Synch parameters in cpPlugins::Pipeline
+  this->Modified( );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::ProcessObject::
+ModifiedInSet( const std::string& name )
+{
+  // WARNING: Synch parameters in cpPlugins::Pipeline
+  this->Modified( );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::ProcessObject::
+ModifiedInSequence( const std::string& name )
+{
+  // WARNING: Synch parameters in cpPlugins::Pipeline
+  this->Modified( );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::ProcessObject::
+ModifiedOutValue( const std::string& name )
+{
+  // WARNING: Synch parameters in cpPlugins::Pipeline
+  this->Modified( );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::ProcessObject::
+ModifiedOutSet( const std::string& name )
+{
+  // WARNING: Synch parameters in cpPlugins::Pipeline
+  this->Modified( );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::ProcessObject::
+ModifiedOutSequence( const std::string& name )
+{
+  // WARNING: Synch parameters in cpPlugins::Pipeline
+  this->Modified( );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::ProcessObject::
+ModifiedChoice( const std::string& name )
+{
+  // WARNING: Synch parameters in cpPlugins::Pipeline
+  this->Modified( );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::ProcessObject::
+Update( )
+{
+  bool is_updated = this->IsUpdated( );
+  is_updated &= this->TParameters::Update( );
+
+  // Check inputs
+  for( TInputs::value_type i: this->m_Inputs )
+  {
+    if( i.second.IsValid( ) )
+    {
+      for( unsigned long j = 0; j < i.second.Count( ); ++j )
+      {
+        cpPlugins::DataObject* dObj = i.second.Get( j );
+        if( dObj )
+        {
+          if( !( dObj->IsUpdated( ) ) )
+          {
+            dObj->Update( );
+            is_updated = false;
+
+          } // end if
+        } // end if
+      } // end for
+    } // end if
+  } // end for
+
+  // Allow some updates in particular cases (i.e. Functors)
+  is_updated &= this->_BeforeUpdate( );
+
+  // Update outputs
+  if( !is_updated )
+  {
+    if( this->m_ExecutionDebug )
+    {
+      *( this->m_OutStream )
+        << "Executing node \"" << this->GetName( ) << "\"... "
+        << std::ends;
+      this->m_OutStream->flush( );
+    } // end if
+    std::chrono::steady_clock::time_point start =
+      std::chrono::steady_clock::now( );
+    this->_GenerateData( );
+    std::chrono::steady_clock::time_point end =
+      std::chrono::steady_clock::now( );
+    this->m_LastDuration =
+      double(
+        std::chrono::duration_cast< std::chrono::microseconds >(
+        end - start
+          ).count( )
+        ) * 1e-6;
+    if( this->m_ExecutionDebug )
+    {
+      *( this->m_OutStream )
+        << "done in "
+        << this->m_LastDuration << " s"
+        << std::endl;
+      this->m_OutStream->flush( );
+    } // end if
+    this->_SynchTime( );
+  } // end if
+}
+
+// -------------------------------------------------------------------------
+template< >
+void cpPlugins::ProcessObject::
+SaveXML( boost::property_tree::ptree* node ) const
+{
+  boost::property_tree::ptree xml;
+  xml.put( "<xmlattr>.name", this->GetName( ) );
+  xml.put( "<xmlattr>.class", this->GetClassName( ) );
+  this->TParameters::SaveXML( &xml );
+  node->add_child( "pipeline.node", xml );
+}
+
+// -------------------------------------------------------------------------
+cpPlugins::ProcessObject::
+ProcessObject( )
+  : Superclass( ),
+    TParameters( ),
+    m_OutStream( &std::cout ),
+    m_ExecutionDebug( false ),
+    m_LastDuration( -1 )
+{
+}
+
+// -------------------------------------------------------------------------
+cpPlugins::ProcessObject::
+~ProcessObject( )
+{
+}
+
+// -------------------------------------------------------------------------
+bool cpPlugins::ProcessObject::
+_BeforeUpdate( )
+{
+  return( true );
+}
+
+// eof - $RCSfile$