32 #include <ArmarXCore/interface/statechart/RemoteStateIce.h>
65 this->watcher = watcher;
70 return completeStateMap;
75 if (watcher && modelState->getStateClass())
77 if (!watcher->subscribeToState(iceBase, modelState->getStateClass()))
84 if (stateEqual(iceBase, modelState))
86 updateState(iceBase, modelState);
90 ARMARX_INFO_S <<
"State " << iceBase->stateName <<
" could not be recognized - resetting";
91 resetState(iceBase, modelState);
94 StateList iceSubstates = iceBase->subStateList;
95 std::sort(iceSubstates.begin(), iceSubstates.end(), &armarx::IceStateConverter::compareIceStates);
98 if (modelState->getStateClass())
100 modelSubstates = modelState->getStateClass()->getSubstates();
103 assert(iceSubstates.size() ==
static_cast<size_t>(modelSubstates.size()));
105 using modelSubstateIter = statechartmodel::StateInstanceMap::const_iterator;
106 StateList::const_iterator iceIter = iceSubstates.begin();
107 modelSubstateIter modelIter = modelSubstates.begin();
109 for (; iceIter != iceSubstates.end(); iceIter++, modelIter++)
111 convert(StateIceBasePtr::dynamicCast(*iceIter), modelIter.value());
122 if (!modelState || iceBase->stateName.c_str() != modelState->getInstanceName())
124 if (!modelState->getStateClass() && iceBase->transitions.size() > 0)
128 if (modelState->getStateClass() && !sameTransitions(iceBase, modelState->getStateClass()))
139 if (
static_cast<size_t>(modelState->getTransitions().size()) != iceBase->transitions.size())
144 assert(
static_cast<size_t>(modelState->getTransitions().size()) == iceBase->transitions.size());
146 std::pair<stringVector, stringVector> sortedTransitions = sortTransitionNames(iceBase->transitions, modelState->getTransitions());
147 stringVector iceTransitions = sortedTransitions.first;
148 stringVector modelTransitions = sortedTransitions.second;
150 using stringIter = stringVector::const_iterator;
151 std::pair<stringIter, stringIter> mismatch;
152 mismatch = std::mismatch<stringIter, stringIter>(iceTransitions.begin(), iceTransitions.end(), modelTransitions.begin());
154 if (mismatch.first == iceTransitions.end())
165 if (modelState->getTransitions().empty())
167 resetTransitions(iceBase, modelState);
171 TransitionTable iceTransitions = iceBase ->transitions;
175 foreach (TransitionIceBase newTrans, iceTransitions)
178 if (!newTrans.sourceState)
180 updateTransitionFromAll(newTrans, modelState);
184 updateSingleTransition(newTrans, modelState);
189 foreach (
auto oldTrans, modelTransitions)
193 foreach (
auto newTrans, iceTransitions)
195 if (newTrans.evt->eventName == oldTrans->eventName.toStdString())
205 modelState->removeTransition(modelState->findTransition(oldTrans));
212 using modelSubstateIter = statechartmodel::StateInstanceMap::const_iterator;
215 if (modelSubstates.empty())
217 resetSubstates(iceBase, modelState);
221 StateList iceSubstates = iceBase->subStateList;
222 std::sort(iceSubstates.begin(), iceSubstates.end(), &armarx::IceStateConverter::compareIceStates);
225 StateList::const_iterator iceIter = iceSubstates.begin();
226 modelSubstateIter modelIter = modelSubstates.begin();
228 while ((iceIter != iceSubstates.end()) || (modelIter != modelSubstates.end()))
233 if ((iceIter != iceSubstates.end()) && ((modelIter == modelSubstates.end()) || ((*iceIter)->stateName.c_str() < modelIter.value()->getInstanceName())))
238 newSubstate->setStateName((*iceIter)->stateName.c_str());
241 modelState->addSubstate(instance);
248 if ((modelIter != modelSubstates.end()) && ((iceIter == iceSubstates.end()) || (*iceIter)->stateName.c_str() > modelIter.value()->getInstanceName()))
252 modelState->removeSubstate(modelIter.key());
277 for (StateParameterMap::const_iterator iter = iceMap.begin(); iter != iceMap.end(); iter++)
281 modelMap.insert((*iter).first.c_str(), param);
287 std::pair<armarx::IceStateConverter::stringVector, armarx::IceStateConverter::stringVector> armarx::IceStateConverter::sortTransitionNames(armarx::TransitionTable iceTransitions,
290 stringVector eventsOfIceTransitions;
292 for (TransitionTable::const_iterator iceIter = iceTransitions.begin() ; iceIter != iceTransitions.end(); iceIter++)
294 eventsOfIceTransitions.push_back((*iceIter).evt->eventName);
297 std::sort(eventsOfIceTransitions.begin(), eventsOfIceTransitions.end());
299 stringVector eventsOfModelTransitions;
301 for (statechartmodel::CTransitionList::const_iterator modelIter = modelTransitions.begin() ; modelIter != modelTransitions.end(); modelIter++)
303 eventsOfModelTransitions.push_back((*modelIter)->eventName.toStdString());
306 std::sort(eventsOfModelTransitions.begin(), eventsOfModelTransitions.end());
308 return make_pair(eventsOfIceTransitions, eventsOfModelTransitions);
311 bool armarx::IceStateConverter::compareIceStates(armarx::AbstractStateIceBasePtr l, armarx::AbstractStateIceBasePtr r)
313 return l->stateName < r->stateName;
319 statechartmodel::StateInstanceMap::const_iterator substateIter = state->getSubstates().find(name);
321 if (substateIter != state->getSubstates().end())
323 return *substateIter;
326 ARMARX_ERROR_S <<
"substate " << name <<
" provided by ice is not found among the substates of "
327 << state->getStateName().toStdString();
334 if (iceBase->initState && modelState->getStartState()
335 && (iceBase->initState->stateName.c_str() == modelState->getStartState()->getInstanceName()))
342 resetStartState(iceBase, modelState);
348 if (!modelState || !iceBase)
352 if (iceBase->activeSubstate && modelState->getActiveSubstate()
353 && (iceBase->activeSubstate->stateName.c_str() == modelState->getActiveSubstate()->getInstanceName()))
361 resetActiveSubstate(iceBase, modelState);
367 if (iceBase->stateName.c_str() != modelState->getInstanceName())
369 resetStateAttributes(iceBase, modelState);
379 if (!newTrans.destinationState)
389 bool transFound =
false;
392 if ((newTrans.evt->eventName == oldTrans->eventName.toStdString())
393 && (substate == oldTrans->sourceState))
398 if (!(oldTrans->destinationState) || (newTrans.destinationState->stateName != oldTrans->destinationState->getInstanceName().toStdString()))
400 modelState->updateTransitionDestination(oldTrans, destination);
412 transition->eventName = newTrans.evt->eventName.c_str();
413 transition->sourceState = substate;
414 transition->destinationState = destination;
416 modelState->addTransition(transition);
430 if ((newTrans.evt->eventName == oldTrans->eventName.toStdString()))
435 if (newTrans.destinationState)
437 if (!(oldTrans->destinationState) || (newTrans.destinationState->stateName != oldTrans->destinationState->getInstanceName().toStdString()))
439 modelState->updateTransitionDestination(oldTrans, findSubstateByName(modelState, newTrans.destinationState->stateName.c_str()));
444 if (oldTrans->destinationState)
446 modelState->detachTransitionDestination(oldTrans);
458 transition->eventName = newTrans.evt->eventName.c_str();
460 transition->sourceState = findSubstateByName(modelState, newTrans.sourceState->stateName.c_str());
462 if (newTrans.destinationState)
464 transition->destinationState = findSubstateByName(modelState, newTrans.destinationState->stateName.c_str());
465 modelState->addTransition(transition);
469 modelState->addDetachedTransition(transition->eventName, transition->sourceState);
479 for (TransitionTable::const_iterator iceIter = iceBase->transitions.begin(); iceIter != iceBase->transitions.end(); iceIter++)
482 transition->eventName = (*iceIter).evt->eventName.c_str();
484 bool detached =
false;
487 if (!((*iceIter).sourceState))
489 if (!((*iceIter).destinationState))
491 ARMARX_ERROR_S <<
"Transition from all states: " << transition->eventName <<
" doesn't have a destination state.";
499 newTransition->eventName = transition->eventName;
500 newTransition->sourceState = substate;
501 newTransition->destinationState = destination;
503 newTransitionList.push_back(newTransition);
508 transition->sourceState = findSubstateByName(modelState, (*iceIter).sourceState->stateName.c_str());
510 if ((*iceIter).destinationState)
512 transition->destinationState = findSubstateByName(modelState, (*iceIter).destinationState->stateName.c_str());
522 detachedTransitionList.push_back(transition);
526 newTransitionList.push_back(transition);
531 modelState->replaceTransitions(newTransitionList);
535 modelState->addDetachedTransition(trans->eventName, trans->sourceState);
543 for (StateList::const_iterator iceIter = iceBase->subStateList.begin(); iceIter != iceBase->subStateList.end(); iceIter++)
546 newSubstate->setStateName((*iceIter)->stateName.c_str());
549 armarx::RemoteStateIceBasePtr remoteState = armarx::RemoteStateIceBasePtr::dynamicCast(*iceIter);
552 proxyName = QString::fromStdString(remoteState->proxyName);
557 instance->inputParameters = (*iceIter)->inputParameters;
558 newSubstateList.insert((*iceIter)->stateName.c_str(), instance);
560 if (watcher && StateIceBasePtr::dynamicCast(*iceIter)->subStateList.size() > 0)
562 if (!watcher->subscribeToState(StateIceBasePtr::dynamicCast(*iceIter), newSubstate))
567 resetState(StateIceBasePtr::dynamicCast(*iceIter), instance);
571 modelState->replaceSubstates(newSubstateList);
576 modelState->setInputParameters(convertToModelParameterMap(iceBase->inputParameters));
577 modelState->setLocalParameters(convertToModelParameterMap(iceBase->localParameters));
578 modelState->setOutputParameters(convertToModelParameterMap(iceBase->outputParameters));
583 if (iceBase->initState)
585 modelState->setStartState(findSubstateByName(modelState, iceBase->initState->stateName.c_str()));
595 if (iceBase->activeSubstate)
599 modelState->setActiveSubstate(activeSubstate);
610 modelState->setInstanceName(iceBase->stateName.c_str());
611 if (modelState->getStateClass())
613 modelState->getStateClass()->setStateName(iceBase->stateClassName.c_str());
615 int subStateCount = modelState->getStateClass() ? modelState->getStateClass()->getSubstates().size() : 0;
616 float sizeFactor = pow(subStateCount, 0.7);
617 sizeFactor =
std::max(sizeFactor, 1.0f);
618 modelState->setBoundingBox(sizeFactor * modelState->defaultBoundingSquareSize);
623 completeStateMap[iceBase->globalStateIdentifier] = std::make_pair(modelState, iceBase);
626 if (modelState->getStateClass())
628 auto stateClass = modelState->getStateClass();
629 resetParameters(iceBase, stateClass);
630 resetSubstates(iceBase, stateClass);
631 resetTransitions(iceBase, stateClass);
632 resetActiveSubstate(iceBase, stateClass);
633 resetStartState(iceBase, stateClass);
636 resetStateAttributes(iceBase, modelState);
642 updateStateAttributes(iceBase, modelState);
645 if (modelState->getStateClass())
647 auto stateClass = modelState->getStateClass();
648 updateSubstates(iceBase, stateClass);
649 if (!sameTransitions(iceBase, stateClass))
652 updateTransitions(iceBase, stateClass);
655 resetParameters(iceBase, stateClass);
656 updateActiveSubstate(iceBase, stateClass);
657 updateStartState(iceBase, stateClass);
661 bool armarx::IceStateConverter::transitionEqual(armarx::TransitionIceBase iceTransition,
664 if (iceTransition.evt->eventName.c_str() == modelTransition->eventName)