LayoutController.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package
19 * @author
20 * @date
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24#include "LayoutController.h"
25
26#include <cassert>
27#include <memory>
28#include <utility>
29
31
32#include "../model/State.h"
34
36 bool startEnabled) :
37 QObject(), idCounter(0), layoutingDisabled(!startEnabled)
38{
39 qRegisterMetaType<size_t>("size_t");
40 timer.setInterval(100);
41 connect(&timer, SIGNAL(timeout()), this, SLOT(startNextLayouting()), Qt::QueuedConnection);
42
43 if (topState)
44 {
45 createMediator(topState);
46 }
47 else
48 {
49 // ARMARX_ERROR_S << "No state provided for constructor of LayoutController";
50 }
51}
52
53void
55{
56 //here (and not at the end of the function) because signals for the layout thread are queued.
57 //This garanties that the WorkerCreator first deletes all current states and then adds the
58 //worker for the new topState.
59 emit reset();
60
61 mediators.clear();
62 states.clear();
63 idCounter = 0;
64
65 createMediator(topState);
66 timer.start();
67}
68
69void
71{
72 for (auto med : mediators)
73 {
74 emit createWorker(med.second, med.first, med.second->getState()->getStateName());
75 }
76}
77
78size_t
80{
81 for (const auto& elem : mediators)
82 {
83 if (elem.second->getState().get() == state.get())
84 {
85 return elem.first;
86 }
87 }
88
89 return -1;
90}
91
92void
94{
95 layoutingDisabled = !enable;
96}
97
98void
99armarx::LayoutController::scheduleMediator(size_t mediatorId, bool layoutAll)
100{
101 MediatorLayoutOptionPair correctPair(mediatorId, layoutAll);
102 MediatorLayoutOptionPair inversePair(mediatorId, !layoutAll);
103
104 size_t count = layoutQueue.count(correctPair) + layoutQueue.count(inversePair);
105 assert(count <= 2);
106
107 if (count == 0) //worker not yet scheduled
108 {
109 layoutQueue.append(correctPair);
110 }
111 else if (layoutAll && layoutQueue.contains(inversePair))
112 //worker is scheduled, but only supposed to layout edges
113 //but now it should also layout the nodes
114 {
115 layoutQueue.removeAll(inversePair);
116 layoutQueue.removeAll(correctPair);
117 layoutQueue.append(correctPair);
118 }
119}
120
121bool
122armarx::LayoutController::layoutNow(size_t mediatorId, bool layoutAll)
123{
124 auto iter = mediators.find(mediatorId);
125
126 if (iter != mediators.end())
127 {
128 if (iter->second->getState()->getSubstates().size() > 0)
129 {
130 iter->second->startLayouting(layoutAll, 1);
131 return true;
132 }
133 return false;
134 }
135 else
136 {
137 ARMARX_ERROR_S << "Mediator with id " << mediatorId << " is scheduled but no mediator"
138 << " with this id could be found.";
139 return false;
140 }
141}
142
143void
145{
146 if (layoutingDisabled)
147 {
148 // ARMARX_INFO_S << "Layouting disabled!";
149 return;
150 }
151
152 // ARMARX_WARNING_S << "Layouting activated";
153 timer.stop();
154 if (!(layoutQueue.empty()))
155 {
156 auto nextMediator = layoutQueue.takeFirst();
157 size_t mediatorId = nextMediator.first;
158 bool layoutAll = nextMediator.second;
159 if (!layoutNow(mediatorId, layoutAll))
160 {
161 QMetaObject::invokeMethod(this, "startNextLayouting");
162 }
163 }
164 else
165 {
166 timer.start();
167 }
168}
169
170void
172{
173 //remove from layoutQueue
174 MediatorLayoutOptionPair truePair{mediatorId, true};
175
176 while (layoutQueue.contains(truePair))
177 {
178 layoutQueue.removeAt(layoutQueue.indexOf(truePair));
179 }
180
181 MediatorLayoutOptionPair falsePair{mediatorId, false};
182
183 while (layoutQueue.contains(falsePair))
184 {
185 layoutQueue.removeAt(layoutQueue.indexOf(falsePair));
186 }
187
188 //remove from states
189 auto iter = mediators.find(mediatorId);
190
191 if (iter != mediators.end())
192 {
193 int statePos = states.indexOf(iter->second->getState());
194
195 if (statePos != -1)
196 {
197 states.removeAt(statePos);
198 }
199
200 //remove from mediators, destructor called by shared_ptr if necessary
201 mediators.erase(mediatorId);
202 }
203 else
204 {
205 ARMARX_INFO_S << "Mediator with id " << mediatorId << " was already deleted";
206 }
207}
208
209void
212{
213 if (signalType == statechartmodel::eAdded)
214 {
215 statechartmodel::StatePtr addedState = substate->getStateClass();
216
217 if (addedState)
218 {
219 if (states.contains(addedState))
220 {
221 return;
222 }
223
224 createMediator(addedState);
225 }
226 }
227}
228
229void
230armarx::LayoutController::createMediator(armarx::statechartmodel::StatePtr state)
231{
232 // ARMARX_INFO_S << "Creating mediator for state " << state->getStateName();
233 MediatorPtr mediator{new StateModelLayoutMediator(state, idCounter)};
234 idCounter++;
235
236 qRegisterMetaType<statechartmodel::TransitionCPtr>("statechartmodel::TransitionCPtr");
237 qRegisterMetaType<statechartmodel::StateInstancePtr>("statechartmodel::StateInstancePtr");
238 qRegisterMetaType<statechartmodel::SignalType>("statechartmodel::SignalType");
239 qRegisterMetaType<SupportPoints>("SupportPoints");
240 qRegisterMetaType<QPointPtr>("QPointPtr");
241 qRegisterMetaType<FloatPtr>("FloatPtr");
242
243 //state und mediator verbinden
244 bool correctConnected = connect(state.get(),
245 SIGNAL(stateDeleted()),
246 mediator.get(),
247 SLOT(stateDeleted()),
248 Qt::QueuedConnection);
249
250 if (!correctConnected)
251 {
252 ARMARX_ERROR_S << "Signal stateDeleted was not connected to slot stateDeleted of mediator";
253 }
254
255 correctConnected = connect(
256 state.get(),
258 mediator.get(),
260 Qt::QueuedConnection);
261
262 if (!correctConnected)
263 {
265 << "Signal substateChanged was not connected to slot substateChanged of mediator";
266 }
267
268 correctConnected = connect(state.get(),
269 SIGNAL(stateChanged(statechartmodel::SignalType)),
270 mediator.get(),
271 SLOT(stateChanged(statechartmodel::SignalType)),
272 Qt::QueuedConnection);
273
274 if (!correctConnected)
275 {
276 ARMARX_ERROR_S << "Signal stateChanged was not connected to slot stateChanged of mediator";
277 }
278
279 correctConnected = connect(
280 state.get(),
282 mediator.get(),
284 Qt::QueuedConnection);
285
286 if (!correctConnected)
287 {
289 << "Signal transitionChanged was not connected to slot transitionChanged of mediator";
290 }
291
292 correctConnected =
293 connect(mediator.get(),
294 SIGNAL(supportPointsChanged(
296 state.get(),
297 SLOT(setTransitionSupportPoints(
299 Qt::QueuedConnection);
300
301 if (!correctConnected)
302 {
303 ARMARX_ERROR_S << "Signal supportPointsChanged of mediator"
304 << " was not connected to slot setTransitionSupportPoints";
305 }
306
307 //Controller und mediator verbinden
308 correctConnected = connect(mediator.get(),
309 SIGNAL(layoutingFinished()),
310 this,
311 SLOT(startNextLayouting()),
312 Qt::QueuedConnection);
313
314 if (!correctConnected)
315 {
316 ARMARX_ERROR_S << "Signal layoutingFinished of mediator"
317 << " was not connected to slot startNextLayouting of controller";
318 }
319
320 correctConnected = connect(mediator.get(),
321 SIGNAL(deleteMe(size_t)),
322 this,
323 SLOT(deleteMediator(size_t)),
324 Qt::QueuedConnection);
325
326 if (!correctConnected)
327 {
328 ARMARX_ERROR_S << "Signal deleteMe of mediator"
329 << " was not connected to slot deleteMediator of controller";
330 }
331
332 correctConnected = connect(mediator.get(),
333 SIGNAL(scheduleMe(size_t, bool)),
334 this,
335 SLOT(scheduleMediator(size_t, bool)));
336
337 if (!correctConnected)
338 {
339 ARMARX_ERROR_S << "Signal scheduleMe of mediator"
340 << " was not connected to slot scheduleMediator of controller";
341 }
342
343 correctConnected = connect(
344 mediator.get(),
346 this,
348 Qt::QueuedConnection);
349
350 if (!correctConnected)
351 {
352 ARMARX_ERROR_S << "Signal substateFound of mediator"
353 << " was not connected to slot potentialStateAdded of controller";
354 }
355
356 //controller und state verbinden
357 correctConnected = connect(
358 state.get(),
360 this,
362 Qt::QueuedConnection);
363
364 if (!correctConnected)
365 {
366 ARMARX_ERROR_S << "Signal substateChanged of state was not connected to slot"
367 << " potentialStateAdded of controller";
368 }
369
370 mediators.insert(std::pair<size_t, MediatorPtr>(mediator->getID(), mediator));
371 states.push_back(state);
372
373 emit createWorker(mediator, mediator->getID(), state->getStateName());
374 QMetaObject::invokeMethod(this,
375 "scheduleMediator",
376 Qt::QueuedConnection,
377 Q_ARG(size_t, mediator->getID()),
378 Q_ARG(bool, true));
379}
void enableLayouting(bool enable=true)
bool layoutNow(size_t mediatorId, bool layoutAll)
void startNextLayouting()
startNextLayouting The last worker that layouted is finished and the next should be started.
LayoutController(armarx::statechartmodel::StatePtr topState, bool startEnabled=true)
void setTopState(statechartmodel::StatePtr topState)
setTopState Resets all workers i.e.
void createAllWorkers()
createAllWorkers Creates workers for all mediators
void potentialStateAdded(statechartmodel::StateInstancePtr substate, statechartmodel::SignalType signalType)
potentialStateAdded If a substate is added it might mean that there is a new state needing a LayoutWo...
void scheduleMediator(size_t mediatorId, bool layoutAll)
scheduleMediator Put the mediator in the layoutQueue.
void deleteMediator(size_t mediatorId)
deleteMediator Call the destructor of mediator and tell LayoutWorkerCreator to delete the correspondi...
void reset()
reset Notifies the LayoutWorkerCreator that the topState has been reset.
size_t getStateId(armarx::statechartmodel::StatePtr state) const
void createWorker(MediatorPtr mediator, size_t id, QString name)
createWorker Tells the LayoutWorkerCreator to create a new worker for this mediator.
#define ARMARX_ERROR_S
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:216
#define ARMARX_INFO_S
Definition Logging.h:202
std::shared_ptr< State > StatePtr
Definition State.h:48
std::shared_ptr< StateInstance > StateInstancePtr
std::shared_ptr< const Transition > TransitionCPtr
Definition Transition.h:91
SignalType
The SignalType enum.
Definition SignalType.h:34
std::shared_ptr< QPointF > QPointPtr
Definition Transition.h:40
std::shared_ptr< StateModelLayoutMediator > MediatorPtr
std::pair< size_t, bool > MediatorLayoutOptionPair
std::shared_ptr< float > FloatPtr
Definition Transition.h:39