From 639b8d1d31008830145eb0ce68905d846c21c42f Mon Sep 17 00:00:00 2001 From: Olivier Stasse <olivier.stasse@aist.go.jp> Date: Mon, 28 Jun 2010 19:15:03 +0900 Subject: [PATCH] First version for the interpreter-helper. API to access framework functions without sotInterpreter. Should ease the port to more complex interpreters without favoring the internal interpreter. The latter one is here for backward compatibility. It will be removed. This version compile but might not work yet. (not tested). --- include/CMakeLists.txt | 1 + include/dynamic-graph/interpreter-helper.h | 149 ++++++++++++++++ src/CMakeLists.txt | 1 + src/dgraph/interpreter-helper.cpp | 187 +++++++++++++++++++++ 4 files changed, 338 insertions(+) create mode 100644 include/dynamic-graph/interpreter-helper.h create mode 100644 src/dgraph/interpreter-helper.cpp diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt index 264c6c57..46f9dbb8 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 00000000..c0411543 --- /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 afefdb4b..076776c8 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 00000000..c0135f49 --- /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); + +} + + +/* --------------------------------------------------------------------- */ +/* --------------------------------------------------------------------- */ +/* --------------------------------------------------------------------- */ + -- GitLab