1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
// ************************************************************************************************
//
// BornAgain: simulate and fit reflection and scattering
//
//! @file GUI/Model/Job/JobsSet.cpp
//! @brief Implements class JobsSet.
//!
//! @homepage http://www.bornagainproject.org
//! @license GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
// ************************************************************************************************
#include "GUI/Model/Job/JobsSet.h"
#include "GUI/Model/File/DatafileItem.h"
#include "GUI/Model/Job/BatchInfo.h"
#include "GUI/Model/Job/JobStatus.h"
#include "GUI/Model/Par/ParameterTreeItems.h"
#include "GUI/Model/Util/UtilXML.h"
namespace {
namespace Tag {
const QString Job("Job");
const QString CurrentIndex("CurrentIndex");
} // namespace Tag
} // namespace
JobsSet::JobsSet(QObject* parent)
: QObject(parent)
{
setObjectName("JobsSet");
}
JobsSet::~JobsSet() = default;
void JobsSet::writeTo(QXmlStreamWriter* w) const
{
for (const auto* job : *this) {
w->writeStartElement(Tag::Job);
XML::writeAttribute(w, XML::Attrib::name, job->batchInfo()->jobName());
job->writeTo(w);
w->writeEndElement();
}
XML::writeTaggedValue(w, Tag::CurrentIndex, (int)currentIndex());
}
void JobsSet::readFrom(QXmlStreamReader* r)
{
ASSERT(empty());
while (r->readNextStartElement()) {
QString tag = r->name().toString();
if (tag == Tag::Job) {
auto* job_item = createJobItem();
XML::readTaggedElement(r, tag, *job_item);
emit jobAdded(job_item);
} else if (tag == Tag::CurrentIndex) {
size_t i = XML::readTaggedInt(r, tag);
setCurrentIndex(i);
} else
r->skipCurrentElement();
}
if (r->hasError())
throw std::runtime_error(r->errorString().toStdString());
}
void JobsSet::saveAllDatafields(const QString& projectDir) const
{
for (const JobItem* job : *this)
job->saveDatafields(projectDir);
dataFilesCleaner.cleanOldFiles(projectDir, dataItems());
}
void JobsSet::loadAllDatafields(const QString& projectDir)
{
for (JobItem* job : *this)
job->loadDatafields(projectDir);
dataFilesCleaner.recollectDataNames(dataItems());
}
JobItem* JobsSet::createJobItem()
{
auto* job_item = new JobItem;
push_back(job_item);
return job_item;
}
//! Main method to add a job
void JobsSet::addJobItem(JobItem* job_item)
{
job_item->batchInfo()->setJobName(generateJobName());
push_back(job_item);
emit jobAdded(job_item);
}
//! restore instrument and sample model from backup for given JobItem
void JobsSet::restoreBackupPars(JobItem* job_item, int index)
{
job_item->parameterContainerItem()->restoreBackupValues(index);
}
QVector<DataItem*> JobsSet::dataItems() const
{
QVector<DataItem*> result;
for (auto* job_item : *this) {
if (auto* dataItem = job_item->simulatedDataItem())
result.push_back(dataItem);
if (const auto* real_data = dynamic_cast<const DatafileItem*>(job_item->dfileItem()))
if (auto* data_item = real_data->dataItem())
result.push_back(data_item);
}
return result;
}
void JobsSet::cancelJob(JobItem* job_item)
{
job_item->haltWorker();
}
void JobsSet::removeJob(JobItem* job_item)
{
ASSERT(job_item);
job_item->haltWorker();
delete_element(job_item);
emit jobPlotContextChanged();
}
bool JobsSet::hasUnfinishedJobs() const
{
for (const JobItem* job_item : *this)
if (isFitting(job_item->batchInfo()->status()))
return true;
return false;
}
//! Submits job and run it in a thread.
void JobsSet::runJob(JobItem* job_item)
{
if (job_item->thread())
return;
connect(job_item, &JobItem::progressIncremented, this, &JobsSet::onProgressUpdate,
Qt::UniqueConnection);
connect(job_item, &JobItem::jobFinished, this, &JobsSet::onFinishedJob, Qt::UniqueConnection);
job_item->initWorker();
job_item->thread()->start();
}
// ------------------------------------------------------------------------------------------------
// private slots
// ------------------------------------------------------------------------------------------------
//! Performs necessary actions when job is finished.
void JobsSet::onFinishedJob(JobItem* job_item)
{
onProgressUpdate();
emit newJobFinished(job_item);
emit jobPlotContextChanged();
}
//! Estimates global progress from the progress of multiple running jobs and emits signal.
void JobsSet::onProgressUpdate()
{
int global_progress = 0;
int nRunningJobs = 0;
for (const JobItem* job_item : *this)
if (isRunning(job_item->batchInfo()->status())) {
global_progress += job_item->batchInfo()->progress();
nRunningJobs++;
}
emit globalProgress(nRunningJobs ? global_progress / nRunningJobs : -1);
}
//! Cancels all running jobs.
void JobsSet::onCancelAllJobs()
{
for (auto* job_item : *this)
job_item->haltWorker();
}
// ------------------------------------------------------------------------------------------------
// private fcts
// ------------------------------------------------------------------------------------------------
//! generates numbered job name with new/unused number
QString JobsSet::generateJobName() const
{
int maxJobIndex = 0;
for (const JobItem* job_item : *this)
if (job_item->batchInfo()->jobName().startsWith("job"))
maxJobIndex = std::max(maxJobIndex, job_item->batchInfo()->jobName().mid(3).toInt());
return QString("job%1").arg(maxJobIndex + 1);
}
|