dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
Controller.hpp
1// SPDX-FileCopyrightText: 2025 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 "NodeFactory.hpp"
19#include <dfx-hooks/Graph.hpp>
21#include <dfx-utilities/RecyclableIdAllocator.hpp>
22
161
162namespace dfx::Graph
163{
213{
214public:
216 Controller() = default;
221
233 Core::Node::Id addNode(std::string_view type, std::string_view name,
234 nlohmann::json config = {});
235
248 std::string_view type, std::string_view name,
249 nlohmann::json config = {});
250
260
268 bool removeNode(std::string_view name);
269
271 bool hasNode(Core::Node::Id id) const;
273 bool hasNode(std::string_view name) const;
275 bool hasNodeType(std::string_view type) const;
276
278 std::vector<std::string> allNodeType() const;
280 std::vector<std::string> allNodeName() const;
282 std::vector<Core::Node::Id> allNodeId() const;
283
289 Core::NodePtr node(std::string_view name) const;
291 std::vector<Core::NodePtr> nodes() const;
292
314 std::vector<std::string> * errors = nullptr,
315 std::size_t errorLimit = 0);
316
328
332 std::vector<Core::Channel::Id> allChannelId() const;
333
337
339 std::vector<Core::ChannelPtr> channels() const;
340
342 NodeFactory & nodeFactory() noexcept { return _factory; }
343
345 NodeFactory const & nodeFactory() const noexcept { return _factory; }
346
347public:
349 bool isChannelVerificationEnabled() const noexcept { return _channelVerificationEnabled; }
356 void setChannelVerification(bool enabled) { _channelVerificationEnabled = enabled; }
357
358public:
365
369
370private:
371 bool _validateChannelConnection(Core::OutputPort const & src, Core::InputPort const & dst,
372 std::vector<std::string> * errors = nullptr,
373 std::size_t errorLimit = 0) const;
374
375 void _addNode(Core::Node::Id nodeId, std::string_view type, std::string_view name,
376 nlohmann::json config);
377 void _removeNode(std::unordered_map<Core::Node::Id, Core::NodePtr>::iterator itr);
378
379private:
380 NodeFactory _factory;
381
382 std::unordered_map<Core::Node::Id, Core::NodePtr> _nodes;
383 std::unordered_map<Core::Channel::Id, Core::ChannelPtr> _channels;
384
387
388 bool _channelVerificationEnabled = true;
389
390private:
391 std::vector<Hooks::Graph *> _hooks;
392};
393} // !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:57
Incoming message endpoint attached to a node.
Definition InputPort.hpp:55
uint32_t Id
Node identifier type (unique and stable within a graph instance).
Definition Node.hpp:113
Outgoing message endpoint attached to a node.
Definition OutputPort.hpp:47
Core::ChannelPtr channel(Core::Channel::Id id) const
Get a channel by id.
Core::Channel::Id addChannel(Core::OutputPort &src, Core::InputPort &dst, std::vector< std::string > *errors=nullptr, std::size_t errorLimit=0)
Create a channel between an output and an input port.
std::vector< Core::Node::Id > allNodeId() const
List all node ids currently in the graph.
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:342
Core::NodePtr node(std::string_view name) const
Get a node by name.
void setChannelVerification(bool enabled)
Enable/disable channel verification.
Definition Controller.hpp:356
Controller()=default
Construct an empty controller.
bool removeChannel(Core::Channel::Id id)
Remove a channel by id.
std::vector< Core::ChannelPtr > channels() const
Return all channels currently in the graph (strong pointers).
ENABLE_DEFAULT_MOVE_DISABLE_COPY(Controller)
Move is allowed, copy is disabled.
std::vector< std::string > allNodeName() const
List all node names currently in the graph.
void deregisterHook(Hooks::Graph *hook)
Deregister a previously registered hook (no-op if not present).
bool hasChannel(Core::Channel::Id id) const
Check whether a channel id exists.
~Controller()
Destroy the controller.
void registerHook(Hooks::Graph *hook)
Register a graph hook.
bool removeNode(Core::Node::Id id)
Remove a node by id.
bool isChannelVerificationEnabled() const noexcept
Whether channel verification is enabled.
Definition Controller.hpp:349
std::vector< std::string > allNodeType() const
List all node types known by the factory.
bool hasNode(Core::Node::Id id) const
Check whether a node id exists.
bool removeNode(std::string_view name)
Remove a node by name.
std::vector< Core::NodePtr > nodes() const
Return all nodes currently in the graph (strong pointers).
bool hasNodeType(std::string_view type) const
Check whether a node type is known by the factory.
NodeFactory const & nodeFactory() const noexcept
Access the node factory used to create nodes.
Definition Controller.hpp:345
bool hasNode(std::string_view name) const
Check whether a node name exists.
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
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 Node.hpp:61
std::shared_ptr< Channel > ChannelPtr
Shared ownership handle for channels.
Definition Channel.hpp:147
Definition Node.hpp:42