41 #include "../Logger.hpp" 42 #include "../TaskContext.hpp" 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 ) );
609 log(
Error) <<filename<<
" :"<< exc.
what() << endlog();
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
bool unloadStateMachine(const std::string &name)
Unload a deactivated State Machine and all its children.
This exception is thrown when a program or state machine could not be unloaded into a ProgramProcesso...
TaskContext * getOwner() const
The owner is the top-level TaskContext owning this service (indirectly).
boost::shared_ptr< ScriptingService > shared_ptr
bool unloadProgram(const std::string &name)
Unload a program from this processor.
virtual bool loadPrograms(const std::string &filename, bool do_throw)
Load a set of programs into the Program Processor for execution.
const char * what() const
bool doExecute(const std::string &code)
virtual Functions loadFunctions(const std::string &filename, bool do_throw)
Load and execute a set of functions into the Program Processor for execution.
Service::shared_ptr provides()
Returns this Service.
virtual bool hasStateMachine(const std::string &name) const
Check if a state machine is loaded.
virtual bool removeFunction(base::ExecutableInterface *f)
Remove a running function added with runFunction.
StateMachineStatus::StateMachineStatus getStateMachineStatus(const std::string &name) const
Return the status of a StateMachine.
StateMap::const_iterator StateMapIt
bool eval(const std::string &code)
Evaluate and run all statements in code.
virtual int getProgramLine(const std::string &name) const
Get the current line of execution of a loaded program script.
ProgramStatus
Enumerates the statuses of a ProgramInterface.
bool startProgram(const std::string &name)
Start a Program.
bool stepProgram(const std::string &name)
Steps a single instruction of a paused program.
bool isStateMachineActive(const std::string &name) const
Returns true if the state machine has been activated.
This class parses and executes a single scripting statement.
const StateMachinePtr getStateMachine(const std::string &name) const
Get a pointer to a loaded StateMachine.
#define ORO_THROW_OR_RETURN(x, rv)
virtual std::string getStateMachineText(const std::string &name) const
Get the original script description of a loaded state machine.
void clear()
Stop and unload all program scripts.
static std::ostream & nl(std::ostream &__os)
Insert a newline ' ' in the ostream.
#define ORO_SERVICE_NAMED_PLUGIN(SERVICE, NAME)
You can use this macro to make any Service available as a plugin.
bool isProgramRunning(const std::string &name) const
Inspect if a loaded program is in the running state.
Operation< Signature > & addOperation(Operation< Signature > &op)
Add an operation object to the interface.
bool inProgramError(const std::string &name) const
Inspect if a loaded program is in the error state.
bool doLoadPrograms(const std::string &filename)
bool loadProgram(ProgramInterfacePtr pi)
Load a new Program.
StatementProcessor * sproc
bool loadStateMachine(StateMachinePtr sc)
Load a new State Machine and all its children.
ProgMap::const_iterator ProgMapIt
boost::shared_ptr< ProgramInterface > ProgramInterfacePtr
void createInterface(void)
This is an exception class that keeps a parse_exception pointer along with the location in the file a...
void runScript(std::string const &code, TaskContext *mowner, ScriptingService *service, std::string const &filename)
Runs all statements in code.
virtual ~ScriptingService()
This class allows storage and retrieval of operations, ports, attributes and properties provided by a...
bool isStateMachineRunning(const std::string &name) const
Returns true if the state machine was successfully started.
ProgramStatus::ProgramStatus getProgramStatus(const std::string &name) const
Return the status of a Program.
bool deleteStateMachine(const std::string &name)
Delete a deactivated State Machine and all its children.
bool doLoadProgramText(const std::string &code)
std::vector< std::string > getStateMachineList() const
Get a list of all loaded StateMachines and their children.
virtual int getStateMachineLine(const std::string &name) const
Get the current line of execution of a loaded state machine script.
const ProgramInterfacePtr getProgram(const std::string &name) const
Get a pointer to a loaded Program.
const char * what() const
bool isProgramPaused(const std::string &name) const
Inspect if a loaded program is in the paused state.
bool doUnloadStateMachine(const std::string &name)
bool pauseProgram(const std::string &name)
Pauses the execution of a running program.
static RTT_API ExecutionEngine * Instance()
This is an overloaded member function, provided for convenience. It differs from the above function o...
const std::string & doc() const
Returns a descriptive text for this service.
std::vector< ProgramInterfacePtr > ParsedPrograms
List of parsed programs.
std::string getStateMachineStatusStr(const std::string &name) const
Return the status of a StateMachine as a human readable string.
static std::ostream & endl(std::ostream &__os)
bool requestStateMachineState(const std::string &name, const std::string &state)
Request a state change in a state machine.
The program was running but is now paused.
This class is the public interface to the Orocos Program Parser Framework.
bool inStateMachineError(const std::string &name) const
Returns true if the state machine is in error.
bool pauseStateMachine(const std::string &name)
Pause a previously activated StateMachine.
boost::shared_ptr< StateMachine > StateMachinePtr
bool resetStateMachine(const std::string &name)
Resets the execution of a scripting::StateMachine and enters the Initial_State.
void recursiveUnloadStateMachine(StateMachinePtr sc)
bool recursiveCheckLoadStateMachine(StateMachinePtr sc)
Property< T > & addProperty(const std::string &name, T &prop)
Adds a variable of any type as a property to the configuration interface.
std::string getStateMachineState(const std::string &name) const
Get the current state of a state machine.
int execute(const std::string &code)
Parse and execute a statement.
std::vector< typename MapT::key_type > keys(const MapT &map)
std::vector< ProgramInterfacePtr > Functions
List of executed functions.
std::string getProgramStatusStr(const std::string &name) const
Return the status of a Program as a human readable string.
This program is currently not loaded in an ExecutionEngine.
bool activateStateMachine(const std::string &name)
Activate a previously loaded StateMachine.
Notify the Logger in which 'module' the message occured.
This exception is thrown when a program or state machine could not be loaded into a ProgramProcessor ...
bool inStateMachineState(const std::string &name, const std::string &state) const
Check if a state machine is in a given state.
static Logger & log()
As Instance(), but more userfriendly.
bool stopStateMachine(const std::string &name)
Stops the execution of a scripting::StateMachine and enters the Final_State.
The TaskContext is the C++ representation of an Orocos component.
std::vector< ParsedStateMachinePtr > ParsedStateMachines
List of parsed State Machines.
bool evalInternal(const std::string &filename, const std::string &code)
void recursiveLoadStateMachine(StateMachinePtr sc)
std::vector< std::string > getProgramList() const
Get a list of all loaded Programs.
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
This interface allows to load program scripts and state machines and allows execution of code...
bool stopProgram(const std::string &name)
Stops the execution of a program.
bool isStateMachinePaused(const std::string &name) const
Returns true if the state machine is paused.
bool ZeroPeriodWarning
This is a property of the Scripting service It is true by default If this is set to false...
int execute(const std::string &code)
Execute a script statement.
virtual bool runFunction(base::ExecutableInterface *f)
Run a given function in step() or loop().
bool runScript(const std::string &filename)
Run a script which is stored in a file.
bool doUnloadProgram(const std::string &name)
bool startStateMachine(const std::string &name)
Start a previously activated StateMachine.
bool deactivateStateMachine(const std::string &name)
Deactivate a stopped StateMachine.
bool doLoadStateMachines(const std::string &filename)
virtual bool loadStateMachines(const std::string &filename, bool do_throw)
Load a set of state machines into the StateMachineProcessor for execution.
The program is not running.
const ExecutionEngine * engine() const
Get a const pointer to the ExecutionEngine of this Task.
virtual std::string getProgramText(const std::string &name) const
Get the original script description of a loaded program.
bool recursiveCheckUnloadStateMachine(StateMachinePtr si)
void clear()
Clear all added operations, properties and sub-services from this Service.
virtual bool hasProgram(const std::string &name) const
Check if a program is loaded.