-
Notifications
You must be signed in to change notification settings - Fork 0
/
PORIS.cpp
325 lines (288 loc) · 9.43 KB
/
PORIS.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// This code is (c) 2021 cosmoBots.eu
#ifndef _PORIS_CPP
#define _PORIS_CPP
#include "PORIS.h"
#include <iostream>
#include <list>
#include <iterator>
#include <string>
#include <typeinfo>
using namespace std;
///////////// Initialization routines ////////////////
void PORISNode::init(void){
cout << "Init de " << this->name << ", número de modos: " << modes.size() << endl;
PORISMode *firstMode = modes.front();
cout << "Init " << this->name << ": " << firstMode->name << endl;
setMode(firstMode);
};
///////////// PORISMode routines ////////////////
PORISValue *PORISMode::getEligibleValue(PORISValue *v){
if (v != NULL){
cout << "Entro en PORISMode getEligibleValue para el modo " << this->name << " con valor propuesto " << v->name << endl;
} else {
cout << "Entro en PORISMode getEligibleValue para el modo " << this->name << " con valor propuesto NULO" << endl;
}
PORISValue *ret = NULL;
list<PORISValue *> :: iterator it;
bool found = false;
for (it = values.begin();
!found && it != values.end();++it){
if (ret == NULL){
ret = *it;
cout << "Ponemos el primer valor eligible que encontramos " << ret->name << endl;
}
if (v != NULL){
if (v->id == (*it)->id){
ret = v;
cout << "Ponemos el valor propuesto " << ret->name << endl;
found = true;
}
}
}
return ret;
};
uint8_t PORISMode::getEligibleValue(uint8_t idx){
auto l_front = values.begin();
advance(l_front, idx);
PORISValue *result = getEligibleValue(*l_front);
if (result == NULL){
return 0;
} else {
return result->idx;
}
};
PORISMode * PORISMode::getEligibleSubMode(PORISMode *m){
cout << "Entro en PORISMode getEligibleSubMode para el modo " << this->name << " con m = " << m->name << endl;
PORISMode *ret = NULL;
list<PORISMode *> :: iterator it;
bool found = false;
for (it = submodes.begin();
!found && it != submodes.end();++it){
//cout << "Itero con el submodo " << (*it)->name;
//cout << " Los padres son " << (*it)->parent->name << " y " << m->parent->name << endl;
if ((*it)->parent->id == m->parent->id){
//cout << "El padre coincide ";
if (ret == NULL){
ret = *it;
cout << " Me lo apunto: " << (*it)->name << endl;
}
if (m->id == (*it)->id){
cout << " Nos coincide con 'm', lo podemos escoger!!! : " << (*it)->name << endl;
ret = m;
found = true;
}
}
}
cout << "No encontre nada " << endl;
return ret;
};
uint8_t PORISMode::getEligibleSubMode(uint8_t idx){
auto l_front = submodes.begin();
advance(l_front, idx);
PORISMode *result = getEligibleSubMode(*l_front);
if (result == NULL){
return 0;
} else {
return result->idx;
}
};
///////////// PORISNode routines ////////////////
PORISMode *PORISNode::getEligibleMode(PORISMode *m){
cout << "Entro en PORISNode "<< this->name <<".getEligibleMode(" << m->name << ")" << endl;
PORISMode *ret = NULL;
if (parent == NULL){
cout << "El padre de "<< this->name <<" es nulo" << endl;
ret = m;
} else {
cout << "Buscamos entre los " << ((PORISNode*)parent)->selectedMode->submodes.size() << " submodos de " << parent->name << endl;
ret = ((PORISNode*)parent)->selectedMode->getEligibleSubMode(m);
}
if (ret == NULL){
cout << "No hubo suerte, el modo a seleccionar es nulo" << endl;
} else {
cout << "El modo seleccionado es " << ret->name << endl;
}
return ret;
};
uint8_t PORISNode::getEligibleMode(uint8_t idx){
auto l_front = modes.begin();
advance(l_front, idx);
PORISMode *result = getEligibleMode(*l_front);
cout << "Vamos 1" << endl;
if (result == NULL) {
return 0;
} else {
return result->idx;
}
};
PORISMode *PORISNode::setEligibleMode(void){
cout << "Entro en PORISNode setEligibleMode " << this->name << endl;
if (selectedMode == NULL){
this->init();
}
cout << "selectedMode es " << selectedMode->name << endl;
PORISMode *ret = setMode(selectedMode);
return ret;
};
uint8_t PORISNode::setMode(uint8_t idx){
uint8_t ret = 0;
auto l_front = modes.begin();
advance(l_front, idx);
PORISMode *result = setMode(*l_front);
if (result == NULL){
ret = 0;
} else {
ret = result->idx;
}
cout << "Acaba la operación setMode con resultado " << (int)ret << endl;
return ret;
}
///////////// PORISSys routines ////////////////
PORISMode* PORISSys::setMode(PORISMode *m){
cout << "Entro en Sys setMode de " << this->name;
cout << " con modo " << m->name << endl;
PORISMode *ret = getEligibleMode(m);
cout << "Vamos 2" << endl;
if (ret == NULL){
cout << "el nuevo modo es NULO que es diferente del seleccionado ";
cout << " Hemos de poner el modo UNKNOWN, que por defecto es el primero " << endl;
ret = this->modes.front();
}
if (ret != selectedMode){
cout << "el nuevo modo es " << ret->name;
if (selectedMode != NULL) {
cout << " que es diferente de " << selectedMode->name << endl;
} else {
cout << " que es diferente de NULO" << endl;
}
selectedMode = ret;
list<PORISParam *> :: iterator itparam;
for (itparam = params.begin(); itparam != params.end();++itparam){
//cout << "Voy a iterar el param " << (*itparam)->name << endl;
(*itparam)->setEligibleMode();
}
list<PORISSys *> :: iterator itsys;
for (itsys = subsystems.begin(); itsys != subsystems.end();++itsys){
(*itsys)->setEligibleMode();
//cout << "Voy a iterar el sistema " << (*itsys)->name << endl;
}
} else {
cout << "El modo escogido es el mismo " << ret->name << endl;
}
cout << "Salgo de Sys setMode de " << this->name << " con m=" << m->name << " y resultado = " << ret->name << endl;
return ret;
}
///////////// PORISParam routines ////////////////
PORISValue *PORISParam::getEligibleValue(PORISValue *v){
if (v == NULL){
cout << "Entro en PORISParam getEligibleValue " << this->name << " con valor NULO " << endl;
} else {
cout << "Entro en PORISParam getEligibleValue " << this->name << " con valor " << v->name << endl;
}
PORISValue *ret = NULL;
if (selectedMode == NULL){
cout << " selectedMode es NULO";
init();
}
ret = selectedMode->getEligibleValue(v);
return ret;
};
uint8_t PORISParam::getEligibleValue(uint8_t idx){
auto l_front = values.begin();
advance(l_front, idx);
PORISValue *result = getEligibleValue(*l_front);
if (result == NULL){
return 0;
} else {
cout << "FIN: Vamos a retornar el valor " << result->name << endl;
return result->idx;
}
};
PORISValue* PORISParam::setValue(PORISValue *v){
if (v == NULL){
cout << "Entro en PORISParam setValue " << this->name << " con valor NULO " << endl;
} else {
cout << "Entro en PORISParam setValue " << this->name << " con valor " << v->name << endl;
}
PORISValue *ret = getEligibleValue(v);
if (ret != selectedValue){
selectedValue = ret;
}
return ret;
};
uint8_t PORISParam::setValue(uint8_t idx){
auto l_front = values.begin();
advance(l_front, idx);
PORISValue *result = setValue(*l_front);
if (result == NULL){
return 0;
} else {
return result->idx;
}
};
PORISValue* PORISParam::setEligibleValue(void){
cout << "Entro en PORISParam setEligibleValue " << this->name << endl;
return setValue(selectedValue);
};
PORISMode* PORISParam::setMode(PORISMode *m){
cout << "Entro en Param " << this->name << ".setMode(" << m->name << ")" << endl;
PORISMode *ret = getEligibleMode(m);
if (ret == NULL){
ret = this->modes.front();
}
if (ret != selectedMode){
selectedMode = ret;
setValue(selectedValue);
}
return ret;
};
double PORISValueFloat::setData(double floatdata){
cout << "Applying " << floatdata << " name: " << name << " min: " << min << " max: " << max << endl;
if (floatdata >= min){
if (floatdata <= max){
data = floatdata;
}
}
return data;
}
PORISSys *PORISSys::getSubSystemFromName(string name) {
for (auto s:subsystems){
if (s->name == name){
return s;
}
}
return NULL;
}
PORISParam *PORISSys::getSubParamFromName(string name) {
for (auto p:params){
if (p->name == name){
return p;
}
}
return NULL;
}
PORISSys *PORISSys::getDescendantFromName(string name) {
PORISSys *ret = getSubSystemFromName(name);
if (ret == NULL){
for (auto s:subsystems){
ret = s->getDescendantFromName(name);
if (ret != NULL){
return ret;
}
}
}
return ret;
}
PORISParam *PORISSys::getDescendantParamFromName(string name) {
PORISParam *ret = getSubParamFromName(name);
if (ret == NULL){
for (auto s:subsystems){
ret = s->getDescendantParamFromName(name);
if (ret != NULL){
return ret;
}
}
}
return ret;
}
#endif //_PORIS_CPP