Medical Imaging Interaction Toolkit  2016.11.0
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,
6  Division of Medical and Biological Informatics.
7  All rights reserved.
8 
9  This software is distributed WITHOUT ANY WARRANTY; without
10  even the implied warranty of MERCHANTABILITY or FITNESS FOR
11  A PARTICULAR PURPOSE.
12 
13  See LICENSE.txt or http://www.mitk.org for details.
14 
15  ===================================================================*/
16 
17 #include "mitkStateMachineState.h"
18 
19 mitk::StateMachineState::StateMachineState(const std::string &stateName, const std::string &stateMode)
20  : m_Name(stateName), m_StateMode(stateMode)
21 {
22 }
23 
25 {
26  return m_StateMode;
27 }
28 
30 {
31  m_Transitions.clear();
32 }
33 
35 {
36  for (TransitionVector::iterator it = m_Transitions.begin(); it != m_Transitions.end(); ++it)
37  {
38  if (transition.GetPointer() == (*it).GetPointer())
39  return false;
40  }
41  m_Transitions.push_back(transition);
42  return true;
43 }
44 
46  const std::string &eventVariant)
47 {
48  TransitionVector transitions = this->GetTransitionList(eventClass, eventVariant);
49 
50  if (transitions.size() > 1)
51  {
52  MITK_WARN << "Multiple transitions have been found for event. Use non-deprecated method "
53  "StateMachineState::GetTransitionList() instead!";
54  }
55 
56  if (transitions.empty())
57  {
58  return NULL;
59  }
60  else
61  {
62  return transitions.at(0);
63  }
64 }
65 
67  const std::string &eventVariant)
68 {
69  TransitionVector transitions;
71  for (TransitionVector::iterator it = m_Transitions.begin(); it != m_Transitions.end(); ++it)
72  {
73  if (**it == *t) // do not switch it and t, order matters, see mitk::StateMachineTransition == operator
74  transitions.push_back(*it);
75  }
76  return transitions;
77 }
78 
80 {
81  return m_Name;
82 }
83 
84 //##Documentation
85 //## Post-processing step, when builing StateMachine from XML.
86 //## Parse all transitions and find the State that matches the String-Name.
87 
89 {
90  for (TransitionVector::iterator transIt = m_Transitions.begin(); transIt != m_Transitions.end(); ++transIt)
91  {
92  bool found = false;
93  for (StateMap::iterator stateIt = allStates->begin(); stateIt != allStates->end(); ++stateIt)
94  {
95  if ((*stateIt)->GetName() == (*transIt)->GetNextStateName())
96  {
97  (*transIt)->SetNextState(*stateIt);
98  found = true;
99  break;
100  }
101  }
102  if (!found)
103  {
104  MITK_WARN << "Target State not found in StateMachine.";
105  return false; // only reached if no state matching the string is found
106  }
107  }
108  return true;
109 }
StateMachineState(const std::string &name, const std::string &stateMode)
std::string GetMode() const
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:23
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.