/////////////////////////////////////////////////////////////////////////// Returns true if the supplied task adds a cycle to the dependency chain.
| 40 | //////////////////////////////////////////////////////////////////////////////// |
| 41 | // Returns true if the supplied task adds a cycle to the dependency chain. |
| 42 | bool dependencyIsCircular(const Task& task) { |
| 43 | // A new task has no UUID assigned yet, and therefore cannot be part of any |
| 44 | // dependency chain. |
| 45 | if (task.has("uuid")) { |
| 46 | auto task_uuid = task.get("uuid"); |
| 47 | |
| 48 | std::stack<Task> s; |
| 49 | s.push(task); |
| 50 | |
| 51 | std::unordered_set<std::string> visited; |
| 52 | visited.insert(task_uuid); |
| 53 | |
| 54 | while (!s.empty()) { |
| 55 | Task& current = s.top(); |
| 56 | auto deps_current = current.getDependencyUUIDs(); |
| 57 | |
| 58 | // This is a basic depth first search that always terminates given the |
| 59 | // fact that we do not visit any task twice |
| 60 | for (const auto& dep : deps_current) { |
| 61 | if (Context::getContext().tdb2.get(dep, current)) { |
| 62 | auto current_uuid = current.get("uuid"); |
| 63 | |
| 64 | if (task_uuid == current_uuid) { |
| 65 | // Cycle found, initial task reached for the second time! |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | if (visited.find(current_uuid) == visited.end()) { |
| 70 | // Push the task to the stack, if it has not been processed yet |
| 71 | s.push(current); |
| 72 | visited.insert(current_uuid); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | s.pop(); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | //////////////////////////////////////////////////////////////////////////////// |
| 85 | // Determine whether a dependency chain is being broken, assuming that 'task' is |
no test coverage detected