| 249 | } |
| 250 | |
| 251 | void GraphElement::flattenSubgraphs(const string& target, NodePredicate filter) |
| 252 | { |
| 253 | vector<NodePtr> nodeQueue = getNodes(); |
| 254 | while (!nodeQueue.empty()) |
| 255 | { |
| 256 | // Determine which nodes require processing, and precompute declarations |
| 257 | // and graph implementations for these nodes. |
| 258 | using PortElementVec = vector<PortElementPtr>; |
| 259 | std::vector<NodePtr> processNodeVec; |
| 260 | std::unordered_map<NodePtr, NodeGraphPtr> graphImplMap; |
| 261 | std::unordered_map<NodePtr, ConstInterfaceElementPtr> declarationMap; |
| 262 | std::unordered_map<NodePtr, PortElementVec> downstreamPortMap; |
| 263 | for (NodePtr node : nodeQueue) |
| 264 | { |
| 265 | if (filter && !filter(node)) |
| 266 | { |
| 267 | continue; |
| 268 | } |
| 269 | |
| 270 | InterfaceElementPtr implement = node->getImplementation(target); |
| 271 | if (implement && implement->isA<NodeGraph>()) |
| 272 | { |
| 273 | processNodeVec.push_back(node); |
| 274 | graphImplMap[node] = implement->asA<NodeGraph>(); |
| 275 | declarationMap[node] = node->getDeclaration(target); |
| 276 | downstreamPortMap[node] = node->getDownstreamPorts(); |
| 277 | for (NodePtr sourceSubNode : implement->asA<NodeGraph>()->getNodes()) |
| 278 | { |
| 279 | downstreamPortMap[sourceSubNode] = sourceSubNode->getDownstreamPorts(); |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | nodeQueue.clear(); |
| 284 | |
| 285 | // Iterate through nodes with graph implementations. |
| 286 | for (NodePtr processNode : processNodeVec) |
| 287 | { |
| 288 | NodeGraphPtr sourceSubGraph = graphImplMap[processNode]; |
| 289 | std::unordered_map<NodePtr, NodePtr> subNodeMap; |
| 290 | |
| 291 | // Create a new instance of each original subnode. |
| 292 | for (NodePtr sourceSubNode : sourceSubGraph->getNodes()) |
| 293 | { |
| 294 | string origName = sourceSubNode->getName(); |
| 295 | string destName = createValidChildName(origName); |
| 296 | NodePtr destSubNode = addNode(sourceSubNode->getCategory(), destName); |
| 297 | |
| 298 | destSubNode->copyContentFrom(sourceSubNode); |
| 299 | setChildIndex(destSubNode->getName(), getChildIndex(processNode->getName())); |
| 300 | |
| 301 | // Store the mapping between subgraphs. |
| 302 | subNodeMap[sourceSubNode] = destSubNode; |
| 303 | |
| 304 | // Add the subnode to the queue, allowing processing of nested subgraphs. |
| 305 | nodeQueue.push_back(destSubNode); |
| 306 | } |
| 307 | |
| 308 | // Update properties of generated subnodes. |
no test coverage detected