41 #include "../Logger.hpp" 42 #include "../TaskContext.hpp" 47 #include "scripting/rtt-scripting-config.h" 50 #include "../Service.hpp" 53 #include "../OperationCaller.hpp" 54 #include "../internal/mystd.hpp" 55 #include "../plugin/ServicePlugin.hpp" 56 #include "../internal/GlobalEngine.hpp" 62 using namespace detail;
67 parent->
provides()->addService( sp );
75 this->
doc(
"Orocos Scripting service. Use this service in order to load or query programs or state machines.");
79 .doc(
"If this is set to false, the warning log when loading a program or a state machine into a Component" 80 " with a null period will not be printed. Be sure you have something else triggering periodically" 81 " your Component activity unless your script may not work.");
93 while ( !
states.empty() ) {
135 if ( it !=
states.end() ) {
136 return it->second->getStatus();
170 return "deactivating";
188 if ( sc->getParent() ) {
190 "Could not register StateMachine \"" + sc->getName() +
191 "\" in the ScriptingService. It is not a root StateMachine." );
199 log(
Warning) <<
"Loading StateMachine "<< sc->getName()
200 <<
" in a TaskContext with getPeriod() == 0." 201 <<
" Use setPeriod(period) in order to setup execution of scripts." 202 <<
" If you know what you are doing, you may disable this warning using scripting.ZeroPeriodWarning=false" 216 "Could not register StateMachine \"" + sc->getName() +
217 "\" in the ScriptingService. A StateMachine with that name is already present." );
220 vector<StateMachinePtr>::const_iterator it2;
221 for (it2 = sc->getChildren().begin(); it2 != sc->getChildren().end(); ++it2)
232 vector<StateMachinePtr>::const_iterator it;
235 states[sc->getName()] = sc;
239 for (it = sc->getChildren().begin(); it != sc->getChildren().end(); ++it)
250 if ( it !=
states.end() ) {
252 if ( it->second->getParent() ) {
254 "Could not unload StateMachine \"" + it->first +
255 "\" in the ScriptingService. It is not a root StateMachine." );
269 vector<StateMachinePtr>::const_iterator it2;
270 for (it2 = si->getChildren().begin();
271 it2 != si->getChildren().end();
275 if ( it ==
states.end() ) {
277 "Could not unload StateMachine \"" + si->getName() +
278 "\" in the ScriptingService. It contains not loaded child "+ (*it2)->getName() );
290 for (vector<StateMachinePtr>::const_iterator it = sc->getChildren().begin();
291 it != sc->getChildren().end(); ++it)
297 StateMap::iterator it =
states.find( sc->getName() );
299 assert( it !=
states.end() );
333 return it->second->getStatus();
363 log(
Error) <<
"Could not load Program "<< pi->getName() <<
" in ScriptingService: name already in use."<<
endlog();
367 log(
Warning) <<
"Loading program " << pi->getName()
368 <<
" in a TaskContext with getPeriod() == 0." 369 <<
" Use setPeriod(period) in order to setup execution of scripts." 370 <<
" If you know what you are doing, you may disable this warning using scripting.ZeroPeriodWarning=false" 377 log(
Error) <<
"Could not load Program "<< pi->getName() <<
" in ExecutionEngine."<<
endlog();
385 ProgMap::iterator it =
programs.find(name);
393 string error(
"Could not unload Program \"" + name +
394 "\" in the ScriptingService. It does not exist." );
418 return this->
execute(code) >= 0;
524 ifstream inputfile(file.c_str());
526 Logger::In in(
"ScriptingService::loadFunctions");
531 inputfile.unsetf( ios_base::skipws );
532 istream_iterator<char> streambegin( inputfile );
533 istream_iterator<char> streamend;
534 std::copy( streambegin, streamend, back_inserter( text ) );
541 Logger::In in(
"ScriptingService::loadFunctions");
547 ret = p.parseFunction(code,
mowner, filename);
565 for( Parser::ParsedFunctions::iterator it = ret.begin(); it != ret.end(); ++it) {
571 exec.push_back( *it );
580 ifstream inputfile(file.c_str());
587 inputfile.unsetf( ios_base::skipws );
588 istream_iterator<char> streambegin( inputfile );
589 istream_iterator<char> streamend;
590 std::copy( streambegin, streamend, back_inserter( text ) );
617 ifstream inputfile(file.c_str());
619 Logger::In in(
"ScriptingService::loadProgram");
624 inputfile.unsetf( ios_base::skipws );
625 istream_iterator<char> streambegin( inputfile );
626 istream_iterator<char> streamend;
627 std::copy( streambegin, streamend, back_inserter( text ) );
638 pg_list = parser.parseProgram(code,
mowner, filename );
649 if ( pg_list.empty() )
657 for( Parser::ParsedPrograms::iterator it = pg_list.begin(); it != pg_list.end(); ++it) {
667 errors +=
"Could not load Program '"+ (*it)->getName() +
"' :\n"+e.
what()+
'\n';
673 if (error && mrethrow )
682 Logger::In in(
"ScriptingService::unloadProgram");
702 ifstream inputfile(file.c_str());
704 Logger::In in(
"ScriptingService::loadStateMachine");
709 inputfile.unsetf( ios_base::skipws );
710 istream_iterator<char> streambegin( inputfile );
711 istream_iterator<char> streamend;
712 std::copy( streambegin, streamend, back_inserter( text ) );
718 Logger::In in(
"ScriptingService::loadStateMachine");
723 pg_list = parser.parseStateMachine( code,
mowner, filename );
734 if ( pg_list.empty() )
742 for( Parser::ParsedStateMachines::iterator it = pg_list.begin(); it != pg_list.end(); ++it) {
752 errors +=
"Could not load Program '"+ (*it)->getName() +
"' :\n"+e.
what()+
'\n';
758 if ( error && mrethrow )
768 Logger::In in(
"ScriptingService::unloadStateMachine");
791 return pi ? pi->getLineNumber() : -1;
796 return pi ? pi->getText() :
"";
805 return sm ? sm->getText() :
"";
810 return sm ? sm->getLineNumber() : -1;
825 return pi->isRunning();
833 return pi->isPaused();
841 return pi->inError();
873 return sm->activate();
881 return sm->deactivate();
913 return sm->isPaused();
921 return sm->isActive();
929 return sm->isAutomatic();
937 return sm->inError();
945 return sm->getCurrentStateName();
953 return sm->requestState(state);
961 return sm->inState(state);
bool doLoadStateMachineText(const std::string &code)
const std::string what() const
TaskContext * getOwner() const
boost::shared_ptr< ScriptingService > shared_ptr
const char * what() const
bool doExecute(const std::string &code)
Service::shared_ptr provides()
virtual bool hasStateMachine(const std::string &name) const
virtual bool removeFunction(base::ExecutableInterface *f)
StateMachineStatus::StateMachineStatus getStateMachineStatus(const std::string &name) const
StateMap::const_iterator StateMapIt
bool eval(const std::string &code)
virtual int getProgramLine(const std::string &name) const
bool unloadProgram(const std::string &name)
bool startProgram(const std::string &name)
bool stepProgram(const std::string &name)
bool isStateMachineActive(const std::string &name) const
#define ORO_THROW_OR_RETURN(x, rv)
virtual std::string getStateMachineText(const std::string &name) const
static std::ostream & nl(std::ostream &__os)
#define ORO_SERVICE_NAMED_PLUGIN(SERVICE, NAME)
const StateMachinePtr getStateMachine(const std::string &name) const
bool isProgramRunning(const std::string &name) const
Operation< Signature > & addOperation(Operation< Signature > &op)
bool inProgramError(const std::string &name) const
bool doLoadPrograms(const std::string &filename)
bool unloadStateMachine(const std::string &name)
virtual bool loadPrograms(const std::string &filename, bool do_throw)
bool loadProgram(ProgramInterfacePtr pi)
StatementProcessor * sproc
bool loadStateMachine(StateMachinePtr sc)
ProgMap::const_iterator ProgMapIt
boost::shared_ptr< ProgramInterface > ProgramInterfacePtr
void createInterface(void)
void runScript(std::string const &code, TaskContext *mowner, ScriptingService *service, std::string const &filename)
virtual ~ScriptingService()
bool isStateMachineRunning(const std::string &name) const
ProgramStatus::ProgramStatus getProgramStatus(const std::string &name) const
bool deleteStateMachine(const std::string &name)
bool doLoadProgramText(const std::string &code)
std::vector< std::string > getStateMachineList() const
virtual int getStateMachineLine(const std::string &name) const
const char * what() const
bool isProgramPaused(const std::string &name) const
bool doUnloadStateMachine(const std::string &name)
bool pauseProgram(const std::string &name)
static RTT_API ExecutionEngine * Instance()
const std::string & doc() const
std::vector< ProgramInterfacePtr > ParsedPrograms
std::string getStateMachineStatusStr(const std::string &name) const
static std::ostream & endl(std::ostream &__os)
bool requestStateMachineState(const std::string &name, const std::string &state)
The program was running but is now paused.
This class is the public interface to the Orocos Program Parser Framework. It parsers Orocos program ...
bool inStateMachineError(const std::string &name) const
bool pauseStateMachine(const std::string &name)
virtual bool loadStateMachines(const std::string &filename, bool do_throw)
boost::shared_ptr< StateMachine > StateMachinePtr
bool resetStateMachine(const std::string &name)
void recursiveUnloadStateMachine(StateMachinePtr sc)
bool recursiveCheckLoadStateMachine(StateMachinePtr sc)
Property< T > & addProperty(const std::string &name, T &prop)
std::string getStateMachineState(const std::string &name) const
virtual Functions loadFunctions(const std::string &filename, bool do_throw)
int execute(const std::string &code)
std::vector< typename MapT::key_type > keys(const MapT &map)
std::vector< ProgramInterfacePtr > Functions
std::string getProgramStatusStr(const std::string &name) const
This program is currently not loaded in an ExecutionEngine.
bool activateStateMachine(const std::string &name)
bool inStateMachineState(const std::string &name, const std::string &state) const
bool stopStateMachine(const std::string &name)
std::vector< ParsedStateMachinePtr > ParsedStateMachines
bool evalInternal(const std::string &filename, const std::string &code)
void recursiveLoadStateMachine(StateMachinePtr sc)
std::vector< std::string > getProgramList() const
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
bool stopProgram(const std::string &name)
bool isStateMachinePaused(const std::string &name) const
int execute(const std::string &code)
virtual bool runFunction(base::ExecutableInterface *f)
bool runScript(const std::string &filename)
bool doUnloadProgram(const std::string &name)
bool startStateMachine(const std::string &name)
bool deactivateStateMachine(const std::string &name)
bool doLoadStateMachines(const std::string &filename)
The program is not running.
const ExecutionEngine * engine() const
virtual std::string getProgramText(const std::string &name) const
static Logger::LogFunction endlog()
bool recursiveCheckUnloadStateMachine(StateMachinePtr si)
virtual bool hasProgram(const std::string &name) const
const ProgramInterfacePtr getProgram(const std::string &name) const