Skip to content

Commit

Permalink
Support port names in FMIWrapperQ
Browse files Browse the repository at this point in the history
  • Loading branch information
robbr48 committed Oct 24, 2024
1 parent 99b7ae0 commit 50e267e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 12 deletions.
8 changes: 7 additions & 1 deletion HopsanGUI/GUIObjects/GUIComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,13 @@ bool Component::setParameterValue(QString name, QString value, bool force)
app->x = double(1+i)/double(1+portSpecs.size());
app->rot = 270;
app->mEnabled = true;
QString portName = "P"+QString::number(i+1);
QString portName;
if(portSpecs[i].contains(':')) {
portName = portSpecs[i].split(',').at(0).split(':').at(1);
}
else {
portName = "P"+QString::number(i+1);
}
this->getAppearanceData()->addPortAppearance(portName, app);
this->createRefreshExternalPort(portName);
}
Expand Down
53 changes: 42 additions & 11 deletions componentLibraries/defaultLibrary/Connectivity/FMIWrapperQ.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "HopsanEssentials.h"
#include "ComponentEssentials.h"
#include "ComponentUtilities.h"
#include "CoreUtilities/StringUtilities.h"
#include <algorithm>

#ifdef USEFMI4C
#include "fmi4c.h"
Expand Down Expand Up @@ -325,8 +327,17 @@ class FMIWrapperQ : public ComponentQ
HVector<HString> ports = mPortSpecs.split(';');
int portNumber = 1;
for(size_t i=0; i<ports.size(); ++i) {
ports[i] = ports[i].replace(" ", "");
HVector<HString> specs = ports[i].split(',');
HString nodeType = specs[0];
HString nodeType, portName;
if(specs[0].containes(':')) {
nodeType = specs[0].split(':')[0];
portName = specs[0].split(':')[1];
}
else {
nodeType = specs[0];
portName = "P"+to_hstring(portNumber);
}

//Check that node is not empty (parameter not given)
if(nodeType.empty()) {
Expand All @@ -338,10 +349,12 @@ class FMIWrapperQ : public ComponentQ
std::vector<HString> v = hopsanCore.getRegisteredNodeTypes();
if(std::find(v.begin(), v.end(), nodeType) == v.end()) {
addErrorMessage("Unsupported node type: "+nodeType);
for(size_t i=0; i<mPorts.size(); ++i) {
removePort(mPorts[i]->getName());
}
mPorts.clear();
return;
}

//Check port name
if(!hopsan::isNameValid(portName)) {
addErrorMessage("Illegal port name: "+portName);
return;
}

Expand All @@ -354,17 +367,35 @@ class FMIWrapperQ : public ComponentQ
if(numberOfVariables != expectedNumberOfVariables)
{
addErrorMessage("Error in port type specification: \""+nodeType+"\" requires "+to_hstring(expectedNumberOfVariables)+" variables.");
for(size_t i=0; i<mPorts.size(); ++i) {
removePort(mPorts[i]->getName());
}
mPorts.clear();
return;
}
Port *pPort = addPowerPort("P"+to_hstring(portNumber), nodeType, "");
mPorts.push_back(pPort);

//Check that all variables exist and are unique
for(size_t i=0; i<numberOfVariables; ++i) {
if(usedVariables.contains(specs[i+1])) {
addErrorMessage("Variable \""+specs[i+1]+"\" can only be used once in the \"portspecs\" parameter.");
return;
}
std::vector<HString> portNames = getPortNames();
if(mFmiVersion == fmiVersion1 && fmi1_getVariableByName(fmu, specs[i+1].c_str()) == nullptr) {
addErrorMessage("Variable \""+specs[i+1]+"\" does not exist in FMU.");
return;
}
else if(mFmiVersion == fmiVersion2 && fmi2_getVariableByName(fmu, specs[i+1].c_str()) == nullptr) {
addErrorMessage("Variable \""+specs[i+1]+"\" does not exist in FMU.");
return;
}
else if(mFmiVersion == fmiVersion3 && fmi3_getVariableByName(fmu, specs[i+1].c_str()) == nullptr) {
addErrorMessage("Variable \""+specs[i+1]+"\" does not exist in FMU.");
return;
}

usedVariables.append(specs[i+1]);
}

Port *pPort = addPowerPort(portName, nodeType, "");
mPorts.push_back(pPort);

++portNumber;
}

Expand Down

0 comments on commit 50e267e

Please sign in to comment.