This library is designed to create and run state graphs. It supports hierarchical states and parallel states execution.
StateMachineLib/source/Transitions.cpp@4:22b4462fcb26, 2021-02-02 (annotated)
- Committer:
- martin13
- Date:
- Tue Feb 02 20:47:45 2021 +0000
- Revision:
- 4:22b4462fcb26
- Parent:
- 0:f4fdca2c4c67
Added unittests
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
martin13 | 0:f4fdca2c4c67 | 1 | //////////////////////////////////////////////////////////////////////////////// |
martin13 | 0:f4fdca2c4c67 | 2 | // Copyright Rottor SAS 2017 |
martin13 | 0:f4fdca2c4c67 | 3 | // All rigths reserved. |
martin13 | 0:f4fdca2c4c67 | 4 | // |
martin13 | 0:f4fdca2c4c67 | 5 | // File Name : Transitions.cpp |
martin13 | 0:f4fdca2c4c67 | 6 | // Authors : Martin Matignon |
martin13 | 0:f4fdca2c4c67 | 7 | // |
martin13 | 0:f4fdca2c4c67 | 8 | // If you find any bug or if you have any question please contact |
martin13 | 0:f4fdca2c4c67 | 9 | // Martin Matignon <martin.matignon@rottor.fr> |
martin13 | 0:f4fdca2c4c67 | 10 | // Nicolas Forestier <nicolas.forestier@rottor.fr> |
martin13 | 0:f4fdca2c4c67 | 11 | // |
martin13 | 0:f4fdca2c4c67 | 12 | //////////////////////////////////////////////////////////////////////////////// |
martin13 | 0:f4fdca2c4c67 | 13 | |
martin13 | 0:f4fdca2c4c67 | 14 | #include "Transitions.h" |
martin13 | 0:f4fdca2c4c67 | 15 | #include "Logger.h" |
martin13 | 0:f4fdca2c4c67 | 16 | |
martin13 | 0:f4fdca2c4c67 | 17 | Transitions::Transition_t::Transition_t(const char* _outcome, State* _target, const char* _exit){ |
martin13 | 0:f4fdca2c4c67 | 18 | outcome = _outcome; |
martin13 | 0:f4fdca2c4c67 | 19 | _exit_ = _exit; |
martin13 | 0:f4fdca2c4c67 | 20 | target = _target; |
martin13 | 0:f4fdca2c4c67 | 21 | next = NULL; |
martin13 | 0:f4fdca2c4c67 | 22 | } |
martin13 | 0:f4fdca2c4c67 | 23 | |
martin13 | 0:f4fdca2c4c67 | 24 | Transitions::Transitions(): |
martin13 | 0:f4fdca2c4c67 | 25 | m_transitionsList(NULL) |
martin13 | 0:f4fdca2c4c67 | 26 | { |
martin13 | 0:f4fdca2c4c67 | 27 | /* Empty */ |
martin13 | 0:f4fdca2c4c67 | 28 | } |
martin13 | 0:f4fdca2c4c67 | 29 | |
martin13 | 0:f4fdca2c4c67 | 30 | Transitions* Transitions::addTransition(const char* outcome, State* target){ |
martin13 | 0:f4fdca2c4c67 | 31 | |
martin13 | 0:f4fdca2c4c67 | 32 | Transition_t *newTransition = new Transition_t(outcome, target); |
martin13 | 0:f4fdca2c4c67 | 33 | |
martin13 | 0:f4fdca2c4c67 | 34 | if(m_transitionsList == NULL){ |
martin13 | 0:f4fdca2c4c67 | 35 | |
martin13 | 0:f4fdca2c4c67 | 36 | m_transitionsList = newTransition; |
martin13 | 0:f4fdca2c4c67 | 37 | |
martin13 | 0:f4fdca2c4c67 | 38 | } else { |
martin13 | 0:f4fdca2c4c67 | 39 | |
martin13 | 0:f4fdca2c4c67 | 40 | Transition_t *temp = m_transitionsList; |
martin13 | 0:f4fdca2c4c67 | 41 | |
martin13 | 0:f4fdca2c4c67 | 42 | while(temp->next != NULL) |
martin13 | 0:f4fdca2c4c67 | 43 | { |
martin13 | 0:f4fdca2c4c67 | 44 | temp = temp->next; |
martin13 | 0:f4fdca2c4c67 | 45 | } |
martin13 | 0:f4fdca2c4c67 | 46 | temp->next = newTransition; |
martin13 | 0:f4fdca2c4c67 | 47 | } |
martin13 | 0:f4fdca2c4c67 | 48 | |
martin13 | 0:f4fdca2c4c67 | 49 | return this; |
martin13 | 0:f4fdca2c4c67 | 50 | } |
martin13 | 0:f4fdca2c4c67 | 51 | |
martin13 | 0:f4fdca2c4c67 | 52 | Transitions* Transitions::addTransition(Transition_t *transition){ |
martin13 | 0:f4fdca2c4c67 | 53 | |
martin13 | 0:f4fdca2c4c67 | 54 | if(m_transitionsList == NULL){ |
martin13 | 0:f4fdca2c4c67 | 55 | |
martin13 | 0:f4fdca2c4c67 | 56 | m_transitionsList = transition; |
martin13 | 0:f4fdca2c4c67 | 57 | |
martin13 | 0:f4fdca2c4c67 | 58 | } else { |
martin13 | 0:f4fdca2c4c67 | 59 | |
martin13 | 0:f4fdca2c4c67 | 60 | Transition_t *temp = m_transitionsList; |
martin13 | 0:f4fdca2c4c67 | 61 | |
martin13 | 0:f4fdca2c4c67 | 62 | while(temp->next != NULL) |
martin13 | 0:f4fdca2c4c67 | 63 | { |
martin13 | 0:f4fdca2c4c67 | 64 | temp = temp->next; |
martin13 | 0:f4fdca2c4c67 | 65 | } |
martin13 | 0:f4fdca2c4c67 | 66 | temp->next = transition; |
martin13 | 0:f4fdca2c4c67 | 67 | } |
martin13 | 0:f4fdca2c4c67 | 68 | |
martin13 | 0:f4fdca2c4c67 | 69 | return this; |
martin13 | 0:f4fdca2c4c67 | 70 | } |
martin13 | 0:f4fdca2c4c67 | 71 | |
martin13 | 0:f4fdca2c4c67 | 72 | Transitions::Transition_t* Transitions::getTransition(const char* outcome){ |
martin13 | 0:f4fdca2c4c67 | 73 | |
martin13 | 0:f4fdca2c4c67 | 74 | Transition_t *tmp = m_transitionsList; |
martin13 | 0:f4fdca2c4c67 | 75 | |
martin13 | 0:f4fdca2c4c67 | 76 | while(tmp != NULL) |
martin13 | 0:f4fdca2c4c67 | 77 | { |
martin13 | 0:f4fdca2c4c67 | 78 | if (strcmp(tmp->outcome, outcome) == 0){ |
martin13 | 0:f4fdca2c4c67 | 79 | return tmp; |
martin13 | 0:f4fdca2c4c67 | 80 | } |
martin13 | 0:f4fdca2c4c67 | 81 | tmp = tmp->next; |
martin13 | 0:f4fdca2c4c67 | 82 | } |
martin13 | 0:f4fdca2c4c67 | 83 | |
martin13 | 0:f4fdca2c4c67 | 84 | return NULL; |
martin13 | 0:f4fdca2c4c67 | 85 | } |
martin13 | 0:f4fdca2c4c67 | 86 | |
martin13 | 0:f4fdca2c4c67 | 87 | State* Transitions::getTargetState(const char* outcome){ |
martin13 | 0:f4fdca2c4c67 | 88 | |
martin13 | 0:f4fdca2c4c67 | 89 | Transition_t *tmp = m_transitionsList; |
martin13 | 0:f4fdca2c4c67 | 90 | |
martin13 | 0:f4fdca2c4c67 | 91 | while(tmp != NULL) |
martin13 | 0:f4fdca2c4c67 | 92 | { |
martin13 | 0:f4fdca2c4c67 | 93 | if (strcmp(tmp->outcome, outcome) == 0){ |
martin13 | 0:f4fdca2c4c67 | 94 | return tmp->target; |
martin13 | 0:f4fdca2c4c67 | 95 | } |
martin13 | 0:f4fdca2c4c67 | 96 | tmp = tmp->next; |
martin13 | 0:f4fdca2c4c67 | 97 | } |
martin13 | 0:f4fdca2c4c67 | 98 | |
martin13 | 0:f4fdca2c4c67 | 99 | return NULL; |
martin13 | 0:f4fdca2c4c67 | 100 | } |
martin13 | 0:f4fdca2c4c67 | 101 | |
martin13 | 0:f4fdca2c4c67 | 102 | void Transitions::printList(string level){ |
martin13 | 0:f4fdca2c4c67 | 103 | |
martin13 | 0:f4fdca2c4c67 | 104 | Transition_t *tmp = m_transitionsList; |
martin13 | 0:f4fdca2c4c67 | 105 | |
martin13 | 0:f4fdca2c4c67 | 106 | while(tmp != NULL) |
martin13 | 0:f4fdca2c4c67 | 107 | { |
martin13 | 0:f4fdca2c4c67 | 108 | |
martin13 | 0:f4fdca2c4c67 | 109 | if (tmp->target != NULL){ |
martin13 | 0:f4fdca2c4c67 | 110 | Logger::debug("%s - OUTCOME(%s) -> TRAGET(%s)", |
martin13 | 0:f4fdca2c4c67 | 111 | level.c_str(), |
martin13 | 0:f4fdca2c4c67 | 112 | tmp->outcome, |
martin13 | 0:f4fdca2c4c67 | 113 | tmp->target->getUUID()); |
martin13 | 0:f4fdca2c4c67 | 114 | } |
martin13 | 0:f4fdca2c4c67 | 115 | else{ |
martin13 | 0:f4fdca2c4c67 | 116 | Logger::debug("%s - OUTCOME(%s) -> EXIT(%s)", |
martin13 | 0:f4fdca2c4c67 | 117 | level.c_str(), |
martin13 | 0:f4fdca2c4c67 | 118 | tmp->outcome, |
martin13 | 0:f4fdca2c4c67 | 119 | tmp->_exit_); |
martin13 | 0:f4fdca2c4c67 | 120 | } |
martin13 | 0:f4fdca2c4c67 | 121 | tmp = tmp->next; |
martin13 | 0:f4fdca2c4c67 | 122 | } |
martin13 | 0:f4fdca2c4c67 | 123 | } |