diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt index 264c6c5712e71e1de16d5ca7166cd70ff31dc7d1..46f9dbb82d6fbbd8b82aaa9ceef8a48093eb2044 100644 --- a/include/CMakeLists.txt +++ b/include/CMakeLists.txt @@ -7,6 +7,7 @@ dynamic-graph-api.h entity.h factory.h interpreter.h +interpreter-helper.h plugin-loader.h pool.h diff --git a/include/dynamic-graph/interpreter-helper.h b/include/dynamic-graph/interpreter-helper.h new file mode 100644 index 0000000000000000000000000000000000000000..c0411543a724a7bcbf420554b7765bc3286593c1 --- /dev/null +++ b/include/dynamic-graph/interpreter-helper.h @@ -0,0 +1,149 @@ +/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + * Copyright Projet JRL-Japan, 2010 + *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + * + * File: interpreter.h + * Project: DYNAMIC-GRAPH + * Author: O. Stasse, + * F. Bleibel, + * N. Mansard + * + * Description + * ============ + * \file This class provides an entry point for any interpreter + * willing to connect to the dynamic-graph. + * + * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ + + + +#ifndef __INTERPRETER_HELPER_HH__ +#define __INTERPRETER_HELPER_HH__ + +/* --------------------------------------------------------------------- */ +/* --- INCLUDE --------------------------------------------------------- */ +/* --------------------------------------------------------------------- */ + +/* DYNAMIC-GRAPH */ +#include <dynamic-graph/signal-base.h> +#include <dynamic-graph/exception-factory.h> +#include <dynamic-graph/pool.h> +#include <dynamic-graph/dynamic-graph-api.h> +#include <dynamic-graph/plugin-loader.h> + +/* --- STD --- */ +#include <string> +#include <map> +#include <sstream> + +/* --- BOOST --- */ +#include <boost/function.hpp> +#include <boost/bind.hpp> + +namespace dynamicgraph { +/* --------------------------------------------------------------------- */ +/* --- CLASS ----------------------------------------------------------- */ +/* --------------------------------------------------------------------- */ + +/*! @ingroup dgraph + \brief This class implements the first level interpretor + to control basic functionnalities of the plugins. + + It provides a shell allowing to : + \li load plugin libraries, + \li create instances of plugin, + \li destroy the plugins, + \li unload the libraries, + \li set a signal, + \li get a signal, + \li compute a signal, +*/ +class DYNAMICGRAPH_EXPORT InterpreterHelper +{ + + public: + /*! \brief Default constructor + \par[in] dlPtr: a plugin loader to perform the actions provided by this shell. + */ + InterpreterHelper( ); + + + protected: + + /*! \brief The plugin loader */ + PluginLoader dlPtr; + + public: + + /*! \name Implements the commands. + @{ + */ + /*! \brief Instanciante an object + Extracts the name and the class of the object, verifies it is unique, + and creates the instance if this is the case. + */ + void cmdNew(const std::string& className, + const std::string& objName, + std::ostream& os); + + /*! \brief Destroy the object. + Destroy the object objName. + */ + void cmdDestroy( const std::string& objName, + std::ostream& os ); + /*! \brief Connect two signals. + Connect an input signal to an output one. + */ + void cmdPlug( const std::string& obj1, const std::string & signame1, + const std::string& obj2, const std::string & signame2, + std::ostream& os ); + + /*! \brief Load a dynamic library which includes a plugin. + Extracts the name first and the directory in second from cmdArg + to load the dynamic library. + */ + void cmdLoadPlugin( const std::string& directory, + const std::string& pluginName, + std::ostream& os ); + + /*! \brief Unload a dynamic library which includes a plugin. + Extracts the name to unload the dynamic library. + */ + void cmdUnloadPlugin( const std::string& pluginName, + std::ostream& os ); + + /*! \brief Set a signal <obj.signal> to a value <value> + with cmdArg = "<obj.signal> <value>" + */ + void cmdSetSignal( const std::string& objname, + const std::string& signame, + const std::string& cmdArg, + std::ostream& os); + + /*! \brief Display the value of the signal <obj.signal> + with cmdArg = "<obj.signal>" + */ + void cmdGetSignal( const std::string& objname, + const std::string& signame, + std::ostream& os); + + /*! \brief Compute the value of the signal <obj.signal> at time <time> + with cmdArg = "<obj.signal> <time>" + */ + void cmdComputeSignal( const std::string& objname, + const std::string& signame, + const int &time, + std::ostream& os ); + +}; + + +} // namespace dynamicgraph + + + +#endif /* #ifndef */ + + + + diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index afefdb4bf0f32db403e0dafa4307060ab66846b6..076776c84f58e2caae1730380a6dfecbcd745ee6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -21,6 +21,7 @@ ADD_LIBRARY(${LIBRARY_NAME} dgraph/entity.cpp dgraph/factory.cpp dgraph/interpreter.cpp + dgraph/interpreter-helper.cpp dgraph/plugin-loader.cpp dgraph/pool.cpp diff --git a/src/dgraph/interpreter-helper.cpp b/src/dgraph/interpreter-helper.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c0135f49d2bbf232aa676937927bd631e643354e --- /dev/null +++ b/src/dgraph/interpreter-helper.cpp @@ -0,0 +1,187 @@ +/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + * Copyright Projet JRL-Japan, 2007 + *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + * + * File: interpretor.cpp + * Project: DYNAMIC-GRAPH + * Author: François Bleibel, Nicolas Mansard + * + * Version control + * =============== + * + * $Id$ + * + * Description + * ============ + * + * + * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ + + +/* --------------------------------------------------------------------- */ +/* --- INCLUDE --------------------------------------------------------- */ +/* --------------------------------------------------------------------- */ + +/* DYNAMIC-GRAPH */ +#include <dynamic-graph/interpreter-helper.h> +#include <dynamic-graph/plugin-loader.h> +#include <dynamic-graph/debug.h> + +/* --- STD --- */ +using namespace std; +using namespace dynamicgraph; + +/* --------------------------------------------------------------------- */ +/* --- CLASS ----------------------------------------------------------- */ +/* --------------------------------------------------------------------- */ + + +InterpreterHelper::InterpreterHelper() +{ +} + + +/* --------------------------------------------------------------------- */ +/* --- NEW ------------------------------------------------------------- */ +/* --------------------------------------------------------------------- */ +#include <dynamic-graph/factory.h> +using namespace std; +#include <dynamic-graph/entity.h> +#include <dynamic-graph/signal-base.h> + +void InterpreterHelper:: +cmdPlug( const std::string& obj1, const std::string & signame1, + const std::string& obj2, const std::string & signame2, + std::ostream& os ) +{ + dgDEBUG(20) << "Get Ent1 <"<<obj1<<"> ."<<endl; + Entity& ent1 = g_pool.getEntity(obj1); + dgDEBUG(20) << "Get Sig1 <"<<signame1<<"> ."<<endl; + SignalBase<int> &sig1 = ent1.getSignal(signame1); + + dgDEBUG(20) << "Get Ent2 <"<<obj2<<"> ."<<endl; + Entity& ent2 = g_pool.getEntity(obj2); + dgDEBUG(20) << "Get Sig2 <"<<signame2<<"> ."<<endl; + SignalBase<int> &sig2 = ent2.getSignal(signame2); + + dgDEBUG(25) << "Plug..."<<endl; + sig2.plug(&sig1); +} + +void InterpreterHelper:: +cmdNew( const std::string& className, + const std::string& objName, + std::ostream& os ) +{ + dgDEBUG(15) << "New <" << className<<"> requested."<<endl; + if( g_factory.existEntity( className ) ) + { + dgDEBUG(15) << "New entity<"<<className<<"> " <<objName<<std::endl; + g_factory.newEntity(className,objName); + } + else os << " !! Class <" << className << "> does not exist."<<endl; +} + + +void InterpreterHelper:: +cmdDestroy( const std::string& objName, + std::ostream& os) +{ + + dgDEBUG(15) << "Destroy <" << objName <<"> requested."<<endl; + delete &( g_pool.getEntity( objName ) ); + +} + +void InterpreterHelper:: +cmdLoadPlugin( const std::string& directory, + const std::string& pluginName, + std::ostream& os ) +{ + + if( directory.length() != 0 ) dlPtr.setDirectory( directory ); + dlPtr.addPlugin( pluginName ); + + try{ + dgDEBUG(15) << "Try to load " << pluginName<< endl; + dlPtr.loadPlugins(); + }catch( ExceptionAbstract& e ) { dgDEBUG(5) << e << endl; throw e; } +} + +void InterpreterHelper:: +cmdUnloadPlugin( const std::string& pluginName, + std::ostream& os ) +{ + + dgDEBUGIN(15); + try{ + dgDEBUG(25) << "Try short name " << pluginName << endl; + const std::string& fullname = dlPtr.searchPlugin(pluginName); + dgDEBUG(25) << "Full name " << fullname << endl; + dlPtr.unloadPlugin(fullname); + } + catch(...) + { + dgDEBUG(25) << "Full name " << pluginName << endl; + dlPtr.unloadPlugin(pluginName); + } + + dgDEBUGOUT(15); +} + +void InterpreterHelper:: +cmdSetSignal( const std::string& objname, + const std::string& signame, + const std::string& value, + std::ostream& os ) +{ + dgDEBUGIN(15); + + Entity& obj = g_pool.getEntity(objname); + SignalBase<int>& sig = obj.getSignal( signame ); + + istringstream cmdArg(value); + sig.set( cmdArg ); + + dgDEBUGOUT(15); + +} + +void InterpreterHelper:: +cmdGetSignal( const std::string& objname, + const std::string& signame, + std::ostream& os ) +{ + dgDEBUGIN(15); + + Entity& obj = g_pool.getEntity(objname); + SignalBase<int>& sig = obj.getSignal( signame ); + + os << signame << " = "; sig.get( os ); + + dgDEBUGOUT(15); + +} + +void InterpreterHelper:: +cmdComputeSignal( const std::string& objname, + const std::string& signame, + const int &time, + std::ostream& os ) +{ + dgDEBUGIN(15); + + Entity& obj = g_pool.getEntity(objname); + SignalBase<int>& sig = obj.getSignal( signame ); + + sig.recompute( time ); + + dgDEBUGOUT(15); + +} + + +/* --------------------------------------------------------------------- */ +/* --------------------------------------------------------------------- */ +/* --------------------------------------------------------------------- */ +