NorMIT-nav  22.09
An IGT application
cxCompositeTimedAlgorithm.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
13 
14 #include <QStringList>
15 #include "cxTypeConversions.h"
16 #include "cxLogger.h"
17 
18 namespace cx
19 {
20 
22  TimedBaseAlgorithm(name, 20)
23 {
24 }
25 
27 {
28  mChildren.push_back(child);
29 }
30 
31 //---------------------------------------------------------
32 //---------------------------------------------------------
33 //---------------------------------------------------------
34 
35 
38  mCurrent(-1)
39 {
40 }
41 
43 {
44  if (mCurrent >= 0 && mCurrent < mChildren.size())
45  {
46  return mChildren[mCurrent]->getProduct();
47  }
48  return "composite";
49 }
50 
52 {
53  // if already started, ignore
54  if (mCurrent>=0)
55  {
56  reportError("Attempt to restart CompositeSerialTimedAlgorithm while running failed.");
57  return;
58  }
59 
60  mChildren.clear();
61  mCurrent = -1;
62 }
63 
65 {
66  // if already started, ignore
67  if (mCurrent>=0)
68  return;
69  this->startTiming();
70  mCurrent = -1;
71  emit started(0);
72  this->jumpToNextChild();
73 }
74 
75 void CompositeSerialTimedAlgorithm::jumpToNextChild()
76 {
77  // teardown old child
78  if (mCurrent >= 0 && mCurrent < mChildren.size())
79  {
80  disconnect(mChildren[mCurrent].get(), SIGNAL(finished()), this, SLOT(jumpToNextChild()));
81  }
82  ++mCurrent;
83  // setup and run next child
84  if (mCurrent >= 0 && mCurrent < mChildren.size())
85  {
86  connect(mChildren[mCurrent].get(), SIGNAL(finished()), this, SLOT(jumpToNextChild()));
87  emit productChanged();
88  mChildren[mCurrent]->execute();
89  }
90 
91  // check for finished
92  if (mCurrent>=mChildren.size())
93  {
94  mCurrent = -1;
95  this->stopTiming();
96  emit finished();
97  }
98 }
99 
101 {
102  return mCurrent == -1;
103 }
104 
106 {
107  return mCurrent >= 0;
108 }
109 
110 //---------------------------------------------------------
111 //---------------------------------------------------------
112 //---------------------------------------------------------
113 
116 {
117 }
118 
120 {
121  QStringList products;
122  for (unsigned i=0; i<mChildren.size(); ++i)
123  {
124  products << mChildren[i]->getProduct();
125  }
126 
127  if (products.isEmpty())
128  return "composite parallel";
129 
130  return products.join(", ");
131 }
132 
134 {
135 
136  // if already started, ignore
137  if (!this->isFinished())
138  {
139  reportError("Attempt to restart CompositeSerialTimedAlgorithm while running failed.");
140  return;
141  }
142 
143  mChildren.clear();
144 }
145 
147 {
148  emit aboutToStart();
149  emit started(0);
150  for (unsigned i=0; i<mChildren.size(); ++i)
151  {
152  connect(mChildren[i].get(), SIGNAL(finished()), this, SLOT(oneFinished()));
153  }
154  for (unsigned i=0; i<mChildren.size(); ++i)
155  {
156  mChildren[i]->execute();
157  }
158 }
159 
160 void CompositeParallelTimedAlgorithm::oneFinished()
161 {
162  emit productChanged();
163 
164  if (this->isFinished())
165  emit finished();
166 }
167 
169 {
170  int count = 0;
171  for (unsigned i=0; i<mChildren.size(); ++i)
172  {
173  if (mChildren[i]->isFinished())
174  ++count;
175  }
176  return (count==mChildren.size());
177 }
178 
180 {
181  return !this->isFinished();
182 }
183 
184 
185 
186 }
cxLogger.h
cx
Namespace for all CustusX production code.
Definition: cx_dev_group_definitions.h:13
cx::CompositeSerialTimedAlgorithm::clear
virtual void clear()
Definition: cxCompositeTimedAlgorithm.cpp:51
cx::CompositeSerialTimedAlgorithm::isFinished
virtual bool isFinished() const
Definition: cxCompositeTimedAlgorithm.cpp:100
cx::CompositeTimedAlgorithm::append
virtual void append(TimedAlgorithmPtr child)
Definition: cxCompositeTimedAlgorithm.cpp:26
cx::TimedAlgorithmPtr
boost::shared_ptr< class TimedBaseAlgorithm > TimedAlgorithmPtr
Definition: cxReconstructionExecuter.h:27
cx::CompositeParallelTimedAlgorithm::execute
virtual void execute()
Definition: cxCompositeTimedAlgorithm.cpp:146
cx::TimedBaseAlgorithm::stopTiming
void stopTiming()
Definition: cxTimedAlgorithm.cpp:46
cxCompositeTimedAlgorithm.h
cxTypeConversions.h
cx::CompositeSerialTimedAlgorithm::execute
virtual void execute()
Definition: cxCompositeTimedAlgorithm.cpp:64
cx::CompositeSerialTimedAlgorithm::isRunning
virtual bool isRunning() const
Definition: cxCompositeTimedAlgorithm.cpp:105
cx::TimedBaseAlgorithm::startTiming
void startTiming()
Definition: cxTimedAlgorithm.cpp:38
cx::CompositeParallelTimedAlgorithm::isFinished
virtual bool isFinished() const
Definition: cxCompositeTimedAlgorithm.cpp:168
cx::CompositeSerialTimedAlgorithm::CompositeSerialTimedAlgorithm
CompositeSerialTimedAlgorithm(QString name="composite")
Definition: cxCompositeTimedAlgorithm.cpp:36
cx::CompositeParallelTimedAlgorithm::clear
virtual void clear()
Definition: cxCompositeTimedAlgorithm.cpp:133
cx::CompositeTimedAlgorithm
Definition: cxCompositeTimedAlgorithm.h:27
cx::TimedBaseAlgorithm::aboutToStart
void aboutToStart()
emitted at start of execute. Use to perform preprocessing
cx::CompositeTimedAlgorithm::CompositeTimedAlgorithm
CompositeTimedAlgorithm(QString name)
Definition: cxCompositeTimedAlgorithm.cpp:21
cx::CompositeTimedAlgorithm::mChildren
std::vector< TimedAlgorithmPtr > mChildren
Definition: cxCompositeTimedAlgorithm.h:35
cx::CompositeParallelTimedAlgorithm::CompositeParallelTimedAlgorithm
CompositeParallelTimedAlgorithm(QString name="parallel")
Definition: cxCompositeTimedAlgorithm.cpp:114
cx::TimedBaseAlgorithm::started
void started(int maxSteps)
emitted at start of run.
cx::TimedBaseAlgorithm
Base class for algorithms that wants to time their execution.
Definition: cxTimedAlgorithm.h:40
cx::TimedBaseAlgorithm::finished
void finished()
should be emitted when at the end of postProcessingSlot
cx::CompositeParallelTimedAlgorithm::getProduct
virtual QString getProduct() const
Definition: cxCompositeTimedAlgorithm.cpp:119
cx::CompositeParallelTimedAlgorithm::isRunning
virtual bool isRunning() const
Definition: cxCompositeTimedAlgorithm.cpp:179
cx::TimedBaseAlgorithm::productChanged
void productChanged()
emitted whenever product string has changed
cx::reportError
void reportError(QString msg)
Definition: cxLogger.cpp:71
cx::CompositeSerialTimedAlgorithm::getProduct
virtual QString getProduct() const
Definition: cxCompositeTimedAlgorithm.cpp:42