28#include <IceUtil/UUID.h>
30#include <SimoxUtility/algorithm/string/string_tools.h>
45 joinStrings(std::vector<std::string>
const& input, std::string
const& seperator)
47 return simox::alg::join(input, seperator);
103 return globalStateIdentifier;
109 return StateBasePtr::dynamicCast(initState);
121 return outputParameters;
127 for (
size_t i = 0; i < subStateList.size(); ++i)
129 if (stateName == StateBasePtr::dynamicCast(subStateList[i])->stateName ||
130 stateName == StateBasePtr::dynamicCast(subStateList[i])->globalStateIdentifier)
132 return StatePtr::dynamicCast(subStateList[i]);
146 "There exists already a substate with name '" + stateName +
147 "' in this hierarchy level. In one hierarchy level (aka one substatelist) the "
148 "names must be unique.");
151 pNewState->__setParentState(
this);
152 subStateList.push_back(pNewState);
162 if (instanceName.empty())
164 instanceName = stateName;
169 throw LocalException(
"There exists already a substate with name '" + instanceName +
170 "' in this hierarchy level. In one hierarchy level aka one "
171 "substatelist the names must be unique.");
175 pNewState->setStateName(instanceName);
176 pNewState->setStateClassName(stateName);
177 pNewState->__setParentState(
this);
178 pNewState->setProxyName(proxyName);
180 subStateList.push_back(pNewState);
185 if (!ManagedIceObjectPtr::dynamicCast(pNewState))
188 "could not cast RemoteStatePtr to ManagedIceObjectPtr");
194 "statechart context ptr is NULL -> could not add state as component");
199 context->
getArmarXManager()->addObject(ManagedIceObjectPtr::dynamicCast(pNewState),
219 throw LocalException(
"There exists already a substate with name '" + stateName +
220 "' in this hierarchy level. In one hierarchy level aka one "
221 "substatelist the names must be unique.");
226 pNewState->__setParentState(
this);
227 pNewState->setStateName(instanceName);
228 subStateList.push_back(pNewState);
236 context->
getArmarXManager()->addObject(ManagedIceObjectPtr::dynamicCast(pNewState),
238 pNewState->getName() + IceUtil::generateUUID());
240 ARMARX_DEBUG <<
"Waiting for RemoteState " << pNewState->getName();
242 pNewState->getObjectScheduler()->waitForObjectStateMinimum(eManagedIceObjectStarted);
244 ARMARX_DEBUG <<
"RemoteState " << pNewState->getName() <<
"started.";
251 StateIceBasePtr sourceState,
252 StateIceBasePtr destinationState,
253 ParameterMappingIceBasePtr mappingToNextStatesInput,
254 ParameterMappingIceBasePtr mappingToParentStatesLocal,
255 ParameterMappingIceBasePtr mappingToParentStatesOutput)
260 assert(sourceState._ptr);
261 assert(destinationState._ptr);
262 t.sourceState = sourceState;
263 t.evt =
createEvent(event->eventName, event->properties);
264 t.destinationState = destinationState;
266 if (mappingToNextStatesInput)
268 t.mappingToNextStatesInput = PMPtr::dynamicCast(mappingToNextStatesInput)->clone();
271 if (mappingToParentStatesLocal)
273 t.mappingToParentStatesLocal = PMPtr::dynamicCast(mappingToParentStatesLocal)->clone();
276 if (mappingToParentStatesOutput)
278 t.mappingToParentStatesOutput =
279 PMPtr::dynamicCast(mappingToParentStatesOutput)->clone();
284 "There already exists a transition on event '" + t.evt->eventName +
"' from '" +
285 sourceState->stateName +
"' to '" + destinationState->stateName +
"'");
287 transitions.push_back(t);
288 return *transitions.rbegin();
305 return addParameter(inputParameters, key, type, optional);
310 const ContainerType& type,
312 VariantContainerBasePtr defaultValue)
322 return addParameter(outputParameters, key, type, optional);
335 const ContainerType& type,
336 VariantContainerBasePtr defaultValue)
339 std::unique_lock lock(simpl->localParameterMutex);
348 std::unique_lock lock(simpl->localParameterMutex);
350 return addParameter(localParameters, key, type,
true, defaultValue);
358 if (initialStateMapping)
360 this->initialStateMapping = initialStateMapping->clone();
363 if (!StateIceBasePtr::dynamicCast(initState))
365 throw LocalException(
"Couldnt cast initstate");
368 this->initState = initState;
402 std::string className = simox::alg::replace_all(typeName,
":",
"_");
409 StateParameterMap result;
410 std::unique_lock lock(simpl->inputParameterMutex);
419 std::unique_lock lock(simpl->inputParameterMutex);
430 return localParameters;
437 std::unique_lock lock(simpl->inputParameterMutex);
441 throw LocalException(
"Input parameter '" + key +
"' in state '" + stateName +
442 "'' is already set and cannot be changed.");
452 std::unique_lock lock(simpl->inputParameterMutex);
456 throw LocalException(
"Input parameter '" + key +
"' in state '" + stateName +
457 "'' is already set and cannot be changed.");
467 std::unique_lock lock(simpl->localParameterMutex);
476 std::unique_lock lock(simpl->localParameterMutex);
485 std::unique_lock lock(simpl->outputParameterMutex);
494 std::unique_lock lock(simpl->outputParameterMutex);
524 VariantContainerBasePtr
528 std::unique_lock lock(simpl->localParameterMutex);
530 VariantContainerBasePtr varList;
532 return VariantContainerBasePtr::dynamicCast(varList);
562 "There already exists a substate with name '" + stateName +
563 "' in this hierarchy level. In one hierarchy level (aka one substatelist) the "
564 "names must be unique.");
567 if (stateName.empty())
573 pNewState->stateName = stateName;
574 pNewState->__setParentState(
this);
575 subStateList.push_back(pNewState);
Base Class for all Logging classes.
ArmarXManagerPtr getArmarXManager() const
Returns the ArmarX manager used to add and remove components.
This class is the implementation of the Slice Definition of a state.
void getParameterContainer(const StateParameterMap ¶mMap, const std::string &key, VariantContainerBasePtr &valueContainer) const
StateBase & operator=(const StateBase &source)
StateBasePtr findSubstateByName(const std::string &substateName)
Utility function to find a substate of this state by the name.
bool isParameterSet(const StateParameterMap ¶mMap, const std::string &key) const
void setParameter(StateParameterMap ¶mMap, const std::string &key, const Variant &variant)
void __checkPhase(StatePhase allowedType, const char *functionName) const
Helper function for checking if a function was called in valid position of the statechart.
void __checkPhaseMin(StatePhase allowedType, const char *functionName) const
bool addParameterContainer(StateParameterMap ¶mMap, const std::string &key, const ContainerType &containerType, bool optional, VariantContainerBasePtr defaultValue=VariantContainerBasePtr()) const
void setStateClassName(std::string className)
setStateClassName() sets the string, that contains a stringrepresentation of this class....
void setParameterContainer(StateParameterMap ¶mMap, const std::string &key, const VariantContainerBasePtr &valueContainer)
bool addParameter(StateParameterMap ¶mMap, const std::string &key, VariantTypeId type, bool optional, VariantPtr defaultValue=VariantPtr()) const
bool init(StatechartContextInterface *context, StatechartManager *manager)
Function to initialize this state. Must be called in the highest level of the hierarchy - and only th...
std::unique_ptr< Impl > impl
StatePhase getStatePhase() const
void getParameter(const StateParameterMap ¶mMap, const std::string &key, VariantPtr &value) const
ContextType * getContext() const
The StateController class processes events and controls the statechart flow.
bool __checkExistenceOfTransition(const TransitionIceBase &transition)
friend class DynamicRemoteState
This class provides utility functions for statechart-implementing-users to communicate with other dis...
EventPtr createEvent()
Utility function to create a new Event.
Class that offers the main functionality needed to create a statechart.
void setStateName(const std::string &newName)
StateParameterMap getInputParameters() override
Returns a new copy of the inputparameters-dictionary, so that the caller cannot modify them (const wo...
StateParameterMap & getLocalParameters()
Getter for the local parameter map.
bool isUnbreakable() const
void setOutput(std::string const &key, const Variant &value)
setOuput() sets an output parameter of this state.
StateBasePtr addState(StateBasePtr pNewState)
bool isOutputParameterSet(const std::string &key) const
StateBasePtr clone() const override
Generates a new copy of this state with the same statename, substates, transitions,...
bool addToInput(const std::string &key, const ContainerType &type, bool optional, VariantContainerBasePtr defaultValue=VariantContainerBasePtr())
Adds a key,type-pair to the input parameters.
std::enable_if_t< std::is_base_of_v< VariantDataClass, T >, IceInternal::Handle< T > > getInput(const std::string &key) const
getInput can be used to access a specific input parameter.
StateBasePtr setInitState(StateBasePtr initState, ParameterMappingPtr initialStateMapping=ParameterMappingPtr())
Sets the initial substate of this state.
bool isLocalParameterSet(const std::string &key) const
TransitionIceBase & addTransition(EventPtr event, StateIceBasePtr sourceState, StateIceBasePtr destinationState, ParameterMappingIceBasePtr mappingToNextStatesInput=nullptr, ParameterMappingIceBasePtr mappingToParentStatesLocal=nullptr, ParameterMappingIceBasePtr mappingToParentStatesOutput=nullptr)
void setUseRunFunction(bool useRunFuntion)
setUseRunFunction can be used to configurate whether the thread with the async.
State & operator=(const State &source)
VariantContainerBasePtr getLocalContainer(std::string const &key)
std::string getGlobalIdString() const
Getter for the global state identifier string.
StateBasePtr createEmptyCopy() const override
Generates a new copy of this state with the same overidden functions. stateName, substates,...
bool addToOutput(const std::string &key, VariantTypeId type, bool optional)
Adds a key,type-pair to the output parameters.
void setStateClassNameFromTypeName(const std::string &typeName)
virtual RemoteStatePtr addDynamicRemoteState(std::string instanceName)
Function to add a new dynamic remote substate to this state.
void setUnbreakable(bool setUnbreakable=true)
With this function the state can be made unbreakable.
StatePtr operator[](std::string const &stateName)
Function to jump directly into a state. Should only be called for statemachine-debugging.
virtual RemoteStatePtr addRemoteState(std::string stateName, std::string proxyName, std::string instanceName="")
Function to add a new remote substate to this state.
bool init(StatechartContextInterface *context, StatechartManager *manager)
Function to initialize this state. Must be called in the highest level of the hierarchy - and only th...
void cancelSubstates()
Use this function in the onEnter() function, if you want to avoid that the substates (i....
bool addToLocal(const std::string &key, VariantTypeId type, VariantPtr defaultValue=VariantPtr())
Adds a key,type-pair to the local parameters.
bool isInputParameterSet(const std::string &key) const
Checks whether a given input parameter is set or not.
void inheritInputFromSubstate(std::string stateName)
void setLocal(std::string const &key, const Variant &value)
setLocal() sets a local parameter.
void setInput(std::string const &key, const Variant &value)
setInput() sets an input parameter.
unsigned int getId() const
Returns an id to this state, that is guaranteed to be unique in this process.
StateBasePtr getInitState() const
Getter for the initial state. The initial state is automatically entered, when this state is entered.
StateParameterMap & getOutputParameters() override
getter function to get the map of output parameters
This class contains a statechart and provides the interfaces to distributed components.
The Variant class is described here: Variants.
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
void copyDictionary(const StringVariantContainerBaseMap &source, StringVariantContainerBaseMap &destination)
Clears the destination map and copies the parameters of the source in it.
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceInternal::Handle< RemoteState > RemoteStatePtr
std::string joinStrings(std::vector< std::string > const &input, std::string const &seperator)
IceInternal::Handle< State > StatePtr
IceInternal::Handle< Variant > VariantPtr
IceInternal::Handle< Event > EventPtr
Typedef of EventPtr as IceInternal::Handle<Event> for convenience.
IceInternal::Handle< StateBase > StateBasePtr
IceInternal::Handle< ParameterMapping > ParameterMappingPtr
std::mutex localParameterMutex
std::mutex outputParameterMutex
std::mutex inputParameterMutex