Skip to content

Commit

Permalink
- Allow to graphically update the simulation parameters for metamodel
Browse files Browse the repository at this point in the history
- Support for undo and redo for metamodel simulation parameters
  • Loading branch information
alash325 committed Mar 16, 2016
1 parent 19e8270 commit ae32d08
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 15 deletions.
48 changes: 48 additions & 0 deletions OMEdit/OMEditGUI/Editors/MetaModelEditor.cpp
Expand Up @@ -327,6 +327,54 @@ void MetaModelEditor::updateConnection(QString fromSubModel, QString toSubModel,
}
}

/*!
* \brief MetaModelEditor::updateSimulationParams
* Updates the simulation parameters.
* \param startTime
* \param stopTime
*/
void MetaModelEditor::updateSimulationParams(QString startTime, QString stopTime)
{
QDomElement simulationParamsElement = mXmlDocument.documentElement().firstChildElement("SimulationParams");
if (!simulationParamsElement.isNull()) {
simulationParamsElement.setAttribute("StartTime", startTime);
simulationParamsElement.setAttribute("StopTime", stopTime);
}
setPlainText(mXmlDocument.toString());
}

/*!
* \brief MetaModelEditor::isSimulationParams
* Updates the simulation parameters.
*/
bool MetaModelEditor::isSimulationParams()
{
QDomElement simulationParamsElement = mXmlDocument.documentElement().firstChildElement("SimulationParams");
if (!simulationParamsElement.isNull()) {
return true;
}
return false;
}

/*!
* \brief MetaModelEditor::getSimulationStartTime
* Gets the simulation start time.
*/
QString MetaModelEditor::getSimulationStartTime()
{
QDomElement simulationParamsElement = mXmlDocument.documentElement().firstChildElement("SimulationParams");
return simulationParamsElement.attribute("StartTime");
}

/*!
* \brief MetaModelEditor::getSimulationStopTime
* Gets the simulation stop time.
*/
QString MetaModelEditor::getSimulationStopTime()
{
QDomElement simulationParamsElement = mXmlDocument.documentElement().firstChildElement("SimulationParams");
return simulationParamsElement.attribute("StopTime");
}
/*!
* \brief MetaModelEditor::addInterfacesData
* Adds the InterfacePoint tag to SubModel.
Expand Down
5 changes: 5 additions & 0 deletions OMEdit/OMEditGUI/Editors/MetaModelEditor.h
Expand Up @@ -75,6 +75,11 @@ class MetaModelEditor : public BaseEditor
void updateSubModelParameters(QString name, QString startCommand, QString exactStepFlag);
bool createConnection(QString From, QString To, QString delay, QString alpha, QString zf, QString zfr, QString points);
void updateConnection(QString fromSubModel, QString toSubModel, QString points);
bool addSimulationParams(QString startTime, QString stopTime);
void updateSimulationParams(QString startTime, QString stopTime);
bool isSimulationParams();
QString getSimulationStartTime();
QString getSimulationStopTime();
void addInterfacesData(QDomElement interfaces);
bool existInterfaceData(QString subModelName, QString interfaceName);
bool deleteSubModel(QString name);
Expand Down
32 changes: 32 additions & 0 deletions OMEdit/OMEditGUI/Modeling/Commands.cpp
Expand Up @@ -1070,3 +1070,35 @@ void UpdateSubModelAttributesCommand::undo()
mpComponent->getComponentInfo()->setStartCommand(mOldComponentInfo.getStartCommand());
mpComponent->getComponentInfo()->setExactStep(mOldComponentInfo.getExactStep());
}

UpdateSimulationParamsCommand::UpdateSimulationParamsCommand(LibraryTreeItem *pLibraryTreeItem, QString oldStartTime, QString newStartTime, QString oldStopTime,
QString newStopTime, QUndoCommand *pParent )
: QUndoCommand(pParent)
{
mpLibraryTreeItem = pLibraryTreeItem;
mOldStartTime = oldStartTime;
mNewStartTime = newStartTime;
mOldStopTime = oldStopTime;
mNewStopTime = newStopTime;
setText(QString("Update %1 simulation parameter").arg(mpLibraryTreeItem->getNameStructure()));
}

/*!
* \brief UpdateSimulationParamsCommand::redo
* Redo the UpdateSimulationParamsCommand.
*/
void UpdateSimulationParamsCommand::redo()
{
MetaModelEditor *pMetaModelEditor = dynamic_cast<MetaModelEditor*>(mpLibraryTreeItem->getModelWidget()->getEditor());
pMetaModelEditor->updateSimulationParams(mNewStartTime, mNewStopTime);
}

/*!
* \brief UpdateSimulationParamsCommand::undo
* Undo the UpdateSimulationParamsCommand.
*/
void UpdateSimulationParamsCommand::undo()
{
MetaModelEditor *pMetaModelEditor = dynamic_cast<MetaModelEditor*>(mpLibraryTreeItem->getModelWidget()->getEditor());
pMetaModelEditor->updateSimulationParams(mOldStartTime, mOldStopTime);
}
14 changes: 14 additions & 0 deletions OMEdit/OMEditGUI/Modeling/Commands.h
Expand Up @@ -227,4 +227,18 @@ class UpdateSubModelAttributesCommand : public QUndoCommand
ComponentInfo mNewComponentInfo;
};

class UpdateSimulationParamsCommand : public QUndoCommand
{
public:
UpdateSimulationParamsCommand(LibraryTreeItem *pLibraryTreeItem, QString oldStartTime, QString newStartTime, QString oldStopTime,
QString newStopTime, QUndoCommand *pParent = 0);
void redo();
void undo();
private:
LibraryTreeItem *mpLibraryTreeItem;
QString mOldStartTime;
QString mNewStartTime;
QString mOldStopTime;
QString mNewStopTime;
};
#endif // COMMANDS_H
61 changes: 48 additions & 13 deletions OMEdit/OMEditGUI/TLM/TLMCoSimulationDialog.cpp
Expand Up @@ -46,6 +46,7 @@

#include "TLMCoSimulationDialog.h"
#include "VariablesWidget.h"
#include "Commands.h"

TLMCoSimulationDialog::TLMCoSimulationDialog(MainWindow *pMainWindow)
: QDialog(pMainWindow, Qt::WindowTitleHint)
Expand Down Expand Up @@ -407,22 +408,29 @@ MetaModelSimulationParamsDialog::MetaModelSimulationParamsDialog(GraphicsView *p
.arg(pGraphicsView->getModelWidget()->getLibraryTreeItem()->getNameStructure()));
setMinimumWidth(400);
mpGraphicsView = pGraphicsView;
mpLibraryTreeItem = mpGraphicsView->getModelWidget()->getModelWidgetContainer()->getCurrentModelWidget()->getLibraryTreeItem();
// Initialize simulation parameters
MetaModelEditor *pMetaModelEditor = dynamic_cast<MetaModelEditor*>(mpGraphicsView->getModelWidget()->getEditor());
if(pMetaModelEditor->isSimulationParams()){
mOldStartTime = pMetaModelEditor->getSimulationStartTime();
mOldStopTime = pMetaModelEditor->getSimulationStopTime();
}
// CoSimulation Interval
mpStartTimeLabel = new Label(tr("Start Time:"));
mpStartTimeTextBox = new QLineEdit("0");
mpStartTimeTextBox = new QLineEdit(mOldStartTime);
mpStopTimeLabel = new Label(tr("Stop Time:"));
mpStopTimeTextBox = new QLineEdit("1");
// Add the validators
mpStopTimeTextBox = new QLineEdit(mOldStopTime);
// Add the validators
QDoubleValidator *pDoubleValidator = new QDoubleValidator(this);
mpStartTimeTextBox->setValidator(pDoubleValidator);
mpStopTimeTextBox->setValidator(pDoubleValidator);
// buttons
mpSaveButton = new QPushButton(Helper::save);
mpSaveButton->setToolTip(tr("Saves the Co-Simulation experiment settings"));
connect(mpSaveButton, SIGNAL(clicked()), this, SLOT(saveExperimentSettings()));
connect(mpSaveButton, SIGNAL(clicked()), this, SLOT(saveSimulationParams()));
mpSaveAndCoSimulateButton = new QPushButton(tr("Save && CoSimulate"));
mpSaveAndCoSimulateButton->setToolTip(tr("Saves the Co-Simulation experiment settings and starts the Co-Simulation"));
connect(mpSaveAndCoSimulateButton, SIGNAL(clicked()), this, SLOT(saveExperimentSettingsAndCoSimulate()));
mpSaveAndCoSimulateButton->setToolTip(tr("Saves the simulation parameters and starts the Co-Simulation"));
connect(mpSaveAndCoSimulateButton, SIGNAL(clicked()), this, SLOT(saveSimulationParamsAndSimulate()));
mpCancelButton = new QPushButton(Helper::cancel);
connect(mpCancelButton, SIGNAL(clicked()), SLOT(reject()));
// adds Co-Simulation Experiment Setting buttons to the button box
Expand All @@ -442,21 +450,48 @@ MetaModelSimulationParamsDialog::MetaModelSimulationParamsDialog(GraphicsView *p
}

/*!
* \brief MetaModelSimulationParamsDialog::saveExperimentSettings
* Saves the Simulation Params setting.
* \brief MetaModelSimulationParamsDialog::saveSimulationParams
* Saves the Simulation Parameters.
* Slot activated when mpSave button clicked signal is raised.
*/
void MetaModelSimulationParamsDialog::saveExperimentSettings()
void MetaModelSimulationParamsDialog::saveSimulationParams()
{

if (validateSimulationParams()) {
UpdateSimulationParamsCommand *pUpdateSimulationParamsCommand;
pUpdateSimulationParamsCommand = new UpdateSimulationParamsCommand(mpLibraryTreeItem, mOldStartTime, mpStartTimeTextBox->text()
, mOldStopTime, mpStopTimeTextBox->text());
mpLibraryTreeItem->getModelWidget()->getUndoStack()->push(pUpdateSimulationParamsCommand);
mpLibraryTreeItem->getModelWidget()->updateModelText();
accept();
}
}

/*!
* \brief MetaModelSimulationParamsDialog::saveExperimentSettingsAndCoSimulate
* Saves the Simulation Params experiment setting and starts the co-simulation.
* \brief MetaModelSimulationParamsDialog::saveSimulationParamsAndSimulate
* Saves the Simulation Parameters and starts the co-simulation.
* Slot activated when mpSaveAndCoSimulateButton clicked signal is raised.
*/
void MetaModelSimulationParamsDialog::saveExperimentSettingsAndCoSimulate()
void MetaModelSimulationParamsDialog::saveSimulationParamsAndSimulate()
{
saveSimulationParams();
mpGraphicsView->getModelWidget()->getModelWidgetContainer()->getMainWindow()->TLMSimulate(mpLibraryTreeItem);
}

/*!
Validates the simulation params entered by the user.
*/
bool MetaModelSimulationParamsDialog::validateSimulationParams()
{
if (mpStartTimeTextBox->text().isEmpty()) {
mpStartTimeTextBox->setText("0");
}
if (mpStopTimeTextBox->text().isEmpty()) {
mpStopTimeTextBox->setText("1");
}
if (mpStartTimeTextBox->text().toDouble() > mpStopTimeTextBox->text().toDouble()) {
QMessageBox::critical(mpGraphicsView->getModelWidget()->getModelWidgetContainer()->getMainWindow(), QString(Helper::applicationName).append(" - ").append(Helper::error),
GUIMessages::getMessage(GUIMessages::SIMULATION_STARTTIME_LESSTHAN_STOPTIME), Helper::ok);
return false;
}
return true;
}
7 changes: 5 additions & 2 deletions OMEdit/OMEditGUI/TLM/TLMCoSimulationDialog.h
Expand Up @@ -113,9 +113,12 @@ class MetaModelSimulationParamsDialog : public QDialog
QPushButton *mpSaveAndCoSimulateButton;
QPushButton *mpCancelButton;
QDialogButtonBox *mpButtonBox;
QString mOldStartTime;
QString mOldStopTime;
bool validateSimulationParams();
private slots:
void saveExperimentSettings();
void saveExperimentSettingsAndCoSimulate();
void saveSimulationParams();
void saveSimulationParamsAndSimulate();
};

#endif // TLMCOSIMULATIONDIALOG_H

0 comments on commit ae32d08

Please sign in to comment.