]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/Workspace.cxx
XML IO added. Workspace singleton added to simplify pipeline definition and execution.
[cpPlugins.git] / lib / cpPlugins / Interface / Workspace.cxx
1 #include <cpPlugins/Interface/Workspace.h>
2
3 // -------------------------------------------------------------------------
4 cpPlugins::Interface::Workspace::
5 Workspace( )
6   : m_LastLoadedPlugin( "" )
7 {
8 }
9
10 // -------------------------------------------------------------------------
11 cpPlugins::Interface::Workspace::
12 ~Workspace( )
13 {
14 }
15
16 // -------------------------------------------------------------------------
17 bool cpPlugins::Interface::Workspace::
18 LoadPluginsPath( const std::string& path, bool r )
19 {
20   // Load all plugins from given folder
21   std::list< std::string > files =
22     this->m_Interface.LoadFromFolder( path, r );
23
24   // Update a simple track
25   bool ret = false;
26   if( files.size( ) > 0 )
27   {
28     for( auto fIt = files.begin( ); fIt != files.end( ); ++fIt )
29     {
30       this->m_LoadedPlugins.insert( *fIt );
31       this->m_LastLoadedPlugin = *fIt;
32
33     } // rof
34     this->_UpdateLoadedPluginsInformation( );
35     ret = true;
36
37   } // fi
38   return( ret );
39 }
40
41 // -------------------------------------------------------------------------
42 bool cpPlugins::Interface::Workspace::
43 LoadPlugins( const std::string& fname )
44 {
45   // Is it already loaded?
46   bool ret = true;
47   if( this->m_LoadedPlugins.find( fname ) == this->m_LoadedPlugins.end( ) )
48   {
49     // Was it succesfully loaded?
50     ret = this->m_Interface.Load( fname );
51
52     // Update a simple track
53     if( ret )
54     {
55       this->m_LoadedPlugins.insert( fname );
56       this->m_LastLoadedPlugin = fname;
57       this->_UpdateLoadedPluginsInformation( );
58
59     } // fi
60
61   } // fi
62   return( ret );
63 }
64
65 // -------------------------------------------------------------------------
66 const cpPlugins::Interface::Workspace::
67 TStringContainer& cpPlugins::Interface::Workspace::
68 GetLoadedPlugins( ) const
69 {
70   return( this->m_LoadedPlugins );
71 }
72
73 // -------------------------------------------------------------------------
74 void cpPlugins::Interface::Workspace::
75 GetLoadedPluginCategories( TStringContainer& categories ) const
76 {
77   categories.clear( );
78   auto fIt = this->m_LoadedFilters.begin( );
79   for( ; fIt != this->m_LoadedFilters.end( ); ++fIt )
80     categories.insert( fIt->first );
81 }
82
83 // -------------------------------------------------------------------------
84 void cpPlugins::Interface::Workspace::
85 GetLoadedPluginFilters( TStringContainer& filters ) const
86 {
87   filters.clear( );
88   auto pIt = this->m_LoadedFilters.begin( );
89   for( ; pIt != this->m_LoadedFilters.end( ); ++pIt )
90     for( auto fIt = pIt->second.begin( ); fIt != pIt->second.end( ); ++fIt )
91       filters.insert( *fIt );
92 }
93
94 // -------------------------------------------------------------------------
95 const cpPlugins::Interface::Workspace::
96 TStringContainer& cpPlugins::Interface::Workspace::
97 GetLoadedPluginFilters( const std::string& category ) const
98 {
99   static const TStringContainer EMPTY;
100   auto pIt = this->m_LoadedFilters.find( category );
101   if( pIt != this->m_LoadedFilters.end( ) )
102     return( pIt->second );
103   else
104     return( EMPTY );
105 }
106
107 // -------------------------------------------------------------------------
108 bool cpPlugins::Interface::Workspace::
109 CreateFilter( const std::string& filter, const std::string& name )
110 {
111   // Get or create new filter from name
112   auto vIt = this->m_Vertices.find( name );
113   if( vIt == this->m_Vertices.end( ) )
114   {
115     TFilter::Pointer o = this->m_Interface.CreateObject( filter );
116     if( o.IsNotNull( ) )
117     {
118       o->SetName( name );
119       this->m_Vertices[ name ] = o;
120       return( true );
121     }
122     else
123       return( false );
124   }
125   else
126     return( true );
127 }
128
129 // -------------------------------------------------------------------------
130 bool cpPlugins::Interface::Workspace::
131 Connect(
132   const std::string& orig_filter, const std::string& dest_filter,
133   const std::string& output_name, const std::string& input_name
134   )
135 {
136   // Get filters
137   auto oIt = this->m_Vertices.find( orig_filter );
138   if( oIt == this->m_Vertices.end( ) )
139     return( false );
140   auto dIt = this->m_Vertices.find( dest_filter );
141   if( dIt == this->m_Vertices.end( ) )
142     return( false );
143   TFilter* orig = dynamic_cast< TFilter* >( oIt->second.GetPointer( ) );
144   TFilter* dest = dynamic_cast< TFilter* >( dIt->second.GetPointer( ) );
145   if( orig == NULL || dest == NULL )
146     return( false );
147
148   // Real connection
149   dest->SetInput( input_name, orig->GetOutput< TData >( output_name ) );
150   this->m_AdjMatrix[ orig_filter ][ dest_filter ].push_back(
151     TConnection( output_name, input_name )
152     );
153   return( false );
154 }
155
156 // -------------------------------------------------------------------------
157 cpPlugins::Interface::Workspace::
158 TParameters* cpPlugins::Interface::Workspace::
159 GetParameters( const std::string& name )
160 {
161   auto vIt = this->m_Vertices.find( name );
162   if( vIt != this->m_Vertices.end( ) )
163   {
164     TFilter* f = dynamic_cast< TFilter* >( vIt->second.GetPointer( ) );
165     if( f != NULL )
166       return( f->GetParameters( ) );
167     else
168       return( NULL );
169   }
170   else
171     return( NULL );
172 }
173
174 // -------------------------------------------------------------------------
175 const cpPlugins::Interface::Workspace::
176 TParameters* cpPlugins::Interface::Workspace::
177 GetParameters( const std::string& name ) const
178 {
179   auto vIt = this->m_Vertices.find( name );
180   if( vIt != this->m_Vertices.end( ) )
181   {
182     const TFilter* f =
183       dynamic_cast< const TFilter* >( vIt->second.GetPointer( ) );
184     if( f != NULL )
185       return( f->GetParameters( ) );
186     else
187       return( NULL );
188   }
189   else
190     return( NULL );
191 }
192
193 // -------------------------------------------------------------------------
194 bool cpPlugins::Interface::Workspace::
195 Reduce( const std::string& name )
196 {
197 }
198
199 // -------------------------------------------------------------------------
200 std::string cpPlugins::Interface::Workspace::
201 Execute( )
202 {
203   // Find sinks
204   std::set< std::string > sinks;
205   auto vIt = this->m_Vertices.begin( );
206   for( ; vIt != this->m_Vertices.end( ); ++vIt )
207     sinks.insert( vIt->first );
208   auto mIt = this->m_AdjMatrix.begin( );
209   for( ; mIt != this->m_AdjMatrix.end( ); ++mIt )
210     sinks.erase( mIt->first );
211
212   // Update sinks
213   std::string err = "";
214   for( auto sIt = sinks.begin( ); sIt != sinks.end( ); ++sIt )
215   {
216     std::string lerr = this->Execute( *sIt );
217     if( lerr != "" )
218       err += lerr + std::string( "\n" );
219
220   } // rof
221   return( err );
222 }
223
224 // -------------------------------------------------------------------------
225 std::string cpPlugins::Interface::Workspace::
226 Execute( const std::string& name )
227 {
228   // Get filter
229   auto vIt = this->m_Vertices.find( name );
230   if( vIt == this->m_Vertices.end( ) )
231     return(
232       std::string( "cpPlugins::Interface::Workspace: No filter \"" ) +
233       name + std::string( "\"" )
234       );
235   TFilter* f = dynamic_cast< TFilter* >( vIt->second.GetPointer( ) );
236   if( f == NULL )
237     return(
238       std::string( "cpPlugins::Interface::Workspace: Vertex \"" ) +
239       name + std::string( "\" is not a filter." )
240       );
241
242   // Execute and return
243   return( f->Update( ) );
244 }
245
246 // -------------------------------------------------------------------------
247 void cpPlugins::Interface::Workspace::
248 _UpdateLoadedPluginsInformation( )
249 {
250   this->m_LoadedFilters.clear( );
251   const TInterface::TClasses& cls = this->m_Interface.GetClasses( );
252   for( auto i = cls.begin( ); i != cls.end( ); ++i )
253   {
254     TFilter::Pointer o = this->m_Interface.CreateObject( i->first );
255     std::string name = o->GetClassName( );
256     std::string category = o->GetClassCategory( );
257     this->m_LoadedFilters[ category ].insert( name );
258
259   } // rof
260 }
261
262 // eof - $RCSfile$