CustusX  16.5
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
34 
35 #include <QStringList>
36 #include "cxTypeConversions.h"
37 #include "cxLogger.h"
38 
39 namespace cx
40 {
41 
43  TimedBaseAlgorithm(name, 20)
44 {
45 }
46 
48 {
49  mChildren.push_back(child);
50 }
51 
52 //---------------------------------------------------------
53 //---------------------------------------------------------
54 //---------------------------------------------------------
55 
56 
59  mCurrent(-1)
60 {
61 }
62 
64 {
65  if (mCurrent >= 0 && mCurrent < mChildren.size())
66  {
67  return mChildren[mCurrent]->getProduct();
68  }
69  return "composite";
70 }
71 
73 {
74  // if already started, ignore
75  if (mCurrent>=0)
76  {
77  reportError("Attempt to restart CompositeSerialTimedAlgorithm while running failed.");
78  return;
79  }
80 
81  mChildren.clear();
82  mCurrent = -1;
83 }
84 
86 {
87  // if already started, ignore
88  if (mCurrent>=0)
89  return;
90  this->startTiming();
91  mCurrent = -1;
92  emit started(0);
93  this->jumpToNextChild();
94 }
95 
96 void CompositeSerialTimedAlgorithm::jumpToNextChild()
97 {
98  // teardown old child
99  if (mCurrent >= 0 && mCurrent < mChildren.size())
100  {
101  disconnect(mChildren[mCurrent].get(), SIGNAL(finished()), this, SLOT(jumpToNextChild()));
102  }
103  ++mCurrent;
104  // setup and run next child
105  if (mCurrent >= 0 && mCurrent < mChildren.size())
106  {
107  connect(mChildren[mCurrent].get(), SIGNAL(finished()), this, SLOT(jumpToNextChild()));
108  emit productChanged();
109  mChildren[mCurrent]->execute();
110  }
111 
112  // check for finished
113  if (mCurrent>=mChildren.size())
114  {
115  mCurrent = -1;
116  this->stopTiming();
117  emit finished();
118  }
119 }
120 
122 {
123  return mCurrent == -1;
124 }
125 
127 {
128  return mCurrent >= 0;
129 }
130 
131 //---------------------------------------------------------
132 //---------------------------------------------------------
133 //---------------------------------------------------------
134 
137 {
138 }
139 
141 {
142  QStringList products;
143  for (unsigned i=0; i<mChildren.size(); ++i)
144  {
145  products << mChildren[i]->getProduct();
146  }
147 
148  if (products.isEmpty())
149  return "composite parallel";
150 
151  return products.join(", ");
152 }
153 
155 {
156 
157  // if already started, ignore
158  if (!this->isFinished())
159  {
160  reportError("Attempt to restart CompositeSerialTimedAlgorithm while running failed.");
161  return;
162  }
163 
164  mChildren.clear();
165 }
166 
168 {
169  emit aboutToStart();
170  emit started(0);
171  for (unsigned i=0; i<mChildren.size(); ++i)
172  {
173  connect(mChildren[i].get(), SIGNAL(finished()), this, SLOT(oneFinished()));
174  }
175  for (unsigned i=0; i<mChildren.size(); ++i)
176  {
177  mChildren[i]->execute();
178  }
179 }
180 
181 void CompositeParallelTimedAlgorithm::oneFinished()
182 {
183  emit productChanged();
184 
185  if (this->isFinished())
186  emit finished();
187 }
188 
190 {
191  int count = 0;
192  for (unsigned i=0; i<mChildren.size(); ++i)
193  {
194  if (mChildren[i]->isFinished())
195  ++count;
196  }
197  return (count==mChildren.size());
198 }
199 
201 {
202  return !this->isFinished();
203 }
204 
205 
206 
207 }
void finished()
should be emitted when at the end of postProcessingSlot
void reportError(QString msg)
Definition: cxLogger.cpp:92
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.