aron_conversions.cpp
Go to the documentation of this file.
1#include "aron_conversions.h"
2
3#include <nlohmann/json.hpp>
4
6
8{
9
10 template <class DTOType, class BOType>
11 static void
12 fromAronOptional(const std::optional<DTOType>& dtoOpt, std::optional<BOType>& boOpt)
13 {
14 if (dtoOpt.has_value())
15 {
16 BOType bo;
17 fromAron(dtoOpt.value(), bo);
18 boOpt = bo;
19 }
20 else
21 {
22 boOpt = std::nullopt;
23 }
24 }
25
26 template <class DTOType, class BOType>
27 static void
28 toAronOptional(std::optional<DTOType>& dtoOpt, const std::optional<BOType>& boOpt)
29 {
30 if (boOpt.has_value())
31 {
32 DTOType dto;
33 toAron(dto, boOpt.value());
34 dtoOpt = dto;
35 }
36 else
37 {
38 dtoOpt = std::nullopt;
39 }
40 }
41
42 // --- TaskOutcomeType ---
43
44 void
45 fromAron(const armarx::task_outcome::arondto::TaskOutcomeType& dto, TaskOutcomeType& bo)
46 {
47 switch (dto.value)
48 {
49 case armarx::task_outcome::arondto::TaskOutcomeType::UNKNOWN:
51 break;
52 case armarx::task_outcome::arondto::TaskOutcomeType::SUCCESS:
54 break;
55 case armarx::task_outcome::arondto::TaskOutcomeType::FAILURE:
57 break;
58 case armarx::task_outcome::arondto::TaskOutcomeType::ABORTED:
60 break;
61 case armarx::task_outcome::arondto::TaskOutcomeType::SUSPENDED:
63 break;
64 }
65 }
66
67 void
68 toAron(armarx::task_outcome::arondto::TaskOutcomeType& dto, const TaskOutcomeType& bo)
69 {
70 switch (bo)
71 {
73 dto = armarx::task_outcome::arondto::TaskOutcomeType::UNKNOWN;
74 break;
76 dto = armarx::task_outcome::arondto::TaskOutcomeType::SUCCESS;
77 break;
79 dto = armarx::task_outcome::arondto::TaskOutcomeType::FAILURE;
80 break;
82 dto = armarx::task_outcome::arondto::TaskOutcomeType::ABORTED;
83 break;
85 dto = armarx::task_outcome::arondto::TaskOutcomeType::SUSPENDED;
86 break;
87 }
88 }
89
90 // --- TaskType ---
91
92 void
93 fromAron(const armarx::task_outcome::arondto::TaskType& dto, TaskType& bo)
94 {
95 switch (dto.value)
96 {
97 case armarx::task_outcome::arondto::TaskType::UNKNOWN:
99 break;
100 case armarx::task_outcome::arondto::TaskType::GRASPING_FAMILIAR:
102 break;
103 case armarx::task_outcome::arondto::TaskType::GRASPING_KNOWN:
105 break;
106 case armarx::task_outcome::arondto::TaskType::MANIPULATION:
108 break;
109 case armarx::task_outcome::arondto::TaskType::VERBALIZATION:
111 break;
112 case armarx::task_outcome::arondto::TaskType::DETECTION:
114 break;
115 case armarx::task_outcome::arondto::TaskType::NAVIGATION:
117 break;
118 case armarx::task_outcome::arondto::TaskType::SPEAKING:
120 break;
121 case armarx::task_outcome::arondto::TaskType::SPECIAL:
123 break;
124 }
125 }
126
127 void
128 toAron(armarx::task_outcome::arondto::TaskType& dto, const TaskType& bo)
129 {
130 switch (bo)
131 {
133 dto = armarx::task_outcome::arondto::TaskType::UNKNOWN;
134 break;
136 dto = armarx::task_outcome::arondto::TaskType::GRASPING_FAMILIAR;
137 break;
139 dto = armarx::task_outcome::arondto::TaskType::GRASPING_KNOWN;
140 break;
142 dto = armarx::task_outcome::arondto::TaskType::MANIPULATION;
143 break;
145 dto = armarx::task_outcome::arondto::TaskType::VERBALIZATION;
146 break;
148 dto = armarx::task_outcome::arondto::TaskType::DETECTION;
149 break;
151 dto = armarx::task_outcome::arondto::TaskType::NAVIGATION;
152 break;
154 dto = armarx::task_outcome::arondto::TaskType::SPEAKING;
155 break;
157 dto = armarx::task_outcome::arondto::TaskType::SPECIAL;
158 break;
159 }
160 }
161
162 // --- PlanningFailures ---
163
164 void
165 fromAron(const armarx::task_outcome::arondto::PlanningFailures& dto, PlanningFailures& bo)
166 {
167 bo.noTrajectoryFound = dto.noTrajectoryFound;
168 bo.noGraspFound = dto.noGraspFound;
169 bo.special = dto.special;
170 }
171
172 void
173 toAron(armarx::task_outcome::arondto::PlanningFailures& dto, const PlanningFailures& bo)
174 {
175 dto.noTrajectoryFound = bo.noTrajectoryFound;
176 dto.noGraspFound = bo.noGraspFound;
177 dto.special = bo.special;
178 }
179
180 // --- PerceptionFailures ---
181
182 void
183 fromAron(const armarx::task_outcome::arondto::PerceptionFailures& dto,
185 {
186 bo.objectNotDetected = dto.objectNotDetected;
187 bo.cameraNotWorking = dto.cameraNotWorking;
188 }
189
190 void
191 toAron(armarx::task_outcome::arondto::PerceptionFailures& dto,
192 const PerceptionFailures& bo)
193 {
194 dto.objectNotDetected = bo.objectNotDetected;
195 dto.cameraNotWorking = bo.cameraNotWorking;
196 }
197
198 // --- KnowledgeRetrievalFailures ---
199
200 void
201 fromAron(const armarx::task_outcome::arondto::KnowledgeRetrievalFailures& dto,
203 {
204 bo.technicalFailure = dto.technicalFailure;
205 bo.knowledgeNotInMemory = dto.knowledgeNotInMemory;
206 }
207
208 void
209 toAron(armarx::task_outcome::arondto::KnowledgeRetrievalFailures& dto,
211 {
212 dto.technicalFailure = bo.technicalFailure;
213 dto.knowledgeNotInMemory = bo.knowledgeNotInMemory;
214 }
215
216 // --- KnowledgeFailures ---
217
218 void
219 fromAron(const armarx::task_outcome::arondto::KnowledgeFailures& dto,
221 {
222 fromAron(dto.perception, bo.perception);
223 fromAron(dto.knowledgeRetrieval, bo.knowledgeRetrieval);
224 }
225
226 void
227 toAron(armarx::task_outcome::arondto::KnowledgeFailures& dto,
228 const KnowledgeFailures& bo)
229 {
230 toAron(dto.perception, bo.perception);
231 toAron(dto.knowledgeRetrieval, bo.knowledgeRetrieval);
232 }
233
234 // --- ExecutionFailures ---
235
236 void
237 fromAron(const armarx::task_outcome::arondto::ExecutionFailures& dto,
239 {
240 bo.robotNotMoving = dto.robotNotMoving;
241 bo.handNotClosing = dto.handNotClosing;
242 bo.graspingFailureDetected = dto.graspingFailureDetected;
243 }
244
245 void
246 toAron(armarx::task_outcome::arondto::ExecutionFailures& dto,
247 const ExecutionFailures& bo)
248 {
249 dto.robotNotMoving = bo.robotNotMoving;
250 dto.handNotClosing = bo.handNotClosing;
251 dto.graspingFailureDetected = bo.graspingFailureDetected;
252 }
253
254 // --- InteractionFailures ---
255
256 void
257 fromAron(const armarx::task_outcome::arondto::InteractionFailures& dto,
259 {
260 bo.noHumanForHandover = dto.noHumanForHandover;
261 bo.desiredObjectNotAtLocation = dto.desiredObjectNotAtLocation;
262 bo.communicationFailure = dto.communicationFailure;
263 }
264
265 void
266 toAron(armarx::task_outcome::arondto::InteractionFailures& dto,
267 const InteractionFailures& bo)
268 {
269 dto.noHumanForHandover = bo.noHumanForHandover;
270 dto.desiredObjectNotAtLocation = bo.desiredObjectNotAtLocation;
271 dto.communicationFailure = bo.communicationFailure;
272 }
273
274 // --- InterruptionFailures ---
275
276 void
277 fromAron(const armarx::task_outcome::arondto::InterruptionFailures& dto,
279 {
280 bo.userStopped = dto.userStopped;
281 }
282
283 void
284 toAron(armarx::task_outcome::arondto::InterruptionFailures& dto,
285 const InterruptionFailures& bo)
286 {
287 dto.userStopped = bo.userStopped;
288 }
289
290 // --- SubskillFailure ---
291
292 void
293 fromAron(const armarx::task_outcome::arondto::SubskillFailure& dto, SubskillFailure& bo)
294 {
295 bo.failed = dto.failed;
296 bo.subskillName = dto.subskillName;
297 }
298
299 void
300 toAron(armarx::task_outcome::arondto::SubskillFailure& dto, const SubskillFailure& bo)
301 {
302 dto.failed = bo.failed;
303 dto.subskillName = bo.subskillName;
304 }
305
306 // --- FailureType ---
307
308 void
309 fromAron(const armarx::task_outcome::arondto::FailureType& dto, FailureType& bo)
310 {
311 fromAron(dto.planning, bo.planning);
312 fromAron(dto.knowledge, bo.knowledge);
313 fromAron(dto.execution, bo.execution);
314 fromAron(dto.interaction, bo.interaction);
315 fromAron(dto.interruption, bo.interruption);
316 fromAron(dto.subskillFailure, bo.subskillFailure);
317 }
318
319 void
320 toAron(armarx::task_outcome::arondto::FailureType& dto, const FailureType& bo)
321 {
322 toAron(dto.planning, bo.planning);
323 toAron(dto.knowledge, bo.knowledge);
324 toAron(dto.execution, bo.execution);
325 toAron(dto.interaction, bo.interaction);
326 toAron(dto.interruption, bo.interruption);
327 toAron(dto.subskillFailure, bo.subskillFailure);
328 }
329
330 // --- FailureInfo ---
331
332 void
333 fromAron(const armarx::task_outcome::arondto::FailureInfo& dto, FailureInfo& bo)
334 {
335 fromAron(dto.failureType, bo.failureType);
336 bo.specialFailureDescription = dto.specialFailureDescription;
337 }
338
339 void
340 toAron(armarx::task_outcome::arondto::FailureInfo& dto, const FailureInfo& bo)
341 {
342 toAron(dto.failureType, bo.failureType);
343 dto.specialFailureDescription = bo.specialFailureDescription;
344 }
345
346 // --- RecoveryInfo ---
347
348 void
349 fromAron(const armarx::task_outcome::arondto::RecoveryInfo& dto, RecoveryInfo& bo)
350 {
352 fromAron(dto.recoveryTime, bo.recoveryTime);
353 bo.recoveryMeasure = dto.recoveryMeasure;
354 }
355
356 void
357 toAron(armarx::task_outcome::arondto::RecoveryInfo& dto, const RecoveryInfo& bo)
358 {
360 toAron(dto.recoveryTime, bo.recoveryTime);
361 dto.recoveryMeasure = bo.recoveryMeasure;
362 }
363
364 // --- TaskOutcomeContext ---
365
366 void
367 fromAron(const armarx::task_outcome::arondto::TaskOutcomeContext& dto,
369 {
370 bo.skillName = dto.skillName;
371 bo.parameters = dto.parameters;
372 bo.additional = dto.additional;
373 bo.naturalLanguageDescription = dto.naturalLanguageDescription;
374
375 bo.taskPreconditions = nlohmann::json::object();
376 for (const auto& [key, value] : dto.taskPreconditions)
377 {
378 auto parsed = nlohmann::json::parse(value, nullptr, false);
379 bo.taskPreconditions[key] = parsed.is_discarded()
380 ? nlohmann::json(value)
381 : parsed;
382 }
383
384 bo.taskPostconditions = nlohmann::json::object();
385 for (const auto& [key, value] : dto.taskPostconditions)
386 {
387 auto parsed = nlohmann::json::parse(value, nullptr, false);
388 bo.taskPostconditions[key] = parsed.is_discarded()
389 ? nlohmann::json(value)
390 : parsed;
391 }
392
393 if (dto.skillExecutionStartedTimestamp.has_value())
394 {
396 armarx::aron::fromAron(dto.skillExecutionStartedTimestamp.value(), dt);
398 }
399 else
400 {
401 bo.skillExecutionStartedTimestamp = std::nullopt;
402 }
403 }
404
405 void
406 toAron(armarx::task_outcome::arondto::TaskOutcomeContext& dto,
407 const TaskOutcomeContext& bo)
408 {
409 dto.skillName = bo.skillName;
410 dto.parameters = bo.parameters;
411 dto.additional = bo.additional;
412 dto.naturalLanguageDescription = bo.naturalLanguageDescription;
413
414 dto.taskPreconditions.clear();
415 for (const auto& [key, value] : bo.taskPreconditions.items())
416 {
417 dto.taskPreconditions[key] = value.dump();
418 }
419
420 dto.taskPostconditions.clear();
421 for (const auto& [key, value] : bo.taskPostconditions.items())
422 {
423 dto.taskPostconditions[key] = value.dump();
424 }
425
426 if (bo.skillExecutionStartedTimestamp.has_value())
427 {
428 dto.skillExecutionStartedTimestamp.emplace();
429 armarx::aron::toAron(dto.skillExecutionStartedTimestamp.value(),
431 }
432 else
433 {
434 dto.skillExecutionStartedTimestamp = std::nullopt;
435 }
436 }
437
438 // --- TaskOutcome ---
439
440 void
441 fromAron(const armarx::task_outcome::arondto::TaskOutcome& dto, TaskOutcome& bo)
442 {
444
445 bo.taskName = dto.taskName;
446 fromAron(dto.taskType, bo.taskType);
447 bo.specialTaskTypeDescription = dto.specialTaskTypeDescription;
448 fromAron(dto.outcomeType, bo.outcomeType);
449 bo.agent = dto.agent;
450 bo.errorMessage = dto.errorMessage;
451 fromAronOptional(dto.failureInfo, bo.failureInfo);
452 fromAron(dto.startTime, bo.startTime);
453 fromAron(dto.endTime, bo.endTime);
454 bo.couldRecover = dto.couldRecover;
455 fromAronOptional(dto.recoveryInfo, bo.recoveryInfo);
456 fromAron(dto.context, bo.context);
457 }
458
459 void
460 toAron(armarx::task_outcome::arondto::TaskOutcome& dto, const TaskOutcome& bo)
461 {
463
464 dto.taskName = bo.taskName;
465 toAron(dto.taskType, bo.taskType);
466 dto.specialTaskTypeDescription = bo.specialTaskTypeDescription;
467 toAron(dto.outcomeType, bo.outcomeType);
468 dto.agent = bo.agent;
469 dto.errorMessage = bo.errorMessage;
470 toAronOptional(dto.failureInfo, bo.failureInfo);
471 toAron(dto.startTime, bo.startTime);
472 toAron(dto.endTime, bo.endTime);
473 dto.couldRecover = bo.couldRecover;
474 toAronOptional(dto.recoveryInfo, bo.recoveryInfo);
475 toAron(dto.context, bo.context);
476 }
477
478} // namespace armarx::armem::task_outcome
constexpr T dt
Represents a point in time.
Definition DateTime.h:25
void fromAron(const armarx::task_outcome::arondto::TaskOutcomeType &dto, TaskOutcomeType &bo)
void toAron(armarx::task_outcome::arondto::TaskOutcomeType &dto, const TaskOutcomeType &bo)
void fromAron(const T &dto, T &bo)
void toAron(T &dto, const T &bo)
Framework for converting ARON DTOs (Data Transfer Objects) to C++ BOs (Business Objects) and back.
std::optional< std::string > specialFailureDescription
Definition types.h:99
InterruptionFailures interruption
Definition types.h:92
InteractionFailures interaction
Definition types.h:91
KnowledgeRetrievalFailures knowledgeRetrieval
Definition types.h:58
armarx::core::time::DateTime recoveryTime
Definition types.h:104
std::optional< std::string > naturalLanguageDescription
Definition types.h:113
std::map< std::string, std::string > parameters
Definition types.h:111
std::optional< armarx::core::time::DateTime > skillExecutionStartedTimestamp
Definition types.h:116
std::map< std::string, std::string > additional
Definition types.h:112
armarx::core::time::DateTime startTime
Definition types.h:128
std::optional< RecoveryInfo > recoveryInfo
Definition types.h:131
std::optional< std::string > errorMessage
Definition types.h:126
armarx::core::time::DateTime endTime
Definition types.h:129
std::optional< bool > couldRecover
Definition types.h:130
std::optional< std::string > specialTaskTypeDescription
Definition types.h:123
std::optional< FailureInfo > failureInfo
Definition types.h:127