iCub-main
Loading...
Searching...
No Matches
IMachineLearnerModule.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2007-2010 RobotCub Consortium, European Commission FP6 Project IST-004370
3 * author: Arjan Gijsberts
4 * email: arjan.gijsberts@iit.it
5 * website: www.robotcub.org
6 * Permission is granted to copy, distribute, and/or modify this program
7 * under the terms of the GNU General Public License, version 2 or any
8 * later version published by the Free Software Foundation.
9 *
10 * A copy of the license can be found at
11 * http://www.robotcub.org/icub/license/gpl.txt
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16 * Public License for more details
17 */
18
19#include <iostream>
20#include <stdexcept>
21
24
25
26namespace iCub {
27namespace learningmachine {
28
29
30void IMachineLearnerModule::registerPort(yarp::os::Contactable& port, std::string name) {
31 if(port.open(name.c_str()) != true) {
32 std::string msg("could not register port ");
33 msg+=name;
34 throw std::runtime_error(msg);
35 }
36}
37
38
41 return true;
42}
43
44std::string IMachineLearnerModule::findFile(std::string fname) {
45 std::string full_fname;
46
47 /*
48 * ResourceFinder does _not_ allow us to search directly for a path. So we
49 * need to hack this by setting a unique fake key and then search for that
50 * key. :(
51 * Uncomment this as soon as searching directly for files is disabled. At
52 * the moment it is deprecated.
53 */
54 //bool ok;
55 //ok = this->getResourceFinder().setDefault(fname.c_str(), fname.c_str());
56 //if(!ok) {
57 // throw std::runtime_error("Could not inject dummy key in resource finder.");
58 //}
59
60 full_fname = std::string(this->getResourceFinder().findFile(fname.c_str()).c_str());
61 if(full_fname == "") {
62 throw std::runtime_error("Could not find file '" + fname +"' in the standard search paths.");
63 }
64
65 return full_fname;
66}
67
68void IMachineLearnerModule::loadCommandFile(std::string fname, yarp::os::Bottle* out) {
70 yarp::os::Bottle* cmd = (yarp::os::Bottle*) 0;
71 yarp::os::Bottle reply;
72
73 // open reader
74 str.open(fname);
75
76 // send all bottles from file to respond method
77 while(str.hasNext()) {
78 cmd = str.getNext();
79 reply.clear();
80 if(*cmd != yarp::os::Bottle::getNullBottle()) {
81 if(out != (yarp::os::Bottle*) 0) {
82 out->addString(cmd->toString().c_str());
83 } else {
84 std::cout << /*">>> " <<*/ cmd->toString().c_str() << std::endl;
85 }
86 this->safeRespond(*cmd, reply);
87 // imitate RFModule way of reporting
88 if(out != (yarp::os::Bottle*) 0) {
89 out->addString(reply.toString().c_str());
90 } else {
91 if(reply.get(0).toString() == "help") {
92 for(int i=0; i<reply.size(); i++) {
93 std::cout << reply.get(i).toString().c_str() << std::endl;
94 }
95 } else {
96 std::cout << reply.toString().c_str() << std::endl;
97 }
98 }
99 }
100 // clean up
101 delete cmd;
102 cmd = (yarp::os::Bottle*) 0;
103
104 }
105}
106
107} // learningmachine
108} // iCub
109
110
Template class that supports reading lines of a file to object instances using a fromString(char* lin...
Definition FileReaderT.h:37
virtual bool hasNext()
Checks whether there are more items left in the stream.
virtual void open(std::istream &s)
Opens the given stream for reading.
Definition FileReaderT.h:95
T * getNext()
Returns a pointer to the next item in the file.
virtual yarp::os::ResourceFinder & getResourceFinder()
Returns a reference to the cached ResourceFinder.
virtual void unregisterAllPorts()=0
Unregisters all ports used by this module.
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.
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.
cmd
Definition dataTypes.h:30
This file contains the definition of unique IDs for the body parts and the skin parts of the robot.
out
Definition sine.m:8