Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkStateMachineState.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include "mitkStateMachineState.h"
14 
15 mitk::StateMachineState::StateMachineState(const std::string &stateName, const std::string &stateMode)
16  : m_Name(stateName), m_StateMode(stateMode)
17 {
18 }
19 
21 {
22  return m_StateMode;
23 }
24 
26 {
27  m_Transitions.clear();
28 }
29 
31 {
32  for (auto it = m_Transitions.begin(); it != m_Transitions.end(); ++it)
33  {
34  if (transition.GetPointer() == (*it).GetPointer())
35  return false;
36  }
37  m_Transitions.push_back(transition);
38  return true;
39 }
40 
42  const std::string &eventVariant)
43 {
44  TransitionVector transitions = this->GetTransitionList(eventClass, eventVariant);
45 
46  if (transitions.size() > 1)
47  {
48  MITK_WARN << "Multiple transitions have been found for event. Use non-deprecated method "
49  "StateMachineState::GetTransitionList() instead!";
50  }
51 
52  if (transitions.empty())
53  {
54  return nullptr;
55  }
56  else
57  {
58  return transitions.at(0);
59  }
60 }
61 
63  const std::string &eventVariant)
64 {
65  TransitionVector transitions;
67  for (auto it = m_Transitions.begin(); it != m_Transitions.end(); ++it)
68  {
69  if (**it == *t) // do not switch it and t, order matters, see mitk::StateMachineTransition == operator
70  transitions.push_back(*it);
71  }
72  return transitions;
73 }
74 
76 {
77  return m_Name;
78 }
79 
80 //##Documentation
81 //## Post-processing step, when builing StateMachine from XML.
82 //## Parse all transitions and find the State that matches the String-Name.
83 
85 {
86  for (auto transIt = m_Transitions.begin(); transIt != m_Transitions.end(); ++transIt)
87  {
88  bool found = false;
89  for (auto stateIt = allStates->begin(); stateIt != allStates->end(); ++stateIt)
90  {
91  if ((*stateIt)->GetName() == (*transIt)->GetNextStateName())
92  {
93  (*transIt)->SetNextState(*stateIt);
94  found = true;
95  break;
96  }
97  }
98  if (!found)
99  {
100  MITK_WARN << "Target State not found in StateMachine.";
101  return false; // only reached if no state matching the string is found
102  }
103  }
104  return true;
105 }
StateMachineState(const std::string &name, const std::string &stateMode)
bool AddTransition(StateMachineTransition::Pointer transition)
TransitionVector GetTransitionList(const std::string &eventClass, const std::string &eventVariant)
Return Transitions that match given event description.
std::string GetName() const
Returns the name.
#define MITK_WARN
Definition: mitkLogMacros.h:19
std::vector< mitk::StateMachineState::Pointer > StateMap
static Pointer New(const std::string &_arga, const std::string &_argb, const std::string &_argc)
bool ConnectTransitions(StateMap *allStates)
Searches dedicated States of all Transitions and sets *nextState of these Transitions. Required for this is a List of all build States of that StateMachine (allStates). This way the StateMachine can be build up.
std::vector< StateMachineTransition::Pointer > TransitionVector
StateMachineTransition::Pointer GetTransition(const std::string &eventClass, const std::string &eventVariant)
Return Transition which matches given event description.