MCPcopy Create free account
hub / github.com/NatLabRockies/OpenStudio / addConnectClause

Method addConnectClause

src/modelica/ModelicaFileImpl.cpp:379–432  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

377}
378
379ConnectClauseImpl* ClassDefinitionImpl::addConnectClause(const std::string& source, const std::string& target) {
380 auto normalized = validateAndNormalizeConnectClause(source, target);
381 const std::string& normalizedSource = normalized.first;
382 const std::string& normalizedTarget = normalized.second;
383 const std::string connectionId = ConnectClauseImpl::connectionIdFromEndpoints(normalizedSource, normalizedTarget);
384
385 ensureConnectionCache();
386 if (auto it = m_connections.find(connectionId); (it != m_connections.end()) && it->second->isActive()) {
387 LOG_AND_THROW("Connection already exists: connect(" + normalizedSource + ", " + normalizedTarget + ")");
388 }
389
390 auto* equationSection = selectEquationSection();
391 if (!equationSection) {
392 LOG_AND_THROW("Cannot find equation section in class definition");
393 }
394
395 auto file = modelicaFileShared();
396 if (!file) {
397 LOG_AND_THROW("ModelicaFile is no longer available");
398 }
399
400 std::string fullContent = file->getText();
401 std::string connectStatement = "connect(" + normalizedSource + ", " + normalizedTarget + ");";
402
403 size_t insertPos = 0;
404 const auto equations = equationSection->equation();
405 if (!equations.empty()) {
406 auto* lastEq = equations.back();
407 auto* stopToken = lastEq->getStop();
408 if (!stopToken) {
409 LOG_AND_THROW("Cannot determine insertion position for connection");
410 }
411 insertPos = static_cast<size_t>(stopToken->getStopIndex() + 1);
412 } else {
413 auto* stopToken = equationSection->getStop();
414 if (!stopToken) {
415 LOG_AND_THROW("Cannot determine insertion position for connection");
416 }
417 insertPos = static_cast<size_t>(stopToken->getStopIndex() + 1);
418 }
419
420 std::string insertion = "\n " + connectStatement;
421 fullContent.insert(insertPos, insertion);
422 fullContent.insert(insertPos + insertion.size(), "\n");
423
424 file->parse(fullContent);
425 markConnectionsDirty();
426 ensureConnectionCache();
427 auto it = m_connections.find(connectionId);
428 if (it == m_connections.end()) {
429 LOG_AND_THROW("Failed to locate connection after insertion");
430 }
431 return it->second.get();
432}
433
434bool ClassDefinitionImpl::removeConnectClause(const std::string& source, const std::string& target) {
435 auto normalized = validateAndNormalizeConnectClause(source, target);

Callers

nothing calls this directly

Calls 10

isActiveMethod · 0.80
equationMethod · 0.80
findMethod · 0.45
endMethod · 0.45
getTextMethod · 0.45
emptyMethod · 0.45
insertMethod · 0.45
sizeMethod · 0.45
parseMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected