arcsstatemachine.cpp
1 /*
2  name: lib/arcsstatemachine.cpp
3 
4  This file is part of ARCS - Augmented Reality Component System
5  (version 2-current), written by Jean-Yves Didier
6  for IBISC Laboratory (http://www.ibisc.univ-evry.fr)
7 
8  Copyright (C) 2013 Université d'Evry-Val d'Essonne
9 
10  This program is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 2 of the License, or
13  (at your option) any later version.
14 
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program. If not, see <http://www.gnu.org/licenses/>.
22 
23 
24  Please send bugreports with examples or suggestions to
25  jean-yves.didier__at__ibisc.univ-evry.fr
26 */
27 
28 
29 #include <arcs/arcsstatemachine.h>
30 #include <iostream>
31 #include <QMutableHashIterator>
32 
33 ARCSStateMachine::ARCSStateMachine()
34 {
35  error = false;
36 
37 
38 }
39 
40 void ARCSStateMachine::renameSheet(QString oldName, QString newName)
41 {
42  if (firstSheetName == oldName)
43  firstSheetName = newName;
44  if (lastSheetName == oldName)
45  lastSheetName = newName;
46  if (currentSheetName == oldName)
47  currentSheetName = newName;
48 
49  if (transitions.contains(oldName))
50  {
51  QHash<QString,QString> value = transitions.take(oldName);
52  transitions.insert(newName,value);
53  }
54 
55 
56  QMutableHashIterator<QString, QHash<QString,QString> > it(transitions);
57 
58  while(it.hasNext())
59  {
60  it.next();
61  QMutableHashIterator<QString,QString> ite(it.value());
62 
63  while(ite.hasNext())
64  {
65  ite.next();
66  if (ite.value() == oldName)
67  ite.setValue(newName);
68  }
69  }
70 }
71 
72 
73 void ARCSStateMachine::addTransition(QString bState, QString token, QString eState)
74 {
75  if (transitions.empty())
76  firstSheetName = bState;
77 
78  //lastSheetName = eState;
79 
80  if ( !transitions.contains(bState) )
81  transitions.insert(bState, QHash<QString, QString>());
82 
83  transitions[bState].insert(token, eState);
84 }
85 
86 
87 void ARCSStateMachine::clear()
88 {
89  transitions.clear();
90  firstSheetName = QString::null;
91  lastSheetName = QString::null;
92  error = false;
93 }
94 
95 void ARCSStateMachine::start()
96 {
97  error = false;
98  currentSheetName = firstSheetName;
99  emit sendSheet(currentSheetName);
100 
101  if (currentSheetName == lastSheetName)
102  emit finished();
103 
104 }
105 
106 
108 {
109  if (transitions.contains(currentSheetName))
110  {
111  if (transitions[currentSheetName].contains(s))
112  {
113  currentSheetName = transitions[currentSheetName][s];
114  emit sendSheet(currentSheetName);
115  if ( currentSheetName == lastSheetName)
116  emit finished();
117  }
118  }
119 }
120 
121 
122 void ARCSStateMachine::getTransitions(QStringList & bState, QStringList & token, QStringList & eState)
123 {
124  bState.clear();
125  token.clear();
126  eState.clear();
127 
128 
129  QHashIterator<QString, QHash<QString,QString> > it(transitions);
130 
131  while(it.hasNext())
132  {
133  it.next();
134  QHashIterator<QString,QString> ite(it.value());
135 
136  while(ite.hasNext())
137  {
138  ite.next();
139  bState << (it.key());
140  token << (ite.key());
141  eState << (ite.value());
142  }
143  }
144 }
void finished()
signal emitted when a state labelled as last state of the statemachine is reached.
void setToken(QString s)
void renameSheet(QString oldName, QString newName)
Renames a sheet in the set of transitions.
void sendSheet(QString s)
signal emitted when a transition has been triggered.