23#include <yarp/os/Network.h>
24#include <yarp/os/Vocab.h>
29namespace learningmachine {
36 yarp::sig::Vector input;
38 bool ok = input.
read(connection);
46 }
catch(
const std::exception&
e) {
47 std::cerr <<
"Error: " <<
e.what() << std::endl;
51 yarp::os::ConnectionWriter* replier = connection.getWriter();
52 if(replier != (yarp::os::ConnectionWriter*) 0) {
53 prediction.
write(*replier);
63 yarp::os::PortablePair<yarp::sig::Vector,yarp::sig::Vector>& output = this->
getOutputPort().prepare();
65 output.body = input.body;
67 }
catch(
const std::exception&
e) {
68 std::cerr <<
"Error: " <<
e.what() << std::endl;
76void TransformModule::printOptions(std::string
error) {
78 std::cout <<
"Error: " <<
error << std::endl;
80 std::cout <<
"Available options" << std::endl;
81 std::cout <<
"--help Display this help message" << std::endl;
82 std::cout <<
"--list Print a list of available algorithms" << std::endl;
83 std::cout <<
"--load file Load serialized transformer from a file" << std::endl;
84 std::cout <<
"--transformer type Desired type of transformer" << std::endl;
85 std::cout <<
"--trainport port Data port for the training samples" << std::endl;
86 std::cout <<
"--predictport port Data port for the prediction samples" << std::endl;
87 std::cout <<
"--port pfx Prefix for registering the ports" << std::endl;
88 std::cout <<
"--commands file Load configuration commands from a file" << std::endl;
91void TransformModule::printTransformerList() {
93 std::cout <<
"Available Transformers:" << std::endl;
94 for(
unsigned int i = 0; i < keys.size(); i++) {
95 std::cout <<
" " << keys[i] << std::endl;
99void TransformModule::registerAllPorts() {
103 this->train_in.setStrict();
106 this->train_out.setStrict();
109 this->predict_inout.setStrict();
117void TransformModule::unregisterAllPorts() {
119 this->train_in.close();
120 this->train_out.close();
121 this->predict_inout.close();
122 this->predictRelay_inout.close();
127 train_in.interrupt();
128 train_out.interrupt();
129 predict_inout.interrupt();
130 predictRelay_inout.interrupt();
136 yarp::os::Value* val;
137 std::string transformerName;
143 if(opt.check(
"help")) {
144 this->printOptions();
149 if(opt.check(
"list")) {
150 this->printTransformerList();
155 if(opt.check(
"port", val)) {
159 if(opt.check(
"load", val)) {
163 if(opt.check(
"transformer", val)) {
164 transformerName = val->asString().c_str();
166 this->printOptions(
"no transformer type specified");
178 this->train_in.useCallback(trainProcessor);
181 this->predict_inout.setReplier(this->predictProcessor);
184 this->registerAllPorts();
187 if(opt.check(
"trainport", val)) {
188 yarp::os::Network::connect(this->train_out.where().getName().c_str(),
189 val->asString().c_str());
195 if(opt.check(
"predictport", val)) {
196 yarp::os::Network::connect(this->predictRelay_inout.where().getName().c_str(),
197 val->asString().c_str());
203 if(opt.check(
"commands", val)) {
204 std::string full_fname = this->
findFile(val->asString().c_str());
210 this->attachTerminal();
219 bool success =
false;
222 switch(
cmd.get(0).asVocab32()) {
223 case yarp::os::createVocab32(
'h',
'e',
'l',
'p'):
224 reply.add(yarp::os::Value::makeVocab32(
"help"));
226 reply.addString(
"Transform module configuration options");
227 reply.addString(
" help Displays this message");
228 reply.addString(
" reset Resets the machine to its current state");
229 reply.addString(
" info Outputs information about the transformer");
230 reply.addString(
" load fname Loads a transformer from a file");
231 reply.addString(
" save fname Saves the current transformer to a file");
232 reply.addString(
" set key val Sets a configuration option for the transformer");
233 reply.addString(
" cmd fname Loads commands from a file");
238 case yarp::os::createVocab32(
'c',
'l',
'e',
'a'):
239 case yarp::os::createVocab32(
'c',
'l',
'r'):
240 case yarp::os::createVocab32(
'r',
'e',
's',
'e'):
241 case yarp::os::createVocab32(
'r',
's',
't'):
243 reply.addString(
"Transformer reset.");
247 case yarp::os::createVocab32(
'i',
'n',
'f',
'o'):
248 case yarp::os::createVocab32(
's',
't',
'a',
't'):
250 reply.add(yarp::os::Value::makeVocab32(
"help"));
251 reply.addString(
"Transformer Information: ");
257 case yarp::os::createVocab32(
's',
'e',
't'):
259 yarp::os::Bottle property;
266 property.addList() =
cmd.tail();
267 std::string replymsg =
"Setting configuration option ";
269 replymsg +=
ok ?
"succeeded" :
270 "failed; please check key and value type.";
271 reply.addString(replymsg.c_str());
276 case yarp::os::createVocab32(
'l',
'o',
'a',
'd'):
278 reply.add(yarp::os::Value::makeVocab32(
"help"));
279 std::string replymsg = std::string(
"Loading transformer from '") +
280 cmd.get(1).asString().c_str() +
"'... " ;
281 if(!
cmd.get(1).isString()) {
282 replymsg +=
"failed";
285 replymsg +=
"succeeded";
287 reply.addString(replymsg.c_str());
292 case yarp::os::createVocab32(
's',
'a',
'v',
'e'):
294 reply.add(yarp::os::Value::makeVocab32(
"help"));
295 std::string replymsg = std::string(
"Saving transformer to '") +
296 cmd.get(1).asString().c_str() +
"'... " ;
297 if(!
cmd.get(1).isString()) {
298 replymsg +=
"failed";
301 replymsg +=
"succeeded";
303 reply.addString(replymsg.c_str());
308 case yarp::os::createVocab32(
'c',
'm',
'd'):
309 case yarp::os::createVocab32(
'c',
'o',
'm',
'm'):
311 reply.add(yarp::os::Value::makeVocab32(
"help"));
312 std::string replymsg;
313 if(!
cmd.get(1).isString()) {
314 replymsg =
"Please supply a valid filename.";
316 std::string full_fname = this->
findFile(cmd.get(1).asString().c_str());
317 replymsg = std::string(
"Loading commands from '") +
318 full_fname +
"'... " ;
320 replymsg +=
"succeeded";
322 reply.addString(replymsg.c_str());
331 }
catch(
const std::exception&
e) {
332 std::string msg = std::string(
"Error: ") +
e.what();
333 reply.addString(msg.c_str());
static FactoryT< K, T > & 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.
void setWrapped(T *w, bool wipe=true)
The mutator for the wrapped object.
bool writeToFile(std::string filename)
Writes a wrapped object to a file.
bool readFromFile(std::string filename)
Reads a wrapped object from a file.
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.