Skip to content

Commit

Permalink
Implemented plugins interface
Browse files Browse the repository at this point in the history
Added a top level menu item `Sensitivity Optimization` which allows to load and run the OMSens plugin.
  • Loading branch information
adeas31 committed Jan 17, 2020
1 parent 0686019 commit 0c802bd
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 1 deletion.
47 changes: 47 additions & 0 deletions OMEdit/OMEditLIB/Interfaces/InformationInterface.h
@@ -0,0 +1,47 @@
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE
* OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3, ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
/*
* @author Adeel Asghar <adeel.asghar@liu.se>
*/

#ifndef INFORMATIONINTERFACE_H
#define INFORMATIONINTERFACE_H

class InformationInterface
{
public:
virtual void setOpenModelicaHome(const QString &omhome) = 0;
virtual void setTempPath(const QString &path) = 0;
};

#define InformationInterface_iid "org.openmodelica.OMEdit.InformationInterface/1.0"
Q_DECLARE_INTERFACE(InformationInterface, InformationInterface_iid)

#endif // INFORMATIONINTERFACE_H
57 changes: 57 additions & 0 deletions OMEdit/OMEditLIB/Interfaces/ModelInterface.h
@@ -0,0 +1,57 @@
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE
* OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3, ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
/*
* @author Adeel Asghar <adeel.asghar@liu.se>
*/

#ifndef MODELINTERFACE_H
#define MODELINTERFACE_H

#include <QVariant>

class ModelInterface
{
public:
/* A list of QVariants
* The list contains,
* - List of input variables.
* - List of output variables.
* - List of auxiliary variables.
* - List of parameters.
* - Model file name
* - Model name.
*/
virtual void analyzeModel(const QList<QVariant> &modelData) = 0;
};

#define ModelInterface_iid "org.openmodelica.OMEdit.ModelInterface/1.0"
Q_DECLARE_INTERFACE(ModelInterface, ModelInterface_iid)

#endif // MODELINTERFACE_H
51 changes: 51 additions & 0 deletions OMEdit/OMEditLIB/MainWindow.cpp
Expand Up @@ -73,6 +73,8 @@
#include "Traceability/TraceabilityInformationURI.h"
#include "Traceability/TraceabilityGraphViewWidget.h"
#include "Plotting/DiagramWindow.h"
#include "Interfaces/InformationInterface.h"
#include "Interfaces/ModelInterface.h"
#include "omc_config.h"

#include <QtSvg/QSvgGenerator>
Expand Down Expand Up @@ -335,6 +337,8 @@ void MainWindow::setUpMainWindow(threadData_t *threadData)
// Create an object of WelcomePageWidget
mpWelcomePageWidget = new WelcomePageWidget(this);
updateRecentFileActions();
// OMSens plugin
mpOMSensPlugin = 0;
// create the Git commands instance
//mpGitCommands = new GitCommands(this);
GitCommands::create();
Expand Down Expand Up @@ -2629,6 +2633,41 @@ void MainWindow::openConfigurationOptions()
OptionsDialog::instance()->show();
}

/*!
* \brief MainWindow::runOMSensPlugin
* Slots activated when run OMSens action is triggered.\n
* Runs OMSens plugin.
*/
void MainWindow::runOMSensPlugin()
{
if (!mpOMSensPlugin) {
// load OMSens plugin
#ifdef Q_OS_WIN
QPluginLoader loader(QString("%1/bin/omsensplugin.dll").arg(Helper::OpenModelicaHome));
#elif defined(Q_OS_MAC)
QPluginLoader loader(QString("%1/bin/omsensplugin.dylib").arg(Helper::OpenModelicaHome));
#else
QPluginLoader loader(QString("%1/bin/omsensplugin.so").arg(Helper::OpenModelicaHome));
#endif
mpOMSensPlugin = loader.instance();
if (!mpOMSensPlugin) {
MessagesWidget::instance()->addGUIMessage(MessageItem(MessageItem::Modelica, tr("Failed to load OMSend plugin. %1").arg(loader.errorString()), Helper::scriptingKind, Helper::errorLevel));
return;
}
}
// if OMSens plugin is already loaded.
InformationInterface *pInformationInterface = qobject_cast<InformationInterface*>(mpOMSensPlugin);
pInformationInterface->setOpenModelicaHome(Helper::OpenModelicaHome);
pInformationInterface->setTempPath(Utilities::tempDirectory());
ModelWidget *pModelWidget = mpModelWidgetContainer->getCurrentModelWidget();
if (pModelWidget) {
ModelInterface *pModelInterface = qobject_cast<ModelInterface*>(mpOMSensPlugin);
pModelInterface->analyzeModel(pModelWidget->toOMSensData());
} else {
QMessageBox::information(this, QString("%1 - %2").arg(Helper::applicationName).arg(Helper::information), tr("Please open a model before starting the OMSens plugin."), Helper::ok);
}
}

/*!
* \brief MainWindow::openUsersGuide
* Slot activated when mpUsersGuideAction triggered signal is raised.\n
Expand Down Expand Up @@ -3483,6 +3522,10 @@ void MainWindow::createActions()
mpOptionsAction->setStatusTip(tr("Shows the options window"));
mpOptionsAction->setMenuRole(QAction::PreferencesRole);
connect(mpOptionsAction, SIGNAL(triggered()), SLOT(openConfigurationOptions()));
// Run Sensitivity Analysis and Optimization action
mpRunOMSensAction = new QAction(tr("Run Sensitivity Analysis and Optimization"), this);
mpRunOMSensAction->setStatusTip(tr("Runs the sensitivity analysis and optimization"));
connect(mpRunOMSensAction, SIGNAL(triggered()), SLOT(runOMSensPlugin()));
// Help Menu
// users guide action
mpUsersGuideAction = new QAction(tr("OpenModelica Users Guide"), this);
Expand Down Expand Up @@ -3869,6 +3912,14 @@ void MainWindow::createMenus()
pOMSimulatorMenu->addAction(mpOMSArchivedSimulationsAction);
// add OMSimulator menu to menu bar
menuBar()->addAction(pOMSimulatorMenu->menuAction());
// Sensitivity Optimization menu
QMenu *pSensitivityOptimizationMenu = new QMenu(menuBar());
pSensitivityOptimizationMenu->setTitle(tr("Sensitivity Optimization"));
// add actions to Sensitivity Optimization menu
pSensitivityOptimizationMenu->addAction(mpRunOMSensAction);
pSensitivityOptimizationMenu->setEnabled(false);
// add Sensitivity Optimization menu to menu bar
menuBar()->addAction(pSensitivityOptimizationMenu->menuAction());
// Git menu
QMenu *pGitMenu = new QMenu(menuBar());
pGitMenu->setTitle(tr("&Git"));
Expand Down
5 changes: 4 additions & 1 deletion OMEdit/OMEditLIB/MainWindow.h
Expand Up @@ -290,6 +290,7 @@ class MainWindow : public QMainWindow
OMSSimulationDialog *mpOMSSimulationDialog;
ModelWidgetContainer *mpModelWidgetContainer;
WelcomePageWidget *mpWelcomePageWidget;
QObject *mpOMSensPlugin;
GitCommands *mpGitCommands;
CommitChangesDialog *mpCommitChangesDialog;
TraceabilityInformationURI *mpTraceabilityInformationURI;
Expand Down Expand Up @@ -382,6 +383,7 @@ class MainWindow : public QMainWindow
QAction *mpOpenWorkingDirectoryAction;
QAction *mpOpenTerminalAction;
QAction *mpOptionsAction;
QAction *mpRunOMSensAction;
// Help Menu
QAction *mpUsersGuideAction;
QAction *mpUsersGuidePdfAction;
Expand Down Expand Up @@ -510,6 +512,7 @@ public slots:
void exportModelXML();
void exportModelFigaro();
void showOpenModelicaCommandPrompt();
void runOMSensPlugin();
void exportModelToOMNotebook();
void importModelfromOMNotebook();
void importNgspiceNetlist();
Expand Down Expand Up @@ -566,7 +569,7 @@ private slots:
void switchToAlgorithmicDebuggingPerspective();
void closeAllWindowsButThis(QMdiArea *pMdiArea);
void tileSubWindows(QMdiArea *pMdiArea, bool horizontally);
void fetchInterfaceDataHelper(LibraryTreeItem *pLibraryTreeItem, QString singleModel=QString());
void fetchInterfaceDataHelper(LibraryTreeItem *pLibraryTreeItem, QString singleModel = QString());
protected:
virtual void dragEnterEvent(QDragEnterEvent *event) override;
virtual void dragMoveEvent(QDragMoveEvent *event) override;
Expand Down
36 changes: 36 additions & 0 deletions OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.cpp
Expand Up @@ -6183,6 +6183,42 @@ void ModelWidget::associateBusWithConnectors(QString busName)
associateBusWithConnectors(pDiagramBusComponent, mpDiagramGraphicsView);
}

/*!
* \brief ModelWidget::toOMSensJson
* Creates a list of QVariant containing the model information needed by OMSens.
* \return
*/
QList<QVariant> ModelWidget::toOMSensData()
{
QStringList inputVariables;
QStringList outputVariables;
QStringList parameters;
QStringList auxVariables;

if (mpDiagramGraphicsView) {
foreach (Component *pComponent, mpDiagramGraphicsView->getComponentsList()) {
ComponentInfo *pComponentInfo = pComponent->getComponentInfo();
if ((pComponentInfo->getCausality().compare("input") == 0) && ((pComponentInfo->getClassName().compare("Real") == 0)
|| (pComponentInfo->getClassName().compare("Modelica.Blocks.Interfaces.RealInput") == 0))) {
inputVariables.append(pComponentInfo->getName());
} else if ((pComponentInfo->getCausality().compare("output") == 0) && ((pComponentInfo->getClassName().compare("Real") == 0)
|| (pComponentInfo->getClassName().compare("Modelica.Blocks.Interfaces.RealOutput") == 0))) {
outputVariables.append(pComponentInfo->getName());
}
if ((pComponentInfo->getVariablity().compare("parameter") == 0) && (pComponentInfo->getClassName().compare("Real") == 0)) {
parameters.append(pComponentInfo->getName());
}
if (pComponentInfo->getClassName().compare("Real") == 0) {
auxVariables.append(pComponentInfo->getName());
}
}
}

QList<QVariant> omSensData;
omSensData << inputVariables << outputVariables << auxVariables << parameters << mpLibraryTreeItem->getFileName() << mpLibraryTreeItem->getNameStructure();
return omSensData;
}

/*!
* \brief ModelWidget::getIconDiagramMap
* Parses the IconMap/DiagramMap annotation and returns the IconDiagramMap object.
Expand Down
1 change: 1 addition & 0 deletions OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.h
Expand Up @@ -554,6 +554,7 @@ class ModelWidget : public QWidget
void associateBusWithConnector(QString busName, QString connectorName);
void dissociateBusWithConnector(QString busName, QString connectorName);
void associateBusWithConnectors(QString busName);
QList<QVariant> toOMSensData();
private:
ModelWidgetContainer *mpModelWidgetContainer;
LibraryTreeItem *mpLibraryTreeItem;
Expand Down
2 changes: 2 additions & 0 deletions OMEdit/OMEditLIB/OMEditLIB.pro
Expand Up @@ -260,6 +260,8 @@ HEADERS += Util/Helper.h \
OMS/OMSSimulationDialog.h \
OMS/OMSSimulationOutputWidget.h \
Animation/TimeManager.h \
Interfaces/InformationInterface.h \
Interfaces/ModelInterface.h \
Util/ResourceCache.h

CONFIG(osg) {
Expand Down

0 comments on commit 0c802bd

Please sign in to comment.