himrep
linearClassifierModule.cpp
1 #include "linearClassifierModule.h"
2 #include <yarp/os/Stamp.h>
3 
4 bool linearClassifierModule::configure(yarp::os::ResourceFinder &rf)
5 {
6 
7 
8  string moduleName = rf.check("name", Value("linearClassifier"), "module name (string)").asString().c_str();
9  setName(moduleName.c_str());
10 
11 
12  handlerPortName = "/";
13  handlerPortName += getName(rf.check("CommandPort",Value("/rpc"),"Output image port (string)").asString().c_str());
14 
15 
16 
17  if (!handlerPort.open(handlerPortName.c_str())) {
18  cout << ": unable to open port " << handlerPortName << endl;
19  return false;
20  }
21 
22  attach(handlerPort);
23 
24 
25  lCThread = new linearClassifierThread(rf,&handlerPort);
26 
27  lCThread->start();
28 
29  return true ;
30 
31 }
32 
33 
34 bool linearClassifierModule::interruptModule()
35 {
36  lCThread->stop();
37 
38 
39  return true;
40 }
41 
42 
43 bool linearClassifierModule::close()
44 {
45 
46  lCThread->stop();
47  delete lCThread;
48 
49  return true;
50 }
51 
52 
53 bool linearClassifierModule::respond(const Bottle& command, Bottle& reply)
54 {
55  if(command.size()==0)
56  {
57  reply.addString("nack");
58  return true;
59  }
60 
61  if(command.get(0).asString()=="save" && command.size()==2)
62  {
63  string obj=command.get(1).asString().c_str();
64  this->lCThread->prepareObjPath(obj);
65  reply.addString("ack");
66  return true;
67  }
68 
69  if(command.get(0).asString()=="stop")
70  {
71  this->lCThread->stopAll();
72  reply.addString("ack");
73  return true;
74  }
75 
76  if(command.get(0).asString()=="objList")
77  {
78  reply.addString("ack");
79  this->lCThread->getClassList(reply.addList());
80  return true;
81  }
82 
83  if(command.get(0).asString()=="changeName")
84  {
85  string old_name=command.get(1).asString().c_str();
86  string new_name=command.get(2).asString().c_str();
87  reply.addString(this->lCThread->changeName(old_name,new_name)?"ack":"nack");
88  return true;
89  }
90 
91  if(command.get(0).asString()=="train")
92  {
93  this->lCThread->trainClassifiers();
94  reply.addString("ack");
95  return true;
96  }
97 
98  if(command.get(0).asString()=="recognize")
99  {
100  bool ack=this->lCThread->startRecognition();
101  reply.addString(ack?"ack":"nack");
102  return true;
103  }
104 
105  if(command.get(0).asString()=="forget" && command.size()>1)
106  {
107  string className=command.get(1).asString().c_str();
108  if(className=="all")
109  this->lCThread->forgetAll();
110  else
111  this->lCThread->forgetClass(className,true);
112  reply.addString("ack");
113  return true;
114  }
115 
116  reply.addString("nack");
117  return true;
118 }
119 
120 
121 bool linearClassifierModule::updateModule()
122 {
123  return true;
124 }
125 
126 
127 double linearClassifierModule::getPeriod()
128 {
129  return 1.0;
130 }