Skip to content

Commit

Permalink
Use model instance to generate the data needed by OMSens (#12208)
Browse files Browse the repository at this point in the history
* Use model instance to generate the data needed by OMSens
- should fix OpenModelica/OMSens#13

* update OMSens_Qt to fix running on Linux
  • Loading branch information
adrpo committed Apr 10, 2024
1 parent b77f6ab commit 6b6c11f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 29 deletions.
89 changes: 61 additions & 28 deletions OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.cpp
Expand Up @@ -7997,6 +7997,40 @@ void ModelWidget::associateBusWithConnectors(QString busName)
associateBusWithConnectors(pDiagramBusComponent, mpDiagramGraphicsView);
}

const QString modelicaBlocksInterfacesRealInput = "Modelica.Blocks.Interfaces.RealInput";
const QString modelicaBlocksInterfacesRealOutput = "Modelica.Blocks.Interfaces.RealOutput";

void walkMe(ModelInstance::Model *model, QStringList &inputVariables, QStringList &outputVariables, QStringList &parameters, QStringList &auxVariables)
{
if (model == NULL) return;

for (ModelInstance::Element *element : model->getElements())
if (element->isComponent()) {
QString causality = element->getDirectionPrefix();
QString variability = element->getVariability();
const bool classNameIsReal = element->getType().compare(QStringLiteral("Real")) == 0;
QString ty = element->getType();
QString qn = element->getQualifiedName();
// printf("C: %s %s %s %s\n", causality.toUtf8().constData(), variability.toUtf8().constData(), ty.toUtf8().constData(), qn.toUtf8().constData());
if (causality.compare(QStringLiteral("input")) == 0) {
if (classNameIsReal || ty.compare(modelicaBlocksInterfacesRealInput) == 0) {
inputVariables.append(qn);
}
} else if (causality.compare(QStringLiteral("output")) == 0) {
if (classNameIsReal || ty.compare(modelicaBlocksInterfacesRealOutput) == 0) {
outputVariables.append(qn);
}
} else if(classNameIsReal && variability.compare(QStringLiteral("parameter")) == 0) {
parameters.append(element->getQualifiedName());
} else if (classNameIsReal) { /* Otherwise we are dealing with an auxiliarly variable */
auxVariables.append(qn);
}
walkMe(element->getModel(), inputVariables, outputVariables, parameters, auxVariables);
} else if (element->isExtend()) {
walkMe(element->getModel(), inputVariables, outputVariables, parameters, auxVariables);
}
}

/*!
* \brief ModelWidget::toOMSensData
* Creates a list of QVariant containing the model information needed by OMSens.
Expand All @@ -8009,39 +8043,38 @@ QList<QVariant> ModelWidget::toOMSensData()
if (!mpDiagramGraphicsView) {
return omSensData;
}

QStringList inputVariables;
QStringList outputVariables;
QStringList parameters;
QStringList auxVariables;
const QString modelicaBlocksInterfacesRealInput = "Modelica.Blocks.Interfaces.RealInput";
const QString modelicaBlocksInterfacesRealOutput = "Modelica.Blocks.Interfaces.RealOutput";
QList<Element*> pInheritedAndComposedComponents;
QList<Element*> pTopMostComponents = mpDiagramGraphicsView->getElementsList() + mpDiagramGraphicsView->getInheritedElementsList();
for (Element *pComponent : pTopMostComponents) {
pInheritedAndComposedComponents = pComponent->getElementsList() + pComponent->getInheritedElementsList();
pInheritedAndComposedComponents.append(pComponent);
for (auto component : pInheritedAndComposedComponents) {
QString causality, variability;
if (isNewApi()) {
causality = component->getModelComponent()->getDirectionPrefix();
variability = component->getModelComponent()->getVariability();
} else {
causality = component->getElementInfo()->getCausality();
variability = component->getElementInfo()->getVariablity();
}
const bool classNameIsReal = component->getClassName().compare(QStringLiteral("Real")) == 0;
if (causality.compare(QStringLiteral("input")) == 0) {
if (classNameIsReal || component->getClassName().compare(modelicaBlocksInterfacesRealInput) == 0) {
inputVariables.append(component->getName());
}
} else if (causality.compare(QStringLiteral("output")) == 0) {
if (classNameIsReal || component->getClassName().compare(modelicaBlocksInterfacesRealOutput) == 0) {
outputVariables.append(component->getName());

if (isNewApi()) {
walkMe(getModelInstance(), inputVariables, outputVariables, parameters, auxVariables);
} else {
QList<Element*> pInheritedAndComposedComponents;
QList<Element*> pTopMostComponents = mpDiagramGraphicsView->getElementsList() + mpDiagramGraphicsView->getInheritedElementsList();
for (Element *pComponent : pTopMostComponents) {
pInheritedAndComposedComponents = pComponent->getElementsList() + pComponent->getInheritedElementsList();
pInheritedAndComposedComponents.append(pComponent);
for (auto element : pInheritedAndComposedComponents) {
QString causality, variability;
causality = element->getElementInfo()->getCausality();
variability = element->getElementInfo()->getVariablity();
const bool classNameIsReal = element->getClassName().compare(QStringLiteral("Real")) == 0;
if (causality.compare(QStringLiteral("input")) == 0) {
if (classNameIsReal || element->getClassName().compare(modelicaBlocksInterfacesRealInput) == 0) {
inputVariables.append(element->getName());
}
} else if (causality.compare(QStringLiteral("output")) == 0) {
if (classNameIsReal || element->getClassName().compare(modelicaBlocksInterfacesRealOutput) == 0) {
outputVariables.append(element->getName());
}
} else if(classNameIsReal && variability.compare(QStringLiteral("parameter")) == 0) {
parameters.append(element->getName());
} /* Otherwise we are dealing with an auxiliarly variable */else if (classNameIsReal) {
auxVariables.append(element->getName());
}
} else if(classNameIsReal && variability.compare(QStringLiteral("parameter")) == 0) {
parameters.append(component->getName());
} /* Otherwise we are dealing with an auxiliarly variable */else if (classNameIsReal) {
auxVariables.append(component->getName());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion OMSens_Qt
Submodule OMSens_Qt updated 1 files
+4 −3 OMSensDialog.cpp

0 comments on commit 6b6c11f

Please sign in to comment.