dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
Controller.hpp
1// SPDX-FileCopyrightText: 2025-2026 Vincent Leroy
2// SPDX-License-Identifier: MIT
3//
4// This file is part of dfx.
5//
6// Licensed under the MIT License. See the LICENSE file in the project root
7// for full license information.
8
9#pragma once
10
11// Standard includes
12#include <unordered_map>
13
14// Third-party includes
15#include <nlohmann/json.hpp>
16
17// Project includes
18#include "ConnectionValidator.hpp"
19#include "NodeFactory.hpp"
20#include "TransportFactory.hpp"
21#include <dfx-hooks/Graph.hpp>
23#include <dfx-utilities/RecyclableIdAllocator.hpp>
24
25namespace dfx::Graph
26{
63{
64public:
68 ~Controller() = default;
71
74
86 Core::Node::Id addNode(std::string_view type, std::string_view name,
87 nlohmann::json config = {});
88
101 std::string_view type, std::string_view name,
102 nlohmann::json config = {});
103
112 bool removeNode(Core::Node::Id id) noexcept;
113
121 bool removeNode(std::string_view name) noexcept;
122
124 bool hasNode(Core::Node::Id id) const noexcept;
126 bool hasNode(std::string_view name) const noexcept;
128 bool hasNodeType(std::string_view type) const noexcept;
129
131 std::vector<std::string> allNodeType() const;
133 std::vector<std::string> allNodeName() const;
135 std::vector<Core::Node::Id> allNodeId() const;
136
142 Core::NodePtr node(std::string_view name) const;
144 std::vector<Core::NodePtr> nodes() const;
145
147
150
167 Core::Channel::Id addChannel(std::string_view src, std::string_view dst,
168 nlohmann::json srcConfig = {}, nlohmann::json dstConfig = {},
169 std::size_t errorLimit = 0);
170
188 nlohmann::json srcConfig = {}, nlohmann::json dstConfig = {},
189 std::size_t errorLimit = 0);
190
202
204 bool hasChannel(Core::Channel::Id id) const noexcept;
206 std::vector<Core::Channel::Id> allChannelId() const;
207
211
213 std::vector<Core::ChannelPtr> channels() const;
214
216
217public:
220
222 NodeFactory & nodeFactory() noexcept { return _nodeFactory; }
223
225 NodeFactory const & nodeFactory() const noexcept { return _nodeFactory; }
226
228 TransportFactory & transportFactory() noexcept { return _transportFactory; }
229
231 TransportFactory const & transportFactory() const noexcept { return _transportFactory; }
232
234 ConnectionValidator & connectionValidator() noexcept { return _connectionValidator; }
235
237 ConnectionValidator const & connectionValidator() const noexcept { return _connectionValidator; }
238
240
241public:
244
251
254 void deregisterHook(Hooks::Graph * hook) noexcept;
255
257
258private:
259 void _addNode(Core::Node::Id nodeId, std::string_view type, std::string_view name,
260 nlohmann::json config);
261 void _removeNode(std::unordered_map<Core::Node::Id, Core::NodePtr>::iterator itr) noexcept;
262
263 Core::Channel::Id _addChannel(Core::SourceTransportPtr src, Core::DestinationTransportPtr dst, std::size_t errorLimit);
264
265private:
266 NodeFactory & _nodeFactory;
267 TransportFactory & _transportFactory;
268 ConnectionValidator _connectionValidator;
269
270 std::unordered_map<Core::Node::Id, Core::NodePtr> _nodes;
271 std::unordered_map<Core::Channel::Id, Core::ChannelPtr> _channels;
272
275
276private:
277 std::vector<Hooks::Graph *> _hooks;
278};
279} // !namespace dfx::Graph
Convenience macros to explicitly control copy and move semantics.
uint32_t Id
Identifier type (unique within a graph instance, by convention).
Definition Channel.hpp:63
uint32_t Id
Node identifier type (unique and stable within a graph instance).
Definition Node.hpp:116
Class used to validate is a channel can be created between 2 ports.
Definition ConnectionValidator.hpp:181
~Controller()=default
Destroy the controller.
bool removeNode(std::string_view name) noexcept
Remove a node by name.
TransportFactory & transportFactory() noexcept
Access the transport factory used to create dfx::Core::Transport.
Definition Controller.hpp:228
bool hasNode(std::string_view name) const noexcept
Check whether a node name exists.
Core::ChannelPtr channel(Core::Channel::Id id) const
Get a channel by id.
bool hasNode(Core::Node::Id id) const noexcept
Check whether a node id exists.
std::vector< Core::Node::Id > allNodeId() const
List all node ids currently in the graph.
Controller(NodeFactory &nodeFactory, TransportFactory &transportFactory)
Construct an empty controller.
DFX_DISABLE_COPY_AND_MOVE(Controller)
Move and copy are disallowed.
Core::Node::Id addNode(Core::Node::Id id, std::string_view type, std::string_view name, nlohmann::json config={})
Add a node using a specific id.
Core::NodePtr node(Core::Node::Id id) const
Get a node by id.
NodeFactory & nodeFactory() noexcept
Access the node factory used to create nodes.
Definition Controller.hpp:222
bool removeChannel(Core::Channel::Id id) noexcept
Remove a channel by id.
void deregisterHook(Hooks::Graph *hook) noexcept
Deregister a previously registered hook (no-op if not present).
Core::NodePtr node(std::string_view name) const
Get a node by name.
bool hasChannel(Core::Channel::Id id) const noexcept
Check whether a channel id exists.
bool hasNodeType(std::string_view type) const noexcept
Check whether a node type is known by the factory.
std::vector< Core::ChannelPtr > channels() const
Return all channels currently in the graph (strong pointers).
std::vector< std::string > allNodeName() const
List all node names currently in the graph.
void registerHook(Hooks::Graph *hook)
Register a graph hook.
ConnectionValidator const & connectionValidator() const noexcept
Access the connection validator used to verify connections between ports.
Definition Controller.hpp:237
ConnectionValidator & connectionValidator() noexcept
Access the connection validator used to verify connections between ports.
Definition Controller.hpp:234
bool removeNode(Core::Node::Id id) noexcept
Remove a node by id.
std::vector< std::string > allNodeType() const
List all node types known by the factory.
std::vector< Core::NodePtr > nodes() const
Return all nodes currently in the graph (strong pointers).
Core::Channel::Id addChannel(std::string_view src, std::string_view dst, nlohmann::json srcConfig={}, nlohmann::json dstConfig={}, std::size_t errorLimit=0)
Create a channel between an output and an input port.
NodeFactory const & nodeFactory() const noexcept
Access the node factory used to create nodes.
Definition Controller.hpp:225
TransportFactory const & transportFactory() const noexcept
Access the transport factory used to create dfx::Core::Transport.
Definition Controller.hpp:231
Core::Channel::Id addChannel(Core::Endpoint::Descriptor src, Core::Endpoint::Descriptor dst, nlohmann::json srcConfig={}, nlohmann::json dstConfig={}, std::size_t errorLimit=0)
Create a channel between an output and an input port.
std::vector< Core::Channel::Id > allChannelId() const
List all channel ids currently in the graph.
Core::Node::Id addNode(std::string_view type, std::string_view name, nlohmann::json config={})
Add a node and allocate a fresh node id.
Runtime node instantiation and validation facility (builders + config/metadata schemas).
Definition NodeFactory.hpp:60
Registry and factory for creating Source and Destination transports.
Definition TransportFactory.hpp:40
Hooks related to the lifecycle of graph objects (nodes and channels).
Definition Graph.hpp:54
ID allocator that can recycle released identifiers.
Definition RecyclableIdAllocator.hpp:78
std::shared_ptr< Node > NodePtr
Shared ownership pointer type for Nodes..
Definition Endpoint.hpp:21
std::shared_ptr< Channel > ChannelPtr
Shared ownership handle for channels.
Definition Channel.hpp:137
std::unique_ptr< SourceTransport > SourceTransportPtr
Unique ownership handle for SourceTransport.
Definition SourceTransport.hpp:48
Definition Node.hpp:43
Parsed metadata for an endpoint, used for routing and graph introspection.
Definition Endpoint.hpp:61