Skip to content

Commit

Permalink
Improve checking of valid connections (#10863)
Browse files Browse the repository at this point in the history
- Check for inside output and public outside input rather than just
  output or input when checking if connectors are compatible.

Fixes #10859
  • Loading branch information
perost committed Jun 19, 2023
1 parent 950c77a commit e6358fa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
22 changes: 20 additions & 2 deletions OMEdit/OMEditLIB/Modeling/Model.cpp
Expand Up @@ -1223,6 +1223,16 @@ namespace ModelInstance
return false;
}

bool isOutsideConnector(const Name &connector, const Model &model)
{
// An outside connector is a connector where the first part of the name is a
// connector. This is automatically true for a simple name.
if (connector.size() == 1) return true;

auto elem = model.lookupElement(connector.first().getName(false));
return elem && elem->getModel() && elem->getModel()->isConnector();
}

bool Model::isValidConnection(const Name &lhsConnector, const Name &rhsConnector) const
{
const Element *lhs = lookupElement(lhsConnector);
Expand All @@ -1233,10 +1243,18 @@ namespace ModelInstance
return true;
}

// An input should not be connected to an input, or an output to an output.
// A inside output should not be connected to an inside output,
// or a public outside input to a public outside input.
auto dir = lhs->getDirection();
if (!dir.isEmpty() && dir == rhs->getDirection()) {
return false;
auto lhs_outside = isOutsideConnector(lhsConnector, *this);
auto rhs_outside = isOutsideConnector(rhsConnector, *this);

if (dir == "output" && !lhs_outside && !rhs_outside) {
return false;
} else if (dir == "input" && lhs_outside && rhs_outside && lhs->isPublic() && rhs->isPublic()) {
return false;
}
}

// Check that the connectors are type compatible.
Expand Down
3 changes: 3 additions & 0 deletions OMEdit/OMEditLIB/Modeling/Model.h
Expand Up @@ -764,6 +764,9 @@ namespace ModelInstance
QStringList getNameParts() const;
const QList<Part> getParts() const { return mParts; }

size_t size() const { return mParts.size(); }
Part first() const { return mParts.empty() ? Part() : mParts[0]; }

private:
QList<Part> mParts;
};
Expand Down

0 comments on commit e6358fa

Please sign in to comment.