Skip to content

Commit

Permalink
Added support for submodel parameters in composite models
Browse files Browse the repository at this point in the history
Added support for submodel parameters in composite models (XML only so far).

Added GUI features for submodel parameters in composite models.

Added scrollbars to parameters dialog.
  • Loading branch information
Robert Braun authored and adeas31 committed Feb 1, 2017
1 parent 379fe79 commit b219433
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 8 deletions.
35 changes: 33 additions & 2 deletions OMEdit/OMEditGUI/Component/ComponentProperties.cpp
Expand Up @@ -44,6 +44,8 @@
#include <QMessageBox>
#include <QDesktopServices>
#include <QDesktopWidget>
#include <QList>
#include <QStringList>

/*!
* \class Parameter
Expand Down Expand Up @@ -1440,6 +1442,15 @@ void MetaModelSubModelAttributes::setUpDialog()
mpGeometryFileBrowseButton = new QPushButton(Helper::browse);
mpGeometryFileBrowseButton->setAutoDefault(false);
connect(mpGeometryFileBrowseButton, SIGNAL(clicked()), this, SLOT(browseGeometryFile()));
// Model parameters
mpParametersLayout = new QGridLayout;
mpParametersLabel = new QLabel("Parameters:");
mpParametersLayout->addWidget(mpParametersLabel,0,0,1,2);
mpParametersScrollArea = new QScrollArea;
mpParametersScrollArea->setWidgetResizable(true);
QWidget *pScrollWidget = new QWidget;
pScrollWidget->setLayout(mpParametersLayout);
mpParametersScrollArea->setWidget(pScrollWidget);
// Create the buttons
mpOkButton = new QPushButton(Helper::ok);
mpOkButton->setAutoDefault(true);
Expand All @@ -1466,7 +1477,8 @@ void MetaModelSubModelAttributes::setUpDialog()
pMainLayout->addWidget(mpGeometryFileLabel, 5, 0);
pMainLayout->addWidget(mpGeometryFileTextBox, 5, 1);
pMainLayout->addWidget(mpGeometryFileBrowseButton, 5, 2);
pMainLayout->addWidget(mpButtonBox, 6, 0, 1, 3, Qt::AlignRight);
pMainLayout->addWidget(mpParametersScrollArea,6,0,1,3);
pMainLayout->addWidget(mpButtonBox, 7, 0, 1, 3, Qt::AlignRight);
setLayout(pMainLayout);
}

Expand All @@ -1487,7 +1499,18 @@ void MetaModelSubModelAttributes::initializeDialog()
mpModelFileTextBox->setText(mpComponent->getComponentInfo()->getModelFile());
// set the geometry file name
mpGeometryFileTextBox->setText(mpComponent->getComponentInfo()->getGeometryFile());

// update parameter widgets
MetaModelEditor *pEditor = dynamic_cast<MetaModelEditor*>(mpComponent->getGraphicsView()->getModelWidget()->getEditor());
QStringList parameters = pEditor->getParameterNames(mpComponent->getName());
mParameterLabels.clear();
mParameterLineEdits.clear();
for(int i=0; i<parameters.size(); ++i) {
mParameterLabels.append(new QLabel(parameters[i]));
mParameterLineEdits.append(new QLineEdit(pEditor->getParameterValue(mpComponent->getName(), parameters[i])));
mpParametersLayout->addWidget(mParameterLabels.last(),i+1,0);
mpParametersLayout->addWidget(mParameterLineEdits.last(),i+1,1);
}
mpParametersLabel->setVisible(!parameters.isEmpty());
}

/*!
Expand Down Expand Up @@ -1549,6 +1572,14 @@ void MetaModelSubModelAttributes::updateSubModelParameters()
pModelWidget->getUndoStack()->push(pUpdateSubModelAttributesCommand);
pModelWidget->updateModelText();
}

for(int i=0; i<mParameterLabels.size(); ++i) {
QString parameterName = mParameterLabels[i]->text();
QString parameterValue = mParameterLineEdits[i]->text();
MetaModelEditor *pEditor = dynamic_cast<MetaModelEditor*>(mpComponent->getGraphicsView()->getModelWidget()->getEditor());
pEditor->setParameterValue(mpComponent->getName(), parameterName, parameterValue);
}

accept();
}

Expand Down
5 changes: 5 additions & 0 deletions OMEdit/OMEditGUI/Component/ComponentProperties.h
Expand Up @@ -240,6 +240,11 @@ class MetaModelSubModelAttributes : public QDialog
Label *mpGeometryFileLabel;
QLineEdit *mpGeometryFileTextBox;
QPushButton *mpGeometryFileBrowseButton;
QScrollArea *mpParametersScrollArea;
QGridLayout *mpParametersLayout;
QLabel *mpParametersLabel;
QList<QLabel*> mParameterLabels;
QList<QLineEdit*> mParameterLineEdits;
QPushButton *mpOkButton;
QPushButton *mpCancelButton;
QDialogButtonBox *mpButtonBox;
Expand Down
123 changes: 122 additions & 1 deletion OMEdit/OMEditGUI/Editors/MetaModelEditor.cpp
Expand Up @@ -126,6 +126,60 @@ QDomElement MetaModelEditor::getSubModelElement(QString name)
return QDomElement();
}

QStringList MetaModelEditor::getParameterNames(QString subModelName)
{
QStringList ret;
QDomNodeList subModelList = mXmlDocument.elementsByTagName("SubModel");
for (int i = 0 ; i < subModelList.size() ; i++) {
QDomElement subModelElement = subModelList.at(i).toElement();
if(subModelElement.attribute("Name").compare(subModelName) == 0) {
QDomElement parameterElement = subModelElement.firstChildElement("Parameter");
while(!parameterElement.isNull()) {
ret << parameterElement.attribute("Name");
parameterElement = parameterElement.nextSiblingElement("Parameter");
}
}
}
return ret;
}

QString MetaModelEditor::getParameterValue(QString subModelName, QString parameterName)
{
QDomNodeList subModelList = mXmlDocument.elementsByTagName("SubModel");
for (int i = 0 ; i < subModelList.size() ; i++) {
QDomElement subModelElement = subModelList.at(i).toElement();
if(subModelElement.attribute("Name").compare(subModelName) == 0) {
QDomElement parameterElement = subModelElement.firstChildElement("Parameter");
while(!parameterElement.isNull()) {
if(parameterElement.attribute("Name").compare(parameterName) == 0) {
return parameterElement.attribute("Value");
}
parameterElement = parameterElement.nextSiblingElement("Parameter");
}
}
}
return ""; //Should never happen
}

void MetaModelEditor::setParameterValue(QString subModelName, QString parameterName, QString value)
{
QDomNodeList subModelList = mXmlDocument.elementsByTagName("SubModel");
for (int i = 0 ; i < subModelList.size() ; i++) {
QDomElement subModelElement = subModelList.at(i).toElement();
if (subModelElement.attribute("Name").compare(subModelName) == 0) {
QDomElement parameterElement = subModelElement.firstChildElement("Parameter");
while(!parameterElement.isNull()) {
if(parameterElement.attribute("Name").compare(parameterName) == 0) {
parameterElement.setAttribute("Value",value);
}
parameterElement = parameterElement.nextSiblingElement("Parameter");
}
setPlainText(mXmlDocument.toString());
return;
}
}
}

/*!
* \brief MetaModelEditor::getMetaModelName
* Gets the MetaModel name.
Expand Down Expand Up @@ -545,7 +599,7 @@ QString MetaModelEditor::getSimulationStopTime()
* Adds the InterfacePoint tag to SubModel.
* \param interfaces
*/
void MetaModelEditor::addInterfacesData(QDomElement interfaces, QString singleModel)
void MetaModelEditor::addInterfacesData(QDomElement interfaces, QDomElement parameters, QString singleModel)
{
QDomNodeList subModelList = mXmlDocument.elementsByTagName("SubModel");
for (int i = 0 ; i < subModelList.size() ; i++) {
Expand Down Expand Up @@ -629,6 +683,52 @@ void MetaModelEditor::addInterfacesData(QDomElement interfaces, QString singleMo
subModelInterfaceDataElement = subModelInterfaceDataElement.nextSiblingElement("InterfacePoint");
}
}

//Add or update parameters
QDomElement parameterDataElement = parameters.firstChildElement();
while (!parameterDataElement.isNull()) {
subModel = subModelList.at(i).toElement();
if (subModel.attribute("Name").compare(parameterDataElement.attribute("model")) == 0 &&
(subModel.attribute("Name") == singleModel || singleModel.isEmpty())) {
QDomElement parameterPoint;
// update parameter
//Do nothing if parameter already exists (i.e. never overwrite values)
if (!existParameter(subModel.attribute("Name"), parameterDataElement)) { // insert parameter
QDomElement parameterPoint = mXmlDocument.createElement("Parameter");
parameterPoint.setAttribute("Name", parameterDataElement.attribute("Name"));
parameterPoint.setAttribute("Value", parameterDataElement.attribute("DefaultValue"));
subModel.appendChild(parameterPoint);
}
}
parameterDataElement = parameterDataElement.nextSiblingElement();
}

//Remove all parameters in sub model that does not exist in fetched data (i.e. has been externally removed)
subModel = subModelList.at(i).toElement();
if(subModel.attribute("Name") != singleModel && !singleModel.isEmpty()){
continue; //Ignore other models if single model is specified
}
pComponent = mpModelWidget->getDiagramGraphicsView()->getComponentObject(subModel.attribute("Name"));
QDomElement subModelParameterDataElement = subModel.firstChildElement("Parameter");
while (!subModelParameterDataElement.isNull()) {
bool parameterExists = false;
parameterDataElement = parameters.firstChildElement();
while (!parameterDataElement.isNull()) {
if (subModelParameterDataElement.attribute("Name") == parameterDataElement.attribute("Name") &&
subModel.attribute("Name") == parameterDataElement.attribute("model")) {
parameterExists = true;
}
parameterDataElement = parameterDataElement.nextSiblingElement();
}
if (!parameterExists) {
QDomElement elementToRemove = subModelParameterDataElement;
subModelParameterDataElement = subModelParameterDataElement.nextSiblingElement("Parameter");
subModel.removeChild(elementToRemove);
}
else {
subModelParameterDataElement = subModelParameterDataElement.nextSiblingElement("Parameter");
}
}
}

//Remove connections between no longer existing elements
Expand Down Expand Up @@ -839,6 +939,26 @@ bool MetaModelEditor::existInterfacePoint(QString subModelName, QString interfac
return false;
}

bool MetaModelEditor::existParameter(QString subModelName, QDomElement parameterDataElement)
{
QDomNodeList subModelList = mXmlDocument.elementsByTagName("SubModel");
for (int i = 0 ; i < subModelList.size() ; i++) {
QDomElement subModel = subModelList.at(i).toElement();
if (subModel.attribute("Name").compare(subModelName) == 0) {
QDomNodeList subModelChildren = subModel.childNodes();
for (int j = 0 ; j < subModelChildren.size() ; j++) {
QDomElement parameterElement = subModelChildren.at(j).toElement();
if (parameterElement.tagName().compare("Parameter") == 0 &&
parameterElement.attribute("Name").compare(parameterDataElement.attribute("Name")) == 0) {
return true;
}
}
break;
}
}
return false;
}

/*!
* \brief MetaModelEditor::getRotationVector
* Computes a rotation vector (321) from a rotation matrix
Expand Down Expand Up @@ -1211,6 +1331,7 @@ void MetaModelHighlighter::initializeSettings()
<< "\\bSubModels\\b"
<< "\\bSubModel\\b"
<< "\\bInterfacePoint\\b"
<< "\\bParameter\\b"
<< "\\bConnections\\b"
<< "\\bConnection\\b"
<< "\\bLines\\b"
Expand Down
6 changes: 5 additions & 1 deletion OMEdit/OMEditGUI/Editors/MetaModelEditor.h
Expand Up @@ -73,6 +73,9 @@ class MetaModelEditor : public BaseEditor
QDomElement getConnectionsElement();
QDomNodeList getConnections();
QDomElement getSubModelElement(QString name);
QStringList getParameterNames(QString subModelName);
QString getParameterValue(QString subModelName, QString parameterName);
void setParameterValue(QString subModelName, QString parameterName, QString value);
void setMetaModelName(QString name);
bool addSubModel(Component *pComponent);
void createAnnotationElement(QDomElement subModel, QString visible, QString origin, QString extent, QString rotation);
Expand All @@ -86,7 +89,7 @@ class MetaModelEditor : public BaseEditor
bool isSimulationParams();
QString getSimulationStartTime();
QString getSimulationStopTime();
void addInterfacesData(QDomElement interfaces, QString singleModel=QString());
void addInterfacesData(QDomElement interfaces, QDomElement parameters, QString singleModel=QString());
void addInterface(Component *pInterfaceComponent, QString subModel);
bool interfacesAligned(QString interface1, QString interface2);
bool deleteSubModel(QString name);
Expand All @@ -98,6 +101,7 @@ class MetaModelEditor : public BaseEditor
XMLDocument mXmlDocument;

bool existInterfacePoint(QString subModelName, QString interfaceName);
bool existParameter(QString subModelName, QDomElement parameterDataElement);
bool getPositionAndRotationVectors(QString interfacePoint, QGenericMatrix<3,1,double> &CG_X_PHI_CG, QGenericMatrix<3,1,double> &X_C_PHI_X,
QGenericMatrix<3,1,double> &CG_X_R_CG, QGenericMatrix<3,1,double> &X_C_R_X);
bool fuzzyCompare(double p1, double p2);
Expand Down
15 changes: 11 additions & 4 deletions OMEdit/OMEditGUI/MainWindow.cpp
Expand Up @@ -2278,17 +2278,24 @@ void MainWindow::readInterfaceData(LibraryTreeItem *pLibraryTreeItem)
GUIMessages::getMessage(GUIMessages::ERROR_OPENING_FILE).arg("interfaceData.xml")
.arg(file.errorString()), Helper::ok);
} else {
QDomDocument interfaceData;
interfaceData.setContent(&file);
QDomDocument modelDataDocument;
modelDataDocument.setContent(&file);
file.close();
// Get the interfaces element
QDomElement interfaces = interfaceData.documentElement();
QDomElement modelData, interfaces,parameters;
modelData = modelDataDocument.documentElement();
interfaces = modelData.firstChildElement("Interfaces");
parameters = modelData.firstChildElement("Parameters");
if(interfaces.isNull() && parameters.isNull()) {
interfaces = modelDataDocument.documentElement(); //Backwards compatibility, remove later /Robert
}

// if we don't have ModelWidget then show it.
if (!pLibraryTreeItem->getModelWidget()) {
mpLibraryWidget->getLibraryTreeModel()->showModelWidget(pLibraryTreeItem);
}
MetaModelEditor *pMetaModelEditor = dynamic_cast<MetaModelEditor*>(pLibraryTreeItem->getModelWidget()->getEditor());
pMetaModelEditor->addInterfacesData(interfaces, singleModel);
pMetaModelEditor->addInterfacesData(interfaces, parameters, singleModel);
pLibraryTreeItem->getModelWidget()->updateModelText();
}
}
Expand Down
9 changes: 9 additions & 0 deletions OMEdit/OMEditGUI/Resources/XMLSchema/tlmModelDescription.xsd
Expand Up @@ -110,6 +110,15 @@
<xs:attribute name="Angle321" type="xs:string" use="optional"/>
</xs:complexType>
</xs:element>
<xs:element name="Parameter" minOccurs="0" maxOccurs="unbounded">
<xs:annotation>
<xs:documentation>TLM parameter for SubModel</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:attribute name="Name" type="xs:string" use="required"/>
<xs:attribute name="Value" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="Name" type="xs:string" use="required"/>
<xs:attribute name="StartCommand" type="xs:string" use="required"/>
Expand Down

0 comments on commit b219433

Please sign in to comment.