23#include <yarp/os/Network.h>
24#include <yarp/os/Vocab.h>
32namespace learningmachine {
39 yarp::sig::Vector input;
41 bool ok = input.
read(connection);
54 }
catch(
const std::exception&
e) {
55 std::cerr <<
"Error: " <<
e.what() << std::endl;
59 yarp::os::ConnectionWriter* replier = connection.getWriter();
60 if(replier != (yarp::os::ConnectionWriter*) 0) {
61 prediction.
write(*replier);
69 std::cout <<
"Error: " <<
error << std::endl;
71 std::cout <<
"Available options for prediction module" << std::endl;
72 std::cout <<
"--help Display this help message" << std::endl;
73 std::cout <<
"--load file Load serialized machine from a file" << std::endl;
74 std::cout <<
"--port pfx Prefix for registering the ports" << std::endl;
75 std::cout <<
"--modelport port Model port of the training module" << std::endl;
76 std::cout <<
"--commands file Load configuration commands from a file" << std::endl;
101 yarp::os::Value* val;
107 if(opt.check(
"help")) {
113 if(opt.check(
"port", val)) {
118 if(opt.check(
"load", val)) {
126 if(opt.check(
"modelport", val)) {
127 yarp::os::Network::connect(val->asString().c_str(),
128 this->model_in.where().getName().c_str());
138 if(opt.check(
"commands", val)) {
144 this->attachTerminal();
152 bool success =
false;
155 switch(
cmd.get(0).asVocab32()) {
156 case yarp::os::createVocab32(
'h',
'e',
'l',
'p'):
158 reply.addVocab32(
"help");
160 reply.addString(
"Training module configuration options");
161 reply.addString(
" help Displays this message");
162 reply.addString(
" reset Resets the machine to its current state");
163 reply.addString(
" info Outputs information about the machine");
164 reply.addString(
" load fname Loads a machine from a file");
165 reply.addString(
" cmd fname Loads commands from a file");
171 case yarp::os::createVocab32(
'c',
'l',
'e',
'a'):
172 case yarp::os::createVocab32(
'c',
'l',
'r'):
173 case yarp::os::createVocab32(
'r',
'e',
's',
'e'):
174 case yarp::os::createVocab32(
'r',
's',
't'):
177 reply.addString(
"Machine reset.");
182 case yarp::os::createVocab32(
'i',
'n',
'f',
'o'):
183 case yarp::os::createVocab32(
's',
't',
'a',
't'):
185 reply.addVocab32(
"help");
186 reply.addString(
"Machine Information: ");
187 reply.addString(this->
getMachine().getInfo().c_str());
192 case yarp::os::createVocab32(
'l',
'o',
'a',
'd'):
194 reply.add(yarp::os::Value::makeVocab32(
"help"));
195 std::string replymsg = std::string(
"Loading machine from '") +
196 cmd.get(1).asString().c_str() +
"'... " ;
197 if(!
cmd.get(1).isString()) {
198 replymsg +=
"failed";
201 replymsg +=
"succeeded";
203 reply.addString(replymsg.c_str());
208 case yarp::os::createVocab32(
'c',
'm',
'd'):
209 case yarp::os::createVocab32(
'c',
'o',
'm',
'm'):
211 reply.add(yarp::os::Value::makeVocab32(
"help"));
212 std::string replymsg;
213 if(!
cmd.get(1).isString()) {
214 replymsg =
"Please supply a valid filename.";
216 std::string full_fname = this->
findFile(cmd.get(1).asString().c_str());
217 replymsg = std::string(
"Loading commands from '") +
218 full_fname +
"'... " ;
220 replymsg +=
"succeeded";
222 reply.addString(replymsg.c_str());
231 }
catch(
const std::exception&
e) {
232 std::string msg = std::string(
"Error: ") +
e.what();
233 reply.addString(msg.c_str());
virtual void raise(IEvent &event)
Raises an IEvent, causing it to be dispatched to each registered IEventListener.
static EventDispatcher & instance()
An instance retrieval method that follows the Singleton pattern.
std::string portPrefix
A prefix path for the ports that will be registered.
virtual void setResourceFinder(yarp::os::ResourceFinder *rf)
Mutator for the locally stored ResourceFinder.
std::string findFile(std::string fname)
Finds the full path to a specified filename using the ResourceFinder.
void registerPort(yarp::os::Contactable &port, std::string name)
Register a port with a given name.
yarp::os::Port cmd_in
An input port for commands.
virtual void loadCommandFile(std::string fname, yarp::os::Bottle *out=(yarp::os::Bottle *) 0)
Reads bottles from a file and sends these one by one to the respond method.
virtual void reset()=0
Forget everything and start over.
virtual Prediction predict(const yarp::sig::Vector &input)=0
Ask the learning machine to predict the output for a given input.
virtual MachinePortable & getMachinePortable()
Retrieve the machine portable machine wrapper.
virtual IMachineLearner & getMachine()
Convenience function to quickly retrieve the machine that is wrapped in the portable machine wrapper.
bool readFromFile(std::string filename)
Reads a wrapped object from a file.
virtual bool interruptModule()
void unregisterAllPorts()
Unregisters all ports used by this module.
yarp::os::BufferedPort< yarp::sig::Vector > predict_inout
Buffered port for the incoming samples and corresponding replies.
MachinePortable machinePortable
A concrete wrapper around a learning machine.
yarp::os::Port model_in
Incoming port for the models from the train module.
void printOptions(std::string error="")
Prints the accepted command line options with an optional error message.
virtual IMachineLearner & getMachine()
Retrieve the machine that is wrapped in the portable machine wrapper.
void registerAllPorts()
Registers all ports used by this module.
virtual bool respond(const yarp::os::Bottle &cmd, yarp::os::Bottle &reply)
virtual bool configure(yarp::os::ResourceFinder &opt)
virtual MachinePortable & getMachinePortable()
Retrieve the machine portable.
PredictProcessor predictProcessor
The processor handling prediction requests.
virtual bool read(yarp::os::ConnectionReader &connection)
A class that represents a prediction result.
bool write(yarp::os::ConnectionWriter &connection) const
bool read(yarp::os::ConnectionReader &connection)
This file contains the definition of unique IDs for the body parts and the skin parts of the robot.