1 |
|
|
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 |
|
|
* Copyright Projet JRL-Japan, 2007 |
3 |
|
|
*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
4 |
|
|
* |
5 |
|
|
* File: Selector.h |
6 |
|
|
* Project: SOT |
7 |
|
|
* Author: Nicolas Mansard |
8 |
|
|
* |
9 |
|
|
* Version control |
10 |
|
|
* =============== |
11 |
|
|
* |
12 |
|
|
* $Id$ |
13 |
|
|
* |
14 |
|
|
* Description |
15 |
|
|
* ============ |
16 |
|
|
* |
17 |
|
|
* |
18 |
|
|
* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ |
19 |
|
|
|
20 |
|
|
// #define VP_DEBUG |
21 |
|
|
// #define VP_DEBUG_MODE 50 |
22 |
|
|
#include <sot/core/debug.hh> |
23 |
|
|
#ifdef VP_DEBUG |
24 |
|
|
class selector__INIT { |
25 |
|
|
public: |
26 |
|
|
selector__INIT(void) { dynamicgraph::sot::DebugTrace::openFile(); } |
27 |
|
|
}; |
28 |
|
|
selector__INIT selector_initiator; |
29 |
|
|
#endif // #ifdef VP_DEBUG |
30 |
|
|
|
31 |
|
|
#include <dynamic-graph/all-commands.h> |
32 |
|
|
#include <dynamic-graph/factory.h> |
33 |
|
|
#include <sot/pattern-generator/exception-pg.h> |
34 |
|
|
#include <sot/pattern-generator/selector.h> |
35 |
|
|
|
36 |
|
|
namespace dynamicgraph { |
37 |
|
|
namespace sot { |
38 |
|
|
|
39 |
|
|
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(Selector, "Selector"); |
40 |
|
|
|
41 |
|
|
Selector::Selector(const std::string &name) |
42 |
|
|
: Entity(name), |
43 |
|
|
selectorSIN(NULL, "Selector(" + name + ")::input(uint)::selec") { |
44 |
|
|
sotDEBUGIN(5); |
45 |
|
|
|
46 |
|
|
signalRegistration(selectorSIN); |
47 |
|
|
initCommands(); |
48 |
|
|
|
49 |
|
|
sotDEBUGOUT(5); |
50 |
|
|
} |
51 |
|
|
|
52 |
|
|
Selector::~Selector(void) { |
53 |
|
|
sotDEBUGIN(5); |
54 |
|
|
|
55 |
|
|
resetSignals(0, 0); |
56 |
|
|
|
57 |
|
|
sotDEBUGOUT(5); |
58 |
|
|
return; |
59 |
|
|
} |
60 |
|
|
|
61 |
|
|
/* --- SIGNALS --------------------------------------------------------- */ |
62 |
|
|
/* --- SIGNALS --------------------------------------------------------- */ |
63 |
|
|
/* --- SIGNALS --------------------------------------------------------- */ |
64 |
|
|
#define SOT_CALL_SIG(sotName, sotType) \ |
65 |
|
|
boost::bind(&Signal<sotType, int>::access, &sotName, _2) |
66 |
|
|
|
67 |
|
|
template <class T> |
68 |
|
|
unsigned int Selector::createSignal(const std::string &shortname, |
69 |
|
|
const int &sigId__) { |
70 |
|
|
sotDEBUGIN(15); |
71 |
|
|
|
72 |
|
|
unsigned int sigId = sigId__; |
73 |
|
|
/* If sigId is not valid, it is set to the first free signal. */ |
74 |
|
|
if ((sigId__ < 0) || (sigId > nbSignals)) |
75 |
|
|
for (unsigned int i = 0; i < nbSignals; ++i) |
76 |
|
|
if (0 == inputsSIN[i].size()) { |
77 |
|
|
sigId = i; |
78 |
|
|
break; |
79 |
|
|
} |
80 |
|
|
if ((sigId__ < 0) || (sigId > nbSignals)) return -1; |
81 |
|
|
|
82 |
|
|
/* Set up the input signal vector. */ |
83 |
|
|
std::vector<SignalBase<int> *> &entriesSIN = inputsSIN[sigId]; |
84 |
|
|
for (unsigned int i = 0; i < entriesSIN.size(); ++i) { |
85 |
|
|
if (NULL != entriesSIN[i]) { |
86 |
|
|
signalDeregistration(entriesSIN[i]->getName()); |
87 |
|
|
delete entriesSIN[i]; |
88 |
|
|
} |
89 |
|
|
} |
90 |
|
|
entriesSIN.resize(nbEntries); |
91 |
|
|
|
92 |
|
|
/* sigDep contains the list of the input signal. |
93 |
|
|
sigOut depends of these. */ |
94 |
|
|
SignalArray<int> sigDep; |
95 |
|
|
std::ostringstream signame; |
96 |
|
|
|
97 |
|
|
/* Set the entries. */ |
98 |
|
|
for (unsigned int i = 0; i < nbEntries; ++i) { |
99 |
|
|
signame.str(""); |
100 |
|
|
signame << "Selector(" << Entity::getName() << ")::input(" |
101 |
|
|
<< typeid(T).name() << ")::" << shortname << i; |
102 |
|
|
SignalPtr<T, int> *sigIn = new SignalPtr<T, int>(NULL, signame.str()); |
103 |
|
|
inputsSIN[sigId][i] = sigIn; |
104 |
|
|
|
105 |
|
|
signalRegistration(*sigIn); |
106 |
|
|
sigDep << (*sigIn); |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
/* Set the output. */ |
110 |
|
|
if (NULL != outputsSOUT[sigId]) { |
111 |
|
|
signalDeregistration(outputsSOUT[sigId]->getName()); |
112 |
|
|
delete outputsSOUT[sigId]; |
113 |
|
|
} |
114 |
|
|
signame.str(""); |
115 |
|
|
signame << "Selector(" << Entity::getName() << ")::output(" |
116 |
|
|
<< typeid(T).name() << ")::" << shortname; |
117 |
|
|
|
118 |
|
|
SignalTimeDependent<T, int> *sigOut = new SignalTimeDependent<T, int>( |
119 |
|
|
boost::bind(&Selector::computeSelection<T>, |
120 |
|
|
SOT_CALL_SIG(selectorSIN, unsigned int), |
121 |
|
|
boost::ref(entriesSIN), _1, _2), |
122 |
|
|
sigDep << selectorSIN, signame.str()); |
123 |
|
|
outputsSOUT[sigId] = sigOut; |
124 |
|
|
signalRegistration(*sigOut); |
125 |
|
|
sigOut->setReady(true); |
126 |
|
|
|
127 |
|
|
sotDEBUGOUT(15); |
128 |
|
|
return sigId; |
129 |
|
|
} |
130 |
|
|
|
131 |
|
|
template <class T> |
132 |
|
|
T &Selector::computeSelection(const unsigned int &sigNum, |
133 |
|
|
std::vector<SignalBase<int> *> &entriesSIN, |
134 |
|
|
T &res, const int &time) { |
135 |
|
|
sotDEBUGIN(15); |
136 |
|
|
|
137 |
|
|
sotDEBUG(25) << "Type " << typeid(T).name() << std::endl; |
138 |
|
|
|
139 |
|
|
if (sigNum > entriesSIN.size()) { |
140 |
|
|
SOT_THROW ExceptionPatternGenerator( |
141 |
|
|
ExceptionPatternGenerator::SELECTOR_RANK, |
142 |
|
|
"Rank of the selector is not valid. ", |
143 |
|
|
"(while calling outputsSOUT with selector" |
144 |
|
|
"=%d).", |
145 |
|
|
sigNum); |
146 |
|
|
} |
147 |
|
|
|
148 |
|
|
sotDEBUG(25) << "Sig name " << entriesSIN[sigNum]->getName() << std::endl; |
149 |
|
|
SignalPtr<T, int> *sigSpec = |
150 |
|
|
dynamic_cast<SignalPtr<T, int> *>(entriesSIN[sigNum]); |
151 |
|
|
if (NULL == sigSpec) { |
152 |
|
|
SOT_THROW ExceptionPatternGenerator( |
153 |
|
|
ExceptionPatternGenerator::BAD_CAST, |
154 |
|
|
"Signal types for IN and OUT uncompatible. ", |
155 |
|
|
"(while calling outputsSOUT of sig<%d>" |
156 |
|
|
"with output type %s and input of %s).", |
157 |
|
|
sigNum, (typeid(T).name()), (typeid(*entriesSIN[sigNum]).name())); |
158 |
|
|
} |
159 |
|
|
|
160 |
|
|
res = sigSpec->access(time); |
161 |
|
|
sotDEBUGOUT(15); |
162 |
|
|
return res; |
163 |
|
|
} |
164 |
|
|
|
165 |
|
|
void Selector::resetSignals(const unsigned int &nbEntries__, |
166 |
|
|
const unsigned int &nbSignals__) { |
167 |
|
|
for (std::vector<std::vector<SignalBase<int> *> >::iterator iter = |
168 |
|
|
inputsSIN.begin(); |
169 |
|
|
iter < inputsSIN.end(); ++iter) { |
170 |
|
|
for (std::vector<SignalBase<int> *>::iterator iterSig = iter->begin(); |
171 |
|
|
iterSig < iter->end(); ++iterSig) { |
172 |
|
|
SignalBase<int> *sigPtr = *iterSig; |
173 |
|
|
if (NULL != sigPtr) delete sigPtr; |
174 |
|
|
} |
175 |
|
|
} |
176 |
|
|
inputsSIN.resize(nbSignals__); |
177 |
|
|
nbSignals = nbSignals__; |
178 |
|
|
nbEntries = nbEntries__; |
179 |
|
|
|
180 |
|
|
for (std::vector<SignalBase<int> *>::iterator iterSig = outputsSOUT.begin(); |
181 |
|
|
iterSig < outputsSOUT.end(); ++iterSig) { |
182 |
|
|
SignalBase<int> *sigPtr = *iterSig; |
183 |
|
|
if (NULL != sigPtr) delete sigPtr; |
184 |
|
|
} |
185 |
|
|
outputsSOUT.resize(nbSignals); |
186 |
|
|
} |
187 |
|
|
|
188 |
|
|
/* --- PARAMS --------------------------------------------------------------- */ |
189 |
|
|
/* --- PARAMS --------------------------------------------------------------- */ |
190 |
|
|
/* --- PARAMS --------------------------------------------------------------- */ |
191 |
|
|
|
192 |
|
|
#define SOT_SELECTOR_CREATE_TYPE(sotType, sotTypeName) \ |
193 |
|
|
if (dORc && (type == sotTypeName)) \ |
194 |
|
|
selec.createSignal<sotType>(name, sigId); \ |
195 |
|
|
else \ |
196 |
|
|
oss << " - " << sotTypeName << std::endl; |
197 |
|
|
|
198 |
|
|
static void displayOrCreate(Selector &selec, bool dORc, std::ostream &os, |
199 |
|
|
const std::string &name = "", |
200 |
|
|
const std::string &type = "", |
201 |
|
|
const int &sigId = -1) { |
202 |
|
|
sotDEBUGIN(15); |
203 |
|
|
std::ostringstream oss; |
204 |
|
|
|
205 |
|
|
/* ------------------------------------------------------------------------ */ |
206 |
|
|
/* *** Enter here the type list *** --------------------------------------- */ |
207 |
|
|
/* ------------------------------------------------------------------------ */ |
208 |
|
|
|
209 |
|
|
SOT_SELECTOR_CREATE_TYPE(Vector, "vector"); |
210 |
|
|
SOT_SELECTOR_CREATE_TYPE(Matrix, "matrix"); |
211 |
|
|
SOT_SELECTOR_CREATE_TYPE(MatrixHomogeneous, "matrixHomo"); |
212 |
|
|
|
213 |
|
|
/* ------------------------------------------------------------------------ */ |
214 |
|
|
/* ------------------------------------------------------------------------ */ |
215 |
|
|
|
216 |
|
|
if (!dORc) os << "Types available:" << std::endl << oss.str(); |
217 |
|
|
sotDEBUGOUT(15); |
218 |
|
|
} |
219 |
|
|
|
220 |
|
|
void Selector::initCommands(void) { |
221 |
|
|
using namespace command; |
222 |
|
|
addCommand( |
223 |
|
|
"reset", |
224 |
|
|
makeCommandVoid2( |
225 |
|
|
*this, &Selector::resetSignals, |
226 |
|
|
docCommandVoid2("Re-set the list MxN in and M out, for M outputs, " |
227 |
|
|
"and M possible inputs for each M.", |
228 |
|
|
"int (N=nb inputs foreach)", "int (M=nb output)"))); |
229 |
|
|
addCommand("create", |
230 |
|
|
makeCommandVoid3( |
231 |
|
|
*this, &Selector::create, |
232 |
|
|
docCommandVoid3( |
233 |
|
|
"Create a new set of input and the corresponding output.", |
234 |
|
|
"string among authorized values (type)", "str(name)", |
235 |
|
|
"int (sig id)"))); |
236 |
|
|
addCommand("getTypeList", |
237 |
|
|
makeCommandVerbose( |
238 |
|
|
*this, &Selector::getTypeList, |
239 |
|
|
docCommandVerbose("Get the list of all possible types."))); |
240 |
|
|
} |
241 |
|
|
|
242 |
|
|
void Selector::create(const std::string &type, const std::string &name, |
243 |
|
|
const int &sigId) { |
244 |
|
|
std::ostringstream dummy; |
245 |
|
|
displayOrCreate(*this, true, dummy, name, type, sigId); |
246 |
|
|
} |
247 |
|
|
std::string Selector::getTypeList(void) { |
248 |
|
|
std::ostringstream sout; |
249 |
|
|
displayOrCreate(*this, false, sout); |
250 |
|
|
return sout.str(); |
251 |
|
|
} |
252 |
|
|
void Selector::getTypeList(std::ostream &os) { os << getTypeList(); } |
253 |
|
|
|
254 |
|
|
void Selector::commandLine(const std::string &cmdLine, |
255 |
|
|
std::istringstream &cmdArgs, std::ostream &os) { |
256 |
|
|
if (cmdLine == "help") { |
257 |
|
|
os << "Selector: " << std::endl |
258 |
|
|
<< " - typeList: display the available types. " << std::endl |
259 |
|
|
<< " - reset <nbEntries> <nbSig>: reset the signal lists. " << std::endl |
260 |
|
|
<< " - create <sigtype> <signame> <sigid>: " |
261 |
|
|
<< "create a new set of signals" << std::endl; |
262 |
|
|
} else if (cmdLine == "typeList") { |
263 |
|
|
displayOrCreate(*this, false, os); |
264 |
|
|
} else if (cmdLine == "create") { |
265 |
|
|
std::string type, name; |
266 |
|
|
int sigId; |
267 |
|
|
cmdArgs >> type >> name >> std::ws; |
268 |
|
|
if (cmdArgs.good()) |
269 |
|
|
cmdArgs >> sigId; |
270 |
|
|
else |
271 |
|
|
sigId = -1; |
272 |
|
|
displayOrCreate(*this, true, os, name, type, sigId); |
273 |
|
|
|
274 |
|
|
} else if (cmdLine == "reset") { |
275 |
|
|
cmdArgs >> std::ws; |
276 |
|
|
if (cmdArgs.good()) { |
277 |
|
|
unsigned int nbSig, nbEntries; |
278 |
|
|
cmdArgs >> nbSig >> std::ws; |
279 |
|
|
if (cmdArgs.good()) |
280 |
|
|
cmdArgs >> nbEntries; |
281 |
|
|
else { |
282 |
|
|
os << "Error: usage is: reset <nbEntries> <nbSig>." << std::endl; |
283 |
|
|
return; |
284 |
|
|
} |
285 |
|
|
resetSignals(nbSig, nbEntries); |
286 |
|
|
} else { |
287 |
|
|
os << "Error: usage is: reset <nbEntries> <nbSig>." << std::endl; |
288 |
|
|
return; |
289 |
|
|
} |
290 |
|
|
} else { |
291 |
|
|
} |
292 |
|
|
} |
293 |
|
|
|
294 |
|
|
} // namespace sot |
295 |
|
|
} // namespace dynamicgraph |