]> Creatis software - cpPlugins.git/blob - appli/bash/InteractivePipelineCreator.cxx.d
Moved to version 1.0
[cpPlugins.git] / appli / bash / InteractivePipelineCreator.cxx.d
1 // =========================================================================
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // =========================================================================
4
5 $cmd=help;list;exit;create;describe;define;save;load;connect
6
7 #include <TinyCon/CompletionConsole.h>
8 #include <cpPlugins/Manager.h>
9 #include <cpPlugins/Pipeline.h>
10
11 // -------------------------------------------------------------------------
12 class Console
13   : public TinyCon::CompletionConsole
14 {
15 public:
16   typedef Console Self;
17   typedef TinyCon::CompletionConsole Superclass;
18
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;
25
26 public:
27   Console( const std::string& prompt );
28   virtual ~Console( );
29
30   virtual int trigger( const std::vector< std::string >& args ) override;
31
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 );
41
42 protected:
43   TTrie* m_CommandsTrie;
44   TTrie* m_PluginsTrie;
45   TTrie* m_InputsTrie;
46   TTrie* m_OutputsTrie;
47
48   TManager m_Manager;
49   TPipeline::SharedPtr m_Pipeline;
50 };
51
52 // -------------------------------------------------------------------------
53 int main( int argc, char* argv[] )
54 {
55   std::cout << "Welcome to cpPlugins' interactive creator" << std::endl;
56   Console console( "> " );
57   console.run( );
58   return( 0 );
59 }
60
61 // -------------------------------------------------------------------------
62 Console::
63 Console( const std::string& prompt )
64   : Superclass( prompt )
65 {
66
67   // Add base commands and prepare help
68   this->m_CommandsTrie = new TTrie( );
69   {{#cmd}}
70   this->addCommand( "{{cmd}}" );
71   this->m_CommandsTrie->insert( "{{cmd}}" );
72   {{/cmd}}
73   this->addCommand( "help", this->m_CommandsTrie );
74
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 );
81
82   // Add "connect" options
83   this->m_InputsTrie = new TTrie( );
84   this->m_OutputsTrie = new TTrie( );
85   this->addCommand( "connect", this->m_OutputsTrie );
86
87   // Prepare pipeline
88   this->m_Pipeline = TPipeline::New( );
89 }
90
91 // -------------------------------------------------------------------------
92 Console::
93 ~Console( )
94 {
95   /* TODO
96      delete this->m_CommandsTrie;
97      delete this->m_PluginsTrie;
98      delete this->m_InputsTrie;
99      delete this->m_OutputsTrie;
100   */
101 }
102
103 // -------------------------------------------------------------------------
104 int Console::
105 trigger( const std::vector< std::string >& args )
106 {
107   {{#cmd}}
108   if( args[ 0 ] == "{{cmd}}" )
109     return( ( this->_cmd_{{cmd}}( args ) )? 0: 1 );
110   else
111   {{/cmd}}
112   return( 0 );
113 }
114
115 // -------------------------------------------------------------------------
116 bool Console::
117 _cmd_help( const std::vector< std::string >& args )
118 {
119   return( true );
120 }
121
122 // -------------------------------------------------------------------------
123 bool Console::
124 _cmd_list( const std::vector< std::string >& args )
125 {
126   std::cout << this->m_Manager << std::endl;
127   return( true );
128 }
129
130 // -------------------------------------------------------------------------
131 bool Console::
132 _cmd_exit( const std::vector< std::string >& args )
133 {
134   this->quit( );
135   return( false );
136 }
137
138 // -------------------------------------------------------------------------
139 bool Console::
140 _cmd_create( const std::vector< std::string >& args )
141 {
142   // Get arguments
143   std::string nClass = "";
144   std::string nName = "";
145   if( args.size( ) > 1 )
146   {
147     nClass = args[ 1 ];
148     if( args.size( ) > 2 )
149       nName = args[ 2 ];
150
151     TProcessObject* p =
152       this->m_Pipeline->Cast< cpPlugins::Pipeline >( )->
153       CreateNode( nClass, nName );
154     if( p != NULL )
155     {
156       // Prepare "describe" command
157       this->addCommand( "describe", p->GetName( ) );
158
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 );
167
168       // Show some information
169       std::cout
170         << "Node \"" << p->GetName( ) << "\" of type \""
171         << p->GetClassName( ) << "\" created." << std::endl;
172     }
173     else
174       std::cout << "Could not create node." << std::endl;
175   }
176   else
177     std::cout
178       << "Invalid \"create\" command: create nClass [nName]"
179       << std::endl;
180   return( true );
181 }
182
183 // -------------------------------------------------------------------------
184 bool Console::
185 _cmd_describe( const std::vector< std::string >& args )
186 {
187   if( args.size( ) > 1 )
188   {
189     TProcessObject* p =
190       this->m_Pipeline->Cast< cpPlugins::Pipeline >( )->GetNode( args[ 1 ] );
191     if( p != NULL )
192       p->Print( std::cout );
193     else
194       std::cout
195         << "Node \"" << args[ 1 ] << "\" does not exists."
196         << std::endl;
197   }
198   else
199     std::cout
200       << "Invalid \"describe\" command: describe nName"
201       << std::endl;
202   return( true );
203 }
204
205 // -------------------------------------------------------------------------
206 bool Console::
207 _cmd_define( const std::vector< std::string >& args )
208 {
209   if( args.size( ) > 3 )
210   {
211     if( args[ 1 ] == "input" )
212     {
213       std::cout << "TODO input" << std::endl;
214     }
215     else if( args[ 1 ] == "output" )
216     {
217       std::cout << "TODO output" << std::endl;
218     }
219     else
220       std::cout
221         << "Invalid \"define\" command: define [input/output] Port%Node node"
222         << std::endl;
223     /* TODO
224        TProcessObject* p =
225        this->m_Pipeline->Cast< cpPlugins::Pipeline >( )->GetNode( args[ 1 ] );
226        if( p != NULL )
227        p->Print( std::cout );
228        else
229        std::cout
230        << "Node \"" << args[ 1 ] << "\" does not exists."
231        << std::endl;
232     */
233   }
234   else
235     std::cout
236       << "Invalid \"define\" command: define [input/output] Port%Node node"
237       << std::endl;
238   return( true );
239 }
240
241 // -------------------------------------------------------------------------
242 bool Console::
243 _cmd_save( const std::vector< std::string >& args )
244 {
245   if( args.size( ) > 1 )
246   {
247     try
248     {
249       this->m_Pipeline->Cast< TPipeline >( )->SaveXML( args[ 1 ] );
250     }
251     catch( std::exception& err )
252     {
253       std::cout << "Error caught: " << err.what( ) << std::endl;
254     } // end try
255   }
256   else
257     std::cout
258       << "Invalid \"save\" command: save filename"
259       << std::endl;
260   return( true );
261 }
262
263 // -------------------------------------------------------------------------
264 bool Console::
265 _cmd_load( const std::vector< std::string >& args )
266 {
267   if( args.size( ) > 1 )
268   {
269     try
270     {
271       this->m_Pipeline->Cast< TPipeline >( )->LoadXML( args[ 1 ] );
272
273       // TODO: update tries!!!
274     }
275     catch( std::exception& err )
276     {
277       std::cout << "Error caught: " << err.what( ) << std::endl;
278     } // end try
279   }
280   else
281     std::cout
282       << "Invalid \"save\" command: save filename"
283       << std::endl;
284   return( true );
285 }
286
287 // -------------------------------------------------------------------------
288 bool Console::
289 _cmd_connect( const std::vector< std::string >& args )
290 {
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 );
297
298   try
299   {
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 )
305       );
306   }
307   catch( std::exception& err )
308   {
309     std::cout << "Error caught: " << err.what( ) << std::endl;
310   } // end try
311   std::cout
312     << "Connection between \"" << args[ 1 ]
313     << "\" and \"" << args[ 2 ] << "\" done!"
314     << std::endl;
315
316   return( true );
317 }
318
319 // eof - $RCSfile$