https://mooseframework.inl.gov
Public Member Functions | Static Public Member Functions | List of all members
DependencyResolverInterface Class Referenceabstract

Interface for sorting dependent vectors of objects. More...

#include <DependencyResolverInterface.h>

Inheritance diagram for DependencyResolverInterface:
[legend]

Public Member Functions

 DependencyResolverInterface ()
 Constructor. More...
 
virtual const std::set< std::string > & getRequestedItems ()=0
 Return a set containing the names of items requested by the object. More...
 
virtual const std::set< std::string > & getSuppliedItems ()=0
 Return a set containing the names of items owned by the object. More...
 

Static Public Member Functions

template<typename T >
static void sort (typename std::vector< T > &vector)
 Given a vector, sort using the getRequested/SuppliedItems sets. More...
 
template<typename T >
static void sortDFS (typename std::vector< T > &vector)
 Given a vector, sort using the depth-first search. More...
 
template<typename T , typename T2 >
static void cyclicDependencyError (CyclicDependencyException< T2 > &e, const std::string &header)
 A helper method for cyclic errors. More...
 

Detailed Description

Interface for sorting dependent vectors of objects.

Definition at line 25 of file DependencyResolverInterface.h.

Constructor & Destructor Documentation

◆ DependencyResolverInterface()

DependencyResolverInterface::DependencyResolverInterface ( )
inline

Constructor.

Definition at line 31 of file DependencyResolverInterface.h.

31 {}

Member Function Documentation

◆ cyclicDependencyError()

template<typename T , typename T2 >
void DependencyResolverInterface::cyclicDependencyError ( CyclicDependencyException< T2 > &  e,
const std::string &  header 
)
static

A helper method for cyclic errors.

Definition at line 119 of file DependencyResolverInterface.h.

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 }
std::string join(Iterator begin, Iterator end, const std::string &delimiter)
Python-like join function for strings over an iterator range.
Definition: MooseUtils.h:142
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
const std::vector< T > & getCyclicDependencies() const
auto index_range(const T &sizable)

◆ getRequestedItems()

virtual const std::set<std::string>& DependencyResolverInterface::getRequestedItems ( )
pure virtual

◆ getSuppliedItems()

virtual const std::set<std::string>& DependencyResolverInterface::getSuppliedItems ( )
pure virtual

◆ sort()

template<typename T >
void DependencyResolverInterface::sort ( typename std::vector< T > &  vector)
static

Given a vector, sort using the getRequested/SuppliedItems sets.

Definition at line 64 of file DependencyResolverInterface.h.

Referenced by TheWarehouse::prepare().

65 {
66  sortDFS(vector);
67 }
static void sortDFS(typename std::vector< T > &vector)
Given a vector, sort using the depth-first search.

◆ sortDFS()

template<typename T >
void DependencyResolverInterface::sortDFS ( typename std::vector< T > &  vector)
static

Given a vector, sort using the depth-first search.

Class that represents the dependency as a graph

Definition at line 71 of file DependencyResolverInterface.h.

Referenced by sort().

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 }
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)
void addNode(const T &a)
Add a node &#39;a&#39; to the graph.
const std::vector< T > & dfs()
Do depth-first search from root nodes to obtain order in which graph nodes should be "executed"...
Class that represents the dependecy as a graph.

The documentation for this class was generated from the following file: