File size: 4,770 Bytes
ef17c16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-    */
/* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */

/*
  Author(s):  Anton Deguet
  Created on: 2016-02-05

  (C) Copyright 2016-2018 Johns Hopkins University (JHU), All Rights Reserved.

  --- begin cisst license - do not edit ---

  This software is provided "as is" under an open source license, with
  no warranty.  The complete license can be found in license.txt and
  http://www.cisst.org/cisst/license.txt.

  --- end cisst license ---
*/

#include <sawIntuitiveResearchKit/mtsStateMachine.h>

void mtsStateMachine::AddState(const StateType state)
{
    if (StateExists(state)) {
        cmnThrow("mtsStateMachine::AddState: "
                 + mName + ", state " + state + " already exists");
    }
    mStates[state] = false;
}

void mtsStateMachine::AddStates(const std::vector<StateType> & states)
{
    typedef std::vector<StateType> VectorType;
    const VectorType::const_iterator end = states.end();
    VectorType::const_iterator iter = states.begin();
    for (; iter != end; ++iter) {
        AddState(*iter);
    }
}

bool mtsStateMachine::StateExists(const StateType state) const
{
    const StateMap::const_iterator found
        = mStates.find(state);
    return (found != mStates.end());
}

void mtsStateMachine::AddAllowedDesiredState(const StateType allowedState)
{
    if (StateExists(allowedState)) {
        mStates[allowedState] = true;
    } else {
        cmnThrow("mtsStateMachine::AddAllowedDesiredState: "
                 + mName + ", state " + allowedState + " needs to be added first");
    }
}

void mtsStateMachine::Run(void)
{
    // on first run, call enter callback for initial state
    if (mFirstRun) {
        // update callbacks
        UpdateCurrentCallbacks();

        // find new state enter callback
        CallbackMap::iterator callback;
        callback = mEnterCallbacks.find(mCurrentState);
        if (callback != mEnterCallbacks.end()) {
            callback->second->Execute();
        }

        // user callback if provided
        if (mStateChangeCallback) {
            mStateChangeCallback->Execute();
        }

        mFirstRun = false;
    }
    // check if a transition should happen
    if (mCurrentTransitionCallback) {
        mCurrentTransitionCallback->Execute();
    }
    // run current state method
    if (mRunCallback) {
        mRunCallback->Execute();
    }
    if (mCurrentRunCallback) {
        mCurrentRunCallback->Execute();
    }
}

void mtsStateMachine::SetDesiredState(const StateType & desiredState)
{
    const StateMap::const_iterator state
        = mStates.find(desiredState);
    if ((state != mStates.end()) // state exists
        && (state->second)) {  // can be set as desired
        mPreviousDesiredState = mDesiredState;
        mDesiredState = desiredState;
        mDesiredStateIsNotCurrent = (mDesiredState != mCurrentState);
        return;
    }
    cmnThrow("mtsStateMachine::SetDesiredState: "
             + desiredState + ", doesn't exists or is not allowed as a desired state");
}

void mtsStateMachine::SetCurrentState(const StateType & newState)
{
    // check if this state exists
    const StateMap::const_iterator state = mStates.find(newState);
    if (state == mStates.end()) {
        cmnThrow("mtsStateMachine::SetCurrentState: "
                 + newState + ", doesn't exists");
        return;
    }

    CallbackMap::iterator callback;

    // find current state leave callback
    callback = mLeaveCallbacks.find(mCurrentState);
    if (callback != mLeaveCallbacks.end()) {
        callback->second->Execute();
    }
    // set the new state and update current callbacks
    mPreviousState = mCurrentState;
    mCurrentState = newState;
    mDesiredStateIsNotCurrent = (mDesiredState != mCurrentState);

    // find new state enter callback
    callback = mEnterCallbacks.find(mCurrentState);
    if (callback != mEnterCallbacks.end()) {
        callback->second->Execute();
    }

    // user callback if provided
    if (mStateChangeCallback) {
        mStateChangeCallback->Execute();
    }

    // update current callbacks
    UpdateCurrentCallbacks();
}

void mtsStateMachine::UpdateCurrentCallbacks(void)
{
    CallbackMap::iterator callback;
    // find state run callback
    callback = mRunCallbacks.find(mCurrentState);
    if (callback != mRunCallbacks.end()) {
        mCurrentRunCallback = callback->second;
    } else {
        mCurrentRunCallback = 0;
    }
    // find transition callback
    callback = mTransitionCallbacks.find(mCurrentState);
    if (callback != mTransitionCallbacks.end()) {
        mCurrentTransitionCallback = callback->second;
    } else {
        mCurrentTransitionCallback = 0;
    }
}