src/modelica/ModelicaFileImpl.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "ModelicaFileImpl.hpp" | ||
| 2 | #include "antlr/modelicaParser.h" | ||
| 3 | #include "antlr/modelicaLexer.h" | ||
| 4 | #include "../utilities/core/Logger.hpp" | ||
| 5 | #include "../utilities/core/Filesystem.hpp" | ||
| 6 | #include <antlr4-runtime.h> | ||
| 7 | #include <antlr4-runtime/tree/xpath/XPath.h> | ||
| 8 | #include <fmt/core.h> | ||
| 9 | #include <cctype> | ||
| 10 | #include <fstream> | ||
| 11 | #include <sstream> | ||
| 12 | #include <unordered_set> | ||
| 13 | #include <utility> | ||
| 14 | |||
| 15 | namespace openstudio::modelica::detail { | ||
| 16 | |||
| 17 | namespace { | ||
| 18 | ✗ | std::string buildLongClassName(modelicaParser::Long_class_specifierContext* longClass) { | |
| 19 | ✗ | if (!longClass) { | |
| 20 | ✗ | return {}; | |
| 21 | } | ||
| 22 | ✗ | std::string name; | |
| 23 | ✗ | const auto& identifiers = longClass->IDENT(); | |
| 24 | ✗ | for (size_t i = 0; i < identifiers.size(); ++i) { | |
| 25 | ✗ | if (i > 0) { | |
| 26 | ✗ | name += "."; | |
| 27 | } | ||
| 28 | ✗ | name += identifiers[i]->getText(); | |
| 29 | } | ||
| 30 | ✗ | return name; | |
| 31 | ✗ | } | |
| 32 | } // namespace | ||
| 33 | |||
| 34 | ✗ | ModelicaFileImpl::ModelicaFileImpl(const openstudio::path& path) { | |
| 35 | ✗ | openstudio::filesystem::ifstream moFile(path, std::ios::in | std::ios::binary); | |
| 36 | ✗ | if (!moFile.is_open()) { | |
| 37 | ✗ | LOG_AND_THROW(fmt::format("Unable to open Modelica file '{}'", path.generic_string())); | |
| 38 | } | ||
| 39 | ✗ | std::stringstream buffer; | |
| 40 | ✗ | buffer << moFile.rdbuf(); | |
| 41 | |||
| 42 | ✗ | parse(buffer.str()); | |
| 43 | ✗ | } | |
| 44 | |||
| 45 | ✗ | void ModelicaFileImpl::parse(std::string_view text) { | |
| 46 | //m_text = text; | ||
| 47 | |||
| 48 | // Create ANTLR input stream | ||
| 49 | ✗ | m_inputStream = std::make_unique<antlr4::ANTLRInputStream>(text); | |
| 50 | ✗ | m_modelicaLexer = std::make_unique<modelicaLexer>(m_inputStream.get()); | |
| 51 | ✗ | m_tokenStream = std::make_unique<antlr4::CommonTokenStream>(m_modelicaLexer.get()); | |
| 52 | ✗ | m_modelicaParser = std::make_unique<modelicaParser>(m_tokenStream.get()); | |
| 53 | //m_ctx = m_modelicaParser->stored_definition(); | ||
| 54 | |||
| 55 | ✗ | updateCTX(); | |
| 56 | ✗ | m_classDefinitionsDirty = true; | |
| 57 | ✗ | } | |
| 58 | |||
| 59 | ✗ | modelicaParser* ModelicaFileImpl::parser() const { | |
| 60 | ✗ | return m_modelicaParser.get(); | |
| 61 | } | ||
| 62 | |||
| 63 | ✗ | void ModelicaFileImpl::updateCTX() { | |
| 64 | ✗ | for (auto& syntax : m_syntaxStore) { | |
| 65 | ✗ | if (syntax) { | |
| 66 | ✗ | syntax->updateCTX(); | |
| 67 | } | ||
| 68 | } | ||
| 69 | ✗ | } | |
| 70 | |||
| 71 | ✗ | std::string ModelicaFileImpl::getText() const { | |
| 72 | ✗ | return m_modelicaParser->getTokenStream()->getText(); | |
| 73 | } | ||
| 74 | |||
| 75 | ✗ | std::string ModelicaFileImpl::printTree() const { | |
| 76 | ✗ | std::stringstream ss; | |
| 77 | |||
| 78 | ✗ | const std::function<void(antlr4::tree::ParseTree*, antlr4::Parser*, int)> to_s = [&ss, &to_s](antlr4::tree::ParseTree* tree, antlr4::Parser* parser, | |
| 79 | int indent) { | ||
| 80 | ✗ | if (!tree) { | |
| 81 | ✗ | return; | |
| 82 | } | ||
| 83 | |||
| 84 | // Print indentation | ||
| 85 | ✗ | for (int i = 0; i < indent; i++) { | |
| 86 | ✗ | ss << " "; | |
| 87 | } | ||
| 88 | |||
| 89 | // Check if the node is a rule (internal) or a leaf (token) | ||
| 90 | ✗ | auto* ruleContext = dynamic_cast<antlr4::ParserRuleContext*>(tree); | |
| 91 | ✗ | if (ruleContext) { | |
| 92 | // Internal rule node - print rule name | ||
| 93 | ✗ | ss << "rule: " << parser->getRuleNames()[ruleContext->getRuleIndex()] << std::endl; // NOLINT | |
| 94 | } else { | ||
| 95 | // Leaf token node - print actual token text | ||
| 96 | ✗ | ss << "token: " << tree->getText() << std::endl; // NOLINT | |
| 97 | } | ||
| 98 | |||
| 99 | // Recursively print children with increased indentation | ||
| 100 | ✗ | for (auto* child : tree->children) { | |
| 101 | ✗ | to_s(child, parser, indent + 1); | |
| 102 | } | ||
| 103 | ✗ | }; | |
| 104 | |||
| 105 | ✗ | to_s(m_modelicaParser->stored_definition(), m_modelicaParser.get(), 0); | |
| 106 | |||
| 107 | ✗ | return ss.str(); | |
| 108 | ✗ | } | |
| 109 | |||
| 110 | ✗ | void ModelicaFileImpl::ensureClassDefinitionCache() { | |
| 111 | ✗ | if (!m_classDefinitionsDirty) { | |
| 112 | ✗ | return; | |
| 113 | } | ||
| 114 | ✗ | rebuildClassDefinitionCache(); | |
| 115 | ✗ | m_classDefinitionsDirty = false; | |
| 116 | } | ||
| 117 | |||
| 118 | ✗ | void ModelicaFileImpl::rebuildClassDefinitionCache() { | |
| 119 | ✗ | m_cachedClassDefinitions.clear(); | |
| 120 | ✗ | if (!m_modelicaParser) { | |
| 121 | ✗ | return; | |
| 122 | } | ||
| 123 | |||
| 124 | auto class_definitions = | ||
| 125 | ✗ | antlr4::tree::xpath::XPath::findAll(m_modelicaParser->stored_definition(), std::string(ClassDefinitionImpl::xpath), m_modelicaParser.get()); | |
| 126 | |||
| 127 | ✗ | std::unordered_set<std::string> seenIds; | |
| 128 | ✗ | auto self = shared_from_this(); | |
| 129 | |||
| 130 | ✗ | for (auto* ctx : class_definitions) { | |
| 131 | ✗ | auto* class_def_ctx = dynamic_cast<modelicaParser::Class_definitionContext*>(ctx); | |
| 132 | ✗ | if (!class_def_ctx || !class_def_ctx->class_specifier() || !class_def_ctx->class_specifier()->long_class_specifier()) { | |
| 133 | ✗ | continue; | |
| 134 | } | ||
| 135 | |||
| 136 | ✗ | const std::string id = ClassDefinitionImpl::idFromCTX(class_def_ctx); | |
| 137 | ✗ | seenIds.insert(id); | |
| 138 | |||
| 139 | ✗ | auto it = m_classDefinitions.find(id); | |
| 140 | ✗ | if (it == m_classDefinitions.end()) { | |
| 141 | ✗ | auto impl = std::make_unique<ClassDefinitionImpl>(self, class_def_ctx); | |
| 142 | ✗ | auto* ptr = impl.get(); | |
| 143 | ✗ | m_classDefinitions.emplace(id, std::move(impl)); | |
| 144 | ✗ | ptr->markComponentClausesDirty(); | |
| 145 | ✗ | m_cachedClassDefinitions.push_back(ptr); | |
| 146 | ✗ | } else { | |
| 147 | ✗ | auto* ptr = it->second.get(); | |
| 148 | ✗ | ptr->setClassContext(class_def_ctx); | |
| 149 | ✗ | m_cachedClassDefinitions.push_back(ptr); | |
| 150 | } | ||
| 151 | ✗ | } | |
| 152 | |||
| 153 | ✗ | for (auto& [id, impl] : m_classDefinitions) { | |
| 154 | ✗ | if (!seenIds.contains(id)) { | |
| 155 | ✗ | impl->setClassContext(nullptr); | |
| 156 | } | ||
| 157 | } | ||
| 158 | ✗ | } | |
| 159 | |||
| 160 | ✗ | std::vector<ClassDefinitionImpl*> ModelicaFileImpl::getClassDefinitions() { | |
| 161 | ✗ | ensureClassDefinitionCache(); | |
| 162 | ✗ | return m_cachedClassDefinitions; | |
| 163 | } | ||
| 164 | |||
| 165 | ✗ | ClassDefinitionImpl* ModelicaFileImpl::getClassDefinitionByName(const std::string& name) { | |
| 166 | ✗ | ensureClassDefinitionCache(); | |
| 167 | ✗ | auto it = m_classDefinitions.find(name); | |
| 168 | ✗ | if (it == m_classDefinitions.end() || !it->second->isActive()) { | |
| 169 | ✗ | LOG_AND_THROW("Class definition not found: " + name); | |
| 170 | } | ||
| 171 | ✗ | return it->second.get(); | |
| 172 | } | ||
| 173 | |||
| 174 | ✗ | ClassDefinitionImpl* ModelicaFileImpl::addClassDefinition(const std::string& text) { | |
| 175 | // First, validate that the new text is a valid class definition | ||
| 176 | ✗ | std::string newClassId; | |
| 177 | try { | ||
| 178 | ✗ | antlr4::ANTLRInputStream testStream(text); | |
| 179 | ✗ | modelicaLexer testLexer(&testStream); | |
| 180 | ✗ | antlr4::CommonTokenStream testTokens(&testLexer); | |
| 181 | ✗ | modelicaParser testParser(&testTokens); | |
| 182 | |||
| 183 | // Try to parse as a class definition | ||
| 184 | ✗ | auto* classDefCtx = testParser.class_definition(); | |
| 185 | ✗ | if (!classDefCtx || testParser.getNumberOfSyntaxErrors() > 0) { | |
| 186 | ✗ | LOG_AND_THROW("Invalid class definition syntax: " + text); | |
| 187 | } | ||
| 188 | |||
| 189 | // Only accept long class definitions, reject short class specifiers | ||
| 190 | ✗ | if (!classDefCtx->class_specifier() || !classDefCtx->class_specifier()->long_class_specifier()) { | |
| 191 | ✗ | LOG_AND_THROW("Only long class definitions are supported, not short class specifiers: " + text); | |
| 192 | } | ||
| 193 | ✗ | newClassId = ClassDefinitionImpl::idFromCTX(classDefCtx); | |
| 194 | ✗ | } catch (const std::exception& e) { | |
| 195 | ✗ | LOG_AND_THROW("Failed to parse class definition: " + std::string(e.what())); | |
| 196 | ✗ | } | |
| 197 | |||
| 198 | // Get current file content | ||
| 199 | ✗ | std::string currentContent = getText(); | |
| 200 | |||
| 201 | // Find insertion point after the last class definition | ||
| 202 | ✗ | size_t insertPos = currentContent.length(); | |
| 203 | |||
| 204 | // Find all existing class definitions to get the end of the last one | ||
| 205 | ✗ | auto existingClasses = getClassDefinitions(); | |
| 206 | ✗ | if (!existingClasses.empty()) { | |
| 207 | // Get the context of the last class definition | ||
| 208 | ✗ | const auto& lastClass = existingClasses.back(); | |
| 209 | ✗ | auto* lastCtx = lastClass->ctx(); | |
| 210 | |||
| 211 | // Find the end position of the last class in the original text | ||
| 212 | ✗ | auto* stopToken = lastCtx->getStop(); | |
| 213 | ✗ | if (stopToken) { | |
| 214 | ✗ | size_t endPos = stopToken->getStopIndex() + 1; | |
| 215 | ✗ | insertPos = endPos; | |
| 216 | } | ||
| 217 | } | ||
| 218 | |||
| 219 | // Insert the new class definition after the last one | ||
| 220 | ✗ | currentContent.insert(insertPos, "\n" + text + "\n"); | |
| 221 | |||
| 222 | // Reparse the entire file with new content | ||
| 223 | ✗ | parse(currentContent); | |
| 224 | ✗ | ensureClassDefinitionCache(); | |
| 225 | ✗ | auto it = m_classDefinitions.find(newClassId); | |
| 226 | ✗ | if (it == m_classDefinitions.end()) { | |
| 227 | ✗ | LOG_AND_THROW("Failed to retrieve class definition after insertion"); | |
| 228 | } | ||
| 229 | ✗ | return it->second.get(); | |
| 230 | ✗ | } | |
| 231 | |||
| 232 | ✗ | std::string ClassDefinitionImpl::longClassSpecifier() const { | |
| 233 | ✗ | if (!ctx() || !ctx()->class_specifier() || !ctx()->class_specifier()->long_class_specifier()) { | |
| 234 | ✗ | return {}; | |
| 235 | } | ||
| 236 | ✗ | return buildLongClassName(ctx()->class_specifier()->long_class_specifier()); | |
| 237 | } | ||
| 238 | |||
| 239 | ✗ | std::vector<ComponentClauseImpl*> ClassDefinitionImpl::componentClauses() { | |
| 240 | ✗ | ensureComponentClauseCache(); | |
| 241 | ✗ | return m_cachedComponentClauses; | |
| 242 | } | ||
| 243 | |||
| 244 | ✗ | void ClassDefinitionImpl::addComponentClause(const std::string& text) { | |
| 245 | try { | ||
| 246 | ✗ | antlr4::ANTLRInputStream testStream(text); | |
| 247 | ✗ | modelicaLexer testLexer(&testStream); | |
| 248 | ✗ | antlr4::CommonTokenStream testTokens(&testLexer); | |
| 249 | ✗ | modelicaParser testParser(&testTokens); | |
| 250 | ✗ | const auto* componentClauseCtx = testParser.component_clause(); | |
| 251 | ✗ | if (!componentClauseCtx || testParser.getNumberOfSyntaxErrors() > 0) { | |
| 252 | ✗ | LOG_AND_THROW("Invalid component clause syntax: " + text); | |
| 253 | } | ||
| 254 | ✗ | } catch (const std::exception& e) { | |
| 255 | ✗ | LOG_AND_THROW("Failed to parse component clause: " + std::string(e.what())); | |
| 256 | ✗ | } | |
| 257 | |||
| 258 | // Find insertion point - look for the composition within this class | ||
| 259 | ✗ | auto* composition = ctx()->class_specifier()->long_class_specifier()->composition(); | |
| 260 | ✗ | if (!composition) { | |
| 261 | ✗ | LOG_AND_THROW("Cannot find composition in class definition"); | |
| 262 | } | ||
| 263 | |||
| 264 | // Get the element list (vector of elements) | ||
| 265 | ✗ | auto elementList = composition->element_list(); | |
| 266 | |||
| 267 | // Find the end position - either after the last element or at the composition end | ||
| 268 | ✗ | antlr4::Token* stopToken = nullptr; | |
| 269 | ✗ | if (!elementList.empty()) { | |
| 270 | // Insert after the last element | ||
| 271 | ✗ | stopToken = elementList.back()->getStop(); | |
| 272 | } else { | ||
| 273 | // Empty element list, insert at the start of composition | ||
| 274 | ✗ | stopToken = composition->getStart(); | |
| 275 | } | ||
| 276 | ✗ | if (!stopToken) { | |
| 277 | ✗ | LOG_AND_THROW("Cannot determine insertion position for component clause"); | |
| 278 | } | ||
| 279 | |||
| 280 | // Get the full file content | ||
| 281 | ✗ | auto file = modelicaFileShared(); | |
| 282 | ✗ | if (!file) { | |
| 283 | ✗ | LOG_AND_THROW("ModelicaFile is no longer available"); | |
| 284 | } | ||
| 285 | |||
| 286 | ✗ | std::string fullContent = file->getText(); | |
| 287 | |||
| 288 | // Calculate the insertion position using ANTLR token positions | ||
| 289 | ✗ | size_t insertPos = stopToken->getStopIndex() + 1; | |
| 290 | |||
| 291 | // Insert the new component clause with proper formatting | ||
| 292 | ✗ | fullContent.insert(insertPos, "\n " + text + "\n"); | |
| 293 | |||
| 294 | // Reparse the entire file | ||
| 295 | ✗ | file->parse(fullContent); | |
| 296 | ✗ | markComponentClausesDirty(); | |
| 297 | ✗ | } | |
| 298 | |||
| 299 | ✗ | void ClassDefinitionImpl::markComponentClausesDirty() { | |
| 300 | ✗ | m_componentClausesDirty = true; | |
| 301 | ✗ | } | |
| 302 | |||
| 303 | ✗ | void ClassDefinitionImpl::setClassContext(modelicaParser::Class_definitionContext* ctx) { | |
| 304 | ✗ | setCTX(ctx); | |
| 305 | ✗ | markComponentClausesDirty(); | |
| 306 | ✗ | markConnectionsDirty(); | |
| 307 | ✗ | if (!ctx) { | |
| 308 | ✗ | deactivateComponentClauses(); | |
| 309 | ✗ | deactivateConnections(); | |
| 310 | } | ||
| 311 | ✗ | } | |
| 312 | |||
| 313 | ✗ | void ClassDefinitionImpl::ensureComponentClauseCache() { | |
| 314 | ✗ | if (!m_componentClausesDirty) { | |
| 315 | ✗ | return; | |
| 316 | } | ||
| 317 | ✗ | rebuildComponentClauseCache(); | |
| 318 | ✗ | m_componentClausesDirty = false; | |
| 319 | } | ||
| 320 | |||
| 321 | ✗ | void ClassDefinitionImpl::rebuildComponentClauseCache() { | |
| 322 | ✗ | m_cachedComponentClauses.clear(); | |
| 323 | |||
| 324 | ✗ | auto parserPtr = parser(); | |
| 325 | ✗ | auto* classCtx = ctx(); | |
| 326 | ✗ | if (!parserPtr || !classCtx) { | |
| 327 | ✗ | deactivateComponentClauses(); | |
| 328 | ✗ | return; | |
| 329 | } | ||
| 330 | |||
| 331 | ✗ | const auto component_clause = antlr4::tree::xpath::XPath::findAll(classCtx, std::string(ComponentClauseImpl::xpath), parserPtr); | |
| 332 | ✗ | std::unordered_set<std::string> seenIds; | |
| 333 | ✗ | auto fileShared = modelicaFileShared(); | |
| 334 | |||
| 335 | ✗ | for (auto* c : component_clause) { | |
| 336 | ✗ | auto* component_clause_ctx = dynamic_cast<modelicaParser::Component_clauseContext*>(c); | |
| 337 | ✗ | if (!component_clause_ctx) { | |
| 338 | ✗ | continue; | |
| 339 | } | ||
| 340 | ✗ | const std::string id = ComponentClauseImpl::idFromCTX(component_clause_ctx); | |
| 341 | ✗ | seenIds.insert(id); | |
| 342 | |||
| 343 | ✗ | auto it = m_componentClauses.find(id); | |
| 344 | ✗ | if (it == m_componentClauses.end()) { | |
| 345 | ✗ | auto impl = std::make_unique<ComponentClauseImpl>(fileShared, component_clause_ctx); | |
| 346 | ✗ | auto* ptr = impl.get(); | |
| 347 | ✗ | m_componentClauses.emplace(id, std::move(impl)); | |
| 348 | ✗ | m_cachedComponentClauses.push_back(ptr); | |
| 349 | ✗ | } else { | |
| 350 | ✗ | auto* ptr = it->second.get(); | |
| 351 | ✗ | ptr->setComponentContext(component_clause_ctx); | |
| 352 | ✗ | m_cachedComponentClauses.push_back(ptr); | |
| 353 | } | ||
| 354 | ✗ | } | |
| 355 | |||
| 356 | ✗ | for (auto& [id, impl] : m_componentClauses) { | |
| 357 | ✗ | if (!seenIds.contains(id)) { | |
| 358 | ✗ | impl->setComponentContext(nullptr); | |
| 359 | } | ||
| 360 | } | ||
| 361 | ✗ | } | |
| 362 | |||
| 363 | ✗ | void ClassDefinitionImpl::deactivateComponentClauses() { | |
| 364 | ✗ | for (auto& [id, impl] : m_componentClauses) { | |
| 365 | ✗ | impl->setComponentContext(nullptr); | |
| 366 | } | ||
| 367 | ✗ | m_cachedComponentClauses.clear(); | |
| 368 | ✗ | } | |
| 369 | |||
| 370 | ✗ | void ClassDefinitionImpl::markConnectionsDirty() { | |
| 371 | ✗ | m_connectionsDirty = true; | |
| 372 | ✗ | } | |
| 373 | |||
| 374 | ✗ | std::vector<ConnectClauseImpl*> ClassDefinitionImpl::getConnectClauses() { | |
| 375 | ✗ | ensureConnectionCache(); | |
| 376 | ✗ | return m_cachedConnections; | |
| 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); | |
| 436 | ✗ | const std::string connectionId = ConnectClauseImpl::connectionIdFromEndpoints(normalized.first, normalized.second); | |
| 437 | |||
| 438 | ✗ | ensureConnectionCache(); | |
| 439 | ✗ | auto it = m_connections.find(connectionId); | |
| 440 | ✗ | if (it == m_connections.end() || !it->second->isActive()) { | |
| 441 | ✗ | return false; | |
| 442 | } | ||
| 443 | |||
| 444 | ✗ | removeConnectClause(it->second.get()); | |
| 445 | ✗ | return true; | |
| 446 | ✗ | } | |
| 447 | |||
| 448 | ✗ | void ClassDefinitionImpl::ensureConnectionCache() { | |
| 449 | ✗ | if (!m_connectionsDirty) { | |
| 450 | ✗ | return; | |
| 451 | } | ||
| 452 | ✗ | rebuildConnectionCache(); | |
| 453 | ✗ | m_connectionsDirty = false; | |
| 454 | } | ||
| 455 | |||
| 456 | ✗ | void ClassDefinitionImpl::rebuildConnectionCache() { | |
| 457 | ✗ | m_cachedConnections.clear(); | |
| 458 | |||
| 459 | ✗ | auto parserPtr = parser(); | |
| 460 | ✗ | auto* classCtx = ctx(); | |
| 461 | ✗ | if (!parserPtr || !classCtx) { | |
| 462 | ✗ | deactivateConnections(); | |
| 463 | ✗ | return; | |
| 464 | } | ||
| 465 | |||
| 466 | ✗ | const auto connectClauses = antlr4::tree::xpath::XPath::findAll(classCtx, std::string(ConnectClauseImpl::xpath), parserPtr); | |
| 467 | ✗ | std::unordered_set<std::string> seenIds; | |
| 468 | ✗ | auto file = modelicaFileShared(); | |
| 469 | ✗ | if (!file) { | |
| 470 | ✗ | deactivateConnections(); | |
| 471 | ✗ | return; | |
| 472 | } | ||
| 473 | |||
| 474 | ✗ | for (auto* clause : connectClauses) { | |
| 475 | ✗ | auto* connectCtx = dynamic_cast<modelicaParser::Connect_clauseContext*>(clause); | |
| 476 | ✗ | if (!connectCtx) { | |
| 477 | ✗ | continue; | |
| 478 | } | ||
| 479 | ✗ | const std::string id = ConnectClauseImpl::idFromCTX(connectCtx); | |
| 480 | ✗ | seenIds.insert(id); | |
| 481 | |||
| 482 | ✗ | auto it = m_connections.find(id); | |
| 483 | ✗ | if (it == m_connections.end()) { | |
| 484 | ✗ | auto impl = std::make_unique<ConnectClauseImpl>(file, this, connectCtx); | |
| 485 | ✗ | auto* ptr = impl.get(); | |
| 486 | ✗ | m_connections.emplace(id, std::move(impl)); | |
| 487 | ✗ | m_cachedConnections.push_back(ptr); | |
| 488 | ✗ | } else { | |
| 489 | ✗ | auto* ptr = it->second.get(); | |
| 490 | ✗ | ptr->setConnectClauseContext(connectCtx); | |
| 491 | ✗ | m_cachedConnections.push_back(ptr); | |
| 492 | } | ||
| 493 | ✗ | } | |
| 494 | |||
| 495 | ✗ | for (auto& [id, impl] : m_connections) { | |
| 496 | ✗ | if (!seenIds.contains(id)) { | |
| 497 | ✗ | impl->setConnectClauseContext(nullptr); | |
| 498 | } | ||
| 499 | } | ||
| 500 | ✗ | } | |
| 501 | |||
| 502 | ✗ | void ClassDefinitionImpl::deactivateConnections() { | |
| 503 | ✗ | for (auto& [id, impl] : m_connections) { | |
| 504 | ✗ | impl->setConnectClauseContext(nullptr); | |
| 505 | } | ||
| 506 | ✗ | m_cachedConnections.clear(); | |
| 507 | ✗ | } | |
| 508 | |||
| 509 | ✗ | void ClassDefinitionImpl::removeConnectClause(const ConnectClauseImpl* connection) { | |
| 510 | ✗ | if (!connection) { | |
| 511 | ✗ | return; | |
| 512 | } | ||
| 513 | |||
| 514 | ✗ | auto* equationCtx = connection->equationContext(); | |
| 515 | ✗ | if (!equationCtx) { | |
| 516 | ✗ | LOG_AND_THROW("Cannot find equation context for connection removal"); | |
| 517 | } | ||
| 518 | |||
| 519 | ✗ | auto file = modelicaFileShared(); | |
| 520 | ✗ | if (!file) { | |
| 521 | ✗ | LOG_AND_THROW("ModelicaFile is no longer available"); | |
| 522 | } | ||
| 523 | |||
| 524 | ✗ | auto* startToken = equationCtx->getStart(); | |
| 525 | ✗ | auto* stopToken = equationCtx->getStop(); | |
| 526 | ✗ | if (!startToken || !stopToken) { | |
| 527 | ✗ | LOG_AND_THROW("Cannot determine connection range for removal"); | |
| 528 | } | ||
| 529 | |||
| 530 | ✗ | std::string fullContent = file->getText(); | |
| 531 | ✗ | size_t startPos = static_cast<size_t>(startToken->getStartIndex()); | |
| 532 | ✗ | size_t endPos = static_cast<size_t>(stopToken->getStopIndex() + 1); | |
| 533 | |||
| 534 | ✗ | while (endPos < fullContent.size() && (fullContent[endPos] == ' ' || fullContent[endPos] == '\t')) { | |
| 535 | ✗ | ++endPos; | |
| 536 | } | ||
| 537 | ✗ | while (endPos < fullContent.size() && (fullContent[endPos] == '\r' || fullContent[endPos] == '\n')) { | |
| 538 | ✗ | ++endPos; | |
| 539 | } | ||
| 540 | |||
| 541 | ✗ | while (startPos > 0 && (fullContent[startPos - 1] == ' ' || fullContent[startPos - 1] == '\t')) { | |
| 542 | ✗ | --startPos; | |
| 543 | } | ||
| 544 | ✗ | if (startPos > 0 && (fullContent[startPos - 1] == '\n')) { | |
| 545 | ✗ | --startPos; | |
| 546 | ✗ | if (startPos > 0 && fullContent[startPos - 1] == '\r') { | |
| 547 | ✗ | --startPos; | |
| 548 | } | ||
| 549 | } | ||
| 550 | |||
| 551 | ✗ | fullContent.erase(startPos, endPos - startPos); | |
| 552 | ✗ | file->parse(fullContent); | |
| 553 | ✗ | markConnectionsDirty(); | |
| 554 | ✗ | } | |
| 555 | |||
| 556 | ✗ | modelicaParser::Equation_sectionContext* ClassDefinitionImpl::selectEquationSection() const { | |
| 557 | ✗ | auto* specifier = ctx()->class_specifier(); | |
| 558 | ✗ | if (!specifier || !specifier->long_class_specifier()) { | |
| 559 | ✗ | return nullptr; | |
| 560 | } | ||
| 561 | |||
| 562 | ✗ | auto* composition = specifier->long_class_specifier()->composition(); | |
| 563 | ✗ | if (!composition) { | |
| 564 | ✗ | return nullptr; | |
| 565 | } | ||
| 566 | |||
| 567 | ✗ | modelicaParser::Equation_sectionContext* fallback = nullptr; | |
| 568 | ✗ | const auto sections = composition->equation_section(); | |
| 569 | ✗ | for (auto* section : sections) { | |
| 570 | ✗ | if (!section) { | |
| 571 | ✗ | continue; | |
| 572 | } | ||
| 573 | ✗ | auto* start = section->getStart(); | |
| 574 | ✗ | if (start && start->getText() != "initial") { | |
| 575 | ✗ | return section; | |
| 576 | } | ||
| 577 | ✗ | if (!fallback) { | |
| 578 | ✗ | fallback = section; | |
| 579 | } | ||
| 580 | } | ||
| 581 | ✗ | return fallback; | |
| 582 | ✗ | } | |
| 583 | |||
| 584 | ✗ | std::pair<std::string, std::string> ClassDefinitionImpl::validateAndNormalizeConnectClause(const std::string& source, const std::string& target) { | |
| 585 | try { | ||
| 586 | ✗ | std::string clause = "connect(" + source + "," + target + ")"; | |
| 587 | ✗ | antlr4::ANTLRInputStream testStream(clause); | |
| 588 | ✗ | modelicaLexer testLexer(&testStream); | |
| 589 | ✗ | antlr4::CommonTokenStream testTokens(&testLexer); | |
| 590 | ✗ | modelicaParser testParser(&testTokens); | |
| 591 | ✗ | auto* connectCtx = testParser.connect_clause(); | |
| 592 | ✗ | if (!connectCtx || testParser.getNumberOfSyntaxErrors() > 0) { | |
| 593 | ✗ | LOG_AND_THROW("Invalid connection syntax: " + clause); | |
| 594 | } | ||
| 595 | ✗ | auto refs = connectCtx->component_reference(); | |
| 596 | ✗ | if (refs.size() != 2) { | |
| 597 | ✗ | LOG_AND_THROW("Connection must reference two components: " + clause); | |
| 598 | } | ||
| 599 | ✗ | return {refs[0]->getText(), refs[1]->getText()}; | |
| 600 | ✗ | } catch (const std::exception& e) { | |
| 601 | ✗ | LOG_AND_THROW("Failed to parse connection clause: " + std::string(e.what())); | |
| 602 | ✗ | } | |
| 603 | } | ||
| 604 | |||
| 605 | ✗ | std::string ClassDefinitionImpl::idFromCTX(modelicaParser::Class_definitionContext* ctx) { | |
| 606 | ✗ | auto* specifier = ctx->class_specifier(); | |
| 607 | ✗ | if (!specifier) { | |
| 608 | ✗ | LOG_AND_THROW("ClassDefinition context has null class_specifier"); | |
| 609 | } | ||
| 610 | |||
| 611 | ✗ | if (!specifier->long_class_specifier()) { | |
| 612 | ✗ | LOG_AND_THROW("ClassDefinition context has null long_class_specifier"); | |
| 613 | } | ||
| 614 | |||
| 615 | ✗ | return buildLongClassName(specifier->long_class_specifier()); | |
| 616 | } | ||
| 617 | |||
| 618 | ✗ | std::string ComponentClauseImpl::idFromCTX(modelicaParser::Component_clauseContext* ctx) { | |
| 619 | ✗ | if (!ctx) { | |
| 620 | ✗ | return ""; | |
| 621 | } | ||
| 622 | |||
| 623 | ✗ | std::string id; | |
| 624 | |||
| 625 | // Find the containing class definition to make this globally unique | ||
| 626 | ✗ | auto* parent = ctx->parent; | |
| 627 | ✗ | while (parent) { | |
| 628 | ✗ | auto* classDefCtx = dynamic_cast<modelicaParser::Class_definitionContext*>(parent); | |
| 629 | ✗ | if (classDefCtx) { | |
| 630 | // Found the containing class, use its name as a prefix | ||
| 631 | ✗ | auto* specifier = classDefCtx->class_specifier(); | |
| 632 | ✗ | if (specifier && specifier->long_class_specifier()) { | |
| 633 | ✗ | id = buildLongClassName(specifier->long_class_specifier()) + "::"; | |
| 634 | } | ||
| 635 | ✗ | break; | |
| 636 | } | ||
| 637 | ✗ | parent = parent->parent; | |
| 638 | } | ||
| 639 | |||
| 640 | // Get the type specifier (e.g., "Real", "parameter Real", "Modelica.Units.SI.Temperature") | ||
| 641 | ✗ | if (ctx->type_specifier()) { | |
| 642 | ✗ | id += ctx->type_specifier()->getText(); | |
| 643 | } | ||
| 644 | |||
| 645 | // Get the first component declaration name to make it unique within the class | ||
| 646 | ✗ | if ((ctx->component_list() != nullptr) && !ctx->component_list()->component_declaration().empty()) { | |
| 647 | ✗ | auto* firstComponent = ctx->component_list()->component_declaration()[0]; | |
| 648 | ✗ | if (firstComponent && firstComponent->declaration() && firstComponent->declaration()->IDENT()) { | |
| 649 | ✗ | id += "::" + firstComponent->declaration()->IDENT()->getText(); | |
| 650 | } | ||
| 651 | } | ||
| 652 | |||
| 653 | ✗ | return id; | |
| 654 | ✗ | } | |
| 655 | |||
| 656 | ✗ | std::string ConnectClauseImpl::source() const { | |
| 657 | ✗ | if (!ctx()) { | |
| 658 | ✗ | return ""; | |
| 659 | } | ||
| 660 | ✗ | const auto refs = ctx()->component_reference(); | |
| 661 | ✗ | if (refs.empty()) { | |
| 662 | ✗ | return ""; | |
| 663 | } | ||
| 664 | ✗ | return refs[0]->getText(); | |
| 665 | ✗ | } | |
| 666 | |||
| 667 | ✗ | std::string ConnectClauseImpl::target() const { | |
| 668 | ✗ | if (!ctx()) { | |
| 669 | ✗ | return ""; | |
| 670 | } | ||
| 671 | ✗ | const auto refs = ctx()->component_reference(); | |
| 672 | ✗ | if (refs.size() < 2) { | |
| 673 | ✗ | return ""; | |
| 674 | } | ||
| 675 | ✗ | return refs[1]->getText(); | |
| 676 | ✗ | } | |
| 677 | |||
| 678 | ✗ | void ConnectClauseImpl::remove() { | |
| 679 | ✗ | if (m_owner) { | |
| 680 | ✗ | m_owner->removeConnectClause(this); | |
| 681 | } | ||
| 682 | ✗ | } | |
| 683 | |||
| 684 | ✗ | std::string ConnectClauseImpl::idFromCTX(modelicaParser::Connect_clauseContext* ctx) { | |
| 685 | ✗ | if (!ctx) { | |
| 686 | ✗ | return ""; | |
| 687 | } | ||
| 688 | ✗ | const auto refs = ctx->component_reference(); | |
| 689 | ✗ | if (refs.size() != 2) { | |
| 690 | ✗ | return ""; | |
| 691 | } | ||
| 692 | ✗ | return connectionIdFromEndpoints(refs[0]->getText(), refs[1]->getText()); | |
| 693 | ✗ | } | |
| 694 | |||
| 695 | ✗ | std::string ConnectClauseImpl::connectionIdFromEndpoints(const std::string& source, const std::string& target) { | |
| 696 | ✗ | return source + "->" + target; | |
| 697 | } | ||
| 698 | |||
| 699 | ✗ | modelicaParser::EquationContext* ConnectClauseImpl::equationContext() const { | |
| 700 | ✗ | auto* parentCtx = ctx() ? ctx()->parent : nullptr; | |
| 701 | ✗ | while (parentCtx) { | |
| 702 | ✗ | if (auto* equationCtx = dynamic_cast<modelicaParser::EquationContext*>(parentCtx)) { | |
| 703 | ✗ | return equationCtx; | |
| 704 | } | ||
| 705 | ✗ | parentCtx = parentCtx->parent; | |
| 706 | } | ||
| 707 | ✗ | return nullptr; | |
| 708 | } | ||
| 709 | |||
| 710 | //std::vector<ComponentDeclaration> ComponentClause::componentDeclarations() { | ||
| 711 | // std::vector<ComponentDeclaration> result; | ||
| 712 | // | ||
| 713 | // const auto component_declarations = antlr4::tree::xpath::XPath::findAll(ctx(), "//Component_declaration", parser()); | ||
| 714 | // | ||
| 715 | // for (auto* c : component_declarations) { | ||
| 716 | // auto* component_declaration_ctx = dynamic_cast<modelicaParser::Component_declarationContext*>(c); | ||
| 717 | // assert(component_declaration_ctx != nullptr); | ||
| 718 | // result.emplace_back(modelicaFile(), component_declaration_ctx); // NOLINT | ||
| 719 | // } | ||
| 720 | // | ||
| 721 | // return result; | ||
| 722 | //} | ||
| 723 | |||
| 724 | //std::string ComponentDeclaration::idFromCTX([[maybe_unused]] modelicaParser::Component_declarationContext* ctx) { | ||
| 725 | // // TODO | ||
| 726 | // return ""; | ||
| 727 | //} | ||
| 728 | |||
| 729 | //std::vector<Declaration> ClassDefinition::declarations() { | ||
| 730 | // class_definition::GetDeclarationsVisitor v; | ||
| 731 | // v.visit(m_ctx); | ||
| 732 | // return v.getResult(); | ||
| 733 | //} | ||
| 734 | // | ||
| 735 | //void ClassDefinition::addDeclaration(const std::string& declarationText) { | ||
| 736 | // const auto el = m_ctx->class_specifier()->long_class_specifier()->composition()->element_list(); | ||
| 737 | // | ||
| 738 | // antlr4::ANTLRInputStream input(declarationText + ";"); // Ensure it's a complete statement | ||
| 739 | // modelicaLexer lexer(&input); | ||
| 740 | // antlr4::CommonTokenStream tempTokens(&lexer); | ||
| 741 | // modelicaParser tempParser(&tempTokens); | ||
| 742 | // | ||
| 743 | // [[maybe_unused]] const auto* component = tempParser.component_clause(); | ||
| 744 | // | ||
| 745 | // // Attempt to parse as a component_clause (variable declaration) | ||
| 746 | // modelicaParser::Component_clauseContext* parsedDecl = nullptr; | ||
| 747 | //} | ||
| 748 | // | ||
| 749 | //Declaration::Declaration(modelicaParser::DeclarationContext* ctx) : m_ctx(ctx) {} | ||
| 750 | // | ||
| 751 | //std::string Declaration::name() const { | ||
| 752 | // return m_ctx->IDENT()->getText(); | ||
| 753 | //} | ||
| 754 | |||
| 755 | } // namespace openstudio::modelica::detail | ||
| 756 |