LogViewer
A modern log viewer with AI-assisted analysis (Qt 6)
Loading...
Searching...
No Matches
PluginDependencyGraph.hpp
Go to the documentation of this file.
1
8#ifndef PLUGIN_DEPENDENCY_GRAPH_HPP
9#define PLUGIN_DEPENDENCY_GRAPH_HPP
10
11#include "Result.hpp"
12#include "Error.hpp"
13#include "Logger.hpp"
14#include <algorithm>
15#include <map>
16#include <set>
17#include <string>
18#include <vector>
19
20namespace plugin {
21
56 public:
62 std::string pluginId;
63 std::vector<std::string> dependencies;
64 };
65
71 void AddNode(const std::string& id, const std::vector<std::string>& dependencies)
72 {
73 m_nodes[id] = {id, dependencies};
74 util::Logger::Trace("Added plugin node: {}, dependencies: {}",
75 id, dependencies.size());
76 }
77
86 {
87 std::set<std::string> visited, recursionStack;
88
89 for (const auto& [id, _] : m_nodes) {
90 if (!visited.count(id)) {
91 if (HasCycle(id, visited, recursionStack)) {
92 std::string msg = "Circular dependency detected starting at: " + id;
93 util::Logger::Error("Plugin: {}", msg);
96 }
97 }
98 }
99
100 util::Logger::Debug("Plugin dependency graph validated - no cycles");
102 }
103
118 {
119 if (auto result = ValidateNoCycles(); result.isErr()) {
121 result.error());
122 }
123
124 std::vector<std::string> order;
125 std::set<std::string> visited;
126
127 for (const auto& [id, _] : m_nodes) {
128 TopologicalSort(id, visited, order);
129 }
130
131 // Don't reverse - DFS already produces correct order (deps before dependents)
132 util::Logger::Debug("Plugin initialization order computed: {} plugins",
133 order.size());
135 }
136
146 {
147 auto order = GetInitializationOrder();
148 if (order.isErr()) {
149 return order;
150 }
151
152 auto shutdown = order.unwrap();
153 std::reverse(shutdown.begin(), shutdown.end());
154
155 util::Logger::Debug("Plugin shutdown order computed: {} plugins",
156 shutdown.size());
158 }
159
168 const std::set<std::string>& availablePlugins) const
169 {
170 std::vector<std::string> missing;
171
172 for (const auto& [id, node] : m_nodes) {
173 for (const auto& dep : node.dependencies) {
174 if (!availablePlugins.count(dep)) {
175 missing.push_back(dep);
177 "Plugin {} missing dependency: {}", id, dep);
178 }
179 }
180 }
181
182 if (!missing.empty()) {
183 std::string list = FormatList(missing);
184 std::string msg = "Missing plugin dependencies: " + list;
185 util::Logger::Error("Plugin: {}", msg);
188 }
189
190 util::Logger::Debug("All plugin dependencies validated");
192 missing);
193 }
194
200 std::vector<std::string> GetDependencies(const std::string& id) const
201 {
202 auto it = m_nodes.find(id);
203 return it != m_nodes.end() ? it->second.dependencies
204 : std::vector<std::string>();
205 }
206
211 size_t GetNodeCount() const { return m_nodes.size(); }
212
213 private:
214 std::map<std::string, DependencyNode> m_nodes;
215
219 bool HasCycle(const std::string& id, std::set<std::string>& visited,
220 std::set<std::string>& recursionStack) const
221 {
222 if (recursionStack.count(id)) {
223 util::Logger::Warn("Circular dependency involving: {}", id);
224 return true;
225 }
226 if (visited.count(id)) {
227 return false;
228 }
229
230 visited.insert(id);
231 recursionStack.insert(id);
232
233 auto it = m_nodes.find(id);
234 if (it != m_nodes.end()) {
235 for (const auto& dep : it->second.dependencies) {
236 if (HasCycle(dep, visited, recursionStack)) {
237 return true;
238 }
239 }
240 }
241
242 recursionStack.erase(id);
243 return false;
244 }
245
249 void TopologicalSort(const std::string& id,
250 std::set<std::string>& visited,
251 std::vector<std::string>& order) const
252 {
253 if (visited.count(id)) {
254 return;
255 }
256 visited.insert(id);
257
258 auto it = m_nodes.find(id);
259 if (it != m_nodes.end()) {
260 for (const auto& dep : it->second.dependencies) {
261 TopologicalSort(dep, visited, order);
262 }
263 }
264
265 order.push_back(id);
266 }
267
271 static std::string FormatList(const std::vector<std::string>& items)
272 {
273 std::string result = "[";
274 for (size_t i = 0; i < items.size(); ++i) {
275 if (i > 0) result += ", ";
276 result += items[i];
277 }
278 result += "]";
279 return result;
280 }
281};
282
283} // namespace plugin
284
285#endif // PLUGIN_DEPENDENCY_GRAPH_HPP
Error handling utilities.
Logging facade for application-wide logging.
Definition Error.hpp:62
Manages plugin dependency relationships and validates for safe initialization.
Definition PluginDependencyGraph.hpp:55
void TopologicalSort(const std::string &id, std::set< std::string > &visited, std::vector< std::string > &order) const
Topological sort using DFS.
Definition PluginDependencyGraph.hpp:249
std::map< std::string, DependencyNode > m_nodes
Definition PluginDependencyGraph.hpp:214
std::vector< std::string > GetDependencies(const std::string &id) const
Gets dependencies for a specific plugin.
Definition PluginDependencyGraph.hpp:200
util::Result< bool, error::Error > ValidateNoCycles() const
Validates that no circular dependencies exist.
Definition PluginDependencyGraph.hpp:85
size_t GetNodeCount() const
Gets number of nodes in graph.
Definition PluginDependencyGraph.hpp:211
static std::string FormatList(const std::vector< std::string > &items)
Formats vector of strings for logging.
Definition PluginDependencyGraph.hpp:271
bool HasCycle(const std::string &id, std::set< std::string > &visited, std::set< std::string > &recursionStack) const
Checks for cycle using depth-first search.
Definition PluginDependencyGraph.hpp:219
void AddNode(const std::string &id, const std::vector< std::string > &dependencies)
Adds a node to the dependency graph.
Definition PluginDependencyGraph.hpp:71
util::Result< std::vector< std::string >, error::Error > GetShutdownOrder() const
Gets plugins in valid shutdown order.
Definition PluginDependencyGraph.hpp:145
util::Result< std::vector< std::string >, error::Error > ValidateDependenciesAvailable(const std::set< std::string > &availablePlugins) const
Validates all dependencies are available.
Definition PluginDependencyGraph.hpp:167
util::Result< std::vector< std::string >, error::Error > GetInitializationOrder() const
Gets plugins in valid initialization order.
Definition PluginDependencyGraph.hpp:117
static void Warn(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logger.hpp:297
static void Trace(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logger.hpp:279
static void Debug(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logger.hpp:285
static void Error(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logger.hpp:303
Definition Result.hpp:15
static Result Ok(T value)
Definition Result.hpp:17
static Result Err(E error)
Definition Result.hpp:22
@ RuntimeError
Runtime error occurred.
Definition IFieldConversionPlugin.hpp:8
Represents a plugin node in the dependency graph.
Definition PluginDependencyGraph.hpp:61
std::string pluginId
Plugin identifier.
Definition PluginDependencyGraph.hpp:62
std::vector< std::string > dependencies
IDs of dependencies.
Definition PluginDependencyGraph.hpp:63