| 377 | } |
| 378 | |
| 379 | ConnectClauseImpl* 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 | |
| 434 | bool ClassDefinitionImpl::removeConnectClause(const std::string& source, const std::string& target) { |
| 435 | auto normalized = validateAndNormalizeConnectClause(source, target); |