CustusX  2023.01.05-dev+develop.0da12
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 }
void finished()
should be emitted when at the end of postProcessingSlot
void reportError(QString msg)
Definition: cxLogger.cpp:71
Base class for algorithms that wants to time their execution.
CompositeParallelTimedAlgorithm(QString name="parallel")
boost::shared_ptr< class TimedBaseAlgorithm > TimedAlgorithmPtr
CompositeSerialTimedAlgorithm(QString name="composite")
std::vector< TimedAlgorithmPtr > mChildren
void productChanged()
emitted whenever product string has changed
void aboutToStart()
emitted at start of execute. Use to perform preprocessing
virtual void append(TimedAlgorithmPtr child)
void started(int maxSteps)
emitted at start of run.
Namespace for all CustusX production code.