Skip to content

Commit

Permalink
Python support: merge duplicated code (opencor#2225).
Browse files Browse the repository at this point in the history
  • Loading branch information
agarny committed Nov 27, 2019
1 parent ccc0719 commit 1d7f062
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 129 deletions.
123 changes: 123 additions & 0 deletions src/misc/initializesolver.cpp.inl
@@ -0,0 +1,123 @@
#ifdef GUI_SUPPORT
bool SimulationExperimentViewSimulationWidget::initializeSolver(const libsedml::SedListOfAlgorithmParameters *pSedmlAlgorithmParameters,
const QString &pKisaoId,
SimulationExperimentViewInformationSolversWidgetData *pSolverData)
#else
QString Simulation::initializeSolver(const libsedml::SedListOfAlgorithmParameters *pSedmlAlgorithmParameters,
const QString &pKisaoId) const
#endif
{
// Initialise our solver using the given SED-ML algorithm parameters and
// KiSAO id

SolverInterface *solverInterface = nullptr;
#ifdef GUI_SUPPORT
Core::Properties solverProperties = Core::Properties();
#endif

for (auto coreSolverInterface : Core::solverInterfaces()) {
if (coreSolverInterface->id(pKisaoId) == coreSolverInterface->solverName()) {
solverInterface = coreSolverInterface;
#ifdef GUI_SUPPORT
solverProperties = pSolverData->solversProperties().value(coreSolverInterface->solverName());

pSolverData->solversListProperty()->setValue(coreSolverInterface->solverName());
#else

mData->setOdeSolverName(coreSolverInterface->solverName());
#endif

break;
}
}

if (solverInterface == nullptr) {
#ifdef GUI_SUPPORT
simulationError(QObject::tr("the requested solver (%1) could not be found").arg(pKisaoId),
Error::InvalidSimulationEnvironment);

return false;
#else
return QObject::tr("the requested solver (%1) could not be found").arg(pKisaoId);
#endif
}

for (uint i = 0, iMax = pSedmlAlgorithmParameters->getNumAlgorithmParameters(); i < iMax; ++i) {
const libsedml::SedAlgorithmParameter *sedmlAlgorithmParameter = pSedmlAlgorithmParameters->get(i);
QString parameterKisaoId = QString::fromStdString(sedmlAlgorithmParameter->getKisaoID());
#ifdef GUI_SUPPORT
QString id = solverInterface->id(parameterKisaoId);
bool propertySet = false;

for (auto solverProperty : solverProperties) {
if (solverProperty->id() == id) {
QVariant solverPropertyValue = QString::fromStdString(sedmlAlgorithmParameter->getValue());

switch (solverProperty->type()) {
case Core::Property::Type::Section:
// We should never come here...

#ifdef QT_DEBUG
qFatal("FATAL ERROR | %s:%d: a solver property cannot be of section type.", __FILE__, __LINE__);
#else
break;
#endif
case Core::Property::Type::String:
solverProperty->setValue(solverPropertyValue.toString());

break;
case Core::Property::Type::Integer:
case Core::Property::Type::IntegerGe0:
case Core::Property::Type::IntegerGt0:
solverProperty->setIntegerValue(solverPropertyValue.toInt());

break;
case Core::Property::Type::Double:
case Core::Property::Type::DoubleGe0:
case Core::Property::Type::DoubleGt0:
solverProperty->setDoubleValue(solverPropertyValue.toDouble());

break;
case Core::Property::Type::List:
solverProperty->setListValue(solverPropertyValue.toString());

break;
case Core::Property::Type::Boolean:
solverProperty->setBooleanValue(solverPropertyValue.toBool());

break;
case Core::Property::Type::Color:
// We should never come here...

#ifdef QT_DEBUG
qFatal("FATAL ERROR | %s:%d: a solver property cannot be of colour type.", __FILE__, __LINE__);
#else
break;
#endif
}

propertySet = solverProperty->type() != Core::Property::Type::Section;

break;
}
}

if (!propertySet) {
simulationError(QObject::tr("the requested solver property (%1) could not be set").arg(parameterKisaoId),
Error::InvalidSimulationEnvironment);

return false;
}
#else

mData->addOdeSolverProperty(solverInterface->id(parameterKisaoId),
QString::fromStdString(sedmlAlgorithmParameter->getValue()));
#endif
}

#ifdef GUI_SUPPORT
return true;
#else
return {};
#endif
}
Expand Up @@ -667,5 +667,13 @@
<source>unable to close the simulation</source>
<translation>incapable de fermer la simulation</translation>
</message>
<message>
<source>the requested solver (%1) could not be found</source>
<translation>le solveur demandé (%1) n&apos;a pas pu être trouvé</translation>
</message>
<message>
<source>the requested solver property (%1) could not be set</source>
<translation>la propriété demandée (%1) du solveur n&apos;a pas pu être spécifiée</translation>
</message>
</context>
</TS>
Expand Up @@ -3130,103 +3130,9 @@ void SimulationExperimentViewSimulationWidget::initializeSimulation()

//==============================================================================

bool SimulationExperimentViewSimulationWidget::initializeSolver(const libsedml::SedListOfAlgorithmParameters *pSedmlAlgorithmParameters,
const QString &pKisaoId,
SimulationExperimentViewInformationSolversWidgetData *pSolverData)
{
// Initialise our solver using the given SED-ML algorithm parameters and
// KiSAO id

SolverInterface *solverInterface = nullptr;
Core::Properties solverProperties = Core::Properties();

for (auto coreSolverInterface : Core::solverInterfaces()) {
if (coreSolverInterface->id(pKisaoId) == coreSolverInterface->solverName()) {
solverInterface = coreSolverInterface;
solverProperties = pSolverData->solversProperties().value(coreSolverInterface->solverName());

pSolverData->solversListProperty()->setValue(coreSolverInterface->solverName());

break;
}
}

if (solverInterface == nullptr) {
simulationError(tr("the requested solver (%1) could not be found").arg(pKisaoId),
Error::InvalidSimulationEnvironment);

return false;
}

for (uint i = 0, iMax = pSedmlAlgorithmParameters->getNumAlgorithmParameters(); i < iMax; ++i) {
const libsedml::SedAlgorithmParameter *sedmlAlgorithmParameter = pSedmlAlgorithmParameters->get(i);
QString parameterKisaoId = QString::fromStdString(sedmlAlgorithmParameter->getKisaoID());
QString id = solverInterface->id(parameterKisaoId);
bool propertySet = false;

for (auto solverProperty : solverProperties) {
if (solverProperty->id() == id) {
QVariant solverPropertyValue = QString::fromStdString(sedmlAlgorithmParameter->getValue());

switch (solverProperty->type()) {
case Core::Property::Type::Section:
// We should never come here...

#ifdef QT_DEBUG
qFatal("FATAL ERROR | %s:%d: a solver property cannot be of section type.", __FILE__, __LINE__);
#else
break;
#endif
case Core::Property::Type::String:
solverProperty->setValue(solverPropertyValue.toString());

break;
case Core::Property::Type::Integer:
case Core::Property::Type::IntegerGe0:
case Core::Property::Type::IntegerGt0:
solverProperty->setIntegerValue(solverPropertyValue.toInt());

break;
case Core::Property::Type::Double:
case Core::Property::Type::DoubleGe0:
case Core::Property::Type::DoubleGt0:
solverProperty->setDoubleValue(solverPropertyValue.toDouble());

break;
case Core::Property::Type::List:
solverProperty->setListValue(solverPropertyValue.toString());

break;
case Core::Property::Type::Boolean:
solverProperty->setBooleanValue(solverPropertyValue.toBool());

break;
case Core::Property::Type::Color:
// We should never come here...

#ifdef QT_DEBUG
qFatal("FATAL ERROR | %s:%d: a solver property cannot be of colour type.", __FILE__, __LINE__);
#else
break;
#endif
}

propertySet = solverProperty->type() != Core::Property::Type::Section;

break;
}
}

if (!propertySet) {
simulationError(tr("the requested solver property (%1) could not be set").arg(parameterKisaoId),
Error::InvalidSimulationEnvironment);

return false;
}
}

return true;
}
#define GUI_SUPPORT
#include "initializesolver.cpp.inl"
#undef GUI_SUPPORT

//==============================================================================

Expand Down
Expand Up @@ -61,5 +61,13 @@
<source>unable to close the simulation</source>
<translation>incapable de fermer la simulation</translation>
</message>
<message>
<source>the requested solver (%1) could not be found</source>
<translation>le solveur demandé (%1) n&apos;a pas pu être trouvé</translation>
</message>
<message>
<source>the requested solver property (%1) could not be set</source>
<translation>la propriété demandée (%1) du solveur n&apos;a pas pu être spécifiée</translation>
</message>
</context>
</TS>
33 changes: 1 addition & 32 deletions src/plugins/support/SimulationSupport/src/simulation.cpp
Expand Up @@ -1394,38 +1394,7 @@ Simulation::~Simulation()

//==============================================================================

QString Simulation::initializeSolver(const libsedml::SedListOfAlgorithmParameters *pSedmlAlgorithmParameters,
const QString &pKisaoId) const
{
// Initialise our solver using the given SED-ML algorithm parameters and
// KiSAO id

SolverInterface *solverInterface = nullptr;

for (auto coreSolverInterface : Core::solverInterfaces()) {
if (coreSolverInterface->id(pKisaoId) == coreSolverInterface->solverName()) {
solverInterface = coreSolverInterface;

mData->setOdeSolverName(coreSolverInterface->solverName());

break;
}
}

if (solverInterface == nullptr) {
return tr("the requested solver (%1) could not be found").arg(pKisaoId);
}

for (uint i = 0, iMax = pSedmlAlgorithmParameters->getNumAlgorithmParameters(); i < iMax; ++i) {
const libsedml::SedAlgorithmParameter *sedmlAlgorithmParameter = pSedmlAlgorithmParameters->get(i);
QString parameterKisaoId = QString::fromStdString(sedmlAlgorithmParameter->getKisaoID());

mData->addOdeSolverProperty(solverInterface->id(parameterKisaoId),
QString::fromStdString(sedmlAlgorithmParameter->getValue()));
}

return {};
}
#include "initializesolver.cpp.inl"

//==============================================================================

Expand Down

0 comments on commit 1d7f062

Please sign in to comment.