www.mooseframework.org
DependencyResolverInterface.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #pragma once
11 
12 // STL includes
13 #include <string>
14 #include <set>
15 #include <iostream>
16 #include <algorithm>
17 
18 // MOOSE includes
19 #include "DependencyResolver.h"
20 #include "MooseUtils.h"
21 
26 {
27 public:
32 
36  virtual const std::set<std::string> & getRequestedItems() = 0;
37 
41  virtual const std::set<std::string> & getSuppliedItems() = 0;
42 
46  template <typename T>
47  static void sort(typename std::vector<T> & vector);
48 
52  template <typename T>
53  static void sortDFS(typename std::vector<T> & vector);
54 
58  template <typename T, typename T2>
59  static void cyclicDependencyError(CyclicDependencyException<T2> & e, const std::string & header);
60 };
61 
62 template <typename T>
63 void
64 DependencyResolverInterface::sort(typename std::vector<T> & vector)
65 {
66  sortDFS(vector);
67 }
68 
69 template <typename T>
70 void
71 DependencyResolverInterface::sortDFS(typename std::vector<T> & vector)
72 {
73  if (vector.size() <= 1)
74  return;
75 
80 
81  // Map of suppliers: what is supplied -> by what object
82  std::multimap<std::string, T> suppliers_map;
83  for (auto & v : vector)
84  {
85  // Whether or not this object supplies something, we will always
86  // add it as a node because we want to make sure that it gets returned
87  graph.addNode(v);
88 
89  for (const auto & supplied_item : v->getSuppliedItems())
90  suppliers_map.emplace(supplied_item, v);
91  }
92 
93  // build the dependency graph
94  for (auto & v : vector)
95  for (const auto & requested_item : v->getRequestedItems())
96  {
97  const auto & [begin_it, end_it] = suppliers_map.equal_range(requested_item);
98  for (const auto & [supplier_name, supplier_object] : as_range(begin_it, end_it))
99  {
100  libmesh_ignore(supplier_name);
101 
102  // We allow an object to have a circular dependency within itself; e.g. we choose to
103  // trust a developer knows what they are doing within a single object
104  if (supplier_object != v)
105  graph.addEdge(supplier_object, v);
106  }
107  }
108 
109  const auto & sorted = graph.dfs();
110 
111  // The set here gets unique objects, as it's valid to pass in duplicates
112  mooseAssert(sorted.size() == std::set<T>(vector.begin(), vector.end()).size(), "Size mismatch");
113 
114  vector = sorted;
115 }
116 
117 template <typename T, typename T2>
118 void
120  const std::string & header)
121 {
122  std::ostringstream oss;
123 
124  oss << header << ":\n";
125  const auto cycle = e.getCyclicDependencies();
126  std::vector<std::string> names(cycle.size());
127  for (const auto i : index_range(cycle))
128  names[i] = static_cast<T>(cycle[i])->name();
129  oss << MooseUtils::join(names, " <- ");
130  mooseError(oss.str());
131 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:299
static void sortDFS(typename std::vector< T > &vector)
Given a vector, sort using the depth-first search.
virtual const std::set< std::string > & getRequestedItems()=0
Return a set containing the names of items requested by the object.
void addEdge(const T &a, const T &b)
Add an edge between nodes &#39;a&#39; and &#39;b&#39;.
void libmesh_ignore(const Args &...)
SimpleRange< IndexType > as_range(const std::pair< IndexType, IndexType > &p)
virtual const std::set< std::string > & getSuppliedItems()=0
Return a set containing the names of items owned by the object.
static void sort(typename std::vector< T > &vector)
Given a vector, sort using the getRequested/SuppliedItems sets.
void addNode(const T &a)
Add a node &#39;a&#39; to the graph.
const std::vector< T > & getCyclicDependencies() const
Interface for sorting dependent vectors of objects.
const std::vector< T > & dfs()
Do depth-first search from root nodes to obtain order in which graph nodes should be "executed"...
static void cyclicDependencyError(CyclicDependencyException< T2 > &e, const std::string &header)
A helper method for cyclic errors.
Class that represents the dependecy as a graph.
auto index_range(const T &sizable)
std::string join(const T &strings, const std::string &delimiter)
Python like join function for strings.
Definition: MooseUtils.h:130