1 // =========================================================================
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // =========================================================================
5 $cmd=help;list;exit;create;describe;define;save;load;connect
7 #include <TinyCon/CompletionConsole.h>
8 #include <cpPlugins/Manager.h>
9 #include <cpPlugins/Pipeline.h>
11 // -------------------------------------------------------------------------
13 : public TinyCon::CompletionConsole
17 typedef TinyCon::CompletionConsole Superclass;
19 typedef cpPlugins::Manager TManager;
20 typedef cpPlugins::Pipeline TPipeline;
21 typedef cpPlugins::ProcessObject TProcessObject;
22 typedef cpPlugins::Filter TFilter;
23 typedef cpPlugins::Functor TFunctor;
24 typedef TManager::TPlugins TPlugins;
27 Console( const std::string& prompt );
30 virtual int trigger( const std::vector< std::string >& args ) override;
32 bool _cmd_help( const std::vector< std::string >& args );
33 bool _cmd_list( const std::vector< std::string >& args );
34 bool _cmd_exit( const std::vector< std::string >& args );
35 bool _cmd_create( const std::vector< std::string >& args );
36 bool _cmd_describe( const std::vector< std::string >& args );
37 bool _cmd_define( const std::vector< std::string >& args );
38 bool _cmd_save( const std::vector< std::string >& args );
39 bool _cmd_load( const std::vector< std::string >& args );
40 bool _cmd_connect( const std::vector< std::string >& args );
43 TTrie* m_CommandsTrie;
49 TPipeline::SharedPtr m_Pipeline;
52 // -------------------------------------------------------------------------
53 int main( int argc, char* argv[] )
55 std::cout << "Welcome to cpPlugins' interactive creator" << std::endl;
56 Console console( "> " );
61 // -------------------------------------------------------------------------
63 Console( const std::string& prompt )
64 : Superclass( prompt )
67 // Add base commands and prepare help
68 this->m_CommandsTrie = new TTrie( );
70 this->addCommand( "{{cmd}}" );
71 this->m_CommandsTrie->insert( "{{cmd}}" );
73 this->addCommand( "help", this->m_CommandsTrie );
75 // Add "create" options
76 this->m_PluginsTrie = new TTrie( );
77 this->m_Manager.Configure( );
78 for( const TPlugins::value_type& p: this->m_Manager.GetPlugins( ) )
79 this->m_PluginsTrie->insert( p.first );
80 this->addCommand( "create", this->m_PluginsTrie );
82 // Add "connect" options
83 this->m_InputsTrie = new TTrie( );
84 this->m_OutputsTrie = new TTrie( );
85 this->addCommand( "connect", this->m_OutputsTrie );
88 this->m_Pipeline = TPipeline::New( );
91 // -------------------------------------------------------------------------
96 delete this->m_CommandsTrie;
97 delete this->m_PluginsTrie;
98 delete this->m_InputsTrie;
99 delete this->m_OutputsTrie;
103 // -------------------------------------------------------------------------
105 trigger( const std::vector< std::string >& args )
108 if( args[ 0 ] == "{{cmd}}" )
109 return( ( this->_cmd_{{cmd}}( args ) )? 0: 1 );
115 // -------------------------------------------------------------------------
117 _cmd_help( const std::vector< std::string >& args )
122 // -------------------------------------------------------------------------
124 _cmd_list( const std::vector< std::string >& args )
126 std::cout << this->m_Manager << std::endl;
130 // -------------------------------------------------------------------------
132 _cmd_exit( const std::vector< std::string >& args )
138 // -------------------------------------------------------------------------
140 _cmd_create( const std::vector< std::string >& args )
143 std::string nClass = "";
144 std::string nName = "";
145 if( args.size( ) > 1 )
148 if( args.size( ) > 2 )
152 this->m_Pipeline->Cast< cpPlugins::Pipeline >( )->
153 CreateNode( nClass, nName );
156 // Prepare "describe" command
157 this->addCommand( "describe", p->GetName( ) );
159 // Prepare "connect" command
160 std::set< std::string > iNames = p->GetAllInputsNames( );
161 for( const std::string& s: iNames )
162 this->m_InputsTrie->insert( p->GetName( ) + "%" + s );
163 std::set< std::string > oNames = p->GetAllOutputsNames( );
164 for( const std::string& s: oNames )
165 this->m_OutputsTrie->insert( p->GetName( ) + "%" + s )->
166 setExtension( this->m_InputsTrie );
168 // Show some information
170 << "Node \"" << p->GetName( ) << "\" of type \""
171 << p->GetClassName( ) << "\" created." << std::endl;
174 std::cout << "Could not create node." << std::endl;
178 << "Invalid \"create\" command: create nClass [nName]"
183 // -------------------------------------------------------------------------
185 _cmd_describe( const std::vector< std::string >& args )
187 if( args.size( ) > 1 )
190 this->m_Pipeline->Cast< cpPlugins::Pipeline >( )->GetNode( args[ 1 ] );
192 p->Print( std::cout );
195 << "Node \"" << args[ 1 ] << "\" does not exists."
200 << "Invalid \"describe\" command: describe nName"
205 // -------------------------------------------------------------------------
207 _cmd_define( const std::vector< std::string >& args )
209 if( args.size( ) > 3 )
211 if( args[ 1 ] == "input" )
213 std::cout << "TODO input" << std::endl;
215 else if( args[ 1 ] == "output" )
217 std::cout << "TODO output" << std::endl;
221 << "Invalid \"define\" command: define [input/output] Port%Node node"
225 this->m_Pipeline->Cast< cpPlugins::Pipeline >( )->GetNode( args[ 1 ] );
227 p->Print( std::cout );
230 << "Node \"" << args[ 1 ] << "\" does not exists."
236 << "Invalid \"define\" command: define [input/output] Port%Node node"
241 // -------------------------------------------------------------------------
243 _cmd_save( const std::vector< std::string >& args )
245 if( args.size( ) > 1 )
249 this->m_Pipeline->Cast< TPipeline >( )->SaveXML( args[ 1 ] );
251 catch( std::exception& err )
253 std::cout << "Error caught: " << err.what( ) << std::endl;
258 << "Invalid \"save\" command: save filename"
263 // -------------------------------------------------------------------------
265 _cmd_load( const std::vector< std::string >& args )
267 if( args.size( ) > 1 )
271 this->m_Pipeline->Cast< TPipeline >( )->LoadXML( args[ 1 ] );
273 // TODO: update tries!!!
275 catch( std::exception& err )
277 std::cout << "Error caught: " << err.what( ) << std::endl;
282 << "Invalid \"save\" command: save filename"
287 // -------------------------------------------------------------------------
289 _cmd_connect( const std::vector< std::string >& args )
291 std::size_t oPos = args[ 1 ].find_first_of( "%" );
292 std::size_t dPos = args[ 2 ].find_first_of( "%" );
293 std::string oObj = args[ 1 ].substr( 0, oPos );
294 std::string dObj = args[ 2 ].substr( 0, dPos );
295 std::string oPort = args[ 1 ].substr( oPos + 1 );
296 std::string dPort = args[ 2 ].substr( dPos + 1 );
300 this->m_Pipeline->Cast< TPipeline >( )->Connect(
301 args[ 1 ].substr( 0, oPos ),
302 args[ 2 ].substr( 0, dPos ),
303 args[ 1 ].substr( oPos + 1 ),
304 args[ 2 ].substr( dPos + 1 )
307 catch( std::exception& err )
309 std::cout << "Error caught: " << err.what( ) << std::endl;
312 << "Connection between \"" << args[ 1 ]
313 << "\" and \"" << args[ 2 ] << "\" done!"