Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Friends

mitk::StateMachineFactory Class Reference
[Interaction Classes]

builds up all specifiyed statemachines and hold them for later access More...

#include <mitkStateMachineFactory.h>

List of all members.

Public Types

typedef std::map< std::string,
mitk::State::Pointer
StartStateMap
 Typedef for all states that are defined as start-states.
typedef StartStateMap::iterator StartStateMapIter
typedef std::set< int > HistorySet
 Typedef to be used for parsing all states of one statemachine.
typedef HistorySet::iterator HistorySetIter
typedef std::map< int,
State::Pointer
StateMachineMapType
 This type holds all states of one statemachine.
typedef std::map< std::string,
StateMachineMapType * > 
AllStateMachineMapType
 this type holds all states of all statemachines so that a specific state can be accessed for persistence

Public Member Functions

 vtkTypeMacro (StateMachineFactory, vtkXMLParser)
StateGetStartState (const char *type)
 Returns the StartState of the StateMachine with the name type;.
bool LoadBehavior (std::string fileName)
 loads the xml file filename and generates the necessary instances
bool LoadBehaviorString (std::string xmlString)
 loads the xml string and generates the necessary instances
bool LoadStandardBehavior ()
 Try to load standard behavior file "StateMachine.xml".
const std::string & GetLastLoadedBehavior ()
bool AddStateMachinePattern (const char *type, mitk::State *startState, StateMachineMapType *allStatesOfStateMachine)
 Adds the given pattern to the internal list of patterns.

Static Public Member Functions

static StateMachineFactoryNew ()

Protected Member Functions

 StateMachineFactory ()
 Default Constructor.
 ~StateMachineFactory ()
 Default Destructor.
void StartElement (const char *elementName, const char **atts)
 Derived from XMLReader.
void EndElement (const char *elementName)
 Derived from XMLReader.

Friends

class StateMachine

Detailed Description

builds up all specifiyed statemachines and hold them for later access

According to the XML-File every different statemachine is build up. A new instance of a new StateMachine grabs a StartState of one certain state machine. Two instances of one kind of state machine share that state machine. During buildprocess at runtime each state machine is parsed for well formed style. Currently different interaction styles are not yet supported. To add individual state machine patterns, call LoadBehavior(...) and it will be parsed added to the internal list of patterns

Definition at line 47 of file mitkStateMachineFactory.h.


Member Typedef Documentation

this type holds all states of all statemachines so that a specific state can be accessed for persistence

Definition at line 73 of file mitkStateMachineFactory.h.

Typedef to be used for parsing all states of one statemachine.

Definition at line 62 of file mitkStateMachineFactory.h.

typedef HistorySet::iterator mitk::StateMachineFactory::HistorySetIter

Definition at line 63 of file mitkStateMachineFactory.h.

Typedef for all states that are defined as start-states.

Definition at line 56 of file mitkStateMachineFactory.h.

typedef StartStateMap::iterator mitk::StateMachineFactory::StartStateMapIter

Definition at line 57 of file mitkStateMachineFactory.h.

This type holds all states of one statemachine.

Definition at line 68 of file mitkStateMachineFactory.h.


Constructor & Destructor Documentation

mitk::StateMachineFactory::StateMachineFactory (  ) [protected]

Default Constructor.

Definition at line 62 of file mitkStateMachineFactory.cpp.

: m_AktStateMachineName(""), m_SkipStateMachine(false)
{}
mitk::StateMachineFactory::~StateMachineFactory (  ) [protected]

Default Destructor.

Definition at line 66 of file mitkStateMachineFactory.cpp.

{
  //free memory

  while (!m_AllStateMachineMap.empty())
  {
    StateMachineMapType* temp = m_AllStateMachineMap.begin()->second;
    m_AllStateMachineMap.erase(m_AllStateMachineMap.begin());
    delete temp;
  }
  
  //should not be necessary due to SmartPointers
  m_StartStates.clear();
  
  //delete WeakPointer
  if (m_AktTransition)
    delete m_AktTransition;
}

Member Function Documentation

bool mitk::StateMachineFactory::AddStateMachinePattern ( const char *  type,
mitk::State startState,
StateMachineMapType allStatesOfStateMachine 
)

Adds the given pattern to the internal list of patterns.

Method to support addition of externaly loaded patterns. Instances of states, transitions and actions are maintained within this class and freed on destruction. The states already have to be connected by transitions and actions and checked for errors. type name of the pattern to add. Will be used during initialization of a new interactor. startState the start state of this pattern. allStatesOfStateMachine a map of state ids and its states to hold their reference and delete them in destructor

Definition at line 442 of file mitkStateMachineFactory.cpp.

References STATEMACHINE_WARN.

Referenced by mitkStateMachineFactoryTest().

{
  if (startState == NULL || allStatesOfStateMachine == NULL)
    return false;

  //check if the pattern has already been added
  StartStateMapIter tempState = m_StartStates.find(type);
  if( tempState != m_StartStates.end() )
  {
    STATEMACHINE_WARN << "Pattern " << type << " has already been added!\n";
    return false;
  }

  //add the start state
  m_StartStates.insert(StartStateMap::value_type(type, startState)); 

  //add all states of the new pattern to hold their references
  m_AllStateMachineMap.insert(AllStateMachineMapType::value_type(type, allStatesOfStateMachine));
  return true;
}
void mitk::StateMachineFactory::EndElement ( const char *  elementName ) [protected]

Derived from XMLReader.

Definition at line 337 of file mitkStateMachineFactory.cpp.

References ACTION, STATE, STATE_MACHINE, STATE_MACHINE_NAME, and TRANSITION.

{
  bool ok = true;
  std::string name(elementName);

  //skip the state machine pattern because the name was not unique!
  if (m_SkipStateMachine && (name != STATE_MACHINE) )
    return;


  if ( name == STATE_MACHINE_NAME )
  {
    if (m_SkipStateMachine)
    {
      m_SkipStateMachine = false;
      return;
    }

    ok = ConnectStates(&m_AllStatesOfOneStateMachine);
    m_AllStatesOfOneStateMachine.clear();
  } 
  else if ( name == STATE_MACHINE ) 
  {
    //doesn't have to be done
  } 
  else if ( name == TRANSITION ) 
  {
    m_AktTransition = NULL; //pointer stored in its state. memory will be freed in destructor of class state
  } 
  else if ( name == ACTION ) 
  {
    m_AktAction = NULL;
  }
  else if ( name == STATE )
  {
    m_AktState = NULL;
  }
}
const std::string& mitk::StateMachineFactory::GetLastLoadedBehavior (  ) [inline]

Definition at line 104 of file mitkStateMachineFactory.h.

    {
      return m_LastLoadedBehavior;
    }
mitk::State * mitk::StateMachineFactory::GetStartState ( const char *  type )

Returns the StartState of the StateMachine with the name type;.

Returns NULL if no entry with string type is found.

Returns NULL if no entry with name type is found. Here a Smartpointer is returned to ensure, that StateMachines are also considered during reference counting.

Definition at line 89 of file mitkStateMachineFactory.cpp.

References MITK_ERROR.

Referenced by mitkStateMachineFactoryTest().

{
  StartStateMapIter tempState = m_StartStates.find(type);
  if( tempState != m_StartStates.end() )
    return (tempState)->second.GetPointer();

  MITK_ERROR << "Error in StateMachineFactory: StartState for pattern \""<< type<< "\"not found! StateMachine might not work!\n";
  return NULL;
}
bool mitk::StateMachineFactory::LoadBehavior ( std::string  fileName )

loads the xml file filename and generates the necessary instances

Loads the xml file filename and generates the necessary instances.

Definition at line 102 of file mitkStateMachineFactory.cpp.

Referenced by mitkStateMachineFactoryTest().

{
  if ( fileName.empty() )
    return false;

  m_LastLoadedBehavior = fileName;

  this->SetFileName(fileName.c_str());

  return this->Parse();
}
bool mitk::StateMachineFactory::LoadBehaviorString ( std::string  xmlString )

loads the xml string and generates the necessary instances

Loads the xml string and generates the necessary instances.

Definition at line 117 of file mitkStateMachineFactory.cpp.

{
  if ( xmlString.empty() )
    return false;

  m_LastLoadedBehavior = "String";

  return ( this->Parse(xmlString.c_str(), xmlString.length()) );
}
bool mitk::StateMachineFactory::LoadStandardBehavior (  )

Try to load standard behavior file "StateMachine.xml".

Search strategy:

  • try environment variable "MITKCONF" (path to "StateMachine.xml")
  • try "./StateMachine.xml"
  • try via source directory (using MITKROOT from cmake-created mitkConfig.h) "MITKROOT/Interactions/mitkBaseInteraction/StateMachine.xml"

Definition at line 127 of file mitkStateMachineFactory.cpp.

References mitk::StandardFileLocations::GetInstance().

Referenced by mitkStateMachineFactoryTest().

{
  std::string xmlFileName( mitk::StandardFileLocations::GetInstance()->FindFile("StateMachine.xml", "Core/Code/Interactions") );

  if (!xmlFileName.empty()) 
    return this->LoadBehavior(xmlFileName);
  else
    return false;
}
static StateMachineFactory* mitk::StateMachineFactory::New (  ) [static]
void mitk::StateMachineFactory::StartElement ( const char *  elementName,
const char **  atts 
) [protected]

Derived from XMLReader.

Definition at line 221 of file mitkStateMachineFactory.cpp.

References ACTION, BOOL_PARAMETER, DOUBLE_PARAMETER, EVENT_ID, FLOAT_PARAMETER, ID, INT_PARAMETER, MITK_INFO, NAME, mitk::StringProperty::New(), mitk::DoubleProperty::New(), mitk::FloatProperty::New(), mitk::IntProperty::New(), mitk::BoolProperty::New(), mitk::Action::New(), mitk::State::New(), NEXT_STATE_ID, START_STATE, STATE, STATE_MACHINE, STATEMACHINE_FATAL, STRING_PARAMETER, TRANSITION, and VALUE.

{
  //skip the state machine pattern because the name was not unique!
  if (m_SkipStateMachine)
    return;

  std::string name(elementName);

  if ( name == STATE_MACHINE )
  { 
    std::string tempStateMachineName = ReadXMLStringAttribut( NAME, atts ); 
    if (m_AllStateMachineMap.find(tempStateMachineName) != m_AllStateMachineMap.end())
    {
      //warning: Statemachine tempStateMachineName already exists!
      STATEMACHINE_FATAL<<"State machine pattern " << tempStateMachineName << " already exists! Skipping state machine pattern";
      m_SkipStateMachine = true;
    }
    else //tempStateMachineName is unique, so add it
    {
      m_AktStateMachineName = tempStateMachineName; 
      m_AllStateMachineMap[ m_AktStateMachineName ] = new StateMachineMapType;
    }
  } 

  else if ( name == STATE )   
  {
    std::string stateMachinName = ReadXMLStringAttribut( NAME, atts ) ;
    int id = ReadXMLIntegerAttribut( ID, atts );

    //create a new instance
    m_AktState = mitk::State::New(stateMachinName , id);
    
    // store all states to be able to access a specific state (for persistence)
    StateMachineMapType* stateMachine = m_AllStateMachineMap[ m_AktStateMachineName ];
    (*stateMachine)[id] = m_AktState;

    std::pair<mitk::State::StateMapIter,bool> ok = m_AllStatesOfOneStateMachine.insert(mitk::State::StateMap::value_type(id , m_AktState));

    if ( ok.second == false ) 
    { 
      MITK_INFO<<std::endl;
      MITK_INFO<<"Warning from StateMachineFactory: STATE_ID was not unique in pattern "<< m_AktStateMachineName<<"!"<<std::endl;
      return; //STATE_ID was not unique or something else didn't work in insert! EXITS the process
    }
    if ( ReadXMLBooleanAttribut( START_STATE, atts ) )
      m_StartStates.insert(StartStateMap::value_type(m_AktStateMachineName, m_AktState));  
  } 

  else if ( name == TRANSITION )   
  {
    std::string transitionName = ReadXMLStringAttribut( NAME, atts ) ;
    int nextStateId = ReadXMLIntegerAttribut( NEXT_STATE_ID, atts );
    int eventId = ReadXMLIntegerAttribut( EVENT_ID, atts );
    m_AktTransition = new Transition(transitionName, nextStateId, eventId);
    if ( m_AktState )
      m_AktState->AddTransition( m_AktTransition );  
  }

  else if ( name == ACTION )
  {
    int actionId = ReadXMLIntegerAttribut( ID, atts );
    m_AktAction = Action::New( actionId );
    m_AktTransition->AddAction( m_AktAction );  
  } 

  else if ( name == BOOL_PARAMETER )
  {
    if ( !m_AktAction )
      return;

    bool value = ReadXMLBooleanAttribut( VALUE, atts );    
    std::string name = ReadXMLStringAttribut( NAME, atts );    
    m_AktAction->AddProperty( name.c_str(), BoolProperty::New( value ) );  
  }  

  else if ( name == INT_PARAMETER )
  {
    if ( !m_AktAction )
      return;

    int value = ReadXMLIntegerAttribut( VALUE, atts );    
    std::string name = ReadXMLStringAttribut( NAME, atts );    
    m_AktAction->AddProperty( name.c_str(), IntProperty::New( value ) );  
  }

  else if ( name == FLOAT_PARAMETER )
  {
    if ( !m_AktAction )
      return;

    float value = ReadXMLIntegerAttribut( VALUE, atts ); 
    std::string name = ReadXMLStringAttribut( NAME, atts );    
    m_AktAction->AddProperty( name.c_str(), FloatProperty::New( value ) );    
  }

  else if ( name == DOUBLE_PARAMETER )
  {
    if ( !m_AktAction )
      return;

    double value = ReadXMLDoubleAttribut( VALUE, atts );
    std::string name = ReadXMLStringAttribut( NAME, atts );    
    m_AktAction->AddProperty( name.c_str(), DoubleProperty::New( value ) );      
  }

  else if ( name == STRING_PARAMETER )
  {
    if ( !m_AktAction )
      return;

    std::string value = ReadXMLStringAttribut( VALUE, atts );
    std::string name = ReadXMLStringAttribut( NAME, atts );    
    m_AktAction->AddProperty( name.c_str(), StringProperty::New( value ) );        
  }
}
mitk::StateMachineFactory::vtkTypeMacro ( StateMachineFactory  ,
vtkXMLParser   
)

Friends And Related Function Documentation

friend class StateMachine [friend]

brief To enable StateMachine to access states

Definition at line 125 of file mitkStateMachineFactory.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines