35#include <ArmarXCore/interface/statechart/RemoteStateIce.h>
66 this->watcher = watcher;
69std::map<std::string, std::pair<armarx::statechartmodel::StateInstancePtr, armarx::StateIceBasePtr>>
72 return completeStateMap;
79 if (watcher && modelState->getStateClass())
81 if (!watcher->subscribeToState(iceBase, modelState->getStateClass()))
88 if (stateEqual(iceBase, modelState))
90 updateState(iceBase, modelState);
94 ARMARX_INFO_S <<
"State " << iceBase->stateName <<
" could not be recognized - resetting";
95 resetState(iceBase, modelState);
98 StateList iceSubstates = iceBase->subStateList;
100 iceSubstates.begin(), iceSubstates.end(), &armarx::IceStateConverter::compareIceStates);
103 if (modelState->getStateClass())
105 modelSubstates = modelState->getStateClass()->getSubstates();
108 assert(iceSubstates.size() ==
static_cast<size_t>(modelSubstates.size()));
110 using modelSubstateIter = statechartmodel::StateInstanceMap::const_iterator;
111 StateList::const_iterator iceIter = iceSubstates.begin();
112 modelSubstateIter modelIter = modelSubstates.begin();
114 for (; iceIter != iceSubstates.end(); iceIter++, modelIter++)
116 convert(StateIceBasePtr::dynamicCast(*iceIter), modelIter.value());
121armarx::IceStateConverter::stateEqual(armarx::StateIceBasePtr iceBase,
129 if (!modelState || iceBase->stateName.c_str() != modelState->getInstanceName())
131 if (!modelState->getStateClass() && iceBase->transitions.size() > 0)
135 if (modelState->getStateClass() && !sameTransitions(iceBase, modelState->getStateClass()))
145armarx::IceStateConverter::sameTransitions(armarx::StateIceBasePtr iceBase,
148 if (
static_cast<size_t>(modelState->getTransitions().size()) != iceBase->transitions.size())
153 assert(
static_cast<size_t>(modelState->getTransitions().size()) == iceBase->transitions.size());
155 std::pair<stringVector, stringVector> sortedTransitions =
156 sortTransitionNames(iceBase->transitions, modelState->getTransitions());
157 stringVector iceTransitions = sortedTransitions.first;
158 stringVector modelTransitions = sortedTransitions.second;
160 using stringIter = stringVector::const_iterator;
161 std::pair<stringIter, stringIter> mismatch;
162 mismatch = std::mismatch<stringIter, stringIter>(
163 iceTransitions.begin(), iceTransitions.end(), modelTransitions.begin());
165 if (mismatch.first == iceTransitions.end())
174armarx::IceStateConverter::updateTransitions(armarx::StateIceBasePtr iceBase,
177 if (modelState->getTransitions().empty())
179 resetTransitions(iceBase, modelState);
183 TransitionTable iceTransitions = iceBase->transitions;
187 foreach (TransitionIceBase newTrans, iceTransitions)
190 if (!newTrans.sourceState)
192 updateTransitionFromAll(newTrans, modelState);
196 updateSingleTransition(newTrans, modelState);
201 foreach (
auto oldTrans, modelTransitions)
205 foreach (
auto newTrans, iceTransitions)
207 if (newTrans.evt->eventName == oldTrans->eventName.toStdString())
217 modelState->removeTransition(modelState->findTransition(oldTrans));
223armarx::IceStateConverter::updateSubstates(armarx::StateIceBasePtr iceBase,
226 using modelSubstateIter = statechartmodel::StateInstanceMap::const_iterator;
229 if (modelSubstates.empty())
231 resetSubstates(iceBase, modelState);
235 StateList iceSubstates = iceBase->subStateList;
237 iceSubstates.begin(), iceSubstates.end(), &armarx::IceStateConverter::compareIceStates);
240 StateList::const_iterator iceIter = iceSubstates.begin();
241 modelSubstateIter modelIter = modelSubstates.begin();
243 while ((iceIter != iceSubstates.end()) || (modelIter != modelSubstates.end()))
248 if ((iceIter != iceSubstates.end()) &&
249 ((modelIter == modelSubstates.end()) ||
250 ((*iceIter)->stateName.c_str() < modelIter.value()->getInstanceName())))
255 newSubstate->setStateName((*iceIter)->stateName.c_str());
259 (*iceIter)->stateType, newSubstate, (*iceIter)->stateName.c_str(), modelState);
260 modelState->addSubstate(instance);
267 if ((modelIter != modelSubstates.end()) &&
268 ((iceIter == iceSubstates.end()) ||
269 (*iceIter)->stateName.c_str() > modelIter.value()->getInstanceName()))
273 modelState->removeSubstate(modelIter.key());
294armarx::IceStateConverter::convertToModelParameterMap(armarx::StateParameterMap iceMap)
299 for (StateParameterMap::const_iterator iter = iceMap.begin(); iter != iceMap.end(); iter++)
304 modelMap.insert((*iter).first.c_str(), param);
310std::pair<armarx::IceStateConverter::stringVector, armarx::IceStateConverter::stringVector>
311armarx::IceStateConverter::sortTransitionNames(
312 armarx::TransitionTable iceTransitions,
315 stringVector eventsOfIceTransitions;
317 for (TransitionTable::const_iterator iceIter = iceTransitions.begin();
318 iceIter != iceTransitions.end();
321 eventsOfIceTransitions.push_back((*iceIter).evt->eventName);
324 std::sort(eventsOfIceTransitions.begin(), eventsOfIceTransitions.end());
326 stringVector eventsOfModelTransitions;
328 for (statechartmodel::CTransitionList::const_iterator modelIter = modelTransitions.begin();
329 modelIter != modelTransitions.end();
332 eventsOfModelTransitions.push_back((*modelIter)->eventName.toStdString());
335 std::sort(eventsOfModelTransitions.begin(), eventsOfModelTransitions.end());
337 return make_pair(eventsOfIceTransitions, eventsOfModelTransitions);
341armarx::IceStateConverter::compareIceStates(armarx::AbstractStateIceBasePtr l,
342 armarx::AbstractStateIceBasePtr r)
344 return l->stateName < r->stateName;
351 statechartmodel::StateInstanceMap::const_iterator substateIter =
352 state->getSubstates().find(name);
354 if (substateIter != state->getSubstates().end())
356 return *substateIter;
359 ARMARX_ERROR_S <<
"substate " << name <<
" provided by ice is not found among the substates of "
360 << state->getStateName().toStdString();
365armarx::IceStateConverter::updateStartState(armarx::StateIceBasePtr iceBase,
369 if (iceBase->initState && modelState->getStartState() &&
370 (iceBase->initState->stateName.c_str() == modelState->getStartState()->getInstanceName()))
377 resetStartState(iceBase, modelState);
382armarx::IceStateConverter::updateActiveSubstate(armarx::StateIceBasePtr iceBase,
385 if (!modelState || !iceBase)
389 if (iceBase->activeSubstate && modelState->getActiveSubstate() &&
390 (iceBase->activeSubstate->stateName.c_str() ==
391 modelState->getActiveSubstate()->getInstanceName()))
399 resetActiveSubstate(iceBase, modelState);
404armarx::IceStateConverter::updateStateAttributes(
405 armarx::StateIceBasePtr iceBase,
408 if (iceBase->stateName.c_str() != modelState->getInstanceName())
410 resetStateAttributes(iceBase, modelState);
415armarx::IceStateConverter::updateTransitionFromAll(armarx::TransitionIceBase newTrans,
422 if (!newTrans.destinationState)
428 findSubstateByName(modelState, newTrans.destinationState->stateName.c_str());
433 bool transFound =
false;
436 if ((newTrans.evt->eventName == oldTrans->eventName.toStdString()) &&
437 (substate == oldTrans->sourceState))
442 if (!(oldTrans->destinationState) ||
443 (newTrans.destinationState->stateName !=
444 oldTrans->destinationState->getInstanceName().toStdString()))
446 modelState->updateTransitionDestination(oldTrans, destination);
458 transition->eventName = newTrans.evt->eventName.c_str();
459 transition->sourceState = substate;
460 transition->destinationState = destination;
462 modelState->addTransition(transition);
468armarx::IceStateConverter::updateSingleTransition(armarx::TransitionIceBase newTrans,
478 if ((newTrans.evt->eventName == oldTrans->eventName.toStdString()))
483 if (newTrans.destinationState)
485 if (!(oldTrans->destinationState) ||
486 (newTrans.destinationState->stateName !=
487 oldTrans->destinationState->getInstanceName().toStdString()))
489 modelState->updateTransitionDestination(
491 findSubstateByName(modelState,
492 newTrans.destinationState->stateName.c_str()));
497 if (oldTrans->destinationState)
499 modelState->detachTransitionDestination(oldTrans);
511 transition->eventName = newTrans.evt->eventName.c_str();
513 transition->sourceState =
514 findSubstateByName(modelState, newTrans.sourceState->stateName.c_str());
519 transition->destinationState =
520 findSubstateByName(modelState, newTrans.destinationState->stateName.c_str());
521 modelState->addTransition(transition);
525 modelState->addDetachedTransition(transition->eventName, transition->sourceState);
531armarx::IceStateConverter::resetTransitions(armarx::StateIceBasePtr iceBase,
537 for (TransitionTable::const_iterator iceIter = iceBase->transitions.begin();
538 iceIter != iceBase->transitions.end();
542 transition->eventName = (*iceIter).evt->eventName.c_str();
544 bool detached =
false;
547 if (!((*iceIter).sourceState))
549 if (!((*iceIter).destinationState))
551 ARMARX_ERROR_S <<
"Transition from all states: " << transition->eventName
552 <<
" doesn't have a destination state.";
556 findSubstateByName(modelState, (*iceIter).destinationState->stateName.c_str());
561 newTransition->eventName = transition->eventName;
562 newTransition->sourceState = substate;
563 newTransition->destinationState = destination;
565 newTransitionList.push_back(newTransition);
570 transition->sourceState =
571 findSubstateByName(modelState, (*iceIter).sourceState->stateName.c_str());
573 if ((*iceIter).destinationState)
575 transition->destinationState =
576 findSubstateByName(modelState, (*iceIter).destinationState->stateName.c_str());
586 detachedTransitionList.push_back(transition);
590 newTransitionList.push_back(transition);
595 modelState->replaceTransitions(newTransitionList);
599 modelState->addDetachedTransition(trans->eventName, trans->sourceState);
604armarx::IceStateConverter::resetSubstates(armarx::StateIceBasePtr iceBase,
609 for (StateList::const_iterator iceIter = iceBase->subStateList.begin();
610 iceIter != iceBase->subStateList.end();
614 newSubstate->setStateName((*iceIter)->stateName.c_str());
617 armarx::RemoteStateIceBasePtr remoteState =
618 armarx::RemoteStateIceBasePtr::dynamicCast(*iceIter);
621 proxyName = QString::fromStdString(remoteState->proxyName);
626 (*iceIter)->stateName.c_str(),
631 instance->inputParameters = (*iceIter)->inputParameters;
632 newSubstateList.insert((*iceIter)->stateName.c_str(), instance);
634 if (watcher && StateIceBasePtr::dynamicCast(*iceIter)->subStateList.size() > 0)
636 if (!watcher->subscribeToState(StateIceBasePtr::dynamicCast(*iceIter), newSubstate))
641 resetState(StateIceBasePtr::dynamicCast(*iceIter), instance);
645 modelState->replaceSubstates(newSubstateList);
649armarx::IceStateConverter::resetParameters(armarx::StateIceBasePtr iceBase,
652 modelState->setInputParameters(convertToModelParameterMap(iceBase->inputParameters));
653 modelState->setLocalParameters(convertToModelParameterMap(iceBase->localParameters));
654 modelState->setOutputParameters(convertToModelParameterMap(iceBase->outputParameters));
658armarx::IceStateConverter::resetStartState(armarx::StateIceBasePtr iceBase,
661 if (iceBase->initState)
663 modelState->setStartState(
664 findSubstateByName(modelState, iceBase->initState->stateName.c_str()));
673armarx::IceStateConverter::resetActiveSubstate(armarx::StateIceBasePtr iceBase,
676 if (iceBase->activeSubstate)
680 findSubstateByName(modelState, iceBase->activeSubstate->stateName.c_str());
681 modelState->setActiveSubstate(activeSubstate);
690armarx::IceStateConverter::resetStateAttributes(
691 armarx::StateIceBasePtr iceBase,
694 modelState->setInstanceName(iceBase->stateName.c_str());
695 if (modelState->getStateClass())
697 modelState->getStateClass()->setStateName(iceBase->stateClassName.c_str());
700 modelState->getStateClass() ? modelState->getStateClass()->getSubstates().size() : 0;
701 float sizeFactor = pow(subStateCount, 0.7);
702 sizeFactor = std::max(sizeFactor, 1.0f);
703 modelState->setBoundingBox(sizeFactor * modelState->defaultBoundingSquareSize);
707armarx::IceStateConverter::resetState(armarx::StateIceBasePtr iceBase,
710 completeStateMap[iceBase->globalStateIdentifier] = std::make_pair(modelState, iceBase);
713 if (modelState->getStateClass())
715 auto stateClass = modelState->getStateClass();
716 resetParameters(iceBase, stateClass);
717 resetSubstates(iceBase, stateClass);
718 resetTransitions(iceBase, stateClass);
719 resetActiveSubstate(iceBase, stateClass);
720 resetStartState(iceBase, stateClass);
723 resetStateAttributes(iceBase, modelState);
727armarx::IceStateConverter::updateState(armarx::StateIceBasePtr iceBase,
730 updateStateAttributes(iceBase, modelState);
733 if (modelState->getStateClass())
735 auto stateClass = modelState->getStateClass();
736 updateSubstates(iceBase, stateClass);
737 if (!sameTransitions(iceBase, stateClass))
740 updateTransitions(iceBase, stateClass);
743 resetParameters(iceBase, stateClass);
744 updateActiveSubstate(iceBase, stateClass);
745 updateStartState(iceBase, stateClass);
750armarx::IceStateConverter::transitionEqual(armarx::TransitionIceBase iceTransition,
753 if (iceTransition.evt->eventName.c_str() == modelTransition->eventName)
statechartmodel::StateInstancePtr getTopState()
getTopState Returns the top state of the internal model that was converted from an ice model.
void convert(StateIceBasePtr iceBase)
Converts the given ice model into a statechartmodel.
void setStateWatcher(StateWatcherPtr watcher)
std::map< std::string, std::pair< statechartmodel::StateInstancePtr, StateIceBasePtr > > getCompleteStateMap() const
IceStateConverter(statechartmodel::StatePtr state=statechartmodel::StatePtr(new statechartmodel::State()))
IceStateConverter Creates a converter whose model's top state is state.
static StateInstancePtr CreateFromIceType(eStateType type, StatePtr stateClass, const QString &instanceName, StatePtr parentState=StatePtr(), const QString &proxyName="")
static statechartmodel::StateParameterPtr FromIceStateParameter(armarx::StateParameterIceBasePtr param)
#define ARMARX_ERROR_S
The logging level for unexpected behaviour, that must be fixed.
#define ARMARX_WARNING_S
The logging level for unexpected behaviour, but not a serious problem.
std::shared_ptr< State > StatePtr
QMap< QString, StateParameterPtr > StateParameterMap
std::shared_ptr< StateInstance > StateInstancePtr
std::shared_ptr< StateParameter > StateParameterPtr
QList< TransitionPtr > TransitionList
std::shared_ptr< const Transition > TransitionCPtr
QList< TransitionCPtr > CTransitionList
std::shared_ptr< Transition > TransitionPtr
QMap< QString, StateInstancePtr > StateInstanceMap
armem::articulated_object::ArticulatedObject convert(const VirtualRobot::Robot &obj, const armem::Time ×tamp)
IceInternal::Handle< StateWatcher > StateWatcherPtr